Skip to content

Commit b779bec

Browse files
RedCMDpokey
andauthored
Api for registering custom languages (#43)
* Api for registering custom languages May not be the best, but it works :} Allows registering a custom wasm with a custom language id Can overwrite the built in languages Can register a custom language id with one of the built in wasms #34 (comment) * Reload documents corresponding to the language id * Revert `let languages = { }` back to `const` * const => let Co-authored-by: Pokey Rule <[email protected]> * find => some Co-authored-by: Pokey Rule <[email protected]> * Parsing a Custom language * Remove `const` on ` absolute` * Remove ability to overwrite languages * `wasm` => `wasmPath` * Update README.md * I assume this is what you mean by exception * Improve documentation * More tweaks Co-authored-by: Pokey Rule <[email protected]>
1 parent 88fb349 commit b779bec

File tree

2 files changed

+49
-11
lines changed

2 files changed

+49
-11
lines changed

README.md

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
# Parse tree
22

3-
Exposes an api function that can be used to get a parse tree node for a given
4-
file location. Can be used as follows:
3+
Exposes an api function that can be used to get a parse tree node for a given file location.
4+
5+
## Usage
6+
7+
Can be used as follows:
58

69
```ts
710
const parseTreeExtension = vscode.extensions.getExtension("pokey.parse-tree");
@@ -13,10 +16,22 @@ if (parseTreeExtension == null) {
1316
const { getNodeAtLocation } = await parseTreeExtension.activate();
1417
```
1518

16-
Don't forget to add add an `extensionDependencies`-entry to `package.json` as
19+
Don't forget to add an `extensionDependencies`-entry to `package.json` as
1720
described in
1821
https://code.visualstudio.com/api/references/vscode-api#extensions.
1922

23+
### Parsing a custom language
24+
25+
If you'd like to add support for a new language, see the [Adding a new language](#adding-a-new-language) section below. Alternatively, your extension can register a custom language with this extension. Although this is not the preferred way to add a new language, it can be convenient when you have a parser that you don't believe belongs in the main extension.
26+
27+
Parsing your own language is as simple as registering your `languageId` with an absolute path to your `.wasm` file:
28+
29+
```ts
30+
const { registerLanguage } = await parseTreeExtension.activate();
31+
32+
registerLanguage(languageId, wasmPath);
33+
```
34+
2035
## Contributing
2136

2237
### Setup
@@ -49,9 +64,9 @@ When working with WSL, the host vscode instance connects to a vscode server on t
4964
We build a custom version of `web-tree-sitter` to ensure that we can always use the latest version and fix any problems as they come up.
5065
To update `web-tree-sitter`:
5166

52-
1. Update the variable `TREE_SITTER_VERSION` in the [`Makefile`](Makefile#L8);
53-
2. Update the path to `web-tree-sitter` in [`package.json`](package.json#103);
54-
3. (Optional) Create a new patch in `patches/tree-sitter+$TREE_SITTER_VERSION.patch`.
67+
1. Update the variable `TREE_SITTER_VERSION` in the [`Makefile`](Makefile#L8);
68+
2. Update the path to `web-tree-sitter` in [`package.json`](package.json#103);
69+
3. (Optional) Create a new patch in `patches/tree-sitter+$TREE_SITTER_VERSION.patch`.
5570

5671
The script which builds our custom version is in the [`Makefile`](Makefile#L37-63).
5772

src/extension.ts

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -70,11 +70,17 @@ export async function activate(context: vscode.ExtensionContext) {
7070
return true;
7171
}
7272

73-
const absolute = path.join(
74-
context.extensionPath,
75-
"parsers",
76-
language.module + ".wasm"
77-
);
73+
let absolute;
74+
if (path.isAbsolute(language.module)) {
75+
absolute = language.module;
76+
} else {
77+
absolute = path.join(
78+
context.extensionPath,
79+
"parsers",
80+
language.module + ".wasm"
81+
);
82+
}
83+
7884
const wasm = path.relative(process.cwd(), absolute);
7985
const lang = await Parser.Language.load(wasm);
8086
const parser = new Parser();
@@ -226,6 +232,23 @@ export async function activate(context: vscode.ExtensionContext) {
226232
return {
227233
loadLanguage,
228234

235+
/**
236+
* Register a parser wasm file for a language not supported by this
237+
* extension. Note that {@link wasmPath} must be an absolute path, and
238+
* {@link languageId} must not already have a registered parser.
239+
* @param languageId The VSCode language id that you'd like to register a
240+
* parser for
241+
* @param wasmPath The absolute path to the wasm file for your parser
242+
*/
243+
registerLanguage(languageId: string, wasmPath: string) {
244+
if (languages[languageId] != null) {
245+
throw new Error(`Language id '${languageId}' is already registered`);
246+
}
247+
248+
languages[languageId] = { module: wasmPath };
249+
colorAllOpen();
250+
},
251+
229252
getTree(document: vscode.TextDocument) {
230253
return getTreeForUri(document.uri);
231254
},

0 commit comments

Comments
 (0)