mirror of
https://github.com/microsoft/vscode.git
synced 2025-12-23 03:39:23 +00:00
Be more lazy computing symbols
This commit is contained in:
@@ -6,6 +6,7 @@
|
|||||||
import * as vscode from 'vscode';
|
import * as vscode from 'vscode';
|
||||||
import { disposeAll } from '../util/dispose';
|
import { disposeAll } from '../util/dispose';
|
||||||
import { isMarkdownFile } from '../util/file';
|
import { isMarkdownFile } from '../util/file';
|
||||||
|
import { Lazy, lazy } from '../util/lazy';
|
||||||
import MDDocumentSymbolProvider from './documentSymbolProvider';
|
import MDDocumentSymbolProvider from './documentSymbolProvider';
|
||||||
|
|
||||||
export interface WorkspaceMarkdownDocumentProvider {
|
export interface WorkspaceMarkdownDocumentProvider {
|
||||||
@@ -87,7 +88,7 @@ class VSCodeWorkspaceMarkdownDocumentProvider implements WorkspaceMarkdownDocume
|
|||||||
|
|
||||||
|
|
||||||
export default class MarkdownWorkspaceSymbolProvider implements vscode.WorkspaceSymbolProvider {
|
export default class MarkdownWorkspaceSymbolProvider implements vscode.WorkspaceSymbolProvider {
|
||||||
private _symbolCache = new Map<string, Thenable<vscode.SymbolInformation[]>>();
|
private _symbolCache = new Map<string, Lazy<Thenable<vscode.SymbolInformation[]>>>();
|
||||||
private _symbolCachePopulated: boolean = false;
|
private _symbolCachePopulated: boolean = false;
|
||||||
private _disposables: vscode.Disposable[] = [];
|
private _disposables: vscode.Disposable[] = [];
|
||||||
|
|
||||||
@@ -106,7 +107,7 @@ export default class MarkdownWorkspaceSymbolProvider implements vscode.Workspace
|
|||||||
this._workspaceMarkdownDocumentProvider.onDidDeleteMarkdownDocument(this.onDidDeleteDocument, this, this._disposables);
|
this._workspaceMarkdownDocumentProvider.onDidDeleteMarkdownDocument(this.onDidDeleteDocument, this, this._disposables);
|
||||||
}
|
}
|
||||||
|
|
||||||
const allSymbolsSets = await Promise.all(Array.from(this._symbolCache.values()));
|
const allSymbolsSets = await Promise.all(Array.from(this._symbolCache.values()).map(x => x.value));
|
||||||
const allSymbols: vscode.SymbolInformation[] = Array.prototype.concat.apply([], allSymbolsSets);
|
const allSymbols: vscode.SymbolInformation[] = Array.prototype.concat.apply([], allSymbolsSets);
|
||||||
return allSymbols.filter(symbolInformation => symbolInformation.name.toLowerCase().indexOf(query.toLowerCase()) !== -1);
|
return allSymbols.filter(symbolInformation => symbolInformation.name.toLowerCase().indexOf(query.toLowerCase()) !== -1);
|
||||||
}
|
}
|
||||||
@@ -122,8 +123,10 @@ export default class MarkdownWorkspaceSymbolProvider implements vscode.Workspace
|
|||||||
disposeAll(this._disposables);
|
disposeAll(this._disposables);
|
||||||
}
|
}
|
||||||
|
|
||||||
private getSymbols(document: vscode.TextDocument): Promise<vscode.SymbolInformation[]> {
|
private getSymbols(document: vscode.TextDocument): Lazy<Thenable<vscode.SymbolInformation[]>> {
|
||||||
return this._symbolProvider.provideDocumentSymbols(document);
|
return lazy(async () => {
|
||||||
|
return this._symbolProvider.provideDocumentSymbols(document);
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
private onDidChangeDocument(document: vscode.TextDocument) {
|
private onDidChangeDocument(document: vscode.TextDocument) {
|
||||||
|
|||||||
39
extensions/markdown-language-features/src/util/lazy.ts
Normal file
39
extensions/markdown-language-features/src/util/lazy.ts
Normal file
@@ -0,0 +1,39 @@
|
|||||||
|
/*---------------------------------------------------------------------------------------------
|
||||||
|
* Copyright (c) Microsoft Corporation. All rights reserved.
|
||||||
|
* Licensed under the MIT License. See License.txt in the project root for license information.
|
||||||
|
*--------------------------------------------------------------------------------------------*/
|
||||||
|
|
||||||
|
export interface Lazy<T> {
|
||||||
|
readonly value: T;
|
||||||
|
readonly hasValue: boolean;
|
||||||
|
map<R>(f: (x: T) => R): Lazy<R>;
|
||||||
|
}
|
||||||
|
|
||||||
|
class LazyValue<T> implements Lazy<T> {
|
||||||
|
private _hasValue: boolean = false;
|
||||||
|
private _value?: T;
|
||||||
|
|
||||||
|
constructor(
|
||||||
|
private readonly _getValue: () => T
|
||||||
|
) { }
|
||||||
|
|
||||||
|
get value(): T {
|
||||||
|
if (!this._hasValue) {
|
||||||
|
this._hasValue = true;
|
||||||
|
this._value = this._getValue();
|
||||||
|
}
|
||||||
|
return this._value!;
|
||||||
|
}
|
||||||
|
|
||||||
|
get hasValue(): boolean {
|
||||||
|
return this._hasValue;
|
||||||
|
}
|
||||||
|
|
||||||
|
public map<R>(f: (x: T) => R): Lazy<R> {
|
||||||
|
return new LazyValue(() => f(this.value));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export function lazy<T>(getValue: () => T): Lazy<T> {
|
||||||
|
return new LazyValue<T>(getValue);
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user