From cea952a2c9d139dc9c029dbac95de55fca95d860 Mon Sep 17 00:00:00 2001 From: Andreas Arvidsson Date: Sun, 21 Apr 2024 20:53:49 +0200 Subject: [PATCH] Community wrapper snippets (#1998) This is the Cursorless side of the community wrapper snippets https://github.com/talonhub/community/pull/1315 ## Checklist - [-] I have added [tests](https://www.cursorless.org/docs/contributing/test-case-recorder/) - [-] I have updated the [docs](https://github.com/cursorless-dev/cursorless/tree/main/docs) and [cheatsheet](https://github.com/cursorless-dev/cursorless/tree/main/cursorless-talon/src/cheatsheet) - [-] I have not broken the cheatsheet --------- Co-authored-by: pre-commit-ci-lite[bot] <117423508+pre-commit-ci-lite[bot]@users.noreply.github.com> Co-authored-by: Pokey Rule <755842+pokey@users.noreply.github.com> --- src/cursorless.talon | 9 --------- src/snippet_cursorless.talon | 14 ++++++++++++++ src/snippets.py | 36 ++++++++++++++++++++++++++++++++++++ src/snippets_community.talon | 13 +++++++++++++ 4 files changed, 63 insertions(+), 9 deletions(-) create mode 100644 src/snippet_cursorless.talon create mode 100644 src/snippets_community.talon diff --git a/src/cursorless.talon b/src/cursorless.talon index f12bab8a3e..f1ab5aa206 100644 --- a/src/cursorless.talon +++ b/src/cursorless.talon @@ -24,15 +24,6 @@ tag: user.cursorless {user.cursorless_wrap_action} : user.private_cursorless_wrap_with_paired_delimiter(cursorless_wrap_action, cursorless_target, cursorless_wrapper_paired_delimiter) -{user.cursorless_insert_snippet_action} : - user.private_cursorless_insert_snippet(cursorless_insertion_snippet) - -{user.cursorless_insert_snippet_action} {user.cursorless_insertion_snippet_single_phrase} [{user.cursorless_phrase_terminator}]: - user.private_cursorless_insert_snippet_with_phrase(cursorless_insertion_snippet_single_phrase, text) - -{user.cursorless_wrapper_snippet} {user.cursorless_wrap_action} : - user.private_cursorless_wrap_with_snippet(cursorless_wrap_action, cursorless_target, cursorless_wrapper_snippet) - {user.cursorless_show_scope_visualizer} [{user.cursorless_visualization_type}]: user.private_cursorless_show_scope_visualizer(cursorless_scope_type, cursorless_visualization_type or "content") {user.cursorless_hide_scope_visualizer}: diff --git a/src/snippet_cursorless.talon b/src/snippet_cursorless.talon new file mode 100644 index 0000000000..ffc28e20b4 --- /dev/null +++ b/src/snippet_cursorless.talon @@ -0,0 +1,14 @@ +mode: command +mode: user.cursorless_spoken_form_test +tag: user.cursorless +and not tag: user.cursorless_use_community_snippets +- + +{user.cursorless_insert_snippet_action} : + user.private_cursorless_insert_snippet(cursorless_insertion_snippet) + +{user.cursorless_insert_snippet_action} {user.cursorless_insertion_snippet_single_phrase} [{user.cursorless_phrase_terminator}]: + user.private_cursorless_insert_snippet_with_phrase(cursorless_insertion_snippet_single_phrase, text) + +{user.cursorless_wrapper_snippet} {user.cursorless_wrap_action} : + user.private_cursorless_wrap_with_snippet(cursorless_wrap_action, cursorless_target, cursorless_wrapper_snippet) diff --git a/src/snippets.py b/src/snippets.py index 5cd9a83214..ae4b766bcb 100644 --- a/src/snippets.py +++ b/src/snippets.py @@ -16,6 +16,19 @@ class InsertionSnippet: destination: CursorlessDestination +@dataclass +class CommunityInsertionSnippet: + body: str + scopes: list[str] = None + + +@dataclass +class CommunityWrapperSnippet: + body: str + variable_name: str + scope: str = None + + mod = Module() mod.list("cursorless_insert_snippet_action", desc="Cursorless insert snippet action") @@ -27,6 +40,11 @@ class InsertionSnippet: desc="tag for enabling experimental snippet support", ) +mod.tag( + "cursorless_use_community_snippets", + "If active use community snippets instead of Cursorless snippets", +) + mod.list("cursorless_wrapper_snippet", desc="Cursorless wrapper snippet") mod.list( "cursorless_insertion_snippet_no_phrase", @@ -181,3 +199,21 @@ def cursorless_wrap_with_snippet( snippet_arg, target, ) + + def private_cursorless_insert_community_snippet( + name: str, destination: CursorlessDestination + ): + """Cursorless: Insert community snippet """ + snippet: CommunityInsertionSnippet = actions.user.get_insertion_snippet(name) + actions.user.cursorless_insert_snippet( + snippet.body, destination, snippet.scopes + ) + + def private_cursorless_wrap_with_community_snippet( + name: str, target: CursorlessTarget + ): + """Cursorless: Wrap target with community snippet """ + snippet: CommunityWrapperSnippet = actions.user.get_wrapper_snippet(name) + actions.user.cursorless_wrap_with_snippet( + snippet.body, target, snippet.variable_name, snippet.scope + ) diff --git a/src/snippets_community.talon b/src/snippets_community.talon new file mode 100644 index 0000000000..f94e491f83 --- /dev/null +++ b/src/snippets_community.talon @@ -0,0 +1,13 @@ +mode: command +mode: user.cursorless_spoken_form_test +tag: user.cursorless +and tag: user.cursorless_use_community_snippets +- + +# These snippets are defined in community + +{user.cursorless_insert_snippet_action} {user.snippet} : + user.private_cursorless_insert_community_snippet(snippet, cursorless_destination) + +{user.snippet_wrapper} {user.cursorless_wrap_action} : + user.private_cursorless_wrap_with_community_snippet(snippet_wrapper, cursorless_target)