2019-11-29 07:53:01 +01:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
// src/extension.ts
|
|
|
|
|
|
|
|
import * as vscode from 'vscode';
|
|
|
|
|
|
|
|
export function activate(context: vscode.ExtensionContext) {
|
|
|
|
|
|
|
|
context.subscriptions.push(
|
|
|
|
vscode.languages.registerDocumentSymbolProvider(
|
2019-11-29 23:52:39 +01:00
|
|
|
{scheme: "file", language: "swmf-config"},
|
2019-11-29 07:53:01 +01:00
|
|
|
new SwmfConfigDocumentSymbolProvider()
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
class SwmfConfigDocumentSymbolProvider implements vscode.DocumentSymbolProvider {
|
|
|
|
|
2019-11-30 09:56:17 +01:00
|
|
|
private format(cmd: string):string{
|
|
|
|
return cmd.substr(1).toLowerCase().replace(/^\w/, c => c.toUpperCase())
|
|
|
|
}
|
|
|
|
|
2019-11-29 07:53:01 +01:00
|
|
|
public provideDocumentSymbols(
|
|
|
|
document: vscode.TextDocument,
|
2019-11-29 23:52:39 +01:00
|
|
|
token: vscode.CancellationToken): Promise<vscode.DocumentSymbol[]>
|
2019-11-29 07:53:01 +01:00
|
|
|
{
|
2019-11-29 23:52:39 +01:00
|
|
|
return new Promise((resolve, reject) =>
|
|
|
|
{
|
|
|
|
let symbols: vscode.DocumentSymbol[] = [];
|
|
|
|
let nodes = [symbols]
|
|
|
|
let inside_marker = false
|
|
|
|
let inside_run = false
|
2019-11-30 09:56:17 +01:00
|
|
|
let inside_userinput = false
|
|
|
|
|
|
|
|
let symbolkind_marker = vscode.SymbolKind.Field
|
|
|
|
let symbolkind_run = vscode.SymbolKind.Event
|
|
|
|
let symbolkind_cmd = vscode.SymbolKind.Function
|
2019-11-29 07:53:01 +01:00
|
|
|
|
|
|
|
for (var i = 0; i < document.lineCount; i++) {
|
|
|
|
var line = document.lineAt(i);
|
|
|
|
|
2019-11-30 09:56:17 +01:00
|
|
|
let tokens = line.text.split(" ")
|
|
|
|
|
2019-11-29 07:53:01 +01:00
|
|
|
if (line.text.startsWith("#BEGIN_COMP")) {
|
2019-11-30 09:56:17 +01:00
|
|
|
|
2019-11-29 23:52:39 +01:00
|
|
|
let marker_symbol = new vscode.DocumentSymbol(
|
2019-11-30 09:56:17 +01:00
|
|
|
this.format(tokens[0]) + " " + tokens[1],
|
|
|
|
'Component',
|
|
|
|
symbolkind_marker,
|
2019-11-29 23:52:39 +01:00
|
|
|
line.range, line.range)
|
|
|
|
|
|
|
|
|
|
|
|
nodes[nodes.length-1].push(marker_symbol)
|
|
|
|
if (!inside_marker) {
|
|
|
|
nodes.push(marker_symbol.children)
|
|
|
|
inside_marker = true
|
2019-11-29 07:53:01 +01:00
|
|
|
}
|
2019-11-29 23:52:39 +01:00
|
|
|
// marker_symbol.children.push(_boot)
|
2019-11-29 07:53:01 +01:00
|
|
|
}
|
2019-11-29 23:52:39 +01:00
|
|
|
else if (line.text.startsWith("#END_COMP")) {
|
|
|
|
// TODO check if nodes has length 1 before popping.
|
|
|
|
if (inside_marker) {
|
|
|
|
nodes.pop()
|
|
|
|
inside_marker = false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (line.text.startsWith("#RUN") || line.text.startsWith("#END")) {
|
2019-11-30 09:56:17 +01:00
|
|
|
|
2019-11-29 23:52:39 +01:00
|
|
|
let run_symbol = new vscode.DocumentSymbol(
|
2019-11-30 09:56:17 +01:00
|
|
|
this.format(tokens[0]),
|
|
|
|
'Session separator',
|
|
|
|
symbolkind_run,
|
2019-11-29 23:52:39 +01:00
|
|
|
line.range, line.range)
|
2019-11-29 07:53:01 +01:00
|
|
|
|
2019-11-29 23:52:39 +01:00
|
|
|
if (inside_run) {
|
|
|
|
nodes.pop()
|
|
|
|
}
|
|
|
|
|
|
|
|
nodes[nodes.length-1].push(run_symbol)
|
|
|
|
nodes.push(run_symbol.children)
|
|
|
|
inside_run = true
|
2019-11-29 07:53:01 +01:00
|
|
|
}
|
2019-11-30 09:56:17 +01:00
|
|
|
else if (line.text.startsWith("#USERINPUTBEGIN")) {
|
|
|
|
|
|
|
|
let user_symbol = new vscode.DocumentSymbol(
|
|
|
|
this.format(tokens[0]),
|
|
|
|
'User module',
|
|
|
|
vscode.SymbolKind.Interface,
|
2019-11-29 23:52:39 +01:00
|
|
|
line.range, line.range)
|
|
|
|
|
|
|
|
|
2019-11-30 09:56:17 +01:00
|
|
|
nodes[nodes.length-1].push(user_symbol)
|
|
|
|
if (!inside_userinput) {
|
|
|
|
nodes.push(user_symbol.children)
|
|
|
|
inside_userinput = true
|
|
|
|
}
|
2019-11-29 23:52:39 +01:00
|
|
|
// marker_symbol.children.push(_boot)
|
2019-11-30 09:56:17 +01:00
|
|
|
}
|
|
|
|
else if (line.text.startsWith("#USERINPUTEND")) {
|
|
|
|
// TODO check if nodes has length 1 before popping.
|
|
|
|
if (inside_userinput) {
|
|
|
|
nodes.pop()
|
|
|
|
inside_userinput = false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (line.text.startsWith("#")) {
|
|
|
|
let cmd_symbol = new vscode.DocumentSymbol(
|
|
|
|
this.format(tokens[0]),
|
|
|
|
'',
|
|
|
|
symbolkind_cmd,
|
|
|
|
line.range, line.range)
|
|
|
|
|
|
|
|
nodes[nodes.length-1].push(cmd_symbol)
|
2019-11-29 07:53:01 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
resolve(symbols);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|