Skip to content
This repository has been archived by the owner on Apr 30, 2024. It is now read-only.

Commit

Permalink
stub out basic markdown headings document symbol provider (#33)
Browse files Browse the repository at this point in the history
to restore default markdown Outline for remapped standard .md documents, and build a custom Evidence markdown Outline for markdown documents navigation in VS Code
  • Loading branch information
RandomFractals committed May 31, 2023
1 parent 03a798a commit 64c01d8
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 1 deletion.
10 changes: 9 additions & 1 deletion src/extension.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {
commands,
languages,
window,
workspace,
ExtensionContext,
} from 'vscode';
Expand All @@ -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.
*
Expand All @@ -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) {
Expand Down
38 changes: 38 additions & 0 deletions src/providers/markdownSymbolProvider.ts
Original file line number Diff line number Diff line change
@@ -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<SymbolInformation[]> {

// 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;
}
}

0 comments on commit 64c01d8

Please sign in to comment.