import * as vscode from 'vscode' ; import { IActionContext } from 'vscode-azureextensionui' ; import { IJsonDocument } from '../documents/templates/IJsonDocument' ; import { assert } from '../fixed_assert' ; import { assertNever } from '../util/assertNever' ; import * as Completion from './Completion' ; import { getVSCodeRangeFromSpan } from './vscodePosition' ; export function toVsCodeCompletionItem ( jsonDocument , item , cursorPosition ) { const range = getVSCodeRangeFromSpan ( jsonDocument , item . span ) ; const vscodeItem = new vscode . CompletionItem ( item . label ) ; vscodeItem . range = range ; const insertText = item . insertText ; vscodeItem . insertText = new vscode . SnippetString ( insertText ) ; vscodeItem . detail = item . detail ; vscodeItem . documentation = item . documention ; vscodeItem . commitCharacters = item . commitCharacters ; vscodeItem . preselect = item . preselect ; vscodeItem . filterText = item . filterText ; vscodeItem . kind = toVsCodeCompletionItemKind ( item . kind ) ; let sortPriorityPrefix ; switch ( item . priority ) { case Completion . CompletionPriority . low : sortPriorityPrefix = `${ String . fromCharCode ( 255 ) }-` ; break ; case Completion . CompletionPriority . high : sortPriorityPrefix = `${ String . fromCharCode ( 1 ) }-` ; break ; case Completion . CompletionPriority . normal : sortPriorityPrefix = '' ; break ; default : assertNever ( item . priority ) ; } vscodeItem . sortText = `${ sortPriorityPrefix }${ item . sortText ?? item . label }` ; if ( item . additionalEdits ) { vscodeItem . additionalTextEdits = item . additionalEdits . map ( e => new vscode . TextEdit ( getVSCodeRangeFromSpan ( jsonDocument , e . span ) , e . insertText ) ) ; } const telemetryArgs = { snippet : item . snippetName , kind : item . kind , function : item . kind === Completion . CompletionKind . tleFunction ? item . label : undefined } ; for ( let key of Object . getOwnPropertyNames ( item . telemetryProperties ?? { } ) ) { telemetryArgs [ key ] = item . telemetryProperties ?. [ key ] ; } vscodeItem . command = { command : "azurerm-vscode-tools.completion-activated" , title : "completion activated" , arguments : [ telemetryArgs ] } ; assert ( vscodeItem . range , "Completion item doesn't have a range" ) ; assert ( vscodeItem . range ! . contains ( cursorPosition ) , "Completion item range doesn't include cursor" ) ; assert ( vscodeItem . range ! . isSingleLine , "Completion item range must be a single line" ) ; return vscodeItem ; } export function toVsCodeCompletionItemKind ( kind ) { switch ( kind ) { case Completion . CompletionKind . tleFunction : case Completion . CompletionKind . tleUserFunction : return vscode . CompletionItemKind . Function ; case Completion . CompletionKind . tleParameter : case Completion . CompletionKind . tleVariable : return vscode . CompletionItemKind . Variable ; case Completion . CompletionKind . tleProperty : return vscode . CompletionItemKind . Field ; case Completion . CompletionKind . tleNamespace : return vscode . CompletionItemKind . Unit ; case Completion . CompletionKind . PropertyValueForExistingProperty : return vscode . CompletionItemKind . Property ; case Completion . CompletionKind . PropertyValueForNewProperty : return vscode . CompletionItemKind . Snippet ; case Completion . CompletionKind . tleResourceIdResTypeParameter : case Completion . CompletionKind . tleResourceIdResNameParameter : case Completion . CompletionKind . dependsOnResourceId : return vscode . CompletionItemKind . Reference ; case Completion . CompletionKind . dependsOnResourceCopyLoop : return vscode . CompletionItemKind . Enum ; case Completion . CompletionKind . Snippet : return vscode . CompletionItemKind . Snippet ; default : assertNever ( kind ) ; } } /**
 * This is called after a snippet or other completion item is executed by vscode.  Gives us a chance to report it in
 * telemetry and do any clean-up
 */ export function onCompletionActivated ( actionContext , telemetryProperties ) { Object . assign ( actionContext . telemetry . properties , telemetryProperties ?? { } ) ; }