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,65 @@
import type { SyntaxNode, Tree } from "web-tree-sitter";
import { captureHasText, Devicetree, findCapture } from "./parser";
import { TextEdit } from "./textedit";
/**
* Upgrades deprecated properties.
*/
export function upgradeProperties(tree: Tree) {
return removeLabels(tree);
}
/**
* Renames "label" properties in keymap layers to "display-name". Removes all
* other "label" properties.
*/
function removeLabels(tree: Tree) {
const edits: TextEdit[] = [];
const query = Devicetree.query("(property name: (identifier) @name) @prop");
const matches = query.matches(tree.rootNode);
for (const { captures } of matches) {
const name = findCapture("name", captures);
const node = findCapture("prop", captures);
if (name?.text === "label" && node) {
if (isLayerLabel(node)) {
edits.push(new TextEdit(name, "display-name"));
} else {
edits.push(new TextEdit(node, ""));
}
}
}
return edits;
}
/**
* Given a "label" property node, returns whether it is a label for a keymap layer.
*/
function isLayerLabel(node: SyntaxNode) {
const maybeKeymap = node.parent?.parent;
if (!maybeKeymap) {
return false;
}
const query = Devicetree.query(
`(property
name: (identifier) @name
value: (string_literal) @value
) @prop`
);
const matches = query.matches(maybeKeymap);
for (const { captures } of matches) {
if (
findCapture("prop", captures)?.parent?.equals(maybeKeymap) &&
captureHasText("name", captures, "compatible") &&
captureHasText("value", captures, '"zmk,keymap"')
) {
return true;
}
}
return false;
}