From 6ef21326609cd1562709477b683726a8f2e0a2eb Mon Sep 17 00:00:00 2001 From: Pine Wu Date: Mon, 4 Jun 2018 11:00:52 -0700 Subject: [PATCH] Correct inform TS language service of updated js/ts docs. Fix #355 --- client/vueMain.ts | 4 ++-- server/src/modes/languageModes.ts | 1 + server/src/modes/script/javascript.ts | 3 +++ server/src/modes/script/serviceHost.ts | 13 ++++++++----- server/src/services/vls.ts | 12 +++++++++++- tslint.json | 1 - 6 files changed, 25 insertions(+), 9 deletions(-) diff --git a/client/vueMain.ts b/client/vueMain.ts index 9191364932..181e8910ec 100644 --- a/client/vueMain.ts +++ b/client/vueMain.ts @@ -74,8 +74,8 @@ export function activate(context: ExtensionContext) { const clientOptions: LanguageClientOptions = { documentSelector, synchronize: { - // the settings to synchronize - configurationSection: ['vetur', 'emmet', 'html', 'javascript', 'typescript', 'prettier', 'stylusSupremacy'] + configurationSection: ['vetur', 'emmet', 'html', 'javascript', 'typescript', 'prettier', 'stylusSupremacy'], + fileEvents: vscode.workspace.createFileSystemWatcher('{**/*.js,**/*.ts}', true, false, true) }, initializationOptions: { config diff --git a/server/src/modes/languageModes.ts b/server/src/modes/languageModes.ts index 47dd98b0dd..1cf4bfdcc8 100644 --- a/server/src/modes/languageModes.ts +++ b/server/src/modes/languageModes.ts @@ -45,6 +45,7 @@ export interface LanguageMode { findDocumentColors?(document: TextDocument): ColorInformation[]; getColorPresentations?(document: TextDocument, color: Color, range: Range): ColorPresentation[]; + onDocumentChanged?(filePath: string): void; onDocumentRemoved(document: TextDocument): void; dispose(): void; } diff --git a/server/src/modes/script/javascript.ts b/server/src/modes/script/javascript.ts index ff88edfe83..3c9ced004f 100644 --- a/server/src/modes/script/javascript.ts +++ b/server/src/modes/script/javascript.ts @@ -394,6 +394,9 @@ export function getJavascriptMode( onDocumentRemoved(document: TextDocument) { jsDocuments.onDocumentRemoved(document); }, + onDocumentChanged(filePath: string) { + serviceHost.updateTypescriptDocument(filePath); + }, dispose() { serviceHost.dispose(); jsDocuments.dispose(); diff --git a/server/src/modes/script/serviceHost.ts b/server/src/modes/script/serviceHost.ts index 602fb7fa03..085f40bedd 100644 --- a/server/src/modes/script/serviceHost.ts +++ b/server/src/modes/script/serviceHost.ts @@ -36,7 +36,7 @@ const vueSys: ts.System = { if (ts.sys.realpath) { const realpath = ts.sys.realpath; - vueSys.realpath = function (path) { + vueSys.realpath = function(path) { if (isVueProject(path)) { return realpath(path.slice(0, -3)) + '.ts'; } @@ -55,10 +55,7 @@ const defaultCompilerOptions: ts.CompilerOptions = { allowSyntheticDefaultImports: true }; -export function getServiceHost( - workspacePath: string, - jsDocuments: LanguageModelCache -) { +export function getServiceHost(workspacePath: string, jsDocuments: LanguageModelCache) { let currentScriptDoc: TextDocument; const versions = new Map(); const scriptDocs = new Map(); @@ -98,6 +95,11 @@ export function getServiceHost( }; } + function updateTypescriptDocument(filePath: string) { + const ver = versions.get(filePath) || 0; + versions.set(filePath, ver + 1); + } + function getScriptDocByFsPath(fsPath: string) { return scriptDocs.get(fsPath); } @@ -200,6 +202,7 @@ export function getServiceHost( let jsLanguageService = ts.createLanguageService(host); return { updateCurrentTextDocument, + updateTypescriptDocument, getScriptDocByFsPath, dispose: () => { jsLanguageService.dispose(); diff --git a/server/src/services/vls.ts b/server/src/services/vls.ts index 5c07ae9d3b..2ed87f3215 100644 --- a/server/src/services/vls.ts +++ b/server/src/services/vls.ts @@ -29,7 +29,8 @@ import { TextDocumentPositionParams, DocumentLinkParams, DocumentFormattingParams, - DidChangeConfigurationParams + DidChangeConfigurationParams, + FileChangeType } from 'vscode-languageserver'; import Uri from 'vscode-uri'; import * as path from 'path'; @@ -103,6 +104,15 @@ export class VLS { this.documentService.onDidClose(e => { this.removeDocument(e.document); }); + this.lspConnection.onDidChangeWatchedFiles(({ changes }) => { + const jsMode = this.languageModes.getMode('javascript'); + changes.forEach(c => { + if (c.type === FileChangeType.Changed) { + const fsPath = Uri.parse(c.uri).fsPath; + jsMode.onDocumentChanged!(fsPath); + } + }); + }); } configure(config: any): void { diff --git a/tslint.json b/tslint.json index bd048f44ea..d4a2b68e6f 100644 --- a/tslint.json +++ b/tslint.json @@ -20,7 +20,6 @@ "quotemark": [true, "single", "avoid-escape"], "whitespace": true, "semicolon": [true, "always"], - "space-before-function-paren": [true, {"anonymous": "always", "named": "never"}], "triple-equals": true, "variable-name": [true, "ban-keywords", "check-format", "allow-leading-underscore"] },