From d4be70587d9e30795accaa4bf5f84afa50dfb2f1 Mon Sep 17 00:00:00 2001 From: Joel Spadin Date: Mon, 22 Jan 2024 12:43:24 -0600 Subject: [PATCH] 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. --- docs/src/keymap-upgrade/keycodes.ts | 27 +++++++++++++++++++++++---- docs/src/pages/keymap-upgrader.mdx | 7 +++++-- 2 files changed, 28 insertions(+), 6 deletions(-) diff --git a/docs/src/keymap-upgrade/keycodes.ts b/docs/src/keymap-upgrade/keycodes.ts index 5069556c..5fc3e66a 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; +} diff --git a/docs/src/pages/keymap-upgrader.mdx b/docs/src/pages/keymap-upgrader.mdx index 5aafc8be..d643ca14 100644 --- a/docs/src/pages/keymap-upgrader.mdx +++ b/docs/src/pages/keymap-upgrader.mdx @@ -11,10 +11,13 @@ Some behaviors, key codes, and other features have been renamed to be more consi Paste the contents of a `.keymap` file below. Then, hover your mouse over the upgraded keymap and click the `Copy` button in the upper-right corner to copy it to your clipboard. -You will likely need to realign columns in the upgraded keymap. The upgrader also does not handle -codes inside a `#define`, so you will need to update those manually using +:::warning + +The upgrader does not handle key codes inside a `#define` or a behavior creation macro such as `ZMK_MACRO()`, so you will need to update those manually using [this list of deprecated codes and replacements](https://github.com/zmkfirmware/zmk/blob/main/docs/src/keymap-upgrade/keycodes.ts). +::: + import KeymapUpgrader from "@site/src/components/KeymapUpgrader/index";