Skip to content

Commit a5ced00

Browse files
committed
add symbol information support
1 parent 5f89451 commit a5ced00

File tree

3 files changed

+49
-2
lines changed

3 files changed

+49
-2
lines changed

extension.js

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
const vscode = require('vscode');
2+
3+
function activate(context) {
4+
const selector = { language: 'lark', scheme: 'file' };
5+
const provider = new LarkSymbolProvider();
6+
context.subscriptions.push(
7+
vscode.languages.registerDocumentSymbolProvider(selector, provider)
8+
);
9+
}
10+
11+
class LarkSymbolProvider {
12+
provideDocumentSymbols(document) {
13+
const symbols = [];
14+
const termRe = /^\s*(?:[?!])?([A-Z0-9_]+)(?:\.\d+)?\s*:/; // optional ?/! prefix, uppercase identifiers, optional .n suffix
15+
const ruleRe = /^\s*(?:[?!])?([a-z0-9_]+)(?:\.\d+)?\s*:/; // optional ?/! prefix, lowercase identifiers, optional .n suffix
16+
17+
for (let i = 0; i < document.lineCount; i++) {
18+
const text = document.lineAt(i).text; // text content of the current line
19+
20+
const termMatch = termRe.exec(text);
21+
if (termMatch) {
22+
const name = termMatch[1]; // terminal name
23+
const kind = vscode.SymbolKind.Constant;
24+
const range = new vscode.Range(i, 0, i, text.length);
25+
symbols.push(new vscode.DocumentSymbol(name, '', kind, range, range));
26+
continue;
27+
}
28+
29+
const ruleMatch = ruleRe.exec(text);
30+
if (ruleMatch) {
31+
const name = ruleMatch[1]; // rule name
32+
const kind = vscode.SymbolKind.Function;
33+
const range = new vscode.Range(i, 0, i, text.length);
34+
symbols.push(new vscode.DocumentSymbol(name, '', kind, range, range));
35+
}
36+
}
37+
return symbols;
38+
}
39+
}
40+
41+
function deactivate() { }
42+
43+
module.exports = { activate, deactivate };

package.json

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "vscode-lark",
3-
"version": "0.1.0",
3+
"version": "0.2.0",
44
"publisher": "dirk-thomas",
55
"engines": {
66
"vscode": "^1.2.0"
@@ -19,6 +19,10 @@
1919
"bugs": {
2020
"url": "https://github.com/dirk-thomas/vscode-lark/issues"
2121
},
22+
"main": "./extension.js",
23+
"activationEvents": [
24+
"onLanguage:lark"
25+
],
2226
"contributes": {
2327
"languages": [
2428
{

syntaxes/lark.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,4 +78,4 @@
7878
}
7979
],
8080
"scopeName": "source.lark"
81-
}
81+
}

0 commit comments

Comments
 (0)