Skip to content

Commit 12ce2f1

Browse files
committed
Address David's comments
1 parent 518c8bf commit 12ce2f1

File tree

1 file changed

+24
-28
lines changed

1 file changed

+24
-28
lines changed

apps/rush-lib/src/cli/actions/TabCompleteAction.ts

Lines changed: 24 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,15 @@ interface IParameter {
1717

1818
const DEFAULT_WORD_TO_AUTOCOMPLETE: string = '';
1919
const DEFAULT_POSITION: number = 0;
20+
21+
interface IActionMap {
22+
[actionName: string]: IParameter[];
23+
}
24+
2025
export class TabCompleteAction extends BaseRushAction {
2126
private _wordToCompleteParameter: CommandLineStringParameter;
2227
private _positionParameter: CommandLineIntegerParameter;
2328

24-
private static _actions: { [actionName: string]: IParameter[] } = {};
25-
2629
public constructor(parser: RushCommandLineParser) {
2730
super({
2831
actionName: 'tab-complete',
@@ -59,6 +62,7 @@ export class TabCompleteAction extends BaseRushAction {
5962
}
6063

6164
public *_getCompletions(commandLine: string, caretPosition: number): IterableIterator<string> {
65+
const actions: IActionMap = {};
6266
this.parser.actions.forEach((element) => {
6367
const actionParameters: IParameter[] = [];
6468
element.parameters.forEach((elem) => {
@@ -67,13 +71,13 @@ export class TabCompleteAction extends BaseRushAction {
6771
actionParameters.push({ name: elem.shortName, kind: elem.kind });
6872
}
6973
});
70-
TabCompleteAction._actions[element.actionName] = actionParameters;
74+
actions[element.actionName] = actionParameters;
7175
});
7276

73-
TabCompleteAction._actions['-d'] = [];
74-
TabCompleteAction._actions['--debug'] = [];
75-
TabCompleteAction._actions['-h'] = [];
76-
TabCompleteAction._actions['--help'] = [];
77+
actions['-d'] = [];
78+
actions['--debug'] = [];
79+
actions['-h'] = [];
80+
actions['--help'] = [];
7781

7882
// yield ('arg count: ' + process.argv.length);
7983

@@ -82,7 +86,7 @@ export class TabCompleteAction extends BaseRushAction {
8286
// }
8387

8488
if (!commandLine || !caretPosition) {
85-
yield* this._getAllActions();
89+
yield* Object.keys(actions); // return all actions
8690
return;
8791
}
8892

@@ -98,7 +102,7 @@ export class TabCompleteAction extends BaseRushAction {
98102
const debugParameterOffset: number = debugParameterUsed ? 1 : 0; // if debug switch is used, then offset everything by 1.
99103

100104
if (commands.length < 2 + debugParameterOffset) {
101-
yield* this._getAllActions();
105+
yield* Object.keys(actions); // return all actions
102106
return;
103107
}
104108

@@ -110,13 +114,13 @@ export class TabCompleteAction extends BaseRushAction {
110114
const completePartialWord: boolean = caretPosition === commandLine.length;
111115

112116
if (completePartialWord && commands.length === 2 + debugParameterOffset) {
113-
for (const actionName of Object.keys(TabCompleteAction._actions)) {
117+
for (const actionName of Object.keys(actions)) {
114118
if (actionName.indexOf(commands[1 + debugParameterOffset]) === 0) {
115119
yield actionName;
116120
}
117121
}
118122
} else {
119-
for (const actionName of Object.keys(TabCompleteAction._actions)) {
123+
for (const actionName of Object.keys(actions)) {
120124
if (actionName === commands[1 + debugParameterOffset]) {
121125
if (actionName === 'build' || actionName === 'rebuild') {
122126
const choiceParameter: string[] = ['-f', '--from', '-t', '--to'];
@@ -156,24 +160,24 @@ export class TabCompleteAction extends BaseRushAction {
156160
}
157161

158162
if (completePartialWord) {
159-
for (let i: number = 0; i < TabCompleteAction._actions[actionName].length; i++) {
160-
if (TabCompleteAction._actions[actionName][i].name.indexOf(lastCommand) === 0) {
161-
yield TabCompleteAction._actions[actionName][i].name;
163+
for (let i: number = 0; i < actions[actionName].length; i++) {
164+
if (actions[actionName][i].name.indexOf(lastCommand) === 0) {
165+
yield actions[actionName][i].name;
162166
}
163167
}
164168
} else {
165-
for (let i: number = 0; i < TabCompleteAction._actions[actionName].length; i++) {
169+
for (let i: number = 0; i < actions[actionName].length; i++) {
166170
if (
167-
lastCommand === TabCompleteAction._actions[actionName][i].name &&
168-
TabCompleteAction._actions[actionName][i].kind !== CommandLineParameterKind.Flag
171+
lastCommand === actions[actionName][i].name &&
172+
actions[actionName][i].kind !== CommandLineParameterKind.Flag
169173
) {
170174
// The parameter is expecting a value, so don't suggest parameter names again
171175
return;
172176
}
173177
}
174178

175-
for (let i: number = 0; i < TabCompleteAction._actions[actionName].length; i++) {
176-
yield TabCompleteAction._actions[actionName][i].name;
179+
for (let i: number = 0; i < actions[actionName].length; i++) {
180+
yield actions[actionName][i].name;
177181
}
178182
}
179183
}
@@ -198,16 +202,8 @@ export class TabCompleteAction extends BaseRushAction {
198202
}
199203
} else {
200204
if (choiceParameter.indexOf(lastCommand) !== -1) {
201-
for (let i: number = 0; i < choiceParamaterValues.length; i++) {
202-
yield choiceParamaterValues[i];
203-
}
205+
yield* choiceParamaterValues;
204206
}
205207
}
206208
}
207-
208-
private *_getAllActions(): IterableIterator<string> {
209-
for (const actionName of Object.keys(TabCompleteAction._actions)) {
210-
yield actionName;
211-
}
212-
}
213209
}

0 commit comments

Comments
 (0)