Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

move definition of which actions exit cursorless mode to the config #2174

Merged
merged 16 commits into from
Jan 20, 2024
183 changes: 128 additions & 55 deletions packages/cursorless-vscode/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -864,61 +864,134 @@
"description": "Define modal keybindings for actions",
"type": "object",
"additionalProperties": {
"type": "string",
"enum": [
"callAsFunction",
"clearAndSetSelection",
"copyToClipboard",
"cutToClipboard",
"deselect",
"editNew",
"editNewLineAfter",
"editNewLineBefore",
"executeCommand",
"extractVariable",
"findInDocument",
"findInWorkspace",
"foldRegion",
"followLink",
"generateSnippet",
"getText",
"highlight",
"indentLine",
"insertCopyAfter",
"insertCopyBefore",
"insertEmptyLineAfter",
"insertEmptyLineBefore",
"insertEmptyLinesAround",
"insertSnippet",
"moveToTarget",
"outdentLine",
"pasteFromClipboard",
"randomizeTargets",
"remove",
"rename",
"replace",
"replaceWithTarget",
"revealDefinition",
"revealTypeDefinition",
"reverseTargets",
"rewrapWithPairedDelimiter",
"scrollToBottom",
"scrollToCenter",
"scrollToTop",
"setSelection",
"setSelectionAfter",
"setSelectionBefore",
"showDebugHover",
"showHover",
"showQuickFix",
"showReferences",
"sortTargets",
"swapTargets",
"toggleLineBreakpoint",
"toggleLineComment",
"unfoldRegion",
"wrapWithPairedDelimiter",
"wrapWithSnippet"
"anyOf": [
{
"type": "string",
"enum": [
"callAsFunction",
"clearAndSetSelection",
"copyToClipboard",
"cutToClipboard",
"deselect",
"editNew",
"editNewLineAfter",
"editNewLineBefore",
"executeCommand",
"extractVariable",
"findInDocument",
"findInWorkspace",
"foldRegion",
"followLink",
"generateSnippet",
"getText",
"highlight",
"indentLine",
"insertCopyAfter",
"insertCopyBefore",
"insertEmptyLineAfter",
"insertEmptyLineBefore",
"insertEmptyLinesAround",
"insertSnippet",
"moveToTarget",
"outdentLine",
"pasteFromClipboard",
"randomizeTargets",
"remove",
"rename",
"replace",
"replaceWithTarget",
"revealDefinition",
"revealTypeDefinition",
"reverseTargets",
"rewrapWithPairedDelimiter",
"scrollToBottom",
"scrollToCenter",
"scrollToTop",
"setSelection",
"setSelectionAfter",
"setSelectionBefore",
"showDebugHover",
"showHover",
"showQuickFix",
"showReferences",
"sortTargets",
"swapTargets",
"toggleLineBreakpoint",
"toggleLineComment",
"unfoldRegion",
"wrap"
]
},
{
"type": "object",
"properties": {
"actionId": {
"type": "string",
"enum": [
"callAsFunction",
"clearAndSetSelection",
"copyToClipboard",
"cutToClipboard",
"deselect",
"editNew",
"editNewLineAfter",
"editNewLineBefore",
"executeCommand",
"extractVariable",
"findInDocument",
"findInWorkspace",
"foldRegion",
"followLink",
"generateSnippet",
"getText",
"highlight",
"indentLine",
"insertCopyAfter",
"insertCopyBefore",
"insertEmptyLineAfter",
"insertEmptyLineBefore",
"insertEmptyLinesAround",
"insertSnippet",
"moveToTarget",
"outdentLine",
"pasteFromClipboard",
"randomizeTargets",
"remove",
"rename",
"replace",
"replaceWithTarget",
"revealDefinition",
"revealTypeDefinition",
"reverseTargets",
"rewrapWithPairedDelimiter",
"scrollToBottom",
"scrollToCenter",
"scrollToTop",
"setSelection",
"setSelectionAfter",
"setSelectionBefore",
"showDebugHover",
"showHover",
"showQuickFix",
"showReferences",
"sortTargets",
"swapTargets",
"toggleLineBreakpoint",
"toggleLineComment",
"unfoldRegion",
"wrap"
],
"description": "The cursorless command to run"
},
"exitCursorlessMode": {
"type": "boolean",
"description": "Indicates whether the command should exit cursorless mode after it is run, defaults false."
}
},
"required": [
"actionId"
]
}
]
}
},
Expand Down
13 changes: 13 additions & 0 deletions packages/cursorless-vscode/src/keyboard/KeyboardActionType.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,19 @@ export type SimpleKeyboardActionType = Exclude<
KeyboardActionType,
ComplexKeyboardActionType
>;

export type SpecificKeyboardActionDescriptor<T extends KeyboardActionType> = {
actionId: T;
exitCursorlessMode: boolean;
};

export type PolymorphicKeyboardActionDescriptor =
| KeyboardActionType
| SpecificKeyboardActionDescriptor<KeyboardActionType>;

export type SimpleKeyboardActionDescriptor =
SpecificKeyboardActionDescriptor<SimpleKeyboardActionType>;

export type KeyboardActionType =
| Exclude<ActionType, ExcludedKeyboardActionType>
| ExtraKeyboardActionType;
Expand Down
34 changes: 21 additions & 13 deletions packages/cursorless-vscode/src/keyboard/KeyboardCommandHandler.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
import { Modifier, SurroundingPairName } from "@cursorless/common";
import * as vscode from "vscode";
import { HatColor, HatShape } from "../ide/vscode/hatStyles.types";
import { SimpleKeyboardActionType } from "./KeyboardActionType";
import {
SimpleKeyboardActionDescriptor,
SpecificKeyboardActionDescriptor,
} from "./KeyboardActionType";
import KeyboardCommandsTargeted from "./KeyboardCommandsTargeted";
import { ModalVscodeCommandDescriptor } from "./TokenTypes";
import { surroundingPairsDelimiters } from "@cursorless/cursorless-engine";
import { isString } from "lodash";

/**
* This class defines the keyboard commands available to our modal keyboard
Expand Down Expand Up @@ -65,21 +69,24 @@ export class KeyboardCommandHandler {
}

performSimpleActionOnTarget({
actionName,
actionDescriptor,
}: {
actionName: SimpleKeyboardActionType;
actionDescriptor: SimpleKeyboardActionDescriptor;
}) {
this.targeted.performSimpleActionOnTarget(actionName);
this.targeted.performSimpleActionOnTarget(actionDescriptor);
}

performWrapActionOnTarget({ delimiter }: { delimiter: SurroundingPairName }) {
performWrapActionOnTarget({ actionDescriptor, delimiter }: WrapActionArg) {
const [left, right] = surroundingPairsDelimiters[delimiter]!;
this.targeted.performActionOnTarget((target) => ({
name: "wrapWithPairedDelimiter",
target,
left,
right,
}));
this.targeted.performActionOnTarget(
(target) => ({
name: "wrapWithPairedDelimiter",
target,
left,
right,
}),
actionDescriptor,
);
}

modifyTarget({ modifier }: { modifier: Modifier }) {
Expand All @@ -95,6 +102,7 @@ interface DecoratedMarkArg {
mode: "replace" | "extend" | "append";
}

function isString(input: any): input is string {
return typeof input === "string" || input instanceof String;
interface WrapActionArg {
actionDescriptor: SpecificKeyboardActionDescriptor<"wrap">;
delimiter: SurroundingPairName;
}
Loading