From eeb7a93cb689aba53193ec70fde7d7ffbcc77a8b Mon Sep 17 00:00:00 2001 From: Andreas Arvidsson Date: Tue, 16 Apr 2024 16:07:17 +0200 Subject: [PATCH] Get snippets actions (#1315) Added actions to get insertion and wrapper snippet content(body, scope, etc). This will be used by example cursorless to leverage the community snippets Follow up from: https://github.com/talonhub/community/pull/1305 --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Pokey Rule <755842+pokey@users.noreply.github.com> --- core/snippets/README.md | 10 +++++++++- core/snippets/snippet_types.py | 13 +++++++++++++ core/snippets/snippets.py | 16 +++++++++++++++- core/snippets/snippets_parser.py | 1 + 4 files changed, 38 insertions(+), 2 deletions(-) diff --git a/core/snippets/README.md b/core/snippets/README.md index 4a74157d48..3d13fed283 100644 --- a/core/snippets/README.md +++ b/core/snippets/README.md @@ -7,7 +7,15 @@ Custom format to represent snippets. - Custom file ending `.snippet`. - Supports syntax highlighting in VSCode via an [extension](https://marketplace.visualstudio.com/items?itemName=AndreasArvidsson.andreas-talon) - Supports auto-formatting in VSCode via an [extension](https://marketplace.visualstudio.com/items?itemName=AndreasArvidsson.andreas-talon) -- Support for insertion and wrapper snippets. Note that while the snippet file syntax here supports wrapper snippets, we still need to add the proper voice commands; stay tuned. +- Support for insertion and wrapper snippets. Note that while the snippet file syntax here supports wrapper snippets, you will need to install [Cursorless](https://www.cursorless.org/) for wrapper snippets to work. You'll also need to add the following line to your `settings.talon` file: + + ```talon + tag(): user.cursorless_use_community_snippets + ``` + + Note that this line will also disable any Cursorless snippets defined in your + Cursorless customization CSVs. You will need to migrate your Cursorless snippets to the new community snippet format described here. If you'd be interested in a tool to help with this migration, please leave a comment on [cursorless-dev/cursorless#2149](https://github.com/cursorless-dev/cursorless/issues/2149), ideally with a link to your custom snippets for us to look at. + - Support for phrase formatters. ## Format diff --git a/core/snippets/snippet_types.py b/core/snippets/snippet_types.py index 7718881ad9..1c6c83db5e 100644 --- a/core/snippets/snippet_types.py +++ b/core/snippets/snippet_types.py @@ -30,3 +30,16 @@ def get_variable_strict(self, name: str): if variable is None: raise ValueError(f"Snippet '{self.name}' has no variable '{name}'") return variable + + +@dataclass +class InsertionSnippet: + body: str + scopes: list[str] = None + + +@dataclass +class WrapperSnippet: + body: str + variable_name: str + scope: str = None diff --git a/core/snippets/snippets.py b/core/snippets/snippets.py index e41f0af6dc..e04f10ebfa 100644 --- a/core/snippets/snippets.py +++ b/core/snippets/snippets.py @@ -5,7 +5,7 @@ from talon import Context, Module, actions, app, fs, settings from ..modes.language_modes import language_ids -from .snippet_types import Snippet +from .snippet_types import InsertionSnippet, Snippet, WrapperSnippet from .snippets_parser import create_snippets_from_file SNIPPETS_DIR = Path(__file__).parent / "snippets" @@ -64,6 +64,20 @@ def get_snippet(name: str) -> Snippet: return snippets_map[name] + def get_insertion_snippet(name: str) -> InsertionSnippet: + """Get insertion snippet named """ + snippet: Snippet = actions.user.get_snippet(name) + return InsertionSnippet(snippet.body, snippet.insertion_scopes) + + def get_wrapper_snippet(name: str) -> WrapperSnippet: + """Get wrapper snippet named """ + index = name.rindex(".") + snippet_name = name[:index] + variable_name = name[index + 1] + snippet: Snippet = actions.user.get_snippet(snippet_name) + variable = snippet.get_variable_strict(variable_name) + return WrapperSnippet(snippet.body, variable.name, variable.wrapper_scope) + def update_snippets(): language_to_snippets = group_by_language(get_snippets()) diff --git a/core/snippets/snippets_parser.py b/core/snippets/snippets_parser.py index bd264429fd..1d836b4a86 100644 --- a/core/snippets/snippets_parser.py +++ b/core/snippets/snippets_parser.py @@ -54,6 +54,7 @@ def create_snippet( name=document.name or default_context.name, languages=document.languages or default_context.languages, phrases=document.phrases or default_context.phrases, + insertion_scopes=document.insertionScopes or default_context.insertionScopes, variables=combine_variables(default_context.variables, document.variables), body=normalize_snippet_body_tabs(document.body), )