Skip to content

Commit 7daf3be

Browse files
authored
Merge pull request #21 from PraneshASP/feat/syntax-highlight-tree-files
♻️ Add Basic syntax highlight support for `.tree` files
2 parents 2a29a87 + e7af2f6 commit 7daf3be

File tree

4 files changed

+105
-4
lines changed

4 files changed

+105
-4
lines changed

package.json

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@
2828
"main": "./src/extension.js",
2929
"activationEvents": [
3030
"onLanguage:solidity",
31-
"onLanguage:json"
31+
"onLanguage:json",
32+
"onLanguage:tree"
3233
],
3334
"contributes": {
3435
"commands": [
@@ -121,7 +122,22 @@
121122
"when": "resourceFilename =~ /\\.json/"
122123
}
123124
]
124-
}
125+
},
126+
"grammars": [
127+
{
128+
"language": "tree",
129+
"scopeName": "source.tree",
130+
"path": "./src/syntaxes/tree-syntax.tmLanguage.json"
131+
}
132+
],
133+
"languages": [
134+
{
135+
"id": "tree",
136+
"extensions": [
137+
".tree"
138+
]
139+
}
140+
]
125141
},
126142
"scripts": {},
127143
"devDependencies": {

src/commands/parse-tree.js

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
const vscode = require("vscode");
2+
3+
// Create a diagnostic collection
4+
const treeFilesDiagnosticCollection = vscode.languages.createDiagnosticCollection('treeNodes');
5+
6+
// Register a Code Action provider
7+
const treeFilesCodeActionProvider = vscode.languages.registerCodeActionsProvider('tree', {
8+
provideCodeActions(document) {
9+
const diagnostics = [];
10+
11+
document.getText().split('\n').forEach((lineText, lineNumber) => {
12+
lineText = lineText.trim();
13+
if (lineNumber == 0 && !lineText.endsWith('.t.sol')) {
14+
const diagnostic = new vscode.Diagnostic(
15+
new vscode.Range(0, 0, 0, lineText.length),
16+
`Should be a valid test name (ending with .t.sol)`,
17+
vscode.DiagnosticSeverity.Error
18+
);
19+
20+
diagnostics.push(diagnostic);
21+
}
22+
if (/^(||)/.test(lineText)) {
23+
const allowedPrefixes = ['it', 'when', 'given'];
24+
lineText = lineText.replace(/^[^a-zA-Z0-9]+/, '');
25+
26+
// Check if the line starts with an allowed prefix
27+
const prefix = lineText.split(' ')[0];
28+
if (!allowedPrefixes.includes(prefix)) {
29+
// Create a diagnostic for the violation
30+
const diagnostic = new vscode.Diagnostic(
31+
new vscode.Range(lineNumber, 0, lineNumber, lineText.length),
32+
`Tree node should start with "it", "when", or "given"`,
33+
vscode.DiagnosticSeverity.Error
34+
);
35+
diagnostics.push(diagnostic);
36+
}
37+
}
38+
39+
});
40+
41+
treeFilesDiagnosticCollection.set(document.uri, diagnostics);
42+
43+
},
44+
});
45+
46+
module.exports = { treeFilesDiagnosticCollection, treeFilesCodeActionProvider };

src/extension.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,9 @@ const {
3232

3333
const {
3434
generateDeploymentReportActiveFile, generateDeploymentReportContextMenu
35-
} = require("./commands/deployment-report")
35+
} = require("./commands/deployment-report");
36+
37+
const { treeFilesCodeActionProvider, treeFilesDiagnosticCollection } = require("./commands/parse-tree");
3638

3739

3840
/** global vars */
@@ -127,7 +129,9 @@ function onActivate(context) {
127129
context.subscriptions.push(generateDeploymentReportActiveFileSubscription);
128130
context.subscriptions.push(generateDeploymentReportContextMenuSubscription);
129131

130-
132+
// Register the Code Action provider and diagnostic collection to the subscriptions
133+
context.subscriptions.push(treeFilesCodeActionProvider);
134+
context.subscriptions.push(treeFilesDiagnosticCollection);
131135

132136
vscode.window.visibleTextEditors.map(editor => {
133137
if (editor && editor.document && editor.document.languageId == "solidity") {
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
{
2+
"name": "Tree File",
3+
"scopeName": "source.tree",
4+
"fileTypes": [
5+
".tree"
6+
],
7+
"patterns": [
8+
{
9+
"include": "#tree-characters"
10+
},
11+
{
12+
"match": "\\b[a-zA-Z0-9_.-]+\\.t\\.sol\\b",
13+
"name": "constant.filename"
14+
},
15+
{
16+
"match": "\\b(when|it|given)\\b",
17+
"name": "keyword.when-it-given"
18+
},
19+
{
20+
"begin": "//",
21+
"end": "$",
22+
"name": "comment"
23+
}
24+
],
25+
"repository": {
26+
"tree-characters": {
27+
"patterns": [
28+
{
29+
"match": "(├──|└──|│)",
30+
"name": "string.tree-characters"
31+
}
32+
]
33+
}
34+
}
35+
}

0 commit comments

Comments
 (0)