Add table of contents provider abstraction (#152504)

We currently re-compute the same table of contents for markdown files multiple times. This is because multiple language features all need table of contents

With this change, we introduce a new `TableOfContentsProvider` which maintains a cache of the table of contents per file. This provider is then passed into every caller that needs a toc
This commit is contained in:
Matt Bierner
2022-06-17 11:20:02 -07:00
committed by GitHub
parent 5947c2a93c
commit dea813ff7c
16 changed files with 133 additions and 86 deletions

View File

@@ -9,6 +9,7 @@ import * as vscode from 'vscode';
import { DiagnosticCollectionReporter, DiagnosticComputer, DiagnosticConfiguration, DiagnosticLevel, DiagnosticManager, DiagnosticOptions, DiagnosticReporter } from '../languageFeatures/diagnostics';
import { MdLinkProvider } from '../languageFeatures/documentLinkProvider';
import { MdReferencesProvider } from '../languageFeatures/references';
import { MdTableOfContentsProvider } from '../tableOfContents';
import { noopToken } from '../util/cancellation';
import { disposeAll } from '../util/dispose';
import { InMemoryDocument } from '../util/inMemoryDocument';
@@ -30,7 +31,8 @@ const defaultDiagnosticsOptions = Object.freeze<DiagnosticOptions>({
async function getComputedDiagnostics(doc: InMemoryDocument, workspace: MdWorkspaceContents, options: Partial<DiagnosticOptions> = {}): Promise<vscode.Diagnostic[]> {
const engine = createNewMarkdownEngine();
const linkProvider = new MdLinkProvider(engine, workspace);
const computer = new DiagnosticComputer(engine, workspace, linkProvider);
const tocProvider = new MdTableOfContentsProvider(engine, workspace);
const computer = new DiagnosticComputer(workspace, linkProvider, tocProvider);
return (
await computer.getDiagnostics(doc, { ...defaultDiagnosticsOptions, ...options, }, noopToken)
).diagnostics;
@@ -430,11 +432,12 @@ suite('Markdown: Diagnostics manager', () => {
) {
const engine = createNewMarkdownEngine();
const linkProvider = new MdLinkProvider(engine, workspace);
const referencesProvider = new MdReferencesProvider(engine, workspace);
const tocProvider = new MdTableOfContentsProvider(engine, workspace);
const referencesProvider = new MdReferencesProvider(engine, workspace, tocProvider);
const manager = new DiagnosticManager(
engine,
workspace,
new DiagnosticComputer(engine, workspace, linkProvider),
new DiagnosticComputer(workspace, linkProvider, tocProvider),
configuration,
reporter,
referencesProvider,