Skip to content

Commit eeb7a93

Browse files
AndreasArvidssonpre-commit-ci[bot]pokey
authored
Get snippets actions (talonhub#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: talonhub#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>
1 parent 801afdd commit eeb7a93

File tree

4 files changed

+38
-2
lines changed

4 files changed

+38
-2
lines changed

core/snippets/README.md

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,15 @@ Custom format to represent snippets.
77
- Custom file ending `.snippet`.
88
- Supports syntax highlighting in VSCode via an [extension](https://marketplace.visualstudio.com/items?itemName=AndreasArvidsson.andreas-talon)
99
- Supports auto-formatting in VSCode via an [extension](https://marketplace.visualstudio.com/items?itemName=AndreasArvidsson.andreas-talon)
10-
- 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.
10+
- 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:
11+
12+
```talon
13+
tag(): user.cursorless_use_community_snippets
14+
```
15+
16+
Note that this line will also disable any Cursorless snippets defined in your
17+
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.
18+
1119
- Support for phrase formatters.
1220

1321
## Format

core/snippets/snippet_types.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,3 +30,16 @@ def get_variable_strict(self, name: str):
3030
if variable is None:
3131
raise ValueError(f"Snippet '{self.name}' has no variable '{name}'")
3232
return variable
33+
34+
35+
@dataclass
36+
class InsertionSnippet:
37+
body: str
38+
scopes: list[str] = None
39+
40+
41+
@dataclass
42+
class WrapperSnippet:
43+
body: str
44+
variable_name: str
45+
scope: str = None

core/snippets/snippets.py

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
from talon import Context, Module, actions, app, fs, settings
66

77
from ..modes.language_modes import language_ids
8-
from .snippet_types import Snippet
8+
from .snippet_types import InsertionSnippet, Snippet, WrapperSnippet
99
from .snippets_parser import create_snippets_from_file
1010

1111
SNIPPETS_DIR = Path(__file__).parent / "snippets"
@@ -64,6 +64,20 @@ def get_snippet(name: str) -> Snippet:
6464

6565
return snippets_map[name]
6666

67+
def get_insertion_snippet(name: str) -> InsertionSnippet:
68+
"""Get insertion snippet named <name>"""
69+
snippet: Snippet = actions.user.get_snippet(name)
70+
return InsertionSnippet(snippet.body, snippet.insertion_scopes)
71+
72+
def get_wrapper_snippet(name: str) -> WrapperSnippet:
73+
"""Get wrapper snippet named <name>"""
74+
index = name.rindex(".")
75+
snippet_name = name[:index]
76+
variable_name = name[index + 1]
77+
snippet: Snippet = actions.user.get_snippet(snippet_name)
78+
variable = snippet.get_variable_strict(variable_name)
79+
return WrapperSnippet(snippet.body, variable.name, variable.wrapper_scope)
80+
6781

6882
def update_snippets():
6983
language_to_snippets = group_by_language(get_snippets())

core/snippets/snippets_parser.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ def create_snippet(
5454
name=document.name or default_context.name,
5555
languages=document.languages or default_context.languages,
5656
phrases=document.phrases or default_context.phrases,
57+
insertion_scopes=document.insertionScopes or default_context.insertionScopes,
5758
variables=combine_variables(default_context.variables, document.variables),
5859
body=normalize_snippet_body_tabs(document.body),
5960
)

0 commit comments

Comments
 (0)