From 4fec3fff0082438dfd06195632fedfaafd182bac Mon Sep 17 00:00:00 2001 From: Megan Rogge Date: Mon, 4 Nov 2024 19:50:08 -0600 Subject: [PATCH] Refactor --- .../src/terminalSuggestMain.ts | 61 ++++++++++--------- extensions/terminal-suggest/src/types.ts | 5 ++ 2 files changed, 36 insertions(+), 30 deletions(-) create mode 100644 extensions/terminal-suggest/src/types.ts diff --git a/extensions/terminal-suggest/src/terminalSuggestMain.ts b/extensions/terminal-suggest/src/terminalSuggestMain.ts index e8d0d51e0c8..fd6d6ab0d2e 100644 --- a/extensions/terminal-suggest/src/terminalSuggestMain.ts +++ b/extensions/terminal-suggest/src/terminalSuggestMain.ts @@ -103,52 +103,53 @@ async function getCompletionSpecs(commands: Set): Promise { builtinCommands?.forEach(command => availableCommands.add(command)); const result: vscode.SimpleTerminalCompletion[] = []; for (const spec of specs) { - let name: string | undefined; - if ('displayName' in spec) { - name = spec.displayName; - } - if (!name && typeof spec.name === 'string') { - name = spec.name; - } else { - name = spec.name[0]; - } + const name = getLabel(spec); if (!name || !availableCommands.has(name)) { continue; } - result.push({ - label: name, - isFile: false, - isDirectory: false, - detail: 'description' in spec && spec.description ? spec.description ?? '' : '', - }); + result.push(createCompletionItem(name, spec.description)); + if (spec.options) { - // arguments for (const option of spec.options) { - let name = option.name; - if (typeof spec.name !== 'string') { - name = spec.name[0]; - } - if (!name) { - continue; - } - if (option.name) { - result.push({ - label: spec.name + ' ' + option.name, - isFile: false, - isDirectory: false, - detail: 'description' in option && option.description ? option.description ?? '' : '', - }); + const optionName = getArgumentLabel(spec, option); + if (optionName) { + result.push(createCompletionItem(`${spec.name} ${option.name}`, option.description)); } } } } + console.log(result.length); // Return the completion results or undefined if no results return result.length ? { items: result } : undefined; } }); +function getLabel(spec: Fig.Spec): string | undefined { + if ('displayName' in spec) { + return spec.displayName; + } + if (typeof spec.name === 'string') { + return spec.name; + } + return spec.name[0]; +} + +function getArgumentLabel(spec: Fig.Spec, option: any): string | undefined { + const commandName = getLabel(spec); + const argumentName = getLabel(option); + return `${commandName} ${argumentName}`; +} + +function createCompletionItem(label: string, description?: string): vscode.SimpleTerminalCompletion { + return { + label, + isFile: false, + isDirectory: false, + detail: description ?? '', + }; +} async function getCommandsInPath(): Promise> { // todo: use semicolon for windows diff --git a/extensions/terminal-suggest/src/types.ts b/extensions/terminal-suggest/src/types.ts new file mode 100644 index 00000000000..11a10ed5950 --- /dev/null +++ b/extensions/terminal-suggest/src/types.ts @@ -0,0 +1,5 @@ +/*--------------------------------------------------------------------------------------------- + * Copyright (c) Microsoft Corporation. All rights reserved. + * Licensed under the MIT License. See License.txt in the project root for license information. + *--------------------------------------------------------------------------------------------*/ +