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ACD,KAAK,EAAE,CAAC;SACT;QAED,OAAO,KAAK,CAAC;IACf,CAAC;IAEO,CAAC,wBAAwB,CAC/B,qBAA+B,EAC/B,SAAiB;QAEjB,KAAK,MAAM,oBAAoB,IAAI,qBAAqB,EAAE;YACxD,IAAI,oBAAoB,CAAC,OAAO,CAAC,SAAS,CAAC,KAAK,CAAC,EAAE;gBACjD,MAAM,oBAAoB,CAAC;aAC5B;SACF;IACH,CAAC;CACF;AA5MD,8CA4MC","sourcesContent":["// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.\n// See LICENSE in the project root for license information.\n\nimport stringArgv from 'string-argv';\n\nimport type { CommandLineIntegerParameter } from '../parameters/CommandLineIntegerParameter';\nimport type { CommandLineStringParameter } from '../parameters/CommandLineStringParameter';\nimport {\n CommandLineParameterKind,\n type CommandLineParameter,\n CommandLineParameterWithArgument\n} from '../parameters/BaseClasses';\nimport { CommandLineChoiceParameter } from '../parameters/CommandLineChoiceParameter';\nimport { CommandLineAction } from './CommandLineAction';\nimport { CommandLineConstants } from '../Constants';\n\nconst DEFAULT_WORD_TO_AUTOCOMPLETE: string = '';\nconst DEFAULT_POSITION: number = 0;\n\nexport class TabCompleteAction extends CommandLineAction {\n private readonly _wordToCompleteParameter: CommandLineStringParameter;\n private readonly _positionParameter: CommandLineIntegerParameter;\n private readonly _actions: Map>;\n private readonly _globalParameters: Map;\n\n public constructor(\n actions: ReadonlyArray,\n globalParameters: ReadonlyArray\n ) {\n super({\n actionName: CommandLineConstants.TabCompletionActionName,\n summary: 'Provides tab completion.',\n documentation: 'Provides tab completion.'\n });\n\n this._actions = new Map>();\n for (const action of actions) {\n const parameterNameToParameterInfoMap: Map = new Map<\n string,\n CommandLineParameter\n >();\n for (const parameter of action.parameters) {\n parameterNameToParameterInfoMap.set(parameter.longName, parameter);\n if (parameter.shortName) {\n parameterNameToParameterInfoMap.set(parameter.shortName, parameter);\n }\n }\n this._actions.set(action.actionName, parameterNameToParameterInfoMap);\n }\n\n this._globalParameters = new Map();\n for (const parameter of globalParameters) {\n this._globalParameters.set(parameter.longName, parameter);\n if (parameter.shortName) {\n this._globalParameters.set(parameter.shortName, parameter);\n }\n }\n\n this._wordToCompleteParameter = this.defineStringParameter({\n parameterLongName: '--word',\n argumentName: 'WORD',\n description: `The word to complete.`,\n defaultValue: DEFAULT_WORD_TO_AUTOCOMPLETE\n });\n\n this._positionParameter = this.defineIntegerParameter({\n parameterLongName: '--position',\n argumentName: 'INDEX',\n description: `The position in the word to be completed.`,\n defaultValue: DEFAULT_POSITION\n });\n }\n\n protected async onExecute(): Promise {\n const commandLine: string = this._wordToCompleteParameter.value || '';\n const caretPosition: number = this._positionParameter.value || (commandLine && commandLine.length) || 0;\n\n for await (const value of this.getCompletions(commandLine, caretPosition)) {\n // eslint-disable-next-line no-console\n console.log(value);\n }\n }\n\n public async *getCompletions(\n commandLine: string,\n caretPosition: number = commandLine.length\n ): AsyncIterable {\n const actions: Map> = this._actions;\n\n if (!commandLine || !caretPosition) {\n yield* this._getAllActions();\n return;\n }\n\n const tokens: string[] = Array.from(this.tokenizeCommandLine(commandLine));\n\n // offset arguments by the number of global params in the input\n const globalParameterOffset: number = this._getGlobalParameterOffset(tokens);\n\n if (tokens.length < 2 + globalParameterOffset) {\n yield* this._getAllActions();\n return;\n }\n\n const lastToken: string = tokens[tokens.length - 1];\n const secondLastToken: string = tokens[tokens.length - 2];\n\n const completePartialWord: boolean = caretPosition === commandLine.length;\n\n if (completePartialWord && tokens.length === 2 + globalParameterOffset) {\n for (const actionName of actions.keys()) {\n if (actionName.indexOf(tokens[1 + globalParameterOffset]) === 0) {\n yield actionName;\n }\n }\n } else {\n for (const actionName of actions.keys()) {\n if (actionName === tokens[1 + globalParameterOffset]) {\n const parameterNameMap: Map = actions.get(actionName)!;\n\n const parameterNames: string[] = Array.from(parameterNameMap.keys());\n\n if (completePartialWord) {\n for (const parameterName of parameterNames) {\n if (parameterName === secondLastToken) {\n const values: string[] = await this._getParameterValueCompletions(\n parameterNameMap.get(parameterName)!\n );\n if (values.length > 0) {\n yield* this._completeParameterValues(values, lastToken);\n return;\n }\n }\n }\n yield* this._completeParameterValues(parameterNames, lastToken);\n } else {\n for (const parameterName of parameterNames) {\n if (parameterName === lastToken) {\n const values: string[] = await this._getParameterValueCompletions(\n parameterNameMap.get(parameterName)!\n );\n if (values.length > 0) {\n yield* values;\n return;\n }\n }\n }\n for (const parameterName of parameterNames) {\n if (\n parameterName === lastToken &&\n parameterNameMap.get(parameterName)!.kind !== CommandLineParameterKind.Flag\n ) {\n // The parameter is expecting a value, so don't suggest parameter names again\n return;\n }\n }\n\n yield* parameterNames;\n }\n\n break;\n }\n }\n }\n }\n\n private *_getAllActions(): IterableIterator {\n yield* this._actions.keys();\n yield* this._globalParameters.keys();\n }\n\n public tokenizeCommandLine(commandLine: string): string[] {\n return stringArgv(commandLine);\n }\n\n private async _getParameterValueCompletions(parameter: CommandLineParameter): Promise {\n let choiceParameterValues: string[] = [];\n if (parameter.kind === CommandLineParameterKind.Choice) {\n choiceParameterValues = (parameter as CommandLineChoiceParameter).alternatives as string[];\n } else if (parameter.kind !== CommandLineParameterKind.Flag) {\n let parameterWithArgumentOrChoices:\n | CommandLineParameterWithArgument\n | CommandLineChoiceParameter\n | undefined = undefined;\n if (parameter instanceof CommandLineParameterWithArgument) {\n parameterWithArgumentOrChoices = parameter as CommandLineParameterWithArgument;\n } else if (parameter instanceof CommandLineChoiceParameter) {\n parameterWithArgumentOrChoices = parameter as CommandLineChoiceParameter;\n }\n if (parameterWithArgumentOrChoices && parameterWithArgumentOrChoices.completions) {\n choiceParameterValues = await parameterWithArgumentOrChoices.completions();\n }\n }\n\n return choiceParameterValues;\n }\n\n private _getGlobalParameterOffset(tokens: string[]): number {\n const globalParameters: Map = this._globalParameters;\n let count: number = 0;\n\n outer: for (let i: number = 1; i < tokens.length; i++) {\n for (const globalParameter of globalParameters.values()) {\n if (tokens[i] !== globalParameter.longName && tokens[i] !== globalParameter.shortName) {\n break outer;\n }\n }\n count++;\n }\n\n return count;\n }\n\n private *_completeParameterValues(\n choiceParameterValues: string[],\n lastToken: string\n ): IterableIterator {\n for (const choiceParameterValue of choiceParameterValues) {\n if (choiceParameterValue.indexOf(lastToken) === 0) {\n yield choiceParameterValue;\n }\n }\n }\n}\n"]}