// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license. // See LICENSE in the project root for license information. import stringArgv from 'string-argv'; import type { IRequiredCommandLineIntegerParameter } from '../parameters/CommandLineIntegerParameter'; import type { IRequiredCommandLineStringParameter } from '../parameters/CommandLineStringParameter'; import { CommandLineParameterKind, type CommandLineParameterBase, CommandLineParameterWithArgument, type CommandLineParameter } from '../parameters/BaseClasses'; import { CommandLineChoiceParameter } from '../parameters/CommandLineChoiceParameter'; import { CommandLineAction } from './CommandLineAction'; import { CommandLineConstants } from '../Constants'; const DEFAULT_WORD_TO_AUTOCOMPLETE: string = ''; const DEFAULT_POSITION: number = 0; export class TabCompleteAction extends CommandLineAction { readonly #wordToCompleteParameter: IRequiredCommandLineStringParameter; readonly #positionParameter: IRequiredCommandLineIntegerParameter; readonly #actions: Map>; readonly #globalParameters: Map; public constructor( actions: ReadonlyArray, globalParameters: ReadonlyArray ) { super({ actionName: CommandLineConstants.TabCompletionActionName, summary: 'Provides tab completion.', documentation: 'Provides tab completion.' }); this.#actions = new Map(); for (const action of actions) { const parameterNameToParameterInfoMap: Map = new Map(); for (const parameter of action.parameters) { parameterNameToParameterInfoMap.set(parameter.longName, parameter as CommandLineParameter); if (parameter.shortName) { parameterNameToParameterInfoMap.set(parameter.shortName, parameter as CommandLineParameter); } } this.#actions.set(action.actionName, parameterNameToParameterInfoMap); } this.#globalParameters = new Map(); for (const parameter of globalParameters) { this.#globalParameters.set(parameter.longName, parameter as CommandLineParameter); if (parameter.shortName) { this.#globalParameters.set(parameter.shortName, parameter as CommandLineParameter); } } this.#wordToCompleteParameter = this.defineStringParameter({ parameterLongName: '--word', argumentName: 'WORD', description: `The word to complete.`, defaultValue: DEFAULT_WORD_TO_AUTOCOMPLETE }); this.#positionParameter = this.defineIntegerParameter({ parameterLongName: '--position', argumentName: 'INDEX', description: `The position in the word to be completed.`, defaultValue: DEFAULT_POSITION }); } protected override async onExecuteAsync(): Promise { const commandLine: string = this.#wordToCompleteParameter.value; const caretPosition: number = this.#positionParameter.value || commandLine.length; for await (const value of this.getCompletionsAsync(commandLine, caretPosition)) { // eslint-disable-next-line no-console console.log(value); } } public async *getCompletionsAsync( commandLine: string, caretPosition: number = commandLine.length ): AsyncIterable { const actions: Map> = this.#actions; if (!commandLine || !caretPosition) { yield* this.#getAllActions(); return; } const tokens: string[] = Array.from(this.tokenizeCommandLine(commandLine)); // offset arguments by the number of global params in the input const globalParameterOffset: number = this.#getGlobalParameterOffset(tokens); if (tokens.length < 2 + globalParameterOffset) { yield* this.#getAllActions(); return; } const lastToken: string = tokens[tokens.length - 1]; const secondLastToken: string = tokens[tokens.length - 2]; const lastCharacterIsWhitespace: boolean = !commandLine.slice(-1).trim(); const completePartialWord: boolean = caretPosition === commandLine.length && !lastCharacterIsWhitespace; if (completePartialWord && tokens.length === 2 + globalParameterOffset) { for (const actionName of actions.keys()) { if (actionName.indexOf(tokens[1 + globalParameterOffset]) === 0) { yield actionName; } } } else { for (const actionName of actions.keys()) { if (actionName === tokens[1 + globalParameterOffset]) { const parameterNameMap: Map = actions.get(actionName)!; const parameterNames: string[] = Array.from(parameterNameMap.keys()); if (completePartialWord) { for (const parameterName of parameterNames) { if (parameterName === secondLastToken) { const values: ReadonlySet = await this.#getParameterValueCompletionsAsync( parameterNameMap.get(parameterName)! ); if (values.size > 0) { yield* this.#completeParameterValues(values, lastToken); return; } } } yield* this.#completeParameterValues(parameterNames, lastToken); } else { for (const parameterName of parameterNames) { if (parameterName === lastToken) { const values: ReadonlySet = await this.#getParameterValueCompletionsAsync( parameterNameMap.get(parameterName)! ); if (values.size > 0) { yield* values; return; } } } for (const parameterName of parameterNames) { if ( parameterName === lastToken && parameterNameMap.get(parameterName)!.kind !== CommandLineParameterKind.Flag ) { // The parameter is expecting a value, so don't suggest parameter names again return; } } yield* parameterNames; } break; } } } } *#getAllActions(): IterableIterator { yield* this.#actions.keys(); yield* this.#globalParameters.keys(); } public tokenizeCommandLine(commandLine: string): string[] { return stringArgv(commandLine); } async #getParameterValueCompletionsAsync( parameter: CommandLineParameter ): Promise> { let choiceParameterValues: ReadonlySet | undefined; if (parameter.kind === CommandLineParameterKind.Choice) { choiceParameterValues = parameter.alternatives; } else if (parameter.kind !== CommandLineParameterKind.Flag) { let parameterWithArgumentOrChoices: | CommandLineParameterWithArgument | CommandLineChoiceParameter | undefined = undefined; if ( parameter instanceof CommandLineParameterWithArgument || parameter instanceof CommandLineChoiceParameter ) { parameterWithArgumentOrChoices = parameter; } const completionValues: ReadonlyArray | ReadonlySet | undefined = await parameterWithArgumentOrChoices?.getCompletionsAsync?.(); choiceParameterValues = completionValues instanceof Set ? completionValues : new Set(completionValues); } return choiceParameterValues ?? new Set(); } #getGlobalParameterOffset(tokens: string[]): number { const globalParameters: Map = this.#globalParameters; let count: number = 0; outer: for (let i: number = 1; i < tokens.length; i++) { for (const globalParameter of globalParameters.values()) { if (tokens[i] !== globalParameter.longName && tokens[i] !== globalParameter.shortName) { break outer; } } count++; } return count; } *#completeParameterValues( choiceParameterValues: ReadonlyArray | ReadonlySet, lastToken: string ): IterableIterator { for (const choiceParameterValue of choiceParameterValues) { if (choiceParameterValue.indexOf(lastToken) === 0) { yield choiceParameterValue; } } } }