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

Quick code actions #102

Closed
wants to merge 1 commit into from
Closed
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
44 changes: 44 additions & 0 deletions src/actions/actions.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,28 @@ class CallbackAction:
callbacks_map = {callback.identifier: callback.callback for callback in callbacks}


@dataclass
class ComplexAction:
defaultSpokenForm: str
identifier: str
action: str
args: list[any]


# NOTE: Please do not change these dicts. Use the CSVs for customization.
# See https://github.com/pokey/cursorless-talon/blob/main/docs/customization.md
complex_actions = [
ComplexAction(
"-quick list",
"experimentalListCodeActions",
"executeCodeAction",
[{"onlyDisplayInfo": True}],
),
]

complex_action_map = {action.identifier: action for action in complex_actions}


mod.list("cursorless_simple_action", desc="Supported actions for cursorless navigation")


Expand Down Expand Up @@ -90,6 +112,10 @@ class CallbackAction:
"unfold": "unfoldRegion",
**{action.term: action.identifier for action in makeshift_actions},
**{callback.term: callback.identifier for callback in callbacks},
**{
complex_action.defaultSpokenForm: complex_action.identifier
for complex_action in complex_actions
},
}


Expand All @@ -101,6 +127,11 @@ def cursorless_simple_action(action: str, targets: dict):
return callbacks_map[action](targets)
elif action in makeshift_action_map:
return run_makeshift_action(action, targets)
elif action in complex_action_map:
action_info = complex_action_map[action]
return actions.user.cursorless_single_target_command(
action_info.action, targets, *action_info.args
)
else:
return actions.user.cursorless_single_target_command(action, targets)

Expand All @@ -114,6 +145,19 @@ def run_makeshift_action(action: str, targets: dict):
actions.sleep(makeshift_action.post_command_sleep)


@mod.capture(
rule=(
"{user.cursorless_simple_action} | "
"{user.cursorless_experimental_quick_code_action}"
)
)
def cursorless_simple_action(m) -> str:
try:
return m.cursorless_simple_action
except AttributeError:
return m.cursorless_experimental_quick_code_action


# NOTE: Please do not change these dicts. Use the CSVs for customization.
# See https://github.com/pokey/cursorless-talon/blob/main/docs/customization.md
default_values = {
Expand Down
29 changes: 29 additions & 0 deletions src/actions/quick_code_action.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
from ..csv_overrides import init_csv_and_watch_changes
from talon import Module, actions, app, Context

mod = Module()

mod.tag(
"cursorless_experimental_snippets",
desc="tag for enabling experimental snippet support",
)

experimental_quick_code_actions_ctx = Context()
experimental_quick_code_actions_ctx.matches = r"""
tag: user.cursorless_experimental_quick_code_actions
"""


def on_ready():
init_csv_and_watch_changes(
"experimental/quick_code_actions",
{
"wrapper_snippet": wrapper_snippets,
},
allow_unknown_values=True,
default_list_name="wrapper_snippet",
ctx=experimental_quick_code_actions_ctx,
)


app.register("ready", on_ready)
2 changes: 1 addition & 1 deletion src/cursorless.talon
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
app: vscode
-

{user.cursorless_simple_action} <user.cursorless_target>:
<user.cursorless_simple_action> <user.cursorless_target>:
user.cursorless_simple_action(cursorless_simple_action, cursorless_target)

{user.cursorless_swap_action} <user.cursorless_swap_targets>:
Expand Down