Skip to content

Commit f75b771

Browse files
Support forced language mode for snippets (#2830)
Today in Cursorless using a community snippet with a forced language mode doesn't work. I also cleaned up the code somewhat uncreated a separate snippet directory. Fixes #2828 ## Release notes Forcing a language mode in community now works with snippets in Cursorless. --------- Co-authored-by: Phillip Cohen <[email protected]>
1 parent 09e4322 commit f75b771

File tree

10 files changed

+254
-221
lines changed

10 files changed

+254
-221
lines changed

cursorless-talon/src/command.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,9 @@ def make_serializable(value: Any) -> Any:
9999
**{
100100
k: v
101101
for k, v in vars(type(value)).items()
102-
if not k.startswith("_") and not isinstance(v, property)
102+
if not k.startswith("_")
103+
and not isinstance(v, property)
104+
and not isinstance(v, staticmethod)
103105
},
104106
**value.__dict__,
105107
}

cursorless-talon/src/cursorless.talon

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,12 @@ tag: user.cursorless
2424
<user.cursorless_wrapper_paired_delimiter> {user.cursorless_wrap_action} <user.cursorless_target>:
2525
user.private_cursorless_wrap_with_paired_delimiter(cursorless_wrap_action, cursorless_target, cursorless_wrapper_paired_delimiter)
2626

27+
{user.cursorless_insert_snippet_action} {user.snippet} <user.cursorless_destination>:
28+
user.private_cursorless_insert_community_snippet(snippet, cursorless_destination)
29+
30+
{user.snippet_wrapper} {user.cursorless_wrap_action} <user.cursorless_target>:
31+
user.private_cursorless_wrap_with_community_snippet(snippet_wrapper, cursorless_target)
32+
2733
{user.cursorless_show_scope_visualizer} <user.cursorless_scope_type> [{user.cursorless_visualization_type}]:
2834
user.private_cursorless_show_scope_visualizer(cursorless_scope_type, cursorless_visualization_type or "content")
2935
{user.cursorless_hide_scope_visualizer}:

cursorless-talon/src/snippets.py

Lines changed: 0 additions & 172 deletions
This file was deleted.

cursorless-talon/src/snippets.talon

Lines changed: 0 additions & 12 deletions
This file was deleted.

cursorless-talon/src/snippet_types.py renamed to cursorless-talon/src/snippets/snippet_types.py

Lines changed: 50 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,39 @@
11
from dataclasses import dataclass
22

3-
from .targets.target_types import CursorlessDestination, CursorlessTarget
3+
from ..targets.target_types import CursorlessDestination, CursorlessTarget
4+
5+
# Scope types
46

57

68
@dataclass
79
class ScopeType:
810
type: str
911

1012

13+
def to_scope_types(scope_types: str | list[str]) -> list[ScopeType]:
14+
if isinstance(scope_types, str):
15+
return [ScopeType(scope_types)]
16+
return [ScopeType(st) for st in scope_types]
17+
18+
19+
# Community types
20+
21+
22+
@dataclass
23+
class CommunityInsertionSnippet:
24+
body: str
25+
languages: list[str] | None = None
26+
scopes: list[str] | None = None
27+
28+
29+
@dataclass
30+
class CommunityWrapperSnippet:
31+
body: str
32+
variable_name: str
33+
languages: list[str] | None
34+
scope: str | None
35+
36+
1137
# Insertion snippets
1238

1339

@@ -19,6 +45,19 @@ class CustomInsertionSnippet:
1945
languages: list[str] | None
2046
substitutions: dict[str, str] | None
2147

48+
@staticmethod
49+
def create(
50+
snippet: CommunityInsertionSnippet,
51+
substitutions: dict[str, str] | None = None,
52+
):
53+
return CustomInsertionSnippet(
54+
snippet.body,
55+
to_scope_types(snippet.scopes) if snippet.scopes else None,
56+
# languages will be missing if the user has an older version of community
57+
snippet.languages if hasattr(snippet, "languages") else None,
58+
substitutions=substitutions,
59+
)
60+
2261

2362
@dataclass
2463
class ListInsertionSnippet:
@@ -45,6 +84,16 @@ class CustomWrapperSnippet:
4584
scopeType: ScopeType | None
4685
languages: list[str] | None
4786

87+
@staticmethod
88+
def create(snippet: CommunityWrapperSnippet):
89+
return CustomWrapperSnippet(
90+
snippet.body,
91+
snippet.variable_name,
92+
ScopeType(snippet.scope) if snippet.scope else None,
93+
# languages will be missing if the user has an older version of community
94+
snippet.languages if hasattr(snippet, "languages") else None,
95+
)
96+
4897

4998
@dataclass
5099
class ListWrapperSnippet:
@@ -57,21 +106,3 @@ class WrapperSnippetAction:
57106
name = "wrapWithSnippet"
58107
snippetDescription: CustomWrapperSnippet | ListWrapperSnippet
59108
target: CursorlessTarget
60-
61-
62-
# Community types
63-
64-
65-
@dataclass
66-
class CommunityInsertionSnippet:
67-
body: str
68-
languages: list[str] | None = None
69-
scopes: list[str] | None = None
70-
71-
72-
@dataclass
73-
class CommunityWrapperSnippet:
74-
body: str
75-
variable_name: str
76-
languages: list[str] | None
77-
scope: str | None

0 commit comments

Comments
 (0)