From 065fa5defa32f52d6ad9cd90f5b30e344946b429 Mon Sep 17 00:00:00 2001 From: yoyo930021 Date: Sat, 26 Sep 2020 22:44:19 +0800 Subject: [PATCH 1/7] Stop computing outdated diagnostics --- server/src/embeddedSupport/languageModes.ts | 3 +- server/src/modes/script/javascript.ts | 29 +++++++++++-- server/src/modes/style/index.ts | 2 +- server/src/modes/template/htmlMode.ts | 9 +++- server/src/modes/template/index.ts | 8 +++- .../src/modes/template/interpolationMode.ts | 11 ++++- server/src/services/vls.ts | 37 +++++++++++----- server/src/utils/cancellationToken.ts | 42 +++++++++++++++++++ 8 files changed, 121 insertions(+), 20 deletions(-) create mode 100644 server/src/utils/cancellationToken.ts diff --git a/server/src/embeddedSupport/languageModes.ts b/server/src/embeddedSupport/languageModes.ts index 13d455e1f5..78ae8f222b 100644 --- a/server/src/embeddedSupport/languageModes.ts +++ b/server/src/embeddedSupport/languageModes.ts @@ -38,6 +38,7 @@ import { getServiceHost, IServiceHost } from '../services/typescriptService/serv import { VLSFullConfig } from '../config'; import { SassLanguageMode } from '../modes/style/sass/sassLanguageMode'; import { getPugMode } from '../modes/pug'; +import { VCancellationToken } from '../utils/cancellationToken'; export interface VLSServices { infoService?: VueInfoService; @@ -49,7 +50,7 @@ export interface LanguageMode { configure?(options: VLSFullConfig): void; updateFileInfo?(doc: TextDocument): void; - doValidation?(document: TextDocument): Diagnostic[]; + doValidation?(document: TextDocument, cancellationToken?: VCancellationToken): Promise; getCodeActions?( document: TextDocument, range: Range, diff --git a/server/src/modes/script/javascript.ts b/server/src/modes/script/javascript.ts index 06b19f1f33..9ce7608586 100644 --- a/server/src/modes/script/javascript.ts +++ b/server/src/modes/script/javascript.ts @@ -46,6 +46,7 @@ import { RefactorAction } from '../../types'; import { IServiceHost } from '../../services/typescriptService/serviceHost'; import { toCompletionItemKind, toSymbolKind } from '../../services/typescriptService/util'; import * as Previewer from './previewer'; +import { isVCancellationTokenCancel, VCancellationToken } from '../../utils/cancellationToken'; // Todo: After upgrading to LS server 4.0, use CompletionContext for filtering trigger chars // https://microsoft.github.io/language-server-protocol/specification#completion-request-leftwards_arrow_with_hook @@ -145,18 +146,38 @@ export async function getJavascriptMode( } }, - doValidation(doc: TextDocument): Diagnostic[] { + async doValidation(doc: TextDocument, cancellationToken?: VCancellationToken): Promise { + if (await isVCancellationTokenCancel(cancellationToken)) { + return []; + } const { scriptDoc, service } = updateCurrentVueTextDocument(doc); if (!languageServiceIncludesFile(service, doc.uri)) { return []; } + if (await isVCancellationTokenCancel(cancellationToken)) { + return []; + } const fileFsPath = getFileFsPath(doc.uri); - const rawScriptDiagnostics = [ - ...service.getSyntacticDiagnostics(fileFsPath), - ...service.getSemanticDiagnostics(fileFsPath) + const program = service.getProgram(); + const sourceFile = program?.getSourceFile(fileFsPath); + if (!program || !sourceFile) { + return []; + } + + let rawScriptDiagnostics = [ + ...program.getSyntacticDiagnostics(sourceFile, cancellationToken?.tsToken), + ...program.getSemanticDiagnostics(sourceFile, cancellationToken?.tsToken) ]; + const compilerOptions = program.getCompilerOptions(); + if (!!(compilerOptions.declaration || compilerOptions.composite)) { + rawScriptDiagnostics = [ + ...rawScriptDiagnostics, + ...program.getDeclarationDiagnostics(sourceFile, cancellationToken?.tsToken) + ]; + } + return rawScriptDiagnostics.map(diag => { const tags: DiagnosticTag[] = []; diff --git a/server/src/modes/style/index.ts b/server/src/modes/style/index.ts index cda2dcdb82..c21f019db9 100644 --- a/server/src/modes/style/index.ts +++ b/server/src/modes/style/index.ts @@ -69,7 +69,7 @@ function getStyleMode( languageService.configure(c && c.css); config = c; }, - doValidation(document) { + async doValidation(document) { if (languageId === 'postcss') { return []; } else { diff --git a/server/src/modes/template/htmlMode.ts b/server/src/modes/template/htmlMode.ts index c31dee2c21..e694cc34ab 100644 --- a/server/src/modes/template/htmlMode.ts +++ b/server/src/modes/template/htmlMode.ts @@ -26,6 +26,7 @@ import { getComponentInfoTagProvider } from './tagProviders/componentInfoTagProv import { VueVersion } from '../../services/typescriptService/vueVersion'; import { doPropValidation } from './services/vuePropValidation'; import { getFoldingRanges } from './services/htmlFolding'; +import { isVCancellationTokenCancel, VCancellationToken } from '../../utils/cancellationToken'; export class HTMLMode implements LanguageMode { private tagProviderSettings: CompletionConfiguration; @@ -60,9 +61,12 @@ export class HTMLMode implements LanguageMode { this.config = c; } - doValidation(document: TextDocument) { + async doValidation(document: TextDocument, cancellationToken?: VCancellationToken) { const diagnostics = []; + if (await isVCancellationTokenCancel(cancellationToken)) { + return []; + } if (this.config.vetur.validation.templateProps) { const info = this.vueInfoService ? this.vueInfoService.getInfo(document) : undefined; if (info && info.componentInfo.childComponents) { @@ -70,6 +74,9 @@ export class HTMLMode implements LanguageMode { } } + if (await isVCancellationTokenCancel(cancellationToken)) { + return diagnostics; + } if (this.config.vetur.validation.template) { const embedded = this.embeddedDocuments.refreshAndGet(document); diagnostics.push(...doESLintValidation(embedded, this.lintEngine)); diff --git a/server/src/modes/template/index.ts b/server/src/modes/template/index.ts index 31af1df5d2..ed9794fef9 100644 --- a/server/src/modes/template/index.ts +++ b/server/src/modes/template/index.ts @@ -18,6 +18,7 @@ import { IServiceHost } from '../../services/typescriptService/serviceHost'; import { T_TypeScript } from '../../services/dependencyService'; import { HTMLDocument, parseHTMLDocument } from './parser/htmlParser'; import { inferVueVersion } from '../../services/typescriptService/vueVersion'; +import { VCancellationToken } from '../../utils/cancellationToken'; type DocumentRegionCache = LanguageModelCache; @@ -47,8 +48,11 @@ export class VueHTMLMode implements LanguageMode { queryVirtualFileInfo(fileName: string, currFileText: string) { return this.vueInterpolationMode.queryVirtualFileInfo(fileName, currFileText); } - doValidation(document: TextDocument) { - return this.htmlMode.doValidation(document).concat(this.vueInterpolationMode.doValidation(document)); + async doValidation(document: TextDocument, cancellationToken?: VCancellationToken) { + return Promise.all([ + this.vueInterpolationMode.doValidation(document, cancellationToken), + this.htmlMode.doValidation(document, cancellationToken) + ]).then(result => [...result[0], ...result[1]]); } doComplete(document: TextDocument, position: Position) { const htmlList = this.htmlMode.doComplete(document, position); diff --git a/server/src/modes/template/interpolationMode.ts b/server/src/modes/template/interpolationMode.ts index a4cc0cfaaa..92d60ebedb 100644 --- a/server/src/modes/template/interpolationMode.ts +++ b/server/src/modes/template/interpolationMode.ts @@ -23,6 +23,7 @@ import { mapBackRange, mapFromPositionToOffset } from '../../services/typescript import { createTemplateDiagnosticFilter } from '../../services/typescriptService/templateDiagnosticFilter'; import { toCompletionItemKind } from '../../services/typescriptService/util'; import { VueInfoService } from '../../services/vueInfoService'; +import { isVCancellationTokenCancel, VCancellationToken } from '../../utils/cancellationToken'; import { getFileFsPath } from '../../utils/paths'; import { NULL_COMPLETION } from '../nullMode'; import { languageServiceIncludesFile } from '../script/javascript'; @@ -52,7 +53,7 @@ export class VueInterpolationMode implements LanguageMode { return this.serviceHost.queryVirtualFileInfo(fileName, currFileText); } - doValidation(document: TextDocument): Diagnostic[] { + async doValidation(document: TextDocument, cancellationToken?: VCancellationToken): Promise { if ( !_.get(this.config, ['vetur', 'experimental', 'templateInterpolationService'], true) || !this.config.vetur.validation.interpolation @@ -60,6 +61,10 @@ export class VueInterpolationMode implements LanguageMode { return []; } + if (await isVCancellationTokenCancel(cancellationToken)) { + return []; + } + // Add suffix to process this doc as vue template. const templateDoc = TextDocument.create( document.uri + '.template', @@ -81,6 +86,10 @@ export class VueInterpolationMode implements LanguageMode { return []; } + if (await isVCancellationTokenCancel(cancellationToken)) { + return []; + } + const templateFileFsPath = getFileFsPath(templateDoc.uri); // We don't need syntactic diagnostics because // compiled template is always valid JavaScript syntax. diff --git a/server/src/services/vls.ts b/server/src/services/vls.ts index aeb9244ce3..dc1fce0647 100644 --- a/server/src/services/vls.ts +++ b/server/src/services/vls.ts @@ -21,7 +21,8 @@ import { CompletionTriggerKind, ExecuteCommandParams, ApplyWorkspaceEditRequest, - FoldingRangeParams + FoldingRangeParams, + CancellationTokenSource } from 'vscode-languageserver'; import { ColorInformation, @@ -46,7 +47,7 @@ import { URI } from 'vscode-uri'; import { LanguageModes, LanguageModeRange, LanguageMode } from '../embeddedSupport/languageModes'; import { NULL_COMPLETION, NULL_HOVER, NULL_SIGNATURE } from '../modes/nullMode'; import { VueInfoService } from './vueInfoService'; -import { DependencyService } from './dependencyService'; +import { DependencyService, State } from './dependencyService'; import * as _ from 'lodash'; import { DocumentContext, RefactorAction } from '../types'; import { DocumentService } from './documentService'; @@ -55,6 +56,7 @@ import { logger } from '../log'; import { getDefaultVLSConfig, VLSFullConfig, VLSConfig } from '../config'; import { LanguageId } from '../embeddedSupport/embeddedSupport'; import { APPLY_REFACTOR_COMMAND } from '../modes/script/javascript'; +import { VCancellationToken, VCancellationTokenSource } from '../utils/cancellationToken'; export class VLS { // @Todo: Remove this and DocumentContext @@ -67,6 +69,7 @@ export class VLS { private languageModes: LanguageModes; private pendingValidationRequests: { [uri: string]: NodeJS.Timer } = {}; + private cancellationTokenValidationRequests: { [uri: string]: VCancellationTokenSource } = {}; private validationDelayMs = 200; private validation: { [k: string]: boolean } = { 'vue-html': true, @@ -509,12 +512,26 @@ export class VLS { } this.cleanPendingValidation(textDocument); + this.cancelPastValidation(textDocument); this.pendingValidationRequests[textDocument.uri] = setTimeout(() => { delete this.pendingValidationRequests[textDocument.uri]; - this.validateTextDocument(textDocument); + const tsDep = this.dependencyService.getDependency('typescript'); + if (tsDep?.state === State.Loaded) { + this.cancellationTokenValidationRequests[textDocument.uri] = new VCancellationTokenSource(tsDep.module); + this.validateTextDocument(textDocument, this.cancellationTokenValidationRequests[textDocument.uri].token); + } }, this.validationDelayMs); } + cancelPastValidation(textDocument: TextDocument): void { + const source = this.cancellationTokenValidationRequests[textDocument.uri]; + if (source) { + source.cancel(); + source.dispose(); + delete this.cancellationTokenValidationRequests[textDocument.uri]; + } + } + cleanPendingValidation(textDocument: TextDocument): void { const request = this.pendingValidationRequests[textDocument.uri]; if (request) { @@ -523,25 +540,25 @@ export class VLS { } } - validateTextDocument(textDocument: TextDocument): void { - const diagnostics: Diagnostic[] = this.doValidate(textDocument); + async validateTextDocument(textDocument: TextDocument, cancellationToken?: VCancellationToken) { + const diagnostics: Diagnostic[] = await this.doValidate(textDocument, cancellationToken); this.lspConnection.sendDiagnostics({ uri: textDocument.uri, diagnostics }); } - doValidate(doc: TextDocument): Diagnostic[] { + async doValidate(doc: TextDocument, cancellationToken?: VCancellationToken) { const diagnostics: Diagnostic[] = []; if (doc.languageId === 'vue') { - this.languageModes.getAllLanguageModeRangesInDocument(doc).forEach(lmr => { + for (const lmr of this.languageModes.getAllLanguageModeRangesInDocument(doc)) { if (lmr.mode.doValidation) { if (this.validation[lmr.mode.getId()]) { - pushAll(diagnostics, lmr.mode.doValidation(doc)); + pushAll(diagnostics, await lmr.mode.doValidation(doc, cancellationToken)); } // Special case for template type checking else if (lmr.mode.getId() === 'vue-html' && this.templateInterpolationValidation) { - pushAll(diagnostics, lmr.mode.doValidation(doc)); + pushAll(diagnostics, await lmr.mode.doValidation(doc, cancellationToken)); } } - }); + } } return diagnostics; } diff --git a/server/src/utils/cancellationToken.ts b/server/src/utils/cancellationToken.ts new file mode 100644 index 0000000000..93a49d56bc --- /dev/null +++ b/server/src/utils/cancellationToken.ts @@ -0,0 +1,42 @@ +import { Runtime } from 'inspector'; +import type { T_TypeScript } from '../services/dependencyService'; +import { CancellationToken as TSCancellationToken, OperationCanceledException } from 'typescript'; +import { CancellationTokenSource, CancellationToken as LSPCancellationToken } from 'vscode-languageserver'; + +export interface VCancellationToken extends LSPCancellationToken { + tsToken: TSCancellationToken; +} + +export class VCancellationTokenSource extends CancellationTokenSource { + constructor(private tsModule: T_TypeScript) { + super(); + } + + get token(): VCancellationToken { + const token = super.token as VCancellationToken; + // tslint:disable-next-line variable-name + const Exception = this.tsModule.OperationCanceledException; + token.tsToken = { + isCancellationRequested() { + return token.isCancellationRequested; + }, + throwIfCancellationRequested() { + if (token.isCancellationRequested) { + throw new Exception(); + } + } + }; + return token; + } +} + +export function isVCancellationTokenCancel(token?: VCancellationToken) { + return new Promise(resolve => { + if (!token) { + return false; + } + setImmediate(() => { + resolve(token.isCancellationRequested); + }); + }); +} From 96b19b54391bbe71684378cf7b6ef0bff2db2781 Mon Sep 17 00:00:00 2001 From: yoyo930021 Date: Sun, 27 Sep 2020 16:57:34 +0800 Subject: [PATCH 2/7] Not send diagnostics when cancel --- server/src/services/vls.ts | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/server/src/services/vls.ts b/server/src/services/vls.ts index dc1fce0647..22b7316afa 100644 --- a/server/src/services/vls.ts +++ b/server/src/services/vls.ts @@ -179,7 +179,7 @@ export class VLS { this.lspConnection.onRequest('$/getDiagnostics', params => { const doc = this.documentService.getDocument(params.uri); if (doc) { - return this.doValidate(doc); + return this.doValidate(doc).then(result => result ?? []); } return []; }); @@ -541,8 +541,10 @@ export class VLS { } async validateTextDocument(textDocument: TextDocument, cancellationToken?: VCancellationToken) { - const diagnostics: Diagnostic[] = await this.doValidate(textDocument, cancellationToken); - this.lspConnection.sendDiagnostics({ uri: textDocument.uri, diagnostics }); + const diagnostics = await this.doValidate(textDocument, cancellationToken); + if (diagnostics) { + this.lspConnection.sendDiagnostics({ uri: textDocument.uri, diagnostics }); + } } async doValidate(doc: TextDocument, cancellationToken?: VCancellationToken) { @@ -560,6 +562,9 @@ export class VLS { } } } + if (cancellationToken?.isCancellationRequested) { + return null; + } return diagnostics; } From e504f6f9972f36aeb046bdaa6efc621ef9141f2a Mon Sep 17 00:00:00 2001 From: yoyo930021 Date: Sun, 27 Sep 2020 16:59:31 +0800 Subject: [PATCH 3/7] CHANGELOG --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 01ad969e2f..fac5155330 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -15,6 +15,7 @@ - 🙌 Complete with `?.` for optional properies in completion. Thanks to contribution from [@yoyo930021](https://github.com/yoyo930021). #2326 and #2357. - 🙌 Respect typescript language settings. Thanks to contribution from [@yoyo930021](https://github.com/yoyo930021). #2109 and #2375. - 🙌 Slim syntax highlighting. Thanks to contribution from [@Antti](https://github.com/Antti). +- 🙌 Stop computing outdated diagnostics with CancellationToken. Thanks to contribution from [@yoyo930021](https://github.com/yoyo930021). #1263 and #2332. ### 0.28.0 | 2020-09-23 | [VSIX](https://marketplace.visualstudio.com/_apis/public/gallery/publishers/octref/vsextensions/vetur/0.28.0/vspackage) From 857e2a96323bd50cd13dc851703f5c1e097eaffa Mon Sep 17 00:00:00 2001 From: yoyo930021 Date: Sat, 17 Oct 2020 20:01:08 +0800 Subject: [PATCH 4/7] Fix error handling undefuned in isVCancellationTokenCancel --- server/src/utils/cancellationToken.ts | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/server/src/utils/cancellationToken.ts b/server/src/utils/cancellationToken.ts index 93a49d56bc..e275b0f5a6 100644 --- a/server/src/utils/cancellationToken.ts +++ b/server/src/utils/cancellationToken.ts @@ -33,10 +33,9 @@ export class VCancellationTokenSource extends CancellationTokenSource { export function isVCancellationTokenCancel(token?: VCancellationToken) { return new Promise(resolve => { if (!token) { - return false; + resolve(false) + } else { + setImmediate(() => resolve(token.isCancellationRequested)); } - setImmediate(() => { - resolve(token.isCancellationRequested); - }); }); } From 3affd7d48d920a2b91cd9776273637b67fb12f07 Mon Sep 17 00:00:00 2001 From: yoyo930021 Date: Mon, 19 Oct 2020 17:49:42 +0800 Subject: [PATCH 5/7] Remove unused import --- server/src/utils/cancellationToken.ts | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/server/src/utils/cancellationToken.ts b/server/src/utils/cancellationToken.ts index e275b0f5a6..3e648209d3 100644 --- a/server/src/utils/cancellationToken.ts +++ b/server/src/utils/cancellationToken.ts @@ -1,6 +1,5 @@ -import { Runtime } from 'inspector'; import type { T_TypeScript } from '../services/dependencyService'; -import { CancellationToken as TSCancellationToken, OperationCanceledException } from 'typescript'; +import { CancellationToken as TSCancellationToken } from 'typescript'; import { CancellationTokenSource, CancellationToken as LSPCancellationToken } from 'vscode-languageserver'; export interface VCancellationToken extends LSPCancellationToken { @@ -33,7 +32,7 @@ export class VCancellationTokenSource extends CancellationTokenSource { export function isVCancellationTokenCancel(token?: VCancellationToken) { return new Promise(resolve => { if (!token) { - resolve(false) + resolve(false); } else { setImmediate(() => resolve(token.isCancellationRequested)); } From 79c14b1f685d2efc5bf0bb62f78174e1a599647b Mon Sep 17 00:00:00 2001 From: Pine Wu Date: Fri, 30 Oct 2020 09:32:38 +0800 Subject: [PATCH 6/7] Clean up --- server/src/modes/script/javascript.ts | 8 ++++---- server/src/modes/template/htmlMode.ts | 6 +++--- server/src/modes/template/interpolationMode.ts | 6 +++--- server/src/utils/cancellationToken.ts | 7 +++---- 4 files changed, 13 insertions(+), 14 deletions(-) diff --git a/server/src/modes/script/javascript.ts b/server/src/modes/script/javascript.ts index 9ce7608586..8ba0cb2983 100644 --- a/server/src/modes/script/javascript.ts +++ b/server/src/modes/script/javascript.ts @@ -46,7 +46,7 @@ import { RefactorAction } from '../../types'; import { IServiceHost } from '../../services/typescriptService/serviceHost'; import { toCompletionItemKind, toSymbolKind } from '../../services/typescriptService/util'; import * as Previewer from './previewer'; -import { isVCancellationTokenCancel, VCancellationToken } from '../../utils/cancellationToken'; +import { isVCancellationRequested, VCancellationToken } from '../../utils/cancellationToken'; // Todo: After upgrading to LS server 4.0, use CompletionContext for filtering trigger chars // https://microsoft.github.io/language-server-protocol/specification#completion-request-leftwards_arrow_with_hook @@ -147,7 +147,7 @@ export async function getJavascriptMode( }, async doValidation(doc: TextDocument, cancellationToken?: VCancellationToken): Promise { - if (await isVCancellationTokenCancel(cancellationToken)) { + if (await isVCancellationRequested(cancellationToken)) { return []; } const { scriptDoc, service } = updateCurrentVueTextDocument(doc); @@ -155,7 +155,7 @@ export async function getJavascriptMode( return []; } - if (await isVCancellationTokenCancel(cancellationToken)) { + if (await isVCancellationRequested(cancellationToken)) { return []; } const fileFsPath = getFileFsPath(doc.uri); @@ -171,7 +171,7 @@ export async function getJavascriptMode( ]; const compilerOptions = program.getCompilerOptions(); - if (!!(compilerOptions.declaration || compilerOptions.composite)) { + if (compilerOptions.declaration || compilerOptions.composite) { rawScriptDiagnostics = [ ...rawScriptDiagnostics, ...program.getDeclarationDiagnostics(sourceFile, cancellationToken?.tsToken) diff --git a/server/src/modes/template/htmlMode.ts b/server/src/modes/template/htmlMode.ts index e694cc34ab..b84c9b1f9b 100644 --- a/server/src/modes/template/htmlMode.ts +++ b/server/src/modes/template/htmlMode.ts @@ -26,7 +26,7 @@ import { getComponentInfoTagProvider } from './tagProviders/componentInfoTagProv import { VueVersion } from '../../services/typescriptService/vueVersion'; import { doPropValidation } from './services/vuePropValidation'; import { getFoldingRanges } from './services/htmlFolding'; -import { isVCancellationTokenCancel, VCancellationToken } from '../../utils/cancellationToken'; +import { isVCancellationRequested, VCancellationToken } from '../../utils/cancellationToken'; export class HTMLMode implements LanguageMode { private tagProviderSettings: CompletionConfiguration; @@ -64,7 +64,7 @@ export class HTMLMode implements LanguageMode { async doValidation(document: TextDocument, cancellationToken?: VCancellationToken) { const diagnostics = []; - if (await isVCancellationTokenCancel(cancellationToken)) { + if (await isVCancellationRequested(cancellationToken)) { return []; } if (this.config.vetur.validation.templateProps) { @@ -74,7 +74,7 @@ export class HTMLMode implements LanguageMode { } } - if (await isVCancellationTokenCancel(cancellationToken)) { + if (await isVCancellationRequested(cancellationToken)) { return diagnostics; } if (this.config.vetur.validation.template) { diff --git a/server/src/modes/template/interpolationMode.ts b/server/src/modes/template/interpolationMode.ts index 92d60ebedb..278fe54894 100644 --- a/server/src/modes/template/interpolationMode.ts +++ b/server/src/modes/template/interpolationMode.ts @@ -23,7 +23,7 @@ import { mapBackRange, mapFromPositionToOffset } from '../../services/typescript import { createTemplateDiagnosticFilter } from '../../services/typescriptService/templateDiagnosticFilter'; import { toCompletionItemKind } from '../../services/typescriptService/util'; import { VueInfoService } from '../../services/vueInfoService'; -import { isVCancellationTokenCancel, VCancellationToken } from '../../utils/cancellationToken'; +import { isVCancellationRequested, VCancellationToken } from '../../utils/cancellationToken'; import { getFileFsPath } from '../../utils/paths'; import { NULL_COMPLETION } from '../nullMode'; import { languageServiceIncludesFile } from '../script/javascript'; @@ -61,7 +61,7 @@ export class VueInterpolationMode implements LanguageMode { return []; } - if (await isVCancellationTokenCancel(cancellationToken)) { + if (await isVCancellationRequested(cancellationToken)) { return []; } @@ -86,7 +86,7 @@ export class VueInterpolationMode implements LanguageMode { return []; } - if (await isVCancellationTokenCancel(cancellationToken)) { + if (await isVCancellationRequested(cancellationToken)) { return []; } diff --git a/server/src/utils/cancellationToken.ts b/server/src/utils/cancellationToken.ts index 3e648209d3..825c8f007d 100644 --- a/server/src/utils/cancellationToken.ts +++ b/server/src/utils/cancellationToken.ts @@ -12,16 +12,15 @@ export class VCancellationTokenSource extends CancellationTokenSource { } get token(): VCancellationToken { + const operationCancelException = this.tsModule.OperationCanceledException; const token = super.token as VCancellationToken; - // tslint:disable-next-line variable-name - const Exception = this.tsModule.OperationCanceledException; token.tsToken = { isCancellationRequested() { return token.isCancellationRequested; }, throwIfCancellationRequested() { if (token.isCancellationRequested) { - throw new Exception(); + throw new operationCancelException(); } } }; @@ -29,7 +28,7 @@ export class VCancellationTokenSource extends CancellationTokenSource { } } -export function isVCancellationTokenCancel(token?: VCancellationToken) { +export function isVCancellationRequested(token?: VCancellationToken) { return new Promise(resolve => { if (!token) { resolve(false); From c314a095a49d7092bf78b8c36936cbcc48297818 Mon Sep 17 00:00:00 2001 From: Pine Wu Date: Fri, 30 Oct 2020 09:42:33 +0800 Subject: [PATCH 7/7] async --- server/src/services/vls.ts | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/server/src/services/vls.ts b/server/src/services/vls.ts index 22b7316afa..37730c954d 100644 --- a/server/src/services/vls.ts +++ b/server/src/services/vls.ts @@ -176,10 +176,11 @@ export class VLS { return (this.languageModes.getMode('vue-html') as VueHTMLMode).queryVirtualFileInfo(fileName, currFileText); }); - this.lspConnection.onRequest('$/getDiagnostics', params => { + this.lspConnection.onRequest('$/getDiagnostics', async params => { const doc = this.documentService.getDocument(params.uri); if (doc) { - return this.doValidate(doc).then(result => result ?? []); + const diagnostics = await this.doValidate(doc); + return diagnostics ?? []; } return []; });