import * as vs from 'vscode' ; import * as serialport from 'serialport' ; export class ComPortAutoCompleteProvider implements vs . CompletionItemProvider { provideCompletionItems ( document , position , token ) { var currentLine = document . getText ( document . lineAt ( position ) . range ) ; if ( ! this . shouldProvide ( currentLine , position . character ) ) { return Promise . resolve ( [ ] ) ; } return this . listComPorts ( ) . then ( function ( ports ) { var result = ports . map ( function ( cfg ) { var item = new vs . CompletionItem ( cfg . comName ) ; item . insertText = " \"" + cfg . comName + "\"" ; item . detail = ( cfg . pnpId || '' ) + '    ' + ( cfg . manufacturer || '' ) ; item . sortText = "c" ; return item ; } ) ; var auto = new vs . CompletionItem ( "auto" ) ; auto . insertText = " \"auto\"" ; auto . detail = "Autodetect NodeMCU port" ; auto . sortText = "a" ; result . push ( auto ) ; return Promise . resolve ( result ) ; } ) ; } listComPorts ( ) { return new Promise ( function ( fulfill , reject ) { serialport . list ( ( error , ports ) => { if ( error != null ) { reject ( error ) ; } else { fulfill ( ports ) ; } } ) ; } ) ; } /**
     * Deterimine if we should provide path completion.
     */ shouldProvide ( currentLine , position ) { return currentLine . match ( "[\"\']port[\"\']\s*\:" ) ; } }