mirror of
https://github.com/microsoft/vscode.git
synced 2026-04-23 01:58:53 +01:00
Only compute diagnostics for opened md files (#153395)
* Only compute diagnostics for opened md files For #152494 * Make tests stable for result ordering
This commit is contained in:
@@ -10,6 +10,7 @@ import { DiagnosticCollectionReporter, DiagnosticComputer, DiagnosticConfigurati
|
||||
import { MdLinkProvider } from '../languageFeatures/documentLinks';
|
||||
import { MdReferencesProvider } from '../languageFeatures/references';
|
||||
import { MdTableOfContentsProvider } from '../tableOfContents';
|
||||
import { ITextDocument } from '../types/textDocument';
|
||||
import { noopToken } from '../util/cancellation';
|
||||
import { DisposableStore } from '../util/dispose';
|
||||
import { InMemoryDocument } from '../util/inMemoryDocument';
|
||||
@@ -79,6 +80,12 @@ class MemoryDiagnosticReporter extends DiagnosticReporter {
|
||||
|
||||
private readonly diagnostics = new ResourceMap<readonly vscode.Diagnostic[]>();
|
||||
|
||||
constructor(
|
||||
private readonly workspace: InMemoryMdWorkspace,
|
||||
) {
|
||||
super();
|
||||
}
|
||||
|
||||
override dispose(): void {
|
||||
super.clear();
|
||||
this.clear();
|
||||
@@ -93,7 +100,7 @@ class MemoryDiagnosticReporter extends DiagnosticReporter {
|
||||
this.diagnostics.set(uri, diagnostics);
|
||||
}
|
||||
|
||||
areDiagnosticsEnabled(_uri: vscode.Uri): boolean {
|
||||
isOpen(_uri: vscode.Uri): boolean {
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -104,6 +111,10 @@ class MemoryDiagnosticReporter extends DiagnosticReporter {
|
||||
get(uri: vscode.Uri): readonly vscode.Diagnostic[] {
|
||||
return orderDiagnosticsByRange(this.diagnostics.get(uri) ?? []);
|
||||
}
|
||||
|
||||
getOpenDocuments(): ITextDocument[] {
|
||||
return this.workspace.values();
|
||||
}
|
||||
}
|
||||
|
||||
suite('markdown: Diagnostic Computer', () => {
|
||||
@@ -454,7 +465,7 @@ suite('Markdown: Diagnostics manager', () => {
|
||||
))
|
||||
]));
|
||||
|
||||
const reporter = store.add(new MemoryDiagnosticReporter());
|
||||
const reporter = store.add(new MemoryDiagnosticReporter(workspace));
|
||||
const config = new MemoryDiagnosticConfiguration({ enabled: true });
|
||||
|
||||
const manager = createDiagnosticsManager(store, workspace, config, reporter);
|
||||
@@ -499,7 +510,7 @@ suite('Markdown: Diagnostics manager', () => {
|
||||
`[text](#no-such-2)`,
|
||||
));
|
||||
const workspace = store.add(new InMemoryMdWorkspace([doc1, doc2]));
|
||||
const reporter = store.add(new MemoryDiagnosticReporter());
|
||||
const reporter = store.add(new MemoryDiagnosticReporter(workspace));
|
||||
|
||||
const manager = createDiagnosticsManager(store, workspace, new MemoryDiagnosticConfiguration({}), reporter);
|
||||
await manager.ready;
|
||||
@@ -554,7 +565,7 @@ suite('Markdown: Diagnostics manager', () => {
|
||||
`# Header`
|
||||
));
|
||||
const workspace = store.add(new InMemoryMdWorkspace([doc1, doc2]));
|
||||
const reporter = store.add(new MemoryDiagnosticReporter());
|
||||
const reporter = store.add(new MemoryDiagnosticReporter(workspace));
|
||||
|
||||
const manager = createDiagnosticsManager(store, workspace, new MemoryDiagnosticConfiguration({}), reporter);
|
||||
await manager.ready;
|
||||
|
||||
@@ -22,7 +22,7 @@ function getFileReferences(store: DisposableStore, resource: vscode.Uri, workspa
|
||||
const engine = createNewMarkdownEngine();
|
||||
const tocProvider = store.add(new MdTableOfContentsProvider(engine, workspace, nulLogger));
|
||||
const computer = store.add(new MdReferencesProvider(engine, workspace, tocProvider, nulLogger));
|
||||
return computer.getAllReferencesToFile(resource, noopToken);
|
||||
return computer.getReferencesToFileInWorkspace(resource, noopToken);
|
||||
}
|
||||
|
||||
function assertReferencesEqual(actualRefs: readonly MdReference[], ...expectedRefs: { uri: vscode.Uri; line: number }[]) {
|
||||
|
||||
@@ -22,10 +22,14 @@ export class InMemoryMdWorkspace extends Disposable implements IMdWorkspace {
|
||||
}
|
||||
}
|
||||
|
||||
public async getAllMarkdownDocuments() {
|
||||
public values() {
|
||||
return Array.from(this._documents.values());
|
||||
}
|
||||
|
||||
public async getAllMarkdownDocuments() {
|
||||
return this.values();
|
||||
}
|
||||
|
||||
public async getOrLoadMarkdownDocument(resource: vscode.Uri): Promise<ITextDocument | undefined> {
|
||||
return this._documents.get(resource);
|
||||
}
|
||||
|
||||
@@ -18,12 +18,19 @@ import { nulLogger } from './nulLogging';
|
||||
import { joinLines, withStore, workspacePath } from './util';
|
||||
|
||||
|
||||
function getReferences(store: DisposableStore, doc: InMemoryDocument, pos: vscode.Position, workspace: IMdWorkspace) {
|
||||
async function getReferences(store: DisposableStore, doc: InMemoryDocument, pos: vscode.Position, workspace: IMdWorkspace) {
|
||||
const engine = createNewMarkdownEngine();
|
||||
const tocProvider = store.add(new MdTableOfContentsProvider(engine, workspace, nulLogger));
|
||||
const computer = store.add(new MdReferencesProvider(engine, workspace, tocProvider, nulLogger));
|
||||
const provider = new MdVsCodeReferencesProvider(computer);
|
||||
return provider.provideReferences(doc, pos, { includeDeclaration: true }, noopToken);
|
||||
const refs = await provider.provideReferences(doc, pos, { includeDeclaration: true }, noopToken);
|
||||
return refs.sort((a, b) => {
|
||||
const pathCompare = a.uri.toString().localeCompare(b.uri.toString());
|
||||
if (pathCompare !== 0) {
|
||||
return pathCompare;
|
||||
}
|
||||
return a.range.start.compareTo(b.range.start);
|
||||
});
|
||||
}
|
||||
|
||||
function assertReferencesEqual(actualRefs: readonly vscode.Location[], ...expectedRefs: { uri: vscode.Uri; line: number; startCharacter?: number; endCharacter?: number }[]) {
|
||||
@@ -130,7 +137,7 @@ suite('Markdown: Find all references', () => {
|
||||
test('Should find references from header across files', withStore(async (store) => {
|
||||
const docUri = workspacePath('doc.md');
|
||||
const other1Uri = workspacePath('sub', 'other.md');
|
||||
const other2Uri = workspacePath('other2.md');
|
||||
const other2Uri = workspacePath('zOther2.md');
|
||||
|
||||
const doc = new InMemoryDocument(docUri, joinLines(
|
||||
`# abc`,
|
||||
@@ -216,7 +223,7 @@ suite('Markdown: Find all references', () => {
|
||||
test('Should find references from link across files', withStore(async (store) => {
|
||||
const docUri = workspacePath('doc.md');
|
||||
const other1Uri = workspacePath('sub', 'other.md');
|
||||
const other2Uri = workspacePath('other2.md');
|
||||
const other2Uri = workspacePath('zOther2.md');
|
||||
|
||||
const doc = new InMemoryDocument(docUri, joinLines(
|
||||
`# abc`,
|
||||
@@ -300,9 +307,9 @@ suite('Markdown: Find all references', () => {
|
||||
|
||||
const refs = await getReferences(store, doc, new vscode.Position(0, 23), workspace);
|
||||
assertReferencesEqual(refs!,
|
||||
{ uri: other1Uri, line: 1 }, // Header definition
|
||||
{ uri: docUri, line: 0 },
|
||||
{ uri: docUri, line: 1 },
|
||||
{ uri: other1Uri, line: 1 }, // Header definition
|
||||
);
|
||||
}));
|
||||
|
||||
@@ -467,7 +474,7 @@ suite('Markdown: Find all references', () => {
|
||||
{
|
||||
// Check refs to header fragment
|
||||
const headerRefs = await getReferences(store, otherDoc, new vscode.Position(0, 16), workspace);
|
||||
assertReferencesEqual(headerRefs!,
|
||||
assertReferencesEqual(headerRefs,
|
||||
{ uri: docUri, line: 0 }, // Header definition
|
||||
{ uri: docUri, line: 2 },
|
||||
{ uri: other1Uri, line: 0 },
|
||||
@@ -477,7 +484,7 @@ suite('Markdown: Find all references', () => {
|
||||
{
|
||||
// Check refs to file itself from link with ext
|
||||
const fileRefs = await getReferences(store, otherDoc, new vscode.Position(0, 9), workspace);
|
||||
assertReferencesEqual(fileRefs!,
|
||||
assertReferencesEqual(fileRefs,
|
||||
{ uri: other1Uri, line: 0, endCharacter: 14 },
|
||||
{ uri: other1Uri, line: 1, endCharacter: 19 },
|
||||
);
|
||||
@@ -485,7 +492,7 @@ suite('Markdown: Find all references', () => {
|
||||
{
|
||||
// Check refs to file itself from link without ext
|
||||
const fileRefs = await getReferences(store, otherDoc, new vscode.Position(1, 17), workspace);
|
||||
assertReferencesEqual(fileRefs!,
|
||||
assertReferencesEqual(fileRefs,
|
||||
{ uri: other1Uri, line: 0 },
|
||||
{ uri: other1Uri, line: 1 },
|
||||
);
|
||||
|
||||
Reference in New Issue
Block a user