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:
parent
1a3529a3e6
commit
a0465391be
14 changed files with 528 additions and 337 deletions
40
docs/src/keymap-upgrade/headers.ts
Normal file
40
docs/src/keymap-upgrade/headers.ts
Normal 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)];
|
||||
};
|
||||
Loading…
Add table
Add a link
Reference in a new issue