Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,20 @@ const testCases: TestCase[] = [
keySequence: ["da", "aw", "wp", "aw", "wp"],
finalContent: "((a))\n",
},
{
name: "modifier range",
initialContent: "aaa bbb ccc ddd",
// clear bat past its next token
keySequence: ["db", "fk", "n", "st", "c"],
finalContent: "aaa ddd",
},
{
name: "modifier list",
initialContent: "aaa bbb ccc ddd",
// clear bat and its second next token
keySequence: ["db", "fa", "2", "n", "st", "c"],
finalContent: "aaa ccc ",
},
];

suite("Basic keyboard test", async function () {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ import {
SimpleKeyboardActionDescriptor,
SpecificKeyboardActionDescriptor,
} from "./KeyboardActionType";
import KeyboardCommandsTargeted from "./KeyboardCommandsTargeted";
import KeyboardCommandsTargeted, {
TargetingMode,
} from "./KeyboardCommandsTargeted";
import { ModalVscodeCommandDescriptor } from "./TokenTypes";
import { surroundingPairsDelimiters } from "@cursorless/cursorless-engine";
import { isString } from "lodash";
Expand Down Expand Up @@ -89,8 +91,14 @@ export class KeyboardCommandHandler {
);
}

modifyTarget({ modifier }: { modifier: Modifier }) {
this.targeted.targetModifier(modifier);
modifyTarget({
modifier,
mode,
}: {
modifier: Modifier;
mode?: TargetingMode;
}) {
this.targeted.targetModifier(modifier, mode);
}
}

Expand All @@ -99,7 +107,7 @@ interface DecoratedMarkArg {
color?: HatColor;
shape?: HatShape;
};
mode: "replace" | "extend" | "append";
mode: TargetingMode;
}

interface WrapActionArg {
Expand Down
162 changes: 69 additions & 93 deletions packages/cursorless-vscode/src/keyboard/KeyboardCommandsTargeted.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import KeyboardCommandsModal from "./KeyboardCommandsModal";
import KeyboardHandler from "./KeyboardHandler";
import { SimpleKeyboardActionDescriptor } from "./KeyboardActionType";

type TargetingMode = "replace" | "extend" | "append";
export type TargetingMode = "replace" | "extend" | "append";

interface TargetDecoratedMarkArgument {
color?: HatColor;
Expand Down Expand Up @@ -84,53 +84,56 @@ export default class KeyboardCommandsTargeted {
return;
}

let target: PartialTargetDescriptor = {
type: "primitive",
mark: {
type: "decoratedSymbol",
symbolColor: getStyleName(color, shape),
character,
},
};
return await setKeyboardTarget(
this.applyTargetingMode(
{
type: "primitive",
mark: {
type: "decoratedSymbol",
symbolColor: getStyleName(color, shape),
character,
},
},
mode,
),
);
};

private applyTargetingMode(
target: PartialPrimitiveTargetDescriptor,
mode: TargetingMode,
): PartialTargetDescriptor {
switch (mode) {
case "extend":
target = {
return {
type: "range",
anchor: {
type: "primitive",
mark: {
type: "that",
},
},
anchor: getKeyboardTarget(),
active: target,
excludeActive: false,
excludeAnchor: false,
};
break;
case "append":
target = {
return {
type: "list",
elements: [
{
type: "primitive",
mark: {
type: "that",
},
},
target,
],
elements: [getKeyboardTarget(), target],
};
break;
case "replace":
break;
return target;
}
}

return await executeCursorlessCommand({
name: "private.setKeyboardTarget",
target,
});
};
/**
* Applies {@link modifier} to the current target
* @param param0 Describes the desired modifier
* @returns A promise that resolves to the result of the cursorless command
*/
targetModifier = async (
modifier: Modifier,
mode: TargetingMode = "replace",
) =>
await setKeyboardTarget(
this.applyTargetingMode(getKeyboardTarget(modifier), mode),
);

/**
* Expands the current target to the containing {@link scopeType}
Expand All @@ -141,37 +144,9 @@ export default class KeyboardCommandsTargeted {
scopeType,
type = "containingScope",
}: ModifyTargetContainingScopeArgument) =>
await executeCursorlessCommand({
name: "private.setKeyboardTarget",
target: {
type: "primitive",
modifiers: [
{
type,
scopeType,
},
],
mark: {
type: "keyboard",
},
},
});

/**
* Applies {@link modifier} to the current target
* @param param0 Describes the desired modifier
* @returns A promise that resolves to the result of the cursorless command
*/
targetModifier = async (modifier: Modifier) =>
await executeCursorlessCommand({
name: "private.setKeyboardTarget",
target: {
type: "primitive",
modifiers: [modifier],
mark: {
type: "keyboard",
},
},
await this.targetModifier({
type,
scopeType,
});

/**
Expand Down Expand Up @@ -249,12 +224,7 @@ export default class KeyboardCommandsTargeted {
) => ActionDescriptor,
{ exitCursorlessMode }: PerformActionOpts,
) => {
const action = constructActionPayload({
type: "primitive",
mark: {
type: "keyboard",
},
});
const action = constructActionPayload(getKeyboardTarget());
const returnValue = await executeCursorlessCommand(action);

if (exitCursorlessMode) {
Expand All @@ -264,13 +234,10 @@ export default class KeyboardCommandsTargeted {
} else {
// If we're not exiting cursorless mode, preserve the keyboard mark
// FIXME: Better to just not clobber the keyboard mark on each action?
await executeCursorlessCommand({
name: "private.setKeyboardTarget",
target: {
type: "primitive",
mark: {
type: "that",
},
await setKeyboardTarget({
type: "primitive",
mark: {
type: "that",
},
});
}
Expand All @@ -295,16 +262,9 @@ export default class KeyboardCommandsTargeted {
exitCursorlessMode,
}: VscodeCommandOnTargetOptions = {},
) => {
const target: PartialPrimitiveTargetDescriptor = {
type: "primitive",
mark: {
type: "keyboard",
},
};

const returnValue = await executeCursorlessCommand({
name: "executeCommand",
target,
target: getKeyboardTarget(),
commandId,
options: {
restoreSelection: !keepChangedSelection,
Expand All @@ -327,13 +287,10 @@ export default class KeyboardCommandsTargeted {
* @returns A promise that resolves to the result of the cursorless command
*/
targetSelection = () =>
executeCursorlessCommand({
name: "private.setKeyboardTarget",
target: {
type: "primitive",
mark: {
type: "cursor",
},
setKeyboardTarget({
type: "primitive",
mark: {
type: "cursor",
},
});

Expand Down Expand Up @@ -367,6 +324,25 @@ interface VscodeCommandOnTargetOptions {
exitCursorlessMode?: boolean;
}

function setKeyboardTarget(target: PartialTargetDescriptor) {
return executeCursorlessCommand({
name: "private.setKeyboardTarget",
target,
});
}

function getKeyboardTarget(
...modifiers: Modifier[]
): PartialPrimitiveTargetDescriptor {
return {
type: "primitive",
modifiers: modifiers.length > 0 ? modifiers : undefined,
mark: {
type: "keyboard",
},
};
}

function executeCursorlessCommand(action: ActionDescriptor) {
return runCursorlessCommand({
action,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ const grammar: Grammar = {
command("targetDecoratedMark", { decoratedMark: $1, mode: "append" })
},
{"name": "main", "symbols": ["modifier"], "postprocess": command("modifyTarget", { modifier: $0 })},
{"name": "main", "symbols": [(keyboardLexer.has("makeRange") ? {type: "makeRange"} : makeRange), "modifier"], "postprocess": command("modifyTarget", { modifier: $1, mode: "extend" })},
{"name": "main", "symbols": [(keyboardLexer.has("makeList") ? {type: "makeList"} : makeList), "modifier"], "postprocess": command("modifyTarget", { modifier: $1, mode: "append" })},
{"name": "main", "symbols": [(keyboardLexer.has("simpleAction") ? {type: "simpleAction"} : simpleAction)], "postprocess": command("performSimpleActionOnTarget", ["actionDescriptor"])},
{"name": "main", "symbols": [(keyboardLexer.has("wrap") ? {type: "wrap"} : wrap), (keyboardLexer.has("pairedDelimiter") ? {type: "pairedDelimiter"} : pairedDelimiter)], "postprocess":
command("performWrapActionOnTarget", ["actionDescriptor", "delimiter"])
Expand Down
2 changes: 2 additions & 0 deletions packages/cursorless-vscode/src/keyboard/grammar/grammar.ne
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ main -> %makeList decoratedMark {%
# --------------------------- Modifier --------------------------

main -> modifier {% command("modifyTarget", { modifier: $0 }) %}
main -> %makeRange modifier {% command("modifyTarget", { modifier: $1, mode: "extend" }) %}
main -> %makeList modifier {% command("modifyTarget", { modifier: $1, mode: "append" }) %}

# --------------------------- Actions --------------------------

Expand Down