import { Position , TextDocument } from "vscode-languageserver" ; import { getLineOffsets } from "./arrayUtils" ; const isEOL = ( c ) => { return c === 0x0a || c === 0x0d ; } ; interface Output { newDocument : TextDocument ; newPosition : Position ; } export const completionHelper = ( document , textDocumentPosition ) : Output => { const linePos = textDocumentPosition . line ; const position = textDocumentPosition ; const lineOffset = getLineOffsets ( document . getText ( ) ) ; const start = lineOffset [ linePos ] ; let end = 0 ; if ( lineOffset [ linePos + 1 ] ) { end = lineOffset [ linePos + 1 ] ; } else { end = document . getText ( ) . length ; } while ( end - 1 >= 0 && isEOL ( document . getText ( ) . charCodeAt ( end - 1 ) ) ) { end -- ; } const textLine = document . getText ( ) . substring ( start , end ) ; if ( textLine . indexOf ( ":" ) === - 1 ) { let newText = "" ; const trimmedText = textLine . trim ( ) ; if ( trimmedText . length === 0 || ( trimmedText . length === 1 && trimmedText [ 0 ] === "-" ) ) { newText = document . getText ( ) . substring ( 0 , start + textLine . length ) + ( trimmedText [ 0 ] === "-" && ! textLine . endsWith ( " " ) ? " " : "" ) + "holder:\r\n" + document . getText ( ) . substr ( lineOffset [ linePos + 1 ] || document . getText ( ) . length ) ; } else { newText = document . getText ( ) . substring ( 0 , start + textLine . length ) + ":\r\n" + document . getText ( ) . substr ( lineOffset [ linePos + 1 ] || document . getText ( ) . length ) ; } return { newDocument : TextDocument . create ( document . uri , document . languageId , document . version , newText ) , newPosition : textDocumentPosition } ; } else { position . character = position . character - 1 ; return { newDocument : document , newPosition : position } ; } } ;