Skip to content

Commit 711b19e

Browse files
committed
rewrite
1 parent 2b3aead commit 711b19e

File tree

3 files changed

+54
-106
lines changed

3 files changed

+54
-106
lines changed

cursorless-talon/src/snippets/snippet_types.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ class CustomInsertionSnippet:
4848
@staticmethod
4949
def create(
5050
snippet: CommunityInsertionSnippet,
51-
substitutions: dict[str, str] | None,
51+
substitutions: dict[str, str] | None = None,
5252
):
5353
return CustomInsertionSnippet(
5454
snippet.body,

cursorless-talon/src/snippets/snippets.py

Lines changed: 29 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,10 @@
1818
to_scope_types,
1919
)
2020
from .snippets_get import (
21-
get_custom_insertion_snippet,
22-
get_custom_wrapper_snippet,
2321
get_insertion_snippet,
2422
get_wrapper_snippet,
23+
get_list_insertion_snippet,
24+
get_list_wrapper_snippet,
2525
)
2626

2727
mod = Module()
@@ -34,85 +34,43 @@
3434
mod.list("cursorless_insert_snippet_action", desc="Cursorless insert snippet action")
3535

3636

37-
def insert_snippet(
38-
snippet: CustomInsertionSnippet | ListInsertionSnippet,
39-
destination: CursorlessDestination,
40-
):
41-
actions.user.private_cursorless_command_and_wait(
42-
InsertSnippetAction(snippet, destination),
43-
)
44-
45-
46-
def wrap_with_snippet(
47-
snippet: CustomWrapperSnippet | ListWrapperSnippet,
48-
target: CursorlessTarget,
49-
):
50-
actions.user.private_cursorless_command_and_wait(
51-
WrapperSnippetAction(snippet, target),
52-
)
53-
54-
55-
def insert_community_snippet(
56-
name: str,
57-
substitutions: dict[str, str] | None,
58-
destination: CursorlessDestination,
59-
use_list: bool,
60-
):
61-
snippet = (
62-
get_insertion_snippet(name, substitutions)
63-
if use_list
64-
else get_custom_insertion_snippet(name, substitutions)
65-
)
66-
insert_snippet(snippet, destination)
67-
68-
69-
def wrap_with_community_snippet(
70-
name: str,
71-
target: CursorlessTarget,
72-
use_list: bool,
73-
):
74-
snippet = (
75-
get_wrapper_snippet(name) if use_list else get_custom_wrapper_snippet(name)
76-
)
77-
wrap_with_snippet(snippet, target)
78-
79-
8037
@ctx.action_class("user")
8138
class UserActions:
82-
# These actions use list snippets since no language mode is forced
39+
# Since we don't have a forced language mode, these actions send all the snippets.
40+
# (note that this is the default mode of action, as most of the time the user will not
41+
# have a forced language mode)
8342

8443
def insert_snippet_by_name(
8544
name: str, # pyright: ignore [reportGeneralTypeIssues]
8645
# Don't add optional; We need to match the type in community
8746
substitutions: dict[str, str] = None,
8847
):
89-
insert_community_snippet(
90-
name,
91-
substitutions,
48+
action = InsertSnippetAction(
49+
get_list_insertion_snippet(name, substitutions),
9250
ImplicitDestination(),
93-
use_list=True,
9451
)
52+
actions.user.private_cursorless_command_and_wait(action)
53+
9554

9655
def private_cursorless_insert_community_snippet(
9756
name: str, # pyright: ignore [reportGeneralTypeIssues]
9857
destination: CursorlessDestination,
9958
):
100-
insert_community_snippet(
101-
name,
102-
substitutions=None,
103-
destination=destination,
104-
use_list=True,
59+
action = InsertSnippetAction(
60+
get_list_insertion_snippet(name),
61+
destination,
10562
)
63+
actions.user.private_cursorless_command_and_wait(action)
10664

10765
def private_cursorless_wrap_with_community_snippet(
10866
name: str, # pyright: ignore [reportGeneralTypeIssues]
10967
target: CursorlessTarget,
11068
):
111-
wrap_with_community_snippet(
112-
name,
69+
action = WrapperSnippetAction(
70+
get_list_wrapper_snippet(name),
11371
target,
114-
use_list=True,
11572
)
73+
actions.user.private_cursorless_command_and_wait(action)
11674

11775

11876
@mod.action_class
@@ -129,7 +87,8 @@ def cursorless_insert_snippet(
12987
languages=None,
13088
substitutions=None,
13189
)
132-
insert_snippet(snippet, destination)
90+
action = InsertSnippetAction(snippet, destination)
91+
actions.user.private_cursorless_command_and_wait(action)
13392

13493
def cursorless_wrap_with_snippet(
13594
body: str, # pyright: ignore [reportGeneralTypeIssues]
@@ -144,7 +103,8 @@ def cursorless_wrap_with_snippet(
144103
ScopeType(scope) if scope else None,
145104
languages=None,
146105
)
147-
wrap_with_snippet(snippet, target)
106+
action = WrapperSnippetAction(snippet, target)
107+
actions.user.private_cursorless_command_and_wait(action)
148108

149109
# These actions use a single custom snippets since a language mode is forced
150110

@@ -153,20 +113,21 @@ def private_cursorless_insert_community_snippet(
153113
destination: CursorlessDestination,
154114
):
155115
"""Cursorless: Insert community snippet <name>"""
156-
insert_community_snippet(
157-
name,
158-
substitutions=None,
159-
destination=destination,
160-
use_list=False,
116+
action = InsertSnippetAction(
117+
CustomInsertionSnippet.create(
118+
actions.user.get_insertion_snippet(name),
119+
),
120+
destination,
161121
)
122+
actions.user.private_cursorless_command_and_wait(action)
162123

163124
def private_cursorless_wrap_with_community_snippet(
164125
name: str, # pyright: ignore [reportGeneralTypeIssues]
165126
target: CursorlessTarget,
166127
):
167128
"""Cursorless: Wrap target with community snippet <name>"""
168-
wrap_with_community_snippet(
169-
name,
129+
action = WrapperSnippetAction(
130+
get_wrapper_snippet(name),
170131
target,
171-
use_list=False,
172132
)
133+
actions.user.private_cursorless_command_and_wait(action)
Lines changed: 24 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
from talon import actions
22

33
from .snippet_types import (
4-
CommunityInsertionSnippet,
5-
CommunityWrapperSnippet,
64
CustomInsertionSnippet,
75
CustomWrapperSnippet,
86
ListInsertionSnippet,
@@ -12,53 +10,42 @@
1210

1311
def get_insertion_snippet(
1412
name: str,
15-
substitutions: dict[str, str] | None,
13+
substitutions: dict[str, str] | None = None
14+
) -> CustomInsertionSnippet:
15+
return CustomInsertionSnippet.create(
16+
actions.user.get_insertion_snippet(name),
17+
substitutions,
18+
)
19+
20+
def get_list_insertion_snippet(
21+
name: str,
22+
substitutions: dict[str, str] | None = None,
1623
) -> ListInsertionSnippet | CustomInsertionSnippet:
1724
try:
18-
return get_list_insertion_snippet(name, substitutions)
19-
except Exception as ex:
20-
if isinstance(ex, KeyError):
21-
return get_custom_insertion_snippet(name, substitutions)
25+
snippets = actions.user.get_list_insertion_snippet(name)
26+
except Exception as e:
27+
# Raised if the user has an older version of community
28+
if isinstance(e, KeyError):
29+
return get_insertion_snippet(name, substitutions)
2230
raise
2331

24-
25-
def get_list_insertion_snippet(
26-
name: str,
27-
substitutions: dict[str, str] | None,
28-
) -> ListInsertionSnippet:
29-
snippets: list[CommunityInsertionSnippet] = actions.user.get_insertion_snippets(
30-
name
31-
)
3232
return ListInsertionSnippet(
3333
substitutions,
34-
[CustomInsertionSnippet.create(s, substitutions=None) for s in snippets],
34+
[CustomInsertionSnippet.create(s) for s in snippets],
3535
)
3636

37+
def get_wrapper_snippet(name: str) -> CustomWrapperSnippet:
38+
return CustomWrapperSnippet.create(actions.user.get_wrapper_snippet(name))
3739

38-
def get_custom_insertion_snippet(
39-
name: str,
40-
substitutions: dict[str, str] | None,
41-
) -> CustomInsertionSnippet:
42-
snippet: CommunityInsertionSnippet = actions.user.get_insertion_snippet(name)
43-
return CustomInsertionSnippet.create(snippet, substitutions)
44-
45-
46-
def get_wrapper_snippet(name: str) -> ListWrapperSnippet | CustomWrapperSnippet:
40+
def get_list_wrapper_snippet(name: str) -> ListWrapperSnippet | CustomWrapperSnippet:
4741
try:
48-
return get_list_wrapper_snippet(name)
49-
except Exception as ex:
50-
if isinstance(ex, KeyError):
51-
return get_custom_wrapper_snippet(name)
42+
snippets = actions.user.get_wrapper_snippets(name)
43+
except Exception as e:
44+
# Raised if the user has an older version of community
45+
if isinstance(e, KeyError):
46+
return get_wrapper_snippet(name)
5247
raise
5348

54-
55-
def get_list_wrapper_snippet(name: str) -> ListWrapperSnippet:
56-
snippets: list[CommunityWrapperSnippet] = actions.user.get_wrapper_snippets(name)
5749
return ListWrapperSnippet(
5850
[CustomWrapperSnippet.create(s) for s in snippets],
5951
)
60-
61-
62-
def get_custom_wrapper_snippet(name: str) -> CustomWrapperSnippet:
63-
snippet: CommunityWrapperSnippet = actions.user.get_wrapper_snippet(name)
64-
return CustomWrapperSnippet.create(snippet)

0 commit comments

Comments
 (0)