Improved outline view

This commit is contained in:
Dag Evensberget 2019-11-30 18:56:17 +10:00
parent 2fb1535ec8
commit 502fbf8594

View File

@ -16,6 +16,10 @@ export function activate(context: vscode.ExtensionContext) {
class SwmfConfigDocumentSymbolProvider implements vscode.DocumentSymbolProvider { class SwmfConfigDocumentSymbolProvider implements vscode.DocumentSymbolProvider {
private format(cmd: string):string{
return cmd.substr(1).toLowerCase().replace(/^\w/, c => c.toUpperCase())
}
public provideDocumentSymbols( public provideDocumentSymbols(
document: vscode.TextDocument, document: vscode.TextDocument,
token: vscode.CancellationToken): Promise<vscode.DocumentSymbol[]> token: vscode.CancellationToken): Promise<vscode.DocumentSymbol[]>
@ -26,16 +30,23 @@ class SwmfConfigDocumentSymbolProvider implements vscode.DocumentSymbolProvider
let nodes = [symbols] let nodes = [symbols]
let inside_marker = false let inside_marker = false
let inside_run = false let inside_run = false
let inside_cmd = false let inside_userinput = false
let symbolkind_marker = vscode.SymbolKind.Field
let symbolkind_run = vscode.SymbolKind.Event
let symbolkind_cmd = vscode.SymbolKind.Function
for (var i = 0; i < document.lineCount; i++) { for (var i = 0; i < document.lineCount; i++) {
var line = document.lineAt(i); var line = document.lineAt(i);
let tokens = line.text.split(" ")
if (line.text.startsWith("#BEGIN_COMP")) { if (line.text.startsWith("#BEGIN_COMP")) {
let marker_symbol = new vscode.DocumentSymbol( let marker_symbol = new vscode.DocumentSymbol(
line.text.substr(1,13), this.format(tokens[0]) + " " + tokens[1],
'Marker', 'Component',
vscode.SymbolKind.Class, symbolkind_marker,
line.range, line.range) line.range, line.range)
@ -54,10 +65,11 @@ class SwmfConfigDocumentSymbolProvider implements vscode.DocumentSymbolProvider
} }
} }
else if (line.text.startsWith("#RUN") || line.text.startsWith("#END")) { else if (line.text.startsWith("#RUN") || line.text.startsWith("#END")) {
let run_symbol = new vscode.DocumentSymbol( let run_symbol = new vscode.DocumentSymbol(
line.text.substr(1,4), this.format(tokens[0]),
'Session', 'Session separator',
vscode.SymbolKind.Subroutine, symbolkind_run,
line.range, line.range) line.range, line.range)
if (inside_run) { if (inside_run) {
@ -68,23 +80,40 @@ class SwmfConfigDocumentSymbolProvider implements vscode.DocumentSymbolProvider
nodes.push(run_symbol.children) nodes.push(run_symbol.children)
inside_run = true inside_run = true
} }
else if (line.text.startsWith("#")) { else if (line.text.startsWith("#USERINPUTBEGIN")) {
let marker_symbol = new vscode.DocumentSymbol(
line.text.substr(1,13).toLowerCase(), let user_symbol = new vscode.DocumentSymbol(
'Command', this.format(tokens[0]),
vscode.SymbolKind.Constructor, 'User module',
vscode.SymbolKind.Interface,
line.range, line.range) line.range, line.range)
nodes[nodes.length-1].push(marker_symbol) nodes[nodes.length-1].push(user_symbol)
// nodes.push(marker_symbol.children) if (!inside_userinput) {
nodes.push(user_symbol.children)
inside_userinput = true
}
// marker_symbol.children.push(_boot) // marker_symbol.children.push(_boot)
inside_cmd = true }
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)
} }
} }
// Need this otherwise the outline view just says
// "Loading document symbols for <file>..." forever.
resolve(symbols); resolve(symbols);
}); });
} }