@dot-agent/language-server 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/AGENTS.md +156 -0
- package/LICENSE +185 -0
- package/README.md +186 -0
- package/features/completions.js +112 -0
- package/features/definition.js +42 -0
- package/features/diagnostics.js +164 -0
- package/features/formatting.js +80 -0
- package/features/hover.js +86 -0
- package/features/links.js +71 -0
- package/features/references.js +65 -0
- package/features/rename.js +66 -0
- package/features/symbols.js +78 -0
- package/package.json +32 -0
- package/parser.js +126 -0
- package/server.js +169 -0
package/package.json
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@dot-agent/language-server",
|
|
3
|
+
"version": "0.2.0",
|
|
4
|
+
"description": "LSP server for .agent DSL files",
|
|
5
|
+
"license": "Apache-2.0",
|
|
6
|
+
"author": "Danilo Borges <contato@daniloborg.es>",
|
|
7
|
+
"repository": {
|
|
8
|
+
"type": "git",
|
|
9
|
+
"url": "https://github.com/dot-agent-spec/language-server.git"
|
|
10
|
+
},
|
|
11
|
+
"homepage": "https://github.com/dot-agent-spec/language-server#readme",
|
|
12
|
+
"bugs": {
|
|
13
|
+
"url": "https://github.com/dot-agent-spec/language-server/issues"
|
|
14
|
+
},
|
|
15
|
+
"keywords": ["lsp", "language-server", "agent", "dsl", "flow"],
|
|
16
|
+
"main": "server.js",
|
|
17
|
+
"engines": {
|
|
18
|
+
"node": ">=18"
|
|
19
|
+
},
|
|
20
|
+
"scripts": {
|
|
21
|
+
"start": "node server.js --stdio"
|
|
22
|
+
},
|
|
23
|
+
"dependencies": {
|
|
24
|
+
"vscode-languageserver": "^9.0.1",
|
|
25
|
+
"vscode-languageserver-textdocument": "^1.0.12",
|
|
26
|
+
"web-tree-sitter": "^0.25.0",
|
|
27
|
+
"@dot-agent/tree-sitter": "^0.2.0"
|
|
28
|
+
},
|
|
29
|
+
"publishConfig": {
|
|
30
|
+
"access": "public"
|
|
31
|
+
}
|
|
32
|
+
}
|
package/parser.js
ADDED
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright (c) 2026 Danilo Borges (https://github.com/daniloborges)
|
|
3
|
+
*
|
|
4
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
5
|
+
* you may not use this file except in compliance with the License.
|
|
6
|
+
* You may obtain a copy of the License at
|
|
7
|
+
*
|
|
8
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
9
|
+
*
|
|
10
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
11
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
12
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
13
|
+
* See the License for the specific language governing permissions and
|
|
14
|
+
* limitations under the License.
|
|
15
|
+
*/
|
|
16
|
+
|
|
17
|
+
'use strict';
|
|
18
|
+
|
|
19
|
+
const path = require('path');
|
|
20
|
+
const { Parser, Language } = require('web-tree-sitter');
|
|
21
|
+
const grammar = require('@dot-agent/tree-sitter');
|
|
22
|
+
|
|
23
|
+
// path.resolve ensures absolute paths survive vsix packaging and cwd changes
|
|
24
|
+
const AGENT_WASM = path.resolve(grammar.agentWasmPath);
|
|
25
|
+
const BEHAVIOR_WASM = path.resolve(grammar.behaviorWasmPath);
|
|
26
|
+
|
|
27
|
+
let agentParser, behaviorParser;
|
|
28
|
+
|
|
29
|
+
async function initParsers() {
|
|
30
|
+
await Parser.init();
|
|
31
|
+
const Agent = await Language.load(AGENT_WASM);
|
|
32
|
+
const Behavior = await Language.load(BEHAVIOR_WASM);
|
|
33
|
+
agentParser = new Parser();
|
|
34
|
+
agentParser.setLanguage(Agent);
|
|
35
|
+
behaviorParser = new Parser();
|
|
36
|
+
behaviorParser.setLanguage(Behavior);
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
// Cache: uri → { version, tree }
|
|
40
|
+
const cache = new Map();
|
|
41
|
+
|
|
42
|
+
function parse(uri, langId, text, version) {
|
|
43
|
+
const prev = cache.get(uri);
|
|
44
|
+
if (prev?.version === version) return prev.tree;
|
|
45
|
+
const parser = langId === 'behavior' ? behaviorParser : agentParser;
|
|
46
|
+
if (!parser) return null;
|
|
47
|
+
const tree = parser.parse(text, prev?.tree); // incremental reuse when possible
|
|
48
|
+
cache.set(uri, { version, tree });
|
|
49
|
+
return tree;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
function evict(uri) {
|
|
53
|
+
cache.delete(uri);
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
// ── AST helpers ──────────────────────────────────────────────────────────────
|
|
57
|
+
|
|
58
|
+
function nodesOfType(tree, type) {
|
|
59
|
+
if (!tree) return [];
|
|
60
|
+
return tree.rootNode.descendantsOfType(type);
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
function nodeAtOffset(tree, offset) {
|
|
64
|
+
if (!tree) return null;
|
|
65
|
+
return tree.rootNode.descendantForIndex(offset);
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
// Convert a tree-sitter {row, column} position to an LSP Range
|
|
69
|
+
function nodeToRange(node) {
|
|
70
|
+
return {
|
|
71
|
+
start: { line: node.startPosition.row, character: node.startPosition.column },
|
|
72
|
+
end: { line: node.endPosition.row, character: node.endPosition.column },
|
|
73
|
+
};
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
// Convert LSP position (line, character) to a byte offset in text
|
|
77
|
+
function positionToOffset(text, line, character) {
|
|
78
|
+
let offset = 0;
|
|
79
|
+
for (let i = 0; i < line; i++) {
|
|
80
|
+
const nl = text.indexOf('\n', offset);
|
|
81
|
+
offset = nl === -1 ? text.length : nl + 1;
|
|
82
|
+
}
|
|
83
|
+
return offset + character;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
// Extract the word (identifier chars including dots) around position
|
|
87
|
+
function wordAtPosition(text, line, character) {
|
|
88
|
+
const lines = text.split('\n');
|
|
89
|
+
const lineText = lines[line] || '';
|
|
90
|
+
let start = character, end = character;
|
|
91
|
+
while (start > 0 && /[a-zA-Z0-9_.]/.test(lineText[start - 1])) start--;
|
|
92
|
+
while (end < lineText.length && /[a-zA-Z0-9_.]/.test(lineText[end])) end++;
|
|
93
|
+
return { word: lineText.slice(start, end), start, end };
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
// Walk up the tree from offset, skipping ERROR/MISSING nodes
|
|
97
|
+
function getContextNode(tree, offset) {
|
|
98
|
+
let node = nodeAtOffset(tree, offset);
|
|
99
|
+
while (node && (node.isError || node.isMissing)) {
|
|
100
|
+
node = node.parent;
|
|
101
|
+
}
|
|
102
|
+
// If still on an error, try the previous sibling's last descendant
|
|
103
|
+
if (!node || node.type === 'ERROR') {
|
|
104
|
+
const raw = nodeAtOffset(tree, offset);
|
|
105
|
+
const parent = raw?.parent;
|
|
106
|
+
if (parent) {
|
|
107
|
+
const siblings = parent.children;
|
|
108
|
+
const idx = siblings.findIndex(c => c.startIndex > offset);
|
|
109
|
+
const prev = idx > 0 ? siblings[idx - 1] : siblings[siblings.length - 1];
|
|
110
|
+
if (prev && !prev.isError) node = prev;
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
return node ?? tree?.rootNode;
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
module.exports = {
|
|
117
|
+
initParsers,
|
|
118
|
+
parse,
|
|
119
|
+
evict,
|
|
120
|
+
nodesOfType,
|
|
121
|
+
nodeAtOffset,
|
|
122
|
+
nodeToRange,
|
|
123
|
+
positionToOffset,
|
|
124
|
+
wordAtPosition,
|
|
125
|
+
getContextNode,
|
|
126
|
+
};
|
package/server.js
ADDED
|
@@ -0,0 +1,169 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright (c) 2026 Danilo Borges (https://github.com/daniloborges)
|
|
3
|
+
*
|
|
4
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
5
|
+
* you may not use this file except in compliance with the License.
|
|
6
|
+
* You may obtain a copy of the License at
|
|
7
|
+
*
|
|
8
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
9
|
+
*
|
|
10
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
11
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
12
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
13
|
+
* See the License for the specific language governing permissions and
|
|
14
|
+
* limitations under the License.
|
|
15
|
+
*/
|
|
16
|
+
|
|
17
|
+
'use strict';
|
|
18
|
+
|
|
19
|
+
const {
|
|
20
|
+
createConnection,
|
|
21
|
+
TextDocuments,
|
|
22
|
+
ProposedFeatures,
|
|
23
|
+
TextDocumentSyncKind,
|
|
24
|
+
SymbolKind,
|
|
25
|
+
} = require('vscode-languageserver/node');
|
|
26
|
+
const { TextDocument } = require('vscode-languageserver-textdocument');
|
|
27
|
+
|
|
28
|
+
const { initParsers, parse, evict } = require('./parser');
|
|
29
|
+
|
|
30
|
+
const { provideHover } = require('./features/hover');
|
|
31
|
+
const { provideCompletions } = require('./features/completions');
|
|
32
|
+
const { diagnose } = require('./features/diagnostics');
|
|
33
|
+
const { provideDocumentSymbols }= require('./features/symbols');
|
|
34
|
+
const { provideDefinition } = require('./features/definition');
|
|
35
|
+
const { provideReferences } = require('./features/references');
|
|
36
|
+
const { provideRenameEdits } = require('./features/rename');
|
|
37
|
+
const { format } = require('./features/formatting');
|
|
38
|
+
const { provideDocumentLinks } = require('./features/links');
|
|
39
|
+
|
|
40
|
+
const connection = createConnection(ProposedFeatures.all);
|
|
41
|
+
const documents = new TextDocuments(TextDocument);
|
|
42
|
+
|
|
43
|
+
// ── Initialization ───────────────────────────────────────────────────────────
|
|
44
|
+
|
|
45
|
+
// web-tree-sitter is strictly async — await before advertising capabilities
|
|
46
|
+
// so no feature handler fires before parsers are ready.
|
|
47
|
+
connection.onInitialize(async () => {
|
|
48
|
+
await initParsers();
|
|
49
|
+
return {
|
|
50
|
+
capabilities: {
|
|
51
|
+
textDocumentSync: TextDocumentSyncKind.Incremental,
|
|
52
|
+
hoverProvider: true,
|
|
53
|
+
completionProvider: { triggerCharacters: ['.', ' '] },
|
|
54
|
+
definitionProvider: true,
|
|
55
|
+
referencesProvider: true,
|
|
56
|
+
renameProvider: { prepareProvider: false },
|
|
57
|
+
documentSymbolProvider: true,
|
|
58
|
+
documentFormattingProvider: true,
|
|
59
|
+
documentLinkProvider: { resolveProvider: false },
|
|
60
|
+
},
|
|
61
|
+
};
|
|
62
|
+
});
|
|
63
|
+
|
|
64
|
+
// ── Helpers ───────────────────────────────────────────────────────────────────
|
|
65
|
+
|
|
66
|
+
function getTree(doc) {
|
|
67
|
+
return parse(doc.uri, doc.languageId, doc.getText(), doc.version);
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
// ── Diagnostics (push on change) ─────────────────────────────────────────────
|
|
71
|
+
|
|
72
|
+
function validate(doc) {
|
|
73
|
+
const langId = doc.languageId;
|
|
74
|
+
if (langId !== 'agent' && langId !== 'behavior') return;
|
|
75
|
+
const tree = getTree(doc);
|
|
76
|
+
const diagnostics = diagnose(langId, tree, doc.getText());
|
|
77
|
+
connection.sendDiagnostics({ uri: doc.uri, diagnostics });
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
documents.onDidChangeContent(e => validate(e.document));
|
|
81
|
+
documents.onDidOpen(e => validate(e.document));
|
|
82
|
+
documents.onDidClose(e => {
|
|
83
|
+
evict(e.document.uri);
|
|
84
|
+
connection.sendDiagnostics({ uri: e.document.uri, diagnostics: [] });
|
|
85
|
+
});
|
|
86
|
+
|
|
87
|
+
// ── Hover ────────────────────────────────────────────────────────────────────
|
|
88
|
+
|
|
89
|
+
connection.onHover(({ textDocument, position }) => {
|
|
90
|
+
const doc = documents.get(textDocument.uri);
|
|
91
|
+
if (!doc) return null;
|
|
92
|
+
return provideHover(doc.languageId, doc.getText(), position);
|
|
93
|
+
});
|
|
94
|
+
|
|
95
|
+
// ── Completion ───────────────────────────────────────────────────────────────
|
|
96
|
+
|
|
97
|
+
connection.onCompletion(({ textDocument, position }) => {
|
|
98
|
+
const doc = documents.get(textDocument.uri);
|
|
99
|
+
if (!doc) return [];
|
|
100
|
+
return provideCompletions(doc.languageId, getTree(doc), doc.getText(), position);
|
|
101
|
+
});
|
|
102
|
+
|
|
103
|
+
// ── Definition ───────────────────────────────────────────────────────────────
|
|
104
|
+
|
|
105
|
+
connection.onDefinition(({ textDocument, position }) => {
|
|
106
|
+
const doc = documents.get(textDocument.uri);
|
|
107
|
+
if (!doc) return null;
|
|
108
|
+
return provideDefinition(doc.languageId, getTree(doc), doc.getText(), doc.uri, position);
|
|
109
|
+
});
|
|
110
|
+
|
|
111
|
+
// ── References ───────────────────────────────────────────────────────────────
|
|
112
|
+
|
|
113
|
+
connection.onReferences(({ textDocument, position }) => {
|
|
114
|
+
const doc = documents.get(textDocument.uri);
|
|
115
|
+
if (!doc) return [];
|
|
116
|
+
return provideReferences(doc.languageId, getTree(doc), doc.getText(), doc.uri, position);
|
|
117
|
+
});
|
|
118
|
+
|
|
119
|
+
// ── Rename ───────────────────────────────────────────────────────────────────
|
|
120
|
+
|
|
121
|
+
connection.onRenameRequest(({ textDocument, position, newName }) => {
|
|
122
|
+
const doc = documents.get(textDocument.uri);
|
|
123
|
+
if (!doc) return null;
|
|
124
|
+
return provideRenameEdits(doc.languageId, getTree(doc), doc.getText(), doc.uri, position, newName);
|
|
125
|
+
});
|
|
126
|
+
|
|
127
|
+
// ── Document Symbols ─────────────────────────────────────────────────────────
|
|
128
|
+
|
|
129
|
+
connection.onDocumentSymbol(({ textDocument }) => {
|
|
130
|
+
try {
|
|
131
|
+
const doc = documents.get(textDocument.uri);
|
|
132
|
+
if (!doc) {
|
|
133
|
+
connection.console.log('[symbols] doc not found for ' + textDocument.uri);
|
|
134
|
+
return [];
|
|
135
|
+
}
|
|
136
|
+
const result = provideDocumentSymbols(doc.languageId, getTree(doc));
|
|
137
|
+
connection.console.log(`[symbols] langId=${doc.languageId} count=${result.length} sample=${JSON.stringify(result[0]).slice(0,120)}`);
|
|
138
|
+
return result;
|
|
139
|
+
} catch (e) {
|
|
140
|
+
connection.console.error('[symbols] threw: ' + e.message + '\n' + e.stack);
|
|
141
|
+
return [];
|
|
142
|
+
}
|
|
143
|
+
});
|
|
144
|
+
|
|
145
|
+
// ── Document Links ───────────────────────────────────────────────────────────
|
|
146
|
+
|
|
147
|
+
connection.onDocumentLinks(({ textDocument }) => {
|
|
148
|
+
try {
|
|
149
|
+
const doc = documents.get(textDocument.uri);
|
|
150
|
+
if (!doc) return [];
|
|
151
|
+
return provideDocumentLinks(doc.languageId, getTree(doc), doc.uri);
|
|
152
|
+
} catch (e) {
|
|
153
|
+
connection.console.error(`documentLinks error: ${e.message}`);
|
|
154
|
+
return [];
|
|
155
|
+
}
|
|
156
|
+
});
|
|
157
|
+
|
|
158
|
+
// ── Formatting ───────────────────────────────────────────────────────────────
|
|
159
|
+
|
|
160
|
+
connection.onDocumentFormatting(({ textDocument }) => {
|
|
161
|
+
const doc = documents.get(textDocument.uri);
|
|
162
|
+
if (!doc) return [];
|
|
163
|
+
return format(doc.languageId, doc.getText());
|
|
164
|
+
});
|
|
165
|
+
|
|
166
|
+
// ── Start ────────────────────────────────────────────────────────────────────
|
|
167
|
+
|
|
168
|
+
documents.listen(connection);
|
|
169
|
+
connection.listen();
|