import * as assert from 'assert' ; import { Selection , SnippetString , TextEditor } from 'vscode' ; import { DeploymentTemplateDoc , getVSCodeRangeFromSpan } from '../../extension.bundle' ; import { delay } from './delay' ; import { stringify } from './stringify' ; import { writeToLog } from './testLog' ; import { typeInDocumentAndWait } from './typeInDocumentAndWait' ; export async function simulateCompletion ( editor , completion , triggerCharacter ) { let pos = editor . selection . anchor ; let deploymentTemplate = new DeploymentTemplateDoc ( editor . document . getText ( ) , editor . document . uri , editor . document . version ) ; let pc = deploymentTemplate . getContextFromDocumentLineAndColumnIndexes ( pos . line , pos . character , undefined , true ) ; if ( triggerCharacter ) { const newContents = await typeInDocumentAndWait ( editor , triggerCharacter ) ; deploymentTemplate = new DeploymentTemplateDoc ( newContents , editor . document . uri , editor . document . version ) ; pos = editor . selection . anchor ; pc = deploymentTemplate . getContextFromDocumentLineAndColumnIndexes ( pos . line , pos . character , undefined , true ) ; } let result = await pc . getCompletionItems ( triggerCharacter , < number > editor . options . tabSize ) ; if ( result . triggerSuggest ) { writeToLog ( "triggering suggestion because result.triggerSuggest=true" ) ; const newContents = await typeInDocumentAndWait ( editor , '\n' ) ; deploymentTemplate = new DeploymentTemplateDoc ( newContents , editor . document . uri , editor . document . version ) ; pos = editor . selection . anchor ; pc = deploymentTemplate . getContextFromDocumentLineAndColumnIndexes ( pos . line , pos . character , undefined , true ) ; result = await pc . getCompletionItems ( undefined , < number > editor . options . tabSize ) ; assert ( ! result . triggerSuggest , "Shouldn't triggerSuggest twice" ) ; } const snippet = result . items . find ( s => s . label === completion || s . label === `"${ completion }"` ) ; writeToLog ( `Available completions: ${ stringify ( result ) }` ) ; if ( ! snippet ) { throw new Error ( `Couldn't find completion with label '${ completion }' that is available at this location` ) ; } const range = getVSCodeRangeFromSpan ( deploymentTemplate , snippet . span ) ; editor . selection = new Selection ( range . start , range . end ) ; if ( snippet . additionalEdits ?. length ) { assert ( snippet . additionalEdits . length === 1 , "Not implemented: More than one edit" ) ; const edit = snippet . additionalEdits [ 0 ] ; const r = getVSCodeRangeFromSpan ( deploymentTemplate , edit . span ) ; await editor . edit ( e => e . replace ( r , edit . insertText ) ) ; } await editor . insertSnippet ( new SnippetString ( snippet . insertText ) ) ; await delay ( 1 ) ; }