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.
This commit is contained in:
Joel Spadin 2024-01-21 17:15:58 -06:00
parent 1a3529a3e6
commit a0465391be
14 changed files with 528 additions and 337 deletions

View file

@ -0,0 +1,40 @@
import type { SyntaxNode, Tree } from "web-tree-sitter";
import { Devicetree, findCapture } from "./parser";
import { getUpgradeEdits, MatchFunc, ReplaceFunc, TextEdit } from "./textedit";
// Map of { "deprecated": "replacement" } header paths.
const HEADERS = {
"dt-bindings/zmk/matrix-transform.h": "dt-bindings/zmk/matrix_transform.h",
};
export function upgradeHeaders(tree: Tree) {
const edits: TextEdit[] = [];
const query = Devicetree.query(
"(preproc_include path: [(string_literal) (system_lib_string)] @path)"
);
const matches = query.matches(tree.rootNode);
for (const { captures } of matches) {
const node = findCapture("path", captures);
if (node) {
edits.push(
...getUpgradeEdits(node, HEADERS, headerReplaceHandler, isHeaderMatch)
);
}
}
return edits;
}
const isHeaderMatch: MatchFunc = (node, text) => {
return node.text === `"${text}"` || node.text === `<${text}>`;
};
const headerReplaceHandler: ReplaceFunc = (node, replacement) => {
if (!replacement) {
throw new Error("Header replacement does not support removing headers");
}
return [new TextEdit(node.startIndex + 1, node.endIndex - 1, replacement)];
};