'use strict' ; import { CompletionItem , createConnection , IConnection , InitializeResult , ProposedFeatures , TextDocumentPositionParams , TextDocumentSyncKind , DidChangeTextDocumentParams , Definition , InitializeParams , DidChangeConfigurationParams , DidChangeWatchedFilesParams , Location } from 'vscode-languageserver' ; import { Builder } from './Builder' ; import { Config } from './Config' ; import { ServerProxy } from './Proxy' ; import URI from 'vscode-uri' ; let connection = createConnection ( ProposedFeatures . all ) ; connection . onInitialize ( ( params ) : InitializeResult => { console . log ( "Init project: " , params . rootUri ) ; Config . initWorkspace ( params . rootUri ) ; ServerProxy . init ( connection ) ; return { capabilities : { textDocumentSync : TextDocumentSyncKind . Full , completionProvider : { resolveProvider : true , triggerCharacters : [ "." , ":" ] } , definitionProvider : true } } ; } ) ; connection . onRequest ( "isQuickX" , ( ) => { return Config . isQuickX ; } ) ; /**
 * 配置初始化或内容改变时
 */ connection . onDidChangeConfiguration ( ( params ) => { Config . changeSettings ( params . settings ) ; } ) ; connection . onDidChangeWatchedFiles ( ( params ) => { console . log ( 'We recevied an file change event' ) ; } ) ; /**
 * 自动完成
 */ connection . onCompletion ( async ( params ) : Promise < CompletionItem [ ] > => { let uri = params . textDocument . uri ; let line = params . position . line ; let character = params . position . character ; let p = { filepath : URI . parse ( uri ) . fsPath , line : line } ; let lineData = await ServerProxy . connection . sendRequest < string > ( "getDocLine" , p ) ; if ( lineData . length < character ) { return [ ] ; } let prevContent = lineData . substring ( 0 , character ) ; return Builder . handleComplete ( uri , prevContent ) ; } ) ; connection . onCompletionResolve ( ( item ) : CompletionItem => { return item ; } ) ; /**
 * 内容改变时
 */ connection . onDidChangeTextDocument ( ( params ) => { if ( params . contentChanges . length > 0 ) { for ( let i = 0 ; i < params . contentChanges . length ; i ++ ) { const data = params . contentChanges [ i ] . text ; Builder . parseCode ( params . textDocument . uri , data ) ; } } } ) ; /**
 * 转到定义
 */ connection . onDefinition ( async ( params ) : Promise < Definition > => { let uri = params . textDocument . uri ; let line = params . position . line ; let character = params . position . character ; let p = { filepath : URI . parse ( uri ) . fsPath , line : line } ; let lineData = await ServerProxy . connection . sendRequest < string > ( "getDocLine" , p ) ; return Builder . handleDefinition ( uri , lineData , character ) ; } ) ; connection . listen ( ) ;