mirror of
https://github.com/microsoft/vscode.git
synced 2026-04-21 17:19:01 +01:00
* added the workspace symbol provider for markdown 46406 * fixed the review comments 46406
This commit is contained in:
committed by
Matt Bierner
parent
9e7763ccdb
commit
5e993f7160
@@ -0,0 +1,61 @@
|
||||
/*---------------------------------------------------------------------------------------------
|
||||
* Copyright (c) Microsoft Corporation. All rights reserved.
|
||||
* Licensed under the MIT License. See License.txt in the project root for license information.
|
||||
*--------------------------------------------------------------------------------------------*/
|
||||
|
||||
import { workspace, WorkspaceSymbolProvider, SymbolInformation, TextDocument, Disposable } from 'vscode';
|
||||
import { isMarkdownFile } from '../util/file';
|
||||
import MDDocumentSymbolProvider from './documentSymbolProvider';
|
||||
import { Dictionary, flatMap } from 'lodash';
|
||||
|
||||
export default class MarkdownWorkspaceSymbolProvider implements WorkspaceSymbolProvider {
|
||||
private symbolProvider: MDDocumentSymbolProvider;
|
||||
private symbolCache: Dictionary<SymbolInformation[]> = {};
|
||||
private symbolCachePopulated: boolean;
|
||||
private deRegisterOnSaveEvent: Disposable;
|
||||
|
||||
public constructor(symbolProvider: MDDocumentSymbolProvider) {
|
||||
this.symbolProvider = symbolProvider;
|
||||
this.symbolCachePopulated = false;
|
||||
this.deRegisterOnSaveEvent = this.registerOnSaveEvent();
|
||||
}
|
||||
|
||||
public async provideWorkspaceSymbols(query: string): Promise<SymbolInformation[]> {
|
||||
if (!this.symbolCachePopulated) {
|
||||
await this.populateSymbolCache();
|
||||
this.symbolCachePopulated = true;
|
||||
}
|
||||
|
||||
return flatMap(this.symbolCache)
|
||||
.filter(symbolInformation => symbolInformation.name.toLowerCase().indexOf(query.toLowerCase()) !== -1);
|
||||
}
|
||||
|
||||
public async populateSymbolCache(): Promise<void> {
|
||||
const markDownDocumentUris = await workspace.findFiles('**/*.md');
|
||||
for (const uri of markDownDocumentUris) {
|
||||
const document = await workspace.openTextDocument(uri);
|
||||
if (isMarkdownFile(document)) {
|
||||
const symbols = await this.getSymbol(document);
|
||||
this.symbolCache[document.fileName] = symbols;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public dispose(): void {
|
||||
this.deRegisterOnSaveEvent.dispose();
|
||||
}
|
||||
|
||||
private async getSymbol(document: TextDocument): Promise<SymbolInformation[]> {
|
||||
return this.symbolProvider.provideDocumentSymbols(document);
|
||||
}
|
||||
|
||||
private registerOnSaveEvent(): Disposable {
|
||||
return workspace.onDidSaveTextDocument(async document => {
|
||||
if (isMarkdownFile(document)) {
|
||||
const symbols = await this.getSymbol(document);
|
||||
this.symbolCache[document.fileName] = symbols;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user