Be more lazy computing symbols

This commit is contained in:
Matt Bierner
2018-04-13 16:34:05 -07:00
parent 7761157030
commit 017d09e2fe
2 changed files with 46 additions and 4 deletions

View File

@@ -6,6 +6,7 @@
import * as vscode from 'vscode';
import { disposeAll } from '../util/dispose';
import { isMarkdownFile } from '../util/file';
import { Lazy, lazy } from '../util/lazy';
import MDDocumentSymbolProvider from './documentSymbolProvider';
export interface WorkspaceMarkdownDocumentProvider {
@@ -87,7 +88,7 @@ class VSCodeWorkspaceMarkdownDocumentProvider implements WorkspaceMarkdownDocume
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 _disposables: vscode.Disposable[] = [];
@@ -106,7 +107,7 @@ export default class MarkdownWorkspaceSymbolProvider implements vscode.Workspace
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);
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);
}
private getSymbols(document: vscode.TextDocument): Promise<vscode.SymbolInformation[]> {
private getSymbols(document: vscode.TextDocument): Lazy<Thenable<vscode.SymbolInformation[]>> {
return lazy(async () => {
return this._symbolProvider.provideDocumentSymbols(document);
});
}
private onDidChangeDocument(document: vscode.TextDocument) {

View 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);
}