From 3a5a458643eab4a198af55cffa961e8baa406b7b Mon Sep 17 00:00:00 2001 From: Matt Bierner Date: Tue, 2 Aug 2022 11:32:02 -0700 Subject: [PATCH] Clear markdown diagnostics when file is closed (#156912) --- .../server/src/languageFeatures/diagnostics.ts | 17 +++++++++++++---- .../server/src/server.ts | 2 +- 2 files changed, 14 insertions(+), 5 deletions(-) diff --git a/extensions/markdown-language-features/server/src/languageFeatures/diagnostics.ts b/extensions/markdown-language-features/server/src/languageFeatures/diagnostics.ts index 6a51b2e986d..9640f052ceb 100644 --- a/extensions/markdown-language-features/server/src/languageFeatures/diagnostics.ts +++ b/extensions/markdown-language-features/server/src/languageFeatures/diagnostics.ts @@ -48,13 +48,13 @@ export function registerValidateSupport( workspace: VsCodeClientWorkspace, ls: md.IMdLanguageService, config: ConfigurationManager, + logger: md.ILogger, ): Disposable { let diagnosticOptions: md.DiagnosticOptions = defaultDiagnosticOptions; function updateDiagnosticsSetting(): void { diagnosticOptions = getDiagnosticsOptions(config); } - const subs: Disposable[] = []; const manager = ls.createPullDiagnosticsManager(); subs.push(manager); @@ -64,14 +64,23 @@ export function registerValidateSupport( connection.languages.diagnostics.refresh(); })); + const emptyDiagnosticsResponse = Object.freeze({ kind: 'full', items: [] }); + connection.languages.diagnostics.on(async (params, token): Promise => { + logger.log(md.LogLevel.Trace, 'Server: connection.languages.diagnostics.on', params.textDocument.uri); + if (!config.getSettings()?.markdown.experimental.validate.enabled) { - return { kind: 'full', items: [] }; + return emptyDiagnosticsResponse; } - const document = await workspace.openMarkdownDocument(URI.parse(params.textDocument.uri)); + const uri = URI.parse(params.textDocument.uri); + if (!workspace.hasMarkdownDocument(uri)) { + return emptyDiagnosticsResponse; + } + + const document = await workspace.openMarkdownDocument(uri); if (!document) { - return { kind: 'full', items: [] }; + return emptyDiagnosticsResponse; } const diagnostics = await manager.computeDiagnostics(document, diagnosticOptions, token); diff --git a/extensions/markdown-language-features/server/src/server.ts b/extensions/markdown-language-features/server/src/server.ts index 63a3d5d4b99..d16c2d0d34e 100644 --- a/extensions/markdown-language-features/server/src/server.ts +++ b/extensions/markdown-language-features/server/src/server.ts @@ -48,7 +48,7 @@ export async function startServer(connection: Connection) { }); registerCompletionsSupport(connection, documents, provider, configurationManager); - registerValidateSupport(connection, workspace, provider, configurationManager); + registerValidateSupport(connection, workspace, provider, configurationManager, logger); workspace.workspaceFolders = (params.workspaceFolders ?? []).map(x => URI.parse(x.uri)); return {