-
-
Notifications
You must be signed in to change notification settings - Fork 43
Expand file tree
/
Copy pathextension.ts
More file actions
352 lines (308 loc) · 10.9 KB
/
extension.ts
File metadata and controls
352 lines (308 loc) · 10.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
import * as fs from "fs";
import * as path from "path";
import * as semver from "semver";
import * as vscode from "vscode";
import * as treeSitter from "web-tree-sitter";
import { LanguageStillLoadingError, UnsupportedLanguageError } from "./errors";
interface Language {
module: string;
parser?: treeSitter.Parser;
}
/* eslint-disable @typescript-eslint/naming-convention */
// Be sure to declare the language in package.json and include a minimalist grammar.
const languages: Record<string, Language | undefined> = {
"java-properties": { module: "tree-sitter-properties" },
"talon-list": { module: "tree-sitter-talon" },
agda: { module: "tree-sitter-agda" },
c: { module: "tree-sitter-c" },
clojure: { module: "tree-sitter-clojure" },
cpp: { module: "tree-sitter-cpp" },
csharp: { module: "tree-sitter-c_sharp" },
css: { module: "tree-sitter-css" },
dart: { module: "tree-sitter-dart" },
elixir: { module: "tree-sitter-elixir" },
elm: { module: "tree-sitter-elm" },
gdscript: { module: "tree-sitter-gdscript" },
gleam: { module: "tree-sitter-gleam" },
go: { module: "tree-sitter-go" },
haskell: { module: "tree-sitter-haskell" },
html: { module: "tree-sitter-html" },
java: { module: "tree-sitter-java" },
javascript: { module: "tree-sitter-javascript" },
javascriptreact: { module: "tree-sitter-javascript" },
json: { module: "tree-sitter-json" },
jsonc: { module: "tree-sitter-json" },
jsonl: { module: "tree-sitter-json" },
julia: { module: "tree-sitter-julia" },
kotlin: { module: "tree-sitter-kotlin" },
latex: { module: "tree-sitter-latex" },
lua: { module: "tree-sitter-lua" },
markdown: { module: "tree-sitter-markdown" },
nix: { module: "tree-sitter-nix" },
perl: { module: "tree-sitter-perl" },
php: { module: "tree-sitter-php" },
properties: { module: "tree-sitter-properties" },
python: { module: "tree-sitter-python" },
r: { module: "tree-sitter-r" },
ruby: { module: "tree-sitter-ruby" },
rust: { module: "tree-sitter-rust" },
scala: { module: "tree-sitter-scala" },
scm: { module: "tree-sitter-query" },
scss: { module: "tree-sitter-scss" },
shellscript: { module: "tree-sitter-bash" },
sparql: { module: "tree-sitter-sparql" },
starlark: { module: "tree-sitter-python" },
swift: { module: "tree-sitter-swift" },
talon: { module: "tree-sitter-talon" },
terraform: { module: "tree-sitter-hcl" },
typescript: { module: "tree-sitter-typescript" },
typescriptreact: { module: "tree-sitter-tsx" },
xml: { module: "tree-sitter-xml" },
yaml: { module: "tree-sitter-yaml" },
};
// For some reason this crashes if we put it inside activate
// Fix: this isn't a field, suppress package member coloring like Go
const initParser = treeSitter.Parser.init();
// Called when the extension is first activated by user opening a file with the appropriate language
export function activate(context: vscode.ExtensionContext) {
console.debug("Activating tree-sitter...");
// Parse of all visible documents
const trees: Record<string, treeSitter.Tree | undefined> = {};
/**
* FIXME: On newer vscode versions some Tree sitter parser throws memory errors
* https://github.com/cursorless-dev/cursorless/issues/2879
* https://github.com/cursorless-dev/vscode-parse-tree/issues/110
*/
const disabledLanguages = semver.gte(vscode.version, "1.98.0")
? new Set(["latex", "swift"])
: null;
const validateGetLanguage = (languageId: string) => {
if (disabledLanguages?.has(languageId)) {
throw new Error(
`${languageId} is disabled on vscode versions >= 1.98.0. See https://github.com/cursorless-dev/cursorless/issues/2879`,
);
}
};
/**
* Load the parser model for a given language
* @param languageId The vscode language id of the language to load
* @returns a promise resolving to boolean an indicating whether the language could be loaded
*/
async function loadLanguage(languageId: string) {
const language = languages[languageId];
if (language == null) {
return false;
}
if (language.parser != null) {
return true;
}
if (disabledLanguages?.has(languageId)) {
return false;
}
let absolute;
if (path.isAbsolute(language.module)) {
absolute = language.module;
} else {
absolute = path.join(
context.extensionPath,
"parsers",
language.module + ".wasm",
);
}
if (!fs.existsSync(absolute)) {
throw Error(`Parser for ${languageId} not found at ${absolute}`);
}
const wasm = path.relative(process.cwd(), absolute);
await initParser;
const lang = await treeSitter.Language.load(wasm);
const parser = new treeSitter.Parser();
parser.setLanguage(lang);
language.parser = parser;
return true;
}
async function open(document: vscode.TextDocument) {
const uriString = document.uri.toString();
if (uriString in trees) {
return;
}
if (!(await loadLanguage(document.languageId))) {
return;
}
const language = languages[document.languageId];
if (language?.parser == null) {
throw new Error(`No parser for language ${document.languageId}`);
}
const t = language.parser?.parse(document.getText());
if (t == null) {
throw Error(`Failed to parse ${document.uri.toString()}`);
}
trees[uriString] = t;
}
function openIfLanguageLoaded(document: vscode.TextDocument) {
const uriString = document.uri.toString();
if (uriString in trees) {
return null;
}
const language = languages[document.languageId];
if (language?.parser == null) {
return null;
}
const t = language.parser.parse(document.getText());
if (t == null) {
throw Error(`Failed to parse ${document.uri.toString()}`);
}
trees[uriString] = t;
return t;
}
// NOTE: if you make this an async function, it seems to cause edit anomalies
function edit(edit: vscode.TextDocumentChangeEvent) {
const language = languages[edit.document.languageId];
if (language == null || language.parser == null) {
return;
}
updateTree(language.parser, edit);
}
function updateTree(
parser: treeSitter.Parser,
edit: vscode.TextDocumentChangeEvent,
) {
if (edit.contentChanges.length === 0) {
return;
}
const old = trees[edit.document.uri.toString()];
if (old == null) {
throw new Error(`No existing tree for ${edit.document.uri.toString()}`);
}
for (const e of edit.contentChanges) {
const startIndex = e.rangeOffset;
const oldEndIndex = e.rangeOffset + e.rangeLength;
const newEndIndex = e.rangeOffset + e.text.length;
const startPos = edit.document.positionAt(startIndex);
const oldEndPos = edit.document.positionAt(oldEndIndex);
const newEndPos = edit.document.positionAt(newEndIndex);
const startPosition = asPoint(startPos);
const oldEndPosition = asPoint(oldEndPos);
const newEndPosition = asPoint(newEndPos);
const delta = {
startIndex,
oldEndIndex,
newEndIndex,
startPosition,
oldEndPosition,
newEndPosition,
};
old.edit(delta);
}
const t = parser.parse(edit.document.getText(), old);
if (t == null) {
throw Error(`Failed to parse ${edit.document.uri.toString()}`);
}
trees[edit.document.uri.toString()] = t;
}
function asPoint(pos: vscode.Position): treeSitter.Point {
return { row: pos.line, column: pos.character };
}
function close(document: vscode.TextDocument) {
delete trees[document.uri.toString()];
}
async function colorAllOpen() {
for (const editor of vscode.window.visibleTextEditors) {
await open(editor.document);
}
}
async function openIfVisible(document: vscode.TextDocument) {
if (
vscode.window.visibleTextEditors.some(
(editor) => editor.document.uri.toString() === document.uri.toString(),
)
) {
await open(document);
}
}
context.subscriptions.push(
vscode.window.onDidChangeVisibleTextEditors(colorAllOpen),
);
context.subscriptions.push(vscode.workspace.onDidChangeTextDocument(edit));
context.subscriptions.push(vscode.workspace.onDidCloseTextDocument(close));
context.subscriptions.push(
vscode.workspace.onDidOpenTextDocument(openIfVisible),
);
// Don't wait for the initial color, it takes too long to inspect the themes and causes VSCode extension host to hang
void colorAllOpen();
function getTreeForUri(uri: vscode.Uri) {
const ret = trees[uri.toString()];
if (ret == null) {
const document = vscode.workspace.textDocuments.find(
(textDocument) => textDocument.uri.toString() === uri.toString(),
);
if (document == null) {
throw new Error(`Document ${uri.toString()} is not open`);
}
const ret = openIfLanguageLoaded(document);
if (ret != null) {
return ret;
}
const languageId = document.languageId;
if (languageId in languages) {
validateGetLanguage(document.languageId);
throw new LanguageStillLoadingError(languageId);
} else {
throw new UnsupportedLanguageError(languageId);
}
}
return ret;
}
return {
loadLanguage,
/**
* @deprecated
*/
getLanguage(languageId: string): treeSitter.Language | undefined {
console.warn(
"vscode-parse-tree: getLanguage is deprecated, use createQuery(languageId, source) instead.",
);
validateGetLanguage(languageId);
return languages[languageId]?.parser?.language ?? undefined;
},
createQuery(
languageId: string,
source: string,
): treeSitter.Query | undefined {
const language = languages[languageId]?.parser?.language;
if (language == null) {
validateGetLanguage(languageId);
return undefined;
}
return new treeSitter.Query(language, source);
},
/**
* Register a parser wasm file for a language not supported by this
* extension. Note that {@link wasmPath} must be an absolute path, and
* {@link languageId} must not already have a registered parser.
* @param languageId The VSCode language id that you'd like to register a
* parser for
* @param wasmPath The absolute path to the wasm file for your parser
*/
registerLanguage(languageId: string, wasmPath: string) {
if (languages[languageId] != null) {
throw new Error(`Language id '${languageId}' is already registered`);
}
languages[languageId] = { module: wasmPath };
void colorAllOpen();
},
getTree(document: vscode.TextDocument) {
return getTreeForUri(document.uri);
},
getTreeForUri,
getNodeAtLocation(location: vscode.Location) {
return getTreeForUri(location.uri).rootNode.descendantForPosition({
row: location.range.start.line,
column: location.range.start.character,
});
},
};
}
// this method is called when your extension is deactivated
export function deactivate() {
// Empty
}