Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added the workspace symbol provider for markdown #46406 #47610

Merged
Merged
Changes from 1 commit
Commits
File filter...
Filter file types
Jump to…
Jump to file or symbol
Failed to load files and symbols.

Always

Just for now

@@ -14,6 +14,7 @@ import { loadDefaultTelemetryReporter } from './telemetryReporter';
import { getMarkdownExtensionContributions } from './markdownExtensions';
import LinkProvider from './features/documentLinkProvider';
import MDDocumentSymbolProvider from './features/documentSymbolProvider';
import MarkdownWorkspaceSymbolProvider from './features/workspaceSymbolProvider';
import { MarkdownContentProvider } from './features/previewContentProvider';
import { MarkdownPreviewManager } from './features/previewManager';
import MarkdownFoldingProvider from './features/foldingProvider';
@@ -32,13 +33,14 @@ export function activate(context: vscode.ExtensionContext) {
const selector = 'markdown';

const contentProvider = new MarkdownContentProvider(engine, context, cspArbiter, contributions, logger);

const symbolProvider = new MDDocumentSymbolProvider(engine);
const previewManager = new MarkdownPreviewManager(contentProvider, logger, contributions);
context.subscriptions.push(previewManager);

context.subscriptions.push(vscode.languages.registerDocumentSymbolProvider(selector, new MDDocumentSymbolProvider(engine)));
context.subscriptions.push(vscode.languages.registerDocumentSymbolProvider(selector, symbolProvider));
context.subscriptions.push(vscode.languages.registerDocumentLinkProvider(selector, new LinkProvider()));
context.subscriptions.push(vscode.languages.registerFoldingProvider(selector, new MarkdownFoldingProvider(engine)));
context.subscriptions.push(vscode.languages.registerWorkspaceSymbolProvider(new MarkdownWorkspaceSymbolProvider(symbolProvider)));

const previewSecuritySelector = new PreviewSecuritySelector(cspArbiter, previewManager);

@@ -0,0 +1,50 @@
/*---------------------------------------------------------------------------------------------
* 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 } 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[]> = {};

public constructor(symbolProvider: MDDocumentSymbolProvider) {
this.symbolProvider = symbolProvider;
this.populateSymbolCache();

This comment has been minimized.

Copy link
@mjbvz

mjbvz Apr 10, 2018

Contributor

This should be done lazily when workspace symbols are first requested

This comment has been minimized.

Copy link
@pradeepmurugesan

pradeepmurugesan Apr 11, 2018

Author Contributor

done..

this.registerOnSaveEvent();
}

public async provideWorkspaceSymbols(query: string): Promise<SymbolInformation[]> {
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;
}
}
}

private async getSymbol(document: TextDocument): Promise<SymbolInformation[]> {
return this.symbolProvider.provideDocumentSymbols(document);
}

private registerOnSaveEvent(): void {
workspace.onDidSaveTextDocument(async document => {

This comment has been minimized.

Copy link
@mjbvz

mjbvz Apr 10, 2018

Contributor

This registration returns a registration handle that handles unregistering the event handler. Add a dispose method on MarkdownWorkspaceSymbolProvider and then call .dipose() on the the registration handle to make sure it is properly cleaned up

This comment has been minimized.

Copy link
@pradeepmurugesan

pradeepmurugesan Apr 11, 2018

Author Contributor

@mjbvz I have one question here. I have made the changes though.. Excuse me if my question is naive..

Who calls the dispose method? I couldn't find the same in the WorkspaceSymbolProvider interface as well.. I assume there should be a common place to check if the method "dispose" exists on an object and then invoke this.

Can you kindly point me to that.

if (isMarkdownFile(document)) {
const symbols = await this.getSymbol(document);
this.symbolCache[document.fileName] = symbols;
}
});
}

}
ProTip! Use n and p to navigate between commits in a pull request.
You can’t perform that action at this time.