aboutsummaryrefslogtreecommitdiffhomepage
path: root/docs/src/keymap-upgrade
diff options
context:
space:
mode:
authorJoel Spadin <[email protected]>2024-01-22 12:43:24 -0600
committerJoel Spadin <[email protected]>2024-01-25 18:03:37 -0600
commitd4be70587d9e30795accaa4bf5f84afa50dfb2f1 (patch)
treee18c83d51b4c16e99da5e822f66295beb8b34586 /docs/src/keymap-upgrade
parent37fcf190e682e1c3b72d9011dfb616268a649e5a (diff)
downloadzmk-d4be70587d9e30795accaa4bf5f84afa50dfb2f1.tar.gz
zmk-d4be70587d9e30795accaa4bf5f84afa50dfb2f1.zip
fix(keymap-upgrader): Filter key codes to bindings
Changed the key code upgrader to only replace codes that appear in "bindings" properties. Modifier flags such as MOD_LCTL are no longer valid as key codes, but they are still used in "mods" properties and should not be replaced there.
Diffstat (limited to 'docs/src/keymap-upgrade')
-rw-r--r--docs/src/keymap-upgrade/keycodes.ts27
1 files changed, 23 insertions, 4 deletions
diff --git a/docs/src/keymap-upgrade/keycodes.ts b/docs/src/keymap-upgrade/keycodes.ts
index 5069556c6e..5fc3e66abd 100644
--- a/docs/src/keymap-upgrade/keycodes.ts
+++ b/docs/src/keymap-upgrade/keycodes.ts
@@ -77,21 +77,24 @@ const CODES = {
MOD_RGUI: "RGUI",
};
+// Regex matching names of properties that can have keymap bindings.
+const BINDINGS_PROPS = /^(bindings|sensor-bindings)$/;
+
/**
* Upgrades deprecated key code identifiers.
*/
export function upgradeKeycodes(tree: Tree) {
const edits: TextEdit[] = [];
- // No need to filter to the bindings array. The C preprocessor would have
- // replaced identifiers anywhere, so upgrading all identifiers preserves the
- // original behavior of the keymap (even if that behavior wasn't intended).
const query = Devicetree.query("(identifier) @name");
const matches = query.matches(tree.rootNode);
for (const { captures } of matches) {
const node = findCapture("name", captures);
- if (node) {
+
+ // Some of the codes are still valid to use in other properties such as
+ // "mods", so only replace those that are inside a "bindings" array.
+ if (node && isInBindingsArray(node)) {
edits.push(...getUpgradeEdits(node, CODES, keycodeReplaceHandler));
}
}
@@ -148,3 +151,19 @@ function findBehaviorNodes(paramNode: SyntaxNode) {
return nodes;
}
+
+/**
+ * Given a identifier, returns whether it is inside a "bindings" property value.
+ */
+function isInBindingsArray(identifier: SyntaxNode) {
+ let node = identifier.parent;
+ while (node) {
+ if (node.type === "property") {
+ return !!node.childForFieldName("name")?.text.match(BINDINGS_PROPS);
+ }
+
+ node = node.parent;
+ }
+
+ return false;
+}