Skip to content

Commit

Permalink
Get snippets actions (talonhub#1315)
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
3 people authored Apr 16, 2024
1 parent 801afdd commit eeb7a93
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 2 deletions.
10 changes: 9 additions & 1 deletion core/snippets/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
13 changes: 13 additions & 0 deletions core/snippets/snippet_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
16 changes: 15 additions & 1 deletion core/snippets/snippets.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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 <name>"""
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 <name>"""
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())
Expand Down
1 change: 1 addition & 0 deletions core/snippets/snippets_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -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),
)
Expand Down

0 comments on commit eeb7a93

Please sign in to comment.