Skip diagnostics for non-open md files (#152687)

Currently we only show diagnostics for opened tabs. This means we shouldn't waste time computing diagnostics for these non open files
This commit is contained in:
Matt Bierner 2022-06-20 16:39:01 -07:00 committed by GitHub
parent 963c961b59
commit 369252a027
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 26 additions and 5 deletions

View file

@ -241,6 +241,8 @@ export abstract class DiagnosticReporter extends Disposable {
public abstract delete(uri: vscode.Uri): void;
public abstract areDiagnosticsEnabled(uri: vscode.Uri): boolean;
public addWorkItem(promise: Promise<any>): Promise<any> {
this.pending.add(promise);
promise.finally(() => this.pending.delete(promise));
@ -253,7 +255,6 @@ export abstract class DiagnosticReporter extends Disposable {
}
export class DiagnosticCollectionReporter extends DiagnosticReporter {
private readonly collection: vscode.DiagnosticCollection;
constructor() {
@ -267,8 +268,12 @@ export class DiagnosticCollectionReporter extends DiagnosticReporter {
}
public set(uri: vscode.Uri, diagnostics: readonly vscode.Diagnostic[]): void {
this.collection.set(uri, this.areDiagnosticsEnabled(uri) ? diagnostics : []);
}
public areDiagnosticsEnabled(uri: vscode.Uri): boolean {
const tabs = this.getAllTabResources();
this.collection.set(uri, tabs.has(uri) ? diagnostics : []);
return tabs.has(uri);
}
public delete(uri: vscode.Uri): void {
@ -331,6 +336,7 @@ export class DiagnosticManager extends Disposable {
this.reporter.delete(uri);
}));
this._register(this.linkWatcher.onDidChangeLinkedToFile(changedDocuments => {
for (const resource of changedDocuments) {
const doc = vscode.workspace.textDocuments.find(doc => doc.uri.toString() === resource.toString());
@ -377,9 +383,14 @@ export class DiagnosticManager extends Disposable {
const doc = await this.workspaceContents.getOrLoadMarkdownDocument(resource);
if (doc) {
await this.inFlightDiagnostics.trigger(doc.uri, async (token) => {
const state = await this.recomputeDiagnosticState(doc, token);
this.linkWatcher.updateLinksForDocument(doc.uri, state.config.enabled && state.config.validateFileLinks ? state.links : []);
this.reporter.set(doc.uri, state.diagnostics);
if (this.reporter.areDiagnosticsEnabled(doc.uri)) {
const state = await this.recomputeDiagnosticState(doc, token);
this.linkWatcher.updateLinksForDocument(doc.uri, state.config.enabled && state.config.validateFileLinks ? state.links : []);
this.reporter.set(doc.uri, state.diagnostics);
} else {
this.linkWatcher.deleteDocument(doc.uri);
this.reporter.delete(doc.uri);
}
});
}
}));
@ -392,6 +403,8 @@ export class DiagnosticManager extends Disposable {
return this.reporter.addWorkItem(
(async () => {
// TODO: This pulls in all md files in the workspace. Instead we only care about opened text documents.
// Need a new way to handle that.
const allDocs = await this.workspaceContents.getAllMarkdownDocuments();
await Promise.all(Array.from(allDocs, doc => this.triggerDiagnostics(doc.uri)));
})()

View file

@ -75,6 +75,7 @@ class MemoryDiagnosticConfiguration implements DiagnosticConfiguration {
}
class MemoryDiagnosticReporter extends DiagnosticReporter {
private readonly diagnostics = new ResourceMap<readonly vscode.Diagnostic[]>();
override dispose(): void {
@ -91,6 +92,10 @@ class MemoryDiagnosticReporter extends DiagnosticReporter {
this.diagnostics.set(uri, diagnostics);
}
areDiagnosticsEnabled(_uri: vscode.Uri): boolean {
return true;
}
delete(uri: vscode.Uri): void {
this.diagnostics.delete(uri);
}

View file

@ -147,6 +147,9 @@ export class VsCodeMdWorkspaceContents extends Disposable implements MdWorkspace
this._register(vscode.workspace.onDidOpenTextDocument(e => {
this._documentCache.delete(e.uri);
if (this.isRelevantMarkdownDocument(e)) {
this._onDidCreateMarkdownDocumentEmitter.fire(e);
}
}));
this._register(vscode.workspace.onDidChangeTextDocument(e => {