import * as vscode from 'vscode' ; import { IParameterDefinition } from '../documents/parameters/IParameterDefinition' ; import { IFunctionMetadata } from '../documents/templates/IFunctionMetadata' ; import { UserFunctionNamespaceDefinition } from '../documents/templates/UserFunctionNamespaceDefinition' ; import { IVariableDefinition } from "../documents/templates/VariableDefinition" ; import { assert } from '../fixed_assert' ; import { Span } from '../language/Span' ; export enum CompletionPriority { normal = "normal" , high = "high" , low = "low" } /**
 * A completion item in the list of completion suggestions that appear when a user invokes auto-completion (Ctrl + Space).
 */ export class Item { public readonly label : string ; public readonly insertText : string ; public readonly span : Span ; public readonly kind : CompletionKind ; public readonly detail : string | undefined ; public readonly documention : string | vscode . MarkdownString | undefined ; public readonly snippetName : string | undefined ; public readonly additionalEdits : { span : Span ; insertText : string ; } [ ] | undefined ; public readonly sortText : string | undefined ; public readonly commitCharacters : string [ ] | undefined ; public readonly priority : CompletionPriority ; public readonly preselect : boolean ; public readonly filterText ? : string ; public readonly telemetryProperties : { [ key ] : string ; } | undefined ; constructor ( options ) { this . label = options . label ; this . insertText = options . insertText ; this . span = options . span ; this . kind = options . kind ; this . detail = options . detail ; this . documention = options . documentation ; this . snippetName = options . snippetName ; this . additionalEdits = options . additionalEdits ; this . sortText = options . sortText ; this . filterText = options . filterText ; this . commitCharacters = options . commitCharacters ; this . priority = options . priority ?? CompletionPriority . normal ; this . preselect = ! ! options . preselect ; this . telemetryProperties = options . telemetryProperties ; } public static fromFunctionMetadata ( metadata , span ) { const insertText = metadata . unqualifiedName ; return new Item ( { label : metadata . fullName , insertText , span , kind : metadata . unqualifiedName === metadata . fullName ? CompletionKind . tleFunction : CompletionKind . tleUserFunction , detail : `(function) ${ metadata . usage }` , documentation : metadata . description } ) ; } public static fromNamespaceDefinition ( namespace , span ) { const label = namespace . nameValue . unquotedValue ; let insertText = `${ label }` ; return new Item ( { label , insertText , span : span , kind : CompletionKind . tleParameter , detail : `(namespace) ${ label }` , documentation : "User-defined namespace" } ) ; } public static fromPropertyName ( propertyName , span ) { return new Item ( { label : propertyName , insertText : propertyName , span , kind : CompletionKind . tleProperty , detail : "(property)" } ) ; } public static fromParameterDefinition ( parameter , span , includeRightParenthesisInCompletion , includeSingleQuotesInCompletion ) { const parameterName = parameter . nameValue . unquotedValue ; const label = includeSingleQuotesInCompletion ? `'${ parameterName }'` : parameterName ; let insertText = label ; if ( includeRightParenthesisInCompletion ) { assert ( includeSingleQuotesInCompletion , "includeSingleQuotesInCompletion required if includeRightParenthesisInCompletion" ) ; insertText = `${ insertText })` ; } return new Item ( { label , insertText , span , kind : CompletionKind . tleParameter , detail : `(parameter)` , documentation : parameter . description } ) ; } public static fromVariableDefinition ( variable , span , includeRightParenthesisInCompletion , includeSingleQuotesInCompletion ) { const variableName = variable . nameValue . unquotedValue ; const label = includeSingleQuotesInCompletion ? `'${ variableName }'` : variableName ; let insertText = label ; if ( includeRightParenthesisInCompletion ) { assert ( includeSingleQuotesInCompletion , "includeSingleQuotesInCompletion required if includeRightParenthesisInCompletion" ) ; insertText = `${ insertText })` ; } return new Item ( { label , insertText , span , kind : CompletionKind . tleVariable , detail : `(variable)` } ) ; } /**
     * Dupes the completions list by label, without affecting order
     */ public static dedupeByLabel ( items ) { const addedLC = new Set < string > ( ) ; const deduped = [ ] ; for ( let item of items ) { const itemLabelLC = item . label . toLowerCase ( ) ; if ( ! addedLC . has ( itemLabelLC ) ) { deduped . push ( item ) ; addedLC . add ( itemLabelLC ) ; } } return deduped ; } } export enum CompletionKind { tleFunction = "tleFunction" , tleParameter = "tleParameter" , tleVariable = "tleVariable" , tleProperty = "tleProperty" , tleNamespace = "tleNamespace" , tleUserFunction = "tleUserFunction" , tleResourceIdResTypeParameter = "tleResourceIdResTypeParameter" , tleResourceIdResNameParameter = "tleResourceIdResNameParameter" , PropertyValueForExistingProperty = "PropertyValueForExistingProperty" , PropertyValueForNewProperty = "PropertyValueForNewProperty" , dependsOnResourceId = "dependsOnResourceId" , dependsOnResourceCopyLoop = "dependsOnResourceCopyLoop" , Snippet = "Snippet" }