Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I implement my own code outline layout in vscode?

I'm looking for an extension in Visual Studio Code (vscode) where I can define my custom code outline. Essentially, listing all my functions/definitions in a tree-like manner.

Let's say I'm using a simple language that looks as follows:

begin foo1 arriving procedure
  move into queue1
  print queue1
  send to foo2 
end

begin foo2 arriving procedure
  move into queue2
  print queue2
  send to foo3
end

I would like to know if there is an extension for vscode that let's me implement something like this:

like this

Would be nice if it was clickable. To navigate/go to definition, and possible expandable in case of more complex code.

What I've found so far.

  1. vscode code outline https://github.com/patrys/vscode-code-outline , I like this extension except it doesn't work for my language. Example image for a .js file

  2. Show Functions https://marketplace.visualstudio.com/items?itemName=qrti.funclist

  3. Sourcecookifier for notepad++ (Can do what I want but for notepad++ obviously)

I like the second extension (Show Functions) as it is easily customizable in the vscode/settings file. You can define your own regular expression from settings. However, it is not in a outline view fixed to the editor. Nor is it refreshing live.

I like the first extension too as it is in a tree view but I don't seem to know how and where to modify the settings in order to achieve my layout described.

If anyone could point me in right directions it would be very appreciated. I already tried a bit with the documentation of code outline extension but I don't think it is making any sense to me.

PS: First post on StackOverflow please let me know if there's something I should add/change.

Thanks in advance.

like image 205
Yuli Hua Avatar asked Jan 17 '18 17:01

Yuli Hua


People also ask

How do I arrange my code in VS Code?

The code formatting is available in Visual Studio Code (VSCode) through the following shortcuts or key combinations: On Windows Shift + Alt + F. On macOS Shift + Option + F. On Linux Ctrl + Shift + I.

How do I create a VS Code template?

In VSCode, open a folder that will contain your new project. Use the Command Palette to execute the command "Project: Create Project From Template". A list of available templates should appear. Select the desired template.


2 Answers

Okay, my request is now solved.

The CodeMap extension, is basically the extension I'm looking for.

I followed their guide on https://github.com/oleg-shilo/codemap.vscode/wiki/Adding-custom-mappers

I created a custom dedicated mapper "mapper_X.js" saved it to an arbitrary location, and in my vscode user-settings I pasted "codemap.X": "mylocation\\mapper_X.js", (as described in the github guide). I then opened up a new file, saved it as untitled.X and typed some syntax (the example code in my question), now I could see my custom outline.

As could be seen in the result-link below I have (deliberately) not defined my mapper to consider any other cases yet. My mapper is still in its infancy state. Just thought I'd share my findings before I forget I posted this question...

result

like image 66
Yuli Hua Avatar answered Sep 23 '22 17:09

Yuli Hua


In recent versions of VS Code there is an API for populating the outline view without needing to depend on third party extensions (except the one you need to write yourself).

This works by registering a DocumentSymbolProvider.

export function activate(context: vscode.ExtensionContext) {
    context.subscriptions.push(
        vscode.languages.registerDocumentSymbolProvider(
            {scheme: "file", language: "swmf-config"}, 
            new SwmfConfigDocumentSymbolProvider())
    );
}

This permits a flat structure or a tree structure in the outline view (the two cannot be mixed).

class SwmfConfigDocumentSymbolProvider implements vscode.DocumentSymbolProvider {
    public provideDocumentSymbols(
        document: vscode.TextDocument,
        token: vscode.CancellationToken): Promise<vscode.DocumentSymbol[]> {
        return new Promise((resolve, reject) => {
            let symbols: vscode.DocumentSymbol[] = [];
            for (var i = 0; i < document.lineCount; i++) {
                var line = document.lineAt(i);
                if (line.text.startsWith("#")) {
                    let symbol = new vscode.DocumentSymbol(
                        line.text, 'Component',
                        vscode.SymbolKind.Function,
                        line.range, line.range)
                    symbols.push(symbol)
                }
            }
            resolve(symbols);
        });
    }
}

For a small, fully working example giving a tree structure in the outline view, see https://github.com/svaberg/SWMF-grammar

like image 22
Svaberg Avatar answered Sep 25 '22 17:09

Svaberg