mirror of
https://github.com/microsoft/vscode.git
synced 2026-04-19 08:08:39 +01:00
@@ -16,7 +16,7 @@ import { isMarkdownFile, looksLikeMarkdownPath } from '../util/file';
|
||||
import { Limiter } from '../util/limiter';
|
||||
import { ResourceMap } from '../util/resourceMap';
|
||||
import { MdWorkspaceContents, SkinnyTextDocument } from '../workspaceContents';
|
||||
import { InternalHref, LinkDefinitionSet, MdLink, MdLinkProvider, MdLinkSource } from './documentLinkProvider';
|
||||
import { InternalHref, LinkDefinitionSet, MdLink, MdLinkProvider, MdLinkSource } from './documentLinks';
|
||||
import { MdReferencesProvider, tryResolveLinkPath } from './references';
|
||||
|
||||
const localize = nls.loadMessageBundle();
|
||||
|
||||
@@ -13,7 +13,7 @@ import { noopToken } from '../util/cancellation';
|
||||
import { Disposable } from '../util/dispose';
|
||||
import { getUriForLinkWithKnownExternalScheme, isOfScheme, Schemes } from '../util/schemes';
|
||||
import { MdWorkspaceContents, SkinnyTextDocument } from '../workspaceContents';
|
||||
import { MdDocumentInfoCache } from './workspaceCache';
|
||||
import { MdDocumentInfoCache } from '../util/workspaceCache';
|
||||
|
||||
const localize = nls.loadMessageBundle();
|
||||
|
||||
@@ -20,12 +20,12 @@ export class MdDocumentSymbolProvider implements vscode.DocumentSymbolProvider {
|
||||
) { }
|
||||
|
||||
public async provideDocumentSymbolInformation(document: SkinnyTextDocument): Promise<vscode.SymbolInformation[]> {
|
||||
const toc = await this.tocProvider.get(document.uri);
|
||||
const toc = await this.tocProvider.getForDocument(document);
|
||||
return toc.entries.map(entry => this.toSymbolInformation(entry));
|
||||
}
|
||||
|
||||
public async provideDocumentSymbols(document: SkinnyTextDocument): Promise<vscode.DocumentSymbol[]> {
|
||||
const toc = await this.tocProvider.get(document.uri);
|
||||
const toc = await this.tocProvider.getForDocument(document);
|
||||
const root: MarkdownSymbol = {
|
||||
level: -Infinity,
|
||||
children: [],
|
||||
@@ -44,14 +44,13 @@ export class MdDocumentSymbolProvider implements vscode.DocumentSymbolProvider {
|
||||
const symbol = this.toDocumentSymbol(entry);
|
||||
symbol.children = [];
|
||||
|
||||
while (parent && entry.level <= parent.level) {
|
||||
while (entry.level <= parent.level) {
|
||||
parent = parent.parent!;
|
||||
}
|
||||
parent.children.push(symbol);
|
||||
this.buildTree({ level: entry.level, children: symbol.children, parent }, entries.slice(1));
|
||||
}
|
||||
|
||||
|
||||
private toSymbolInformation(entry: TocEntry): vscode.SymbolInformation {
|
||||
return new vscode.SymbolInformation(
|
||||
this.getSymbolName(entry),
|
||||
@@ -9,7 +9,7 @@ import { IMdParser } from '../markdownEngine';
|
||||
import { TableOfContents } from '../tableOfContents';
|
||||
import { resolveUriToMarkdownFile } from '../util/openDocumentLink';
|
||||
import { SkinnyTextDocument } from '../workspaceContents';
|
||||
import { MdLinkProvider } from './documentLinkProvider';
|
||||
import { MdLinkProvider } from './documentLinks';
|
||||
|
||||
enum CompletionContextKind {
|
||||
/** `[...](|)` */
|
||||
|
||||
@@ -10,8 +10,8 @@ import { noopToken } from '../util/cancellation';
|
||||
import { Disposable } from '../util/dispose';
|
||||
import { looksLikeMarkdownPath } from '../util/file';
|
||||
import { MdWorkspaceContents, SkinnyTextDocument } from '../workspaceContents';
|
||||
import { InternalHref, MdLink, MdLinkComputer } from './documentLinkProvider';
|
||||
import { MdWorkspaceInfoCache } from './workspaceCache';
|
||||
import { InternalHref, MdLink, MdLinkComputer } from './documentLinks';
|
||||
import { MdWorkspaceInfoCache } from '../util/workspaceCache';
|
||||
|
||||
|
||||
/**
|
||||
|
||||
@@ -10,7 +10,7 @@ import { Slugifier } from '../slugify';
|
||||
import { Disposable } from '../util/dispose';
|
||||
import { resolveDocumentLink } from '../util/openDocumentLink';
|
||||
import { MdWorkspaceContents, SkinnyTextDocument } from '../workspaceContents';
|
||||
import { InternalHref } from './documentLinkProvider';
|
||||
import { InternalHref } from './documentLinks';
|
||||
import { MdHeaderReference, MdLinkReference, MdReference, MdReferencesProvider, tryResolveLinkPath } from './references';
|
||||
|
||||
const localize = nls.loadMessageBundle();
|
||||
|
||||
@@ -1,152 +0,0 @@
|
||||
/*---------------------------------------------------------------------------------------------
|
||||
* Copyright (c) Microsoft Corporation. All rights reserved.
|
||||
* Licensed under the MIT License. See License.txt in the project root for license information.
|
||||
*--------------------------------------------------------------------------------------------*/
|
||||
|
||||
import * as vscode from 'vscode';
|
||||
import { Disposable } from '../util/dispose';
|
||||
import { Lazy, lazy } from '../util/lazy';
|
||||
import { ResourceMap } from '../util/resourceMap';
|
||||
import { MdWorkspaceContents, SkinnyTextDocument } from '../workspaceContents';
|
||||
|
||||
class LazyResourceMap<T> {
|
||||
private readonly _map = new ResourceMap<Lazy<Promise<T>>>();
|
||||
|
||||
public has(resource: vscode.Uri): boolean {
|
||||
return this._map.has(resource);
|
||||
}
|
||||
|
||||
public get(resource: vscode.Uri): Promise<T> | undefined {
|
||||
return this._map.get(resource)?.value;
|
||||
}
|
||||
|
||||
public set(resource: vscode.Uri, value: Lazy<Promise<T>>) {
|
||||
this._map.set(resource, value);
|
||||
}
|
||||
|
||||
public delete(resource: vscode.Uri) {
|
||||
this._map.delete(resource);
|
||||
}
|
||||
|
||||
public entries(): Promise<Array<[vscode.Uri, T]>> {
|
||||
return Promise.all(Array.from(this._map.entries(), async ([key, entry]) => {
|
||||
return [key, await entry.value];
|
||||
}));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Cache of information per-document in the workspace.
|
||||
*
|
||||
* The values are computed lazily and invalidated when the document changes.
|
||||
*/
|
||||
export class MdDocumentInfoCache<T> extends Disposable {
|
||||
|
||||
private readonly _cache = new LazyResourceMap<T>();
|
||||
|
||||
public constructor(
|
||||
private readonly workspaceContents: MdWorkspaceContents,
|
||||
private readonly getValue: (document: SkinnyTextDocument) => Promise<T>,
|
||||
) {
|
||||
super();
|
||||
|
||||
this._register(this.workspaceContents.onDidChangeMarkdownDocument(doc => this.onDidChangeDocument(doc)));
|
||||
this._register(this.workspaceContents.onDidCreateMarkdownDocument(doc => this.onDidChangeDocument(doc)));
|
||||
this._register(this.workspaceContents.onDidDeleteMarkdownDocument(this.onDidDeleteDocument, this));
|
||||
}
|
||||
|
||||
public async get(resource: vscode.Uri): Promise<T | undefined> {
|
||||
const existing = this._cache.get(resource);
|
||||
if (existing) {
|
||||
return existing;
|
||||
}
|
||||
|
||||
const doc = await this.workspaceContents.getOrLoadMarkdownDocument(resource);
|
||||
return doc && this.onDidChangeDocument(doc, true)?.value;
|
||||
}
|
||||
|
||||
public async getForDocument(document: SkinnyTextDocument): Promise<T> {
|
||||
const existing = this._cache.get(document.uri);
|
||||
if (existing) {
|
||||
return existing;
|
||||
}
|
||||
|
||||
return this.onDidChangeDocument(document, true)!.value;
|
||||
}
|
||||
|
||||
public async entries(): Promise<Array<[vscode.Uri, T]>> {
|
||||
return this._cache.entries();
|
||||
}
|
||||
|
||||
private onDidChangeDocument(document: SkinnyTextDocument, forceAdd = false): Lazy<Promise<T>> | undefined {
|
||||
if (forceAdd || this._cache.has(document.uri)) {
|
||||
const value = lazy(() => this.getValue(document));
|
||||
this._cache.set(document.uri, value);
|
||||
return value;
|
||||
}
|
||||
return undefined;
|
||||
}
|
||||
|
||||
private onDidDeleteDocument(resource: vscode.Uri) {
|
||||
this._cache.delete(resource);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Cache of information across all markdown files in the workspace.
|
||||
*
|
||||
* Unlike {@link MdDocumentInfoCache}, the entries here are computed eagerly for every file in the workspace.
|
||||
* However the computation of the values is still lazy.
|
||||
*/
|
||||
export class MdWorkspaceInfoCache<T> extends Disposable {
|
||||
|
||||
private readonly _cache = new LazyResourceMap<T>();
|
||||
private _init?: Promise<void>;
|
||||
|
||||
public constructor(
|
||||
private readonly workspaceContents: MdWorkspaceContents,
|
||||
private readonly getValue: (document: SkinnyTextDocument) => Promise<T>,
|
||||
) {
|
||||
super();
|
||||
}
|
||||
|
||||
public async entries(): Promise<Array<[vscode.Uri, T]>> {
|
||||
await this.ensureInit();
|
||||
return this._cache.entries();
|
||||
}
|
||||
|
||||
public async values(): Promise<Array<T>> {
|
||||
await this.ensureInit();
|
||||
return Array.from(await this._cache.entries(), x => x[1]);
|
||||
}
|
||||
|
||||
private async ensureInit(): Promise<void> {
|
||||
if (!this._init) {
|
||||
this._init = this.populateCache();
|
||||
|
||||
this._register(this.workspaceContents.onDidChangeMarkdownDocument(this.onDidChangeDocument, this));
|
||||
this._register(this.workspaceContents.onDidCreateMarkdownDocument(this.onDidChangeDocument, this));
|
||||
this._register(this.workspaceContents.onDidDeleteMarkdownDocument(this.onDidDeleteDocument, this));
|
||||
}
|
||||
await this._init;
|
||||
}
|
||||
|
||||
private async populateCache(): Promise<void> {
|
||||
const markdownDocumentUris = await this.workspaceContents.getAllMarkdownDocuments();
|
||||
for (const document of markdownDocumentUris) {
|
||||
this.update(document);
|
||||
}
|
||||
}
|
||||
|
||||
private update(document: SkinnyTextDocument): void {
|
||||
this._cache.set(document.uri, lazy(() => this.getValue(document)));
|
||||
}
|
||||
|
||||
private onDidChangeDocument(document: SkinnyTextDocument) {
|
||||
this.update(document);
|
||||
}
|
||||
|
||||
private onDidDeleteDocument(resource: vscode.Uri) {
|
||||
this._cache.delete(resource);
|
||||
}
|
||||
}
|
||||
@@ -6,8 +6,8 @@
|
||||
import * as vscode from 'vscode';
|
||||
import { Disposable } from '../util/dispose';
|
||||
import { MdWorkspaceContents } from '../workspaceContents';
|
||||
import { MdDocumentSymbolProvider } from './documentSymbolProvider';
|
||||
import { MdWorkspaceInfoCache } from './workspaceCache';
|
||||
import { MdDocumentSymbolProvider } from './documentSymbols';
|
||||
import { MdWorkspaceInfoCache } from '../util/workspaceCache';
|
||||
|
||||
export class MdWorkspaceSymbolProvider extends Disposable implements vscode.WorkspaceSymbolProvider {
|
||||
|
||||
Reference in New Issue
Block a user