Reduce recomputation of state in markdown extension (#152804)

* Reduce recomputation of state in markdown extension

- Use `getForDocument` more often to avoid refetching documents
- Debounce `MdTableOfContentsWatcher`. We don't want this to trigger on every keystroke :)

* Cache LinkDefinitionSet

* Add test file change

* Fix toc watcher for tests
This commit is contained in:
Matt Bierner
2022-06-21 16:25:10 -07:00
committed by GitHub
parent f9d332c692
commit c84655d123
9 changed files with 64 additions and 31 deletions

View File

@@ -427,12 +427,17 @@ export class MdLinkComputer {
}
}
interface MdDocumentLinks {
readonly links: readonly MdLink[];
readonly definitions: LinkDefinitionSet;
}
/**
* Stateful object which provides links for markdown files the workspace.
*/
export class MdLinkProvider extends Disposable {
private readonly _linkCache: MdDocumentInfoCache<readonly MdLink[]>;
private readonly _linkCache: MdDocumentInfoCache<MdDocumentLinks>;
private readonly linkComputer: MdLinkComputer;
@@ -443,21 +448,19 @@ export class MdLinkProvider extends Disposable {
) {
super();
this.linkComputer = new MdLinkComputer(tokenizer);
this._linkCache = this._register(new MdDocumentInfoCache(workspaceContents, doc => {
this._linkCache = this._register(new MdDocumentInfoCache(workspaceContents, async doc => {
logger.verbose('LinkProvider', `compute - ${doc.uri}`);
return this.linkComputer.getAllLinks(doc, noopToken);
const links = await this.linkComputer.getAllLinks(doc, noopToken);
return {
links,
definitions: new LinkDefinitionSet(links),
};
}));
}
public async getLinks(document: SkinnyTextDocument): Promise<{
readonly links: readonly MdLink[];
readonly definitions: LinkDefinitionSet;
}> {
const links = (await this._linkCache.get(document.uri)) ?? [];
return {
links,
definitions: new LinkDefinitionSet(links),
};
public async getLinks(document: SkinnyTextDocument): Promise<MdDocumentLinks> {
return this._linkCache.getForDocument(document);
}
}