A VS Code extension that exposes the Tree Sitter API to other extensions.
It can:
- Load the Tree Sitter WebAssembly module, as well as supported languages.
- Cache parsed trees.
- Detect languages.
This is work-in-progress intended for future use within Dance. A few more polishing touches are needed before it will be ready to publish on the extension store. More importantly, the API is very much subject to change.
See extension.test.ts
for an up-to-date
example.
const document = await vscode.workspace.openTextDocument({
content: `
pub fn foo() {
println!("bar");
}
`,
language: "rust",
});
const TreeSitter = await vscode.extensions.getExtension<API>(
"gregoire.tree-sitter",
).activate()!;
await TreeSitter.withDocumentTree(document, async (tree) => {
await TreeSitter.withQuery(
document,
`(macro_invocation) @macro`,
(query) => {
const captures = query.captures(tree.rootNode);
const macroCapture = captures.find(({ name }) => name === "macro");
assert.ok(macroCapture);
assert.strictEqual(macroCapture.node.type, "macro_invocation");
assert.strictEqual(macroCapture.node.text, 'println!("bar")');
},
);
});
Note:
- Detection of the language of
document
was done automatically. - The Tree Sitter library and Rust parser were both loaded implicitly when
calling
documentTree()
. - No object had to be manually deleted with
.delete()
, despite Tree Sitter being a WebAssembly library which requires manual deallocation.- It is still possible to manage your objects manually by using
documentTree()
andquery()
(instead of theirwith*
counterparts), but you will have to make sure to call.delete()
after using them.
- It is still possible to manage your objects manually by using
Deno and Emscripten must be
installed with deno
and emcc
both available in the PATH
.
Install dependencies with yarn
:
$ yarn
A few files must be generated for the build process to continue. Since building these files can be slow and requires internet access, this must be done manually:
$ ./tools.ts --build-wasm --update-text-objects
Run yarn run compile
to build the extension, and yarn vsce package
to
package it for publishing in the store.
In VS Code, start the watch:test
task, then launch "Extension Tests" in the
"Run and Debug" menu.
This extension provides a command named "Inspect Scopes" which displays the current scope in the status bar; hovering the scope will display all its ancestors in a tooltip. This may help write commands that operate on the returned tree or that perform queries.