'use strict' ; import { createConnection , TextDocuments , TextDocument , Diagnostic , DiagnosticSeverity , ProposedFeatures , InitializeParams , DidChangeConfigurationNotification , CompletionItem , CompletionItemKind , TextDocumentPositionParams } from 'vscode-languageserver' ; import Uri from 'vscode-uri' ; var c64jasm = require ( 'c64jasm' ) ; let connection = createConnection ( ProposedFeatures . all ) ; let documents = new TextDocuments ( ) ; let hasConfigurationCapability = false ; let hasWorkspaceFolderCapability = false ; connection . onInitialize ( ( params ) => { let capabilities = params . capabilities ; hasConfigurationCapability = capabilities . workspace && ! ! capabilities . workspace . configuration ; hasWorkspaceFolderCapability = capabilities . workspace && ! ! capabilities . workspace . workspaceFolders ; return { capabilities : { textDocumentSync : documents . syncKind , completionProvider : { resolveProvider : true } } } ; } ) ; connection . onInitialized ( ( ) => { if ( hasConfigurationCapability ) { connection . client . register ( DidChangeConfigurationNotification . type , undefined ) ; } if ( hasWorkspaceFolderCapability ) { connection . workspace . onDidChangeWorkspaceFolders ( _event => { connection . console . log ( 'Workspace folder change event received.' ) ; } ) ; } } ) ; interface C64jasmSettings { maxNumberOfProblems : number ; } const defaultSettings = { maxNumberOfProblems : 1000 } ; let globalSettings = defaultSettings ; let documentSettings = new Map ( ) ; connection . onDidChangeConfiguration ( change => { if ( hasConfigurationCapability ) { documentSettings . clear ( ) ; } else { globalSettings = < C64jasmSettings > ( ( change . settings . languageServerC64jasm || defaultSettings ) ) ; } documents . all ( ) . forEach ( validateTextDocument ) ; } ) ; function getDocumentSettings ( resource ) { if ( ! hasConfigurationCapability ) { return Promise . resolve ( globalSettings ) ; } let result = documentSettings . get ( resource ) ; if ( ! result ) { result = connection . workspace . getConfiguration ( { scopeUri : resource , section : 'languageServerC64jasm' } ) ; documentSettings . set ( resource , result ) ; } return result ; } documents . onDidClose ( e => { documentSettings . delete ( e . document . uri ) ; } ) ; documents . onDidChangeContent ( _change => { } ) ; documents . onDidOpen ( change => { validateTextDocument ( change . document ) ; } ) ; documents . onDidSave ( ( event ) => { validateTextDocument ( event . document ) ; } ) ; async function validateTextDocument ( textDocument ) { let settings = await getDocumentSettings ( textDocument . uri ) ; const sourceFname = Uri . parse ( textDocument . uri ) . fsPath ; const { errors } = c64jasm . assemble ( sourceFname ) ; let diagnostics = [ ] ; for ( let errIdx = 0 ; errIdx < errors . length ; errIdx ++ ) { if ( errIdx >= settings . maxNumberOfProblems ) { break ; } const err = errors [ errIdx ] ; connection . console . log ( `error from asm=${ JSON . stringify ( err ) }` ) ; const loc = err . loc ; let diagnostic = { severity : DiagnosticSeverity . Error , range : { start : textDocument . positionAt ( loc . start . offset ) , end : textDocument . positionAt ( loc . end . offset ) } , message : err . msg , source : 'c64jasm' } ; diagnostics . push ( diagnostic ) ; } connection . sendDiagnostics ( { uri : textDocument . uri , diagnostics } ) ; } connection . onDidChangeWatchedFiles ( _change => { connection . console . log ( 'We received an file change event' ) ; } ) ; connection . onCompletion ( ( _textDocumentPosition ) : CompletionItem [ ] => { return [ { label : 'TypeScript' , kind : CompletionItemKind . Text , data : 1 } , { label : 'JavaScript' , kind : CompletionItemKind . Text , data : 2 } ] ; } ) ; connection . onCompletionResolve ( ( item ) : CompletionItem => { if ( item . data === 1 ) { ( item . detail = 'TypeScript details' ) , ( item . documentation = 'TypeScript documentation' ) ; } else if ( item . data === 2 ) { ( item . detail = 'JavaScript details' ) , ( item . documentation = 'JavaScript documentation' ) ; } return item ; } ) ; documents . listen ( connection ) ; connection . listen ( ) ;