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.
This commit is contained in:
Joel Spadin 2024-01-22 12:43:24 -06:00
parent 37fcf190e6
commit d4be70587d
2 changed files with 28 additions and 6 deletions

View File

@ -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;
}

View File

@ -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";
<KeymapUpgrader />