Skip to content

Commit 3dc53fc

Browse files
Added fallback language to list snippets (#2984)
1 parent d7ec6ce commit 3dc53fc

File tree

12 files changed

+377
-111
lines changed

12 files changed

+377
-111
lines changed

cursorless-talon/src/check_community_repo.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
]
99

1010
required_actions = [
11+
"code.language",
1112
"user.homophones_get",
1213
"user.insert_snippet_by_name",
1314
"user.reformat_text",

cursorless-talon/src/snippets/snippet_types.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ def create(
6262
@dataclass
6363
class ListInsertionSnippet:
6464
type = "list"
65+
fallbackLanguage: str | None
6566
substitutions: dict[str, str] | None
6667
snippets: list[CustomInsertionSnippet]
6768

@@ -98,6 +99,7 @@ def create(snippet: CommunityWrapperSnippet):
9899
@dataclass
99100
class ListWrapperSnippet:
100101
type = "list"
102+
fallbackLanguage: str | None
101103
snippets: list[CustomWrapperSnippet]
102104

103105

cursorless-talon/src/snippets/snippets.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ class UserActions:
100100
def insert_snippet_by_name(
101101
name: str, # pyright: ignore [reportGeneralTypeIssues]
102102
# Don't add optional: we need to match the type in community
103-
substitutions: dict[str, str] = None,
103+
substitutions: dict[str, str] = None, # type: ignore
104104
):
105105
action = InsertSnippetAction(
106106
get_list_insertion_snippet(name, substitutions),

cursorless-talon/src/snippets/snippets_get.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ def get_list_insertion_snippet(
3030
raise
3131

3232
return ListInsertionSnippet(
33+
get_fallback_language(),
3334
substitutions,
3435
[CustomInsertionSnippet.create(s) for s in snippets],
3536
)
@@ -49,5 +50,13 @@ def get_list_wrapper_snippet(name: str) -> ListWrapperSnippet | CustomWrapperSni
4950
raise
5051

5152
return ListWrapperSnippet(
53+
get_fallback_language(),
5254
[CustomWrapperSnippet.create(s) for s in snippets],
5355
)
56+
57+
58+
def get_fallback_language():
59+
language = actions.code.language()
60+
if language and isinstance(language, str):
61+
return language
62+
return None
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
languageId: typescript
2+
command:
3+
version: 7
4+
spokenForm: if wrap air
5+
action:
6+
name: wrapWithSnippet
7+
snippetDescription:
8+
type: list
9+
snippets:
10+
- type: custom
11+
body: "if ($1) {\n\t$0\n}"
12+
variableName: "0"
13+
languages: [c, cpp, csharp, java, javascript, typescript, javascriptreact, typescriptreact]
14+
target:
15+
type: primitive
16+
mark: {type: decoratedSymbol, symbolColor: default, character: a}
17+
usePrePhraseSnapshot: true
18+
spokenFormError: list wrap with snippet
19+
initialState:
20+
documentContents: aaa
21+
selections:
22+
- anchor: {line: 0, character: 0}
23+
active: {line: 0, character: 0}
24+
marks:
25+
default.a:
26+
start: {line: 0, character: 0}
27+
end: {line: 0, character: 3}
28+
finalState:
29+
documentContents: |-
30+
if () {
31+
aaa
32+
}
33+
selections:
34+
- anchor: {line: 0, character: 4}
35+
active: {line: 0, character: 4}
36+
thatMark:
37+
- type: UntypedTarget
38+
contentRange:
39+
start: {line: 0, character: 0}
40+
end: {line: 2, character: 1}
41+
isReversed: false
42+
hasExplicitRange: true
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
languageId: plaintext
2+
command:
3+
version: 7
4+
spokenForm: if wrap air
5+
action:
6+
name: wrapWithSnippet
7+
snippetDescription:
8+
type: list
9+
fallbackLanguage: typescript
10+
snippets:
11+
- type: custom
12+
body: "if ($1) {\n\t$0\n}"
13+
variableName: "0"
14+
languages: [c, cpp, csharp, java, javascript, typescript, javascriptreact, typescriptreact]
15+
target:
16+
type: primitive
17+
mark: {type: decoratedSymbol, symbolColor: default, character: a}
18+
usePrePhraseSnapshot: true
19+
spokenFormError: list wrap with snippet
20+
initialState:
21+
documentContents: aaa
22+
selections:
23+
- anchor: {line: 0, character: 0}
24+
active: {line: 0, character: 0}
25+
marks:
26+
default.a:
27+
start: {line: 0, character: 0}
28+
end: {line: 0, character: 3}
29+
finalState:
30+
documentContents: |-
31+
if () {
32+
aaa
33+
}
34+
selections:
35+
- anchor: {line: 0, character: 4}
36+
active: {line: 0, character: 4}
37+
thatMark:
38+
- type: UntypedTarget
39+
contentRange:
40+
start: {line: 0, character: 0}
41+
end: {line: 2, character: 1}
42+
isReversed: false
43+
hasExplicitRange: true
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
languageId: plaintext
2+
command:
3+
version: 7
4+
spokenForm: snip if
5+
action:
6+
name: insertSnippet
7+
snippetDescription:
8+
type: list
9+
fallbackLanguage: typescript
10+
snippets:
11+
- type: custom
12+
body: "if ($1) {\n\t$0\n}"
13+
scopeTypes:
14+
- {type: statement}
15+
languages: [c, cpp, csharp, java, javascript, typescript, javascriptreact, typescriptreact]
16+
- type: custom
17+
body: "if $1:\n\t$0"
18+
scopeTypes:
19+
- {type: statement}
20+
languages: [python]
21+
- type: custom
22+
body: "if $1 then\n\t$0\nend"
23+
scopeTypes:
24+
- {type: statement}
25+
languages: [lua]
26+
destination: {type: implicit}
27+
usePrePhraseSnapshot: false
28+
spokenFormError: list insertion snippet
29+
initialState:
30+
documentContents: ""
31+
selections:
32+
- anchor: {line: 0, character: 0}
33+
active: {line: 0, character: 0}
34+
marks: {}
35+
finalState:
36+
documentContents: |-
37+
if () {
38+
39+
}
40+
selections:
41+
- anchor: {line: 0, character: 4}
42+
active: {line: 0, character: 4}
43+
thatMark:
44+
- type: UntypedTarget
45+
contentRange:
46+
start: {line: 0, character: 0}
47+
end: {line: 2, character: 1}
48+
isReversed: false
49+
hasExplicitRange: true

packages/common/src/types/command/ActionDescriptor.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -159,8 +159,9 @@ export interface CustomInsertSnippetArg {
159159
substitutions?: Record<string, string>;
160160
}
161161

162-
interface ListInsertSnippetArg {
162+
export interface ListInsertSnippetArg {
163163
type: "list";
164+
fallbackLanguage?: string;
164165
substitutions?: Record<string, string>;
165166
snippets: CustomInsertSnippetArg[];
166167
}
@@ -190,8 +191,9 @@ export interface CustomWrapWithSnippetArg {
190191
languages?: string[];
191192
}
192193

193-
interface ListWrapWithSnippetArg {
194+
export interface ListWrapWithSnippetArg {
194195
type: "list";
196+
fallbackLanguage?: string;
195197
snippets: CustomWrapWithSnippetArg[];
196198
}
197199

packages/common/src/types/snippet.types.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import type { SimpleScopeTypeType } from "./command/PartialTargetDescriptor.types";
22

3+
// FIXME: Legacy snippets
4+
35
export interface SnippetScope {
46
/**
57
* VSCode language ids where this snippet definition should be active

packages/cursorless-engine/src/core/compareSnippetDefinitions.ts

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

0 commit comments

Comments
 (0)