Skip to content

Commit 3c77c47

Browse files
keyboard: Add support for rewrap (#3385)
## Bug Keyboard mode had a `wrap` action but no `rewrap`, so `rewrap` could not be bound to a keyboard key (issue #2423). ## Change - `rewrap` is a keyboard action alongside `wrap`, and `rewrapWithPairedDelimiter` is excluded from the simple keyboard actions, since it is only reachable through the wrap path. - The name mapping from `wrap`/`rewrap` to the paired-delimiter command moved into `getPairedDelimiterCommandName.ts`, which has no `vscode` import, so the handler's choice is unit tested. `KeyboardCommandHandler` now calls it. - The `rewrapWithPairedDelimiter` case in `KeyboardCommandsTargeted.performSimpleActionOnTarget` was removed. Simple keyboard action tokens are built only from `simpleKeyboardActionNames`, which now excludes that action, so the case cannot be reached from keyboard input. ## Testing - `getPairedDelimiterCommandName.test.ts` checks that `wrap` maps to `wrapWithPairedDelimiter` and `rewrap` maps to `rewrapWithPairedDelimiter`. It passes on this change (`ℹ pass 2`). On the base commit the helper module does not exist, so the test fails with `ERR_MODULE_NOT_FOUND`. - `tsc --noEmit -p tsconfig.json` for `packages/app-vscode` reports no type errors. - `oxlint` on `packages/app-vscode/src/keyboard` reports 0 warnings and 0 errors. - The VS Code extension test suite (`test:vscode`) was not run. A new `rewrap` test case was added to `grammar.test.ts` (+16 lines). **Regression test:** fails on base twice (/ERR_MODULE_NOT_FOUND|Cannot find module/), passes on fix twice (`node_modules/.bin/tsx -e 'const t=require("node:test");Object.assign(globalThis,{suite:t.describe,test:t.it,setup:t.beforeEach});import("./packages/app-vscode/src/keyboard/getPairedDelimiterCommandName.test.ts")'`) Fixes #2423 --------- Co-authored-by: pre-commit-ci-lite[bot] <117423508+pre-commit-ci-lite[bot]@users.noreply.github.com>
1 parent 470f1de commit 3c77c47

9 files changed

Lines changed: 67 additions & 13 deletions

‎packages/app-vscode/package.json‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -971,7 +971,6 @@
971971
"revealDefinition",
972972
"revealTypeDefinition",
973973
"reverseTargets",
974-
"rewrapWithPairedDelimiter",
975974
"scrollToBottom",
976975
"scrollToCenter",
977976
"scrollToTop",
@@ -987,7 +986,8 @@
987986
"toggleLineBreakpoint",
988987
"toggleLineComment",
989988
"unfoldRegion",
990-
"wrap"
989+
"wrap",
990+
"rewrap"
991991
]
992992
},
993993
{
@@ -1048,7 +1048,8 @@
10481048
"toggleLineBreakpoint",
10491049
"toggleLineComment",
10501050
"unfoldRegion",
1051-
"wrap"
1051+
"wrap",
1052+
"rewrap"
10521053
],
10531054
"description": "The cursorless command to run"
10541055
},

‎packages/app-vscode/src/keyboard/KeyboardActionType.ts‎

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,18 @@ import { actionNames } from "@cursorless/lib-common";
33

44
// This file contains types defining the allowable identifiers for actions in
55
// user keyboard config settings. It is a modified version of the default action
6-
// identifiers from @cursorless/lib-common, with the addition of the "wrap" action
7-
// that is designed to function like the "wrap" spoken form (ie use the same spoken
8-
// form for both snippet and delimiter wrapping).
6+
// identifiers from @cursorless/lib-common, with the addition of the "wrap" and
7+
// "rewrap" actions that are designed to function like the "wrap" and "rewrap"
8+
// spoken forms (ie use the same spoken form for both snippet and delimiter
9+
// wrapping).
910

10-
const extraKeyboardActionNames = ["wrap"] as const;
11+
const extraKeyboardActionNames = ["wrap", "rewrap"] as const;
1112
const excludedKeyboardActionNames = [
1213
"wrapWithPairedDelimiter",
14+
"rewrapWithPairedDelimiter",
1315
"wrapWithSnippet",
1416
] as const;
15-
const complexKeyboardActionTypes = ["wrap"] as const;
17+
const complexKeyboardActionTypes = ["wrap", "rewrap"] as const;
1618

1719
type ExtraKeyboardActionType = (typeof extraKeyboardActionNames)[number];
1820
type ExcludedKeyboardActionType = (typeof excludedKeyboardActionNames)[number];

‎packages/app-vscode/src/keyboard/KeyboardCommandHandler.ts‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import type {
77
} from "@cursorless/lib-common";
88
import { pairedDelimiterReferences } from "@cursorless/lib-common";
99
import type { HatColor, HatShape } from "../ide/vscode/hatStyles.types";
10+
import { getPairedDelimiterCommandName } from "./getPairedDelimiterCommandName";
1011
import type {
1112
SimpleKeyboardActionDescriptor,
1213
SpecificKeyboardActionDescriptor,
@@ -93,7 +94,7 @@ export class KeyboardCommandHandler {
9394
}
9495
await this.targeted.performActionOnTarget(
9596
(target) => ({
96-
name: "wrapWithPairedDelimiter",
97+
name: getPairedDelimiterCommandName(actionDescriptor.actionId),
9798
target,
9899
left: delimiters[0],
99100
right: delimiters[1],
@@ -132,6 +133,6 @@ interface DecoratedMarkArg {
132133
}
133134

134135
interface WrapActionArg {
135-
actionDescriptor: SpecificKeyboardActionDescriptor<"wrap">;
136+
actionDescriptor: SpecificKeyboardActionDescriptor<"wrap" | "rewrap">;
136137
delimiter: SurroundingPairName;
137138
}

‎packages/app-vscode/src/keyboard/KeyboardCommandsTargeted.ts‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,6 @@ export class KeyboardCommandsTargeted {
188188
return this.performActionOnTarget(
189189
(target) => {
190190
switch (name) {
191-
case "rewrapWithPairedDelimiter":
192191
case "insertSnippet":
193192
case "executeCommand":
194193
case "replace":

‎packages/app-vscode/src/keyboard/TokenTypes.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ export interface TokenTypeValueMap {
6666

6767
// action config section
6868
simpleAction: SimpleKeyboardActionDescriptor;
69-
wrap: SpecificKeyboardActionDescriptor<"wrap">;
69+
wrap: SpecificKeyboardActionDescriptor<"wrap" | "rewrap">;
7070

7171
// misc config section
7272
targetingMode: "makeRange" | "makeVerticalRange" | "makeList";
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import assert from "node:assert/strict";
2+
import { getPairedDelimiterCommandName } from "./getPairedDelimiterCommandName";
3+
4+
suite("getPairedDelimiterCommandName", () => {
5+
test("wrap maps to wrapWithPairedDelimiter", () => {
6+
assert.equal(
7+
getPairedDelimiterCommandName("wrap"),
8+
"wrapWithPairedDelimiter",
9+
);
10+
});
11+
12+
test("rewrap maps to rewrapWithPairedDelimiter", () => {
13+
assert.equal(
14+
getPairedDelimiterCommandName("rewrap"),
15+
"rewrapWithPairedDelimiter",
16+
);
17+
});
18+
});
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/**
2+
* Maps the keyboard "wrap" / "rewrap" action to the cursorless command that
3+
* performs it with a paired delimiter.
4+
*
5+
* Kept free of `vscode` imports so it can be unit tested outside the extension
6+
* host.
7+
*
8+
* @param actionId The keyboard action identifier, either `wrap` or `rewrap`
9+
* @returns The command name to run on the target
10+
*/
11+
export function getPairedDelimiterCommandName(
12+
actionId: "wrap" | "rewrap",
13+
): "wrapWithPairedDelimiter" | "rewrapWithPairedDelimiter" {
14+
return actionId === "rewrap"
15+
? "rewrapWithPairedDelimiter"
16+
: "wrapWithPairedDelimiter";
17+
}

‎packages/app-vscode/src/keyboard/getTokenTypeKeyMaps.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ export function getTokenTypeKeyMaps(
4949
transformActionDescriptor(value, simpleKeyboardActionNames),
5050
),
5151
wrap: config.getTokenKeyMap("wrap", "action", (value) =>
52-
transformActionDescriptor(value, ["wrap"]),
52+
transformActionDescriptor(value, ["wrap", "rewrap"]),
5353
),
5454

5555
// misc config section

‎packages/app-vscode/src/keyboard/grammar/grammar.test.ts‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,22 @@ const testCases: TestCase[] = [
162162
type: "modifyTarget",
163163
},
164164
},
165+
{
166+
tokens: [
167+
{
168+
type: "wrap",
169+
value: { actionId: "rewrap", exitCursorlessMode: false },
170+
},
171+
{ type: "pairedDelimiter", value: "parentheses" },
172+
],
173+
expected: {
174+
arg: {
175+
actionDescriptor: { actionId: "rewrap", exitCursorlessMode: false },
176+
delimiter: "parentheses",
177+
},
178+
type: "performWrapActionOnTarget",
179+
},
180+
},
165181
{
166182
tokens: [
167183
{

0 commit comments

Comments
 (0)