From 64c01d857628a358b4fc6ecb09373819b0f81c64 Mon Sep 17 00:00:00 2001 From: RandomFractals Date: Wed, 31 May 2023 10:53:06 -0500 Subject: [PATCH] stub out basic markdown headings document symbol provider (#33) to restore default markdown Outline for remapped standard .md documents, and build a custom Evidence markdown Outline for markdown documents navigation in VS Code --- src/extension.ts | 10 ++++++- src/providers/markdownSymbolProvider.ts | 38 +++++++++++++++++++++++++ 2 files changed, 47 insertions(+), 1 deletion(-) create mode 100644 src/providers/markdownSymbolProvider.ts diff --git a/src/extension.ts b/src/extension.ts index 583a224..c93a5e7 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -1,5 +1,6 @@ import { - commands, + languages, + window, workspace, ExtensionContext, } from 'vscode'; @@ -10,6 +11,8 @@ import { updateProjectContext } from './config'; import { statusBar } from './statusBar'; import { closeTerminal } from './terminal'; +import { MarkdownSymbolProvider } from './providers/markdownSymbolProvider'; + /** * Activates Evidence vscode extension. * @@ -19,6 +22,11 @@ export async function activate(context: ExtensionContext) { setExtensionContext(context); registerCommands(context); + // register markdown symbol provider + const markdownLanguage = { language: 'emd', scheme: 'file' }; + const provider = new MarkdownSymbolProvider(); + languages.registerDocumentSymbolProvider(markdownLanguage, provider); + // check for evidence app files const evidenceFiles = await workspace.findFiles('**/.evidence/**/*.*'); if (workspace.workspaceFolders && evidenceFiles.length > 0) { diff --git a/src/providers/markdownSymbolProvider.ts b/src/providers/markdownSymbolProvider.ts new file mode 100644 index 0000000..1e681fb --- /dev/null +++ b/src/providers/markdownSymbolProvider.ts @@ -0,0 +1,38 @@ +import { + CancellationToken, + DocumentSymbolProvider, + Location, + ProviderResult, + SymbolInformation, + SymbolKind, + TextDocument, +} from 'vscode'; + +/** + * Implements custom Evidence markdown document symbol provider. + */ +export class MarkdownSymbolProvider implements DocumentSymbolProvider { + + provideDocumentSymbols(document: TextDocument, token: CancellationToken): + ProviderResult { + + // parse text document symbols per line + const symbols: SymbolInformation[] = []; + for (let i = 0; i < document.lineCount; i++) { + const line = document.lineAt(i); + + // match heading lines + const match = line.text.match(/^#+\s+(.*)$/); + if (match) { + const symbol = new SymbolInformation(match[1], SymbolKind.String, + '', // container name + new Location(document.uri, line.range) + ); + + symbols.push(symbol); + } + } + + return symbols; + } +}