Skip to content

Commit e816c7d

Browse files
AndreasArvidssonpokeypre-commit-ci-lite[bot]
authored andcommitted
Added insertions scope for insert snippet action (#1879)
`snip funk after air` ## Checklist - [x] I have added [tests](https://www.cursorless.org/docs/contributing/test-case-recorder/) - [x] 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) - [x] I have not broken the cheatsheet - [x] Add changelog file --------- Co-authored-by: Pokey Rule <[email protected]> Co-authored-by: pre-commit-ci-lite[bot] <117423508+pre-commit-ci-lite[bot]@users.noreply.github.com>
1 parent 6c946c3 commit e816c7d

File tree

1 file changed

+29
-14
lines changed

1 file changed

+29
-14
lines changed

src/snippets.py

Lines changed: 29 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from dataclasses import dataclass
2-
from typing import Any, Optional
2+
from typing import Any, Optional, Union
33

44
from talon import Module, actions
55

@@ -90,14 +90,20 @@ def insert_named_snippet(
9090
insert_snippet(snippet, destination)
9191

9292

93-
def insert_custom_snippet(body: str, destination: CursorlessDestination):
94-
insert_snippet(
95-
{
96-
"type": "custom",
97-
"body": body,
98-
},
99-
destination,
100-
)
93+
def insert_custom_snippet(
94+
body: str,
95+
destination: CursorlessDestination,
96+
scope_types: Optional[list[dict]] = None,
97+
):
98+
snippet = {
99+
"type": "custom",
100+
"body": body,
101+
}
102+
103+
if scope_types:
104+
snippet["scopeTypes"] = scope_types
105+
106+
insert_snippet(snippet, destination)
101107

102108

103109
@mod.action_class
@@ -127,12 +133,21 @@ def cursorless_insert_snippet_by_name(name: str):
127133
ImplicitDestination(),
128134
)
129135

130-
def cursorless_insert_snippet(body: str):
136+
def cursorless_insert_snippet(
137+
body: str,
138+
destination: Optional[CursorlessDestination] = ImplicitDestination(),
139+
scope_type: Optional[Union[str, list[str]]] = None,
140+
):
131141
"""Cursorless: Insert custom snippet <body>"""
132-
insert_custom_snippet(
133-
body,
134-
ImplicitDestination(),
135-
)
142+
if isinstance(scope_type, str):
143+
scope_type = [scope_type]
144+
145+
if scope_type is not None:
146+
scope_types = [{"type": st} for st in scope_type]
147+
else:
148+
scope_types = None
149+
150+
insert_custom_snippet(body, destination, scope_types)
136151

137152
def cursorless_wrap_with_snippet_by_name(
138153
name: str, variable_name: str, target: CursorlessTarget

0 commit comments

Comments
 (0)