zmk/docs/src/keymap-upgrade/parser.ts
Joel Spadin a0465391be refactor: Improve keymap upgrader
Moved the keymap upgrader to a top-level page like the power profiler
to make it more discoverable. It upgrades more things than key codes
now, so putting it in the codes category doesn't make much sense.

Converted the upgrader code to TypeScript and split it up into smaller
files to make it easier to add new upgrade functions.

Added upgrade functions to remove/replace "label" properties and rename
matrix-transform.h to matrix_transform.h.
2024-01-25 18:03:37 -06:00

56 lines
1.3 KiB
TypeScript

import Parser from "web-tree-sitter";
const TREE_SITTER_WASM_URL = new URL(
"/node_modules/web-tree-sitter/tree-sitter.wasm",
import.meta.url
);
export let Devicetree: Parser.Language;
export async function initParser() {
await Parser.init({
locateFile: (path: string, prefix: string) => {
// When locating tree-sitter.wasm, use a path that Webpack can map to the correct URL.
if (path == "tree-sitter.wasm") {
return TREE_SITTER_WASM_URL.href;
}
return prefix + path;
},
});
Devicetree = await Parser.Language.load("/tree-sitter-devicetree.wasm");
}
export function createParser() {
if (!Devicetree) {
throw new Error("Parser not loaded. Call initParser() first.");
}
const parser = new Parser();
parser.setLanguage(Devicetree);
return parser;
}
/**
* Returns the node for the named capture.
*/
export function findCapture(name: string, captures: Parser.QueryCapture[]) {
for (const c of captures) {
if (c.name === name) {
return c.node;
}
}
return null;
}
/**
* Returns whether the node for the named capture exists and has the given text.
*/
export function captureHasText(
name: string,
captures: Parser.QueryCapture[],
text: string
) {
const node = findCapture(name, captures);
return node?.text === text;
}