import { CompletionList , createConnection , Diagnostic , InitializeParams , ProposedFeatures , TextDocuments , TextDocumentSyncKind } from 'vscode-languageserver' ; import { getLanguageModes , LanguageModes } from './languageModes' ; import { TextDocument } from 'vscode-languageserver-textdocument' ; const connection = createConnection ( ProposedFeatures . all ) ; const documents = new TextDocuments ( TextDocument ) ; let languageModes ; connection . onInitialize ( ( _params ) => { languageModes = getLanguageModes ( ) ; documents . onDidClose ( e => { languageModes . onDocumentRemoved ( e . document ) ; } ) ; connection . onShutdown ( ( ) => { languageModes . dispose ( ) ; } ) ; return { capabilities : { textDocumentSync : TextDocumentSyncKind . Full , completionProvider : { resolveProvider : false } } } ; } ) ; connection . onDidChangeConfiguration ( _change => { documents . all ( ) . forEach ( validateTextDocument ) ; } ) ; documents . onDidChangeContent ( change => { validateTextDocument ( change . document ) ; } ) ; async function validateTextDocument ( textDocument ) { try { const version = textDocument . version ; const diagnostics = [ ] ; if ( textDocument . languageId === 'html1' ) { const modes = languageModes . getAllModesInDocument ( textDocument ) ; const latestTextDocument = documents . get ( textDocument . uri ) ; if ( latestTextDocument && latestTextDocument . version === version ) { modes . forEach ( mode => { if ( mode . doValidation ) { mode . doValidation ( latestTextDocument ) . forEach ( d => { diagnostics . push ( d ) ; } ) ; } } ) ; connection . sendDiagnostics ( { uri : latestTextDocument . uri , diagnostics } ) ; } } } catch ( e ) { connection . console . error ( `Error while validating ${ textDocument . uri }` ) ; connection . console . error ( String ( e ) ) ; } } connection . onCompletion ( async ( textDocumentPosition , token ) => { const document = documents . get ( textDocumentPosition . textDocument . uri ) ; if ( ! document ) { return null ; } const mode = languageModes . getModeAtPosition ( document , textDocumentPosition . position ) ; if ( ! mode || ! mode . doComplete ) { return CompletionList . create ( ) ; } const doComplete = mode . doComplete ! ; return doComplete ( document , textDocumentPosition . position ) ; } ) ; documents . listen ( connection ) ; connection . listen ( ) ;