Skip to content

Commit a143486

Browse files
committed
Prevent exponential function call in getFigSuggestions
1 parent 264d466 commit a143486

File tree

2 files changed

+14
-3
lines changed

2 files changed

+14
-3
lines changed

extensions/terminal-suggest/src/fig/figInterface.ts

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,16 +46,26 @@ export async function getFigSuggestions(
4646
items: [],
4747
};
4848
const currentCommand = currentCommandAndArgString.split(' ')[0];
49+
50+
// Assemble a map to allow O(1) access to the available command from a spec
51+
// label. The label does not include an extension on Windows.
52+
const specLabelToAvailableCommandMap = new Map<string, ICompletionResource>();
53+
for (const command of availableCommands) {
54+
let label = typeof command.label === 'string' ? command.label : command.label.label;
55+
if (osIsWindows()) {
56+
label = removeAnyFileExtension(label);
57+
}
58+
specLabelToAvailableCommandMap.set(label, command);
59+
}
60+
4961
for (const spec of specs) {
5062
const specLabels = getFigSuggestionLabel(spec);
5163

5264
if (!specLabels) {
5365
continue;
5466
}
5567
for (const specLabel of specLabels) {
56-
const availableCommand = (osIsWindows()
57-
? availableCommands.find(command => (typeof command.label === 'string' ? command.label : command.label.label).match(new RegExp(`${specLabel}(\\.[^ ]+)?$`)))
58-
: availableCommands.find(command => (typeof command.label === 'string' ? command.label : command.label.label) === (specLabel)));
68+
const availableCommand = specLabelToAvailableCommandMap.get(specLabel);
5969
if (!availableCommand || (token && token.isCancellationRequested)) {
6070
continue;
6171
}

extensions/terminal-suggest/src/terminalSuggestMain.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -295,6 +295,7 @@ export async function activate(context: vscode.ExtensionContext) {
295295
}
296296
}
297297

298+
298299
if (terminal.shellIntegration?.cwd && (result.filesRequested || result.foldersRequested)) {
299300
return new vscode.TerminalCompletionList(result.items, {
300301
filesRequested: result.filesRequested,

0 commit comments

Comments
 (0)