Skip to content

Commit af14956

Browse files
authored
Migrate Python name and type scopes (#1922)
## Checklist - [ ] I have added [tests](https://www.cursorless.org/docs/contributing/test-case-recorder/) - [ ] 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) - [ ] I have not broken the cheatsheet
1 parent 5347894 commit af14956

File tree

10 files changed

+298
-16
lines changed

10 files changed

+298
-16
lines changed

packages/cursorless-engine/src/languages/python.ts

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -69,17 +69,6 @@ const nodeMatchers: Partial<
6969
// Ternaries
7070
patternMatcher("conditional_expression[1]"),
7171
),
72-
type: leadingMatcher(
73-
["function_definition[return_type]", "*[type]"],
74-
[":", "->"],
75-
),
76-
name: [
77-
"assignment[left]",
78-
"augmented_assignment[left]",
79-
"typed_parameter.identifier!",
80-
"parameters.identifier!",
81-
"*[name]",
82-
],
8372
argumentOrParameter: cascadingMatcher(
8473
argumentMatcher("parameters", "argument_list"),
8574
matcher(patternFinder("call.generator_expression!"), childRangeSelector()),
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
languageId: python
2+
command:
3+
version: 6
4+
spokenForm: change every name
5+
action:
6+
name: clearAndSetSelection
7+
target:
8+
type: primitive
9+
modifiers:
10+
- type: everyScope
11+
scopeType: {type: name}
12+
usePrePhraseSnapshot: true
13+
initialState:
14+
documentContents: |-
15+
aaa = bbb
16+
aaa: str = bbb
17+
aaa: str
18+
selections:
19+
- anchor: {line: 2, character: 8}
20+
active: {line: 2, character: 8}
21+
marks: {}
22+
finalState:
23+
documentContents: |2-
24+
= bbb
25+
: str = bbb
26+
: str
27+
selections:
28+
- anchor: {line: 0, character: 0}
29+
active: {line: 0, character: 0}
30+
- anchor: {line: 1, character: 0}
31+
active: {line: 1, character: 0}
32+
- anchor: {line: 2, character: 0}
33+
active: {line: 2, character: 0}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
languageId: python
2+
command:
3+
version: 6
4+
spokenForm: change every name
5+
action:
6+
name: clearAndSetSelection
7+
target:
8+
type: primitive
9+
modifiers:
10+
- type: everyScope
11+
scopeType: {type: name}
12+
usePrePhraseSnapshot: true
13+
initialState:
14+
documentContents: |-
15+
def aaa():
16+
bbb = ccc
17+
ddd = eee
18+
selections:
19+
- anchor: {line: 2, character: 13}
20+
active: {line: 2, character: 13}
21+
marks: {}
22+
finalState:
23+
documentContents: |-
24+
def aaa():
25+
= ccc
26+
= eee
27+
selections:
28+
- anchor: {line: 1, character: 4}
29+
active: {line: 1, character: 4}
30+
- anchor: {line: 2, character: 4}
31+
active: {line: 2, character: 4}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
languageId: python
2+
command:
3+
version: 6
4+
spokenForm: change every name
5+
action:
6+
name: clearAndSetSelection
7+
target:
8+
type: primitive
9+
modifiers:
10+
- type: everyScope
11+
scopeType: {type: name}
12+
usePrePhraseSnapshot: true
13+
initialState:
14+
documentContents: |-
15+
def aaa(bbb, ccc: str):
16+
pass
17+
selections:
18+
- anchor: {line: 0, character: 21}
19+
active: {line: 0, character: 21}
20+
marks: {}
21+
finalState:
22+
documentContents: |-
23+
def aaa(, : str):
24+
pass
25+
selections:
26+
- anchor: {line: 0, character: 8}
27+
active: {line: 0, character: 8}
28+
- anchor: {line: 0, character: 10}
29+
active: {line: 0, character: 10}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
languageId: python
2+
command:
3+
version: 6
4+
spokenForm: change every type
5+
action:
6+
name: clearAndSetSelection
7+
target:
8+
type: primitive
9+
modifiers:
10+
- type: everyScope
11+
scopeType: {type: type}
12+
usePrePhraseSnapshot: true
13+
initialState:
14+
documentContents: |-
15+
def aaa(bbb, ccc: str, ddd: str):
16+
pass
17+
selections:
18+
- anchor: {line: 0, character: 8}
19+
active: {line: 0, character: 8}
20+
marks: {}
21+
finalState:
22+
documentContents: |-
23+
def aaa(bbb, ccc: , ddd: ):
24+
pass
25+
selections:
26+
- anchor: {line: 0, character: 18}
27+
active: {line: 0, character: 18}
28+
- anchor: {line: 0, character: 25}
29+
active: {line: 0, character: 25}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
languageId: python
2+
command:
3+
version: 6
4+
spokenForm: change every type
5+
action:
6+
name: clearAndSetSelection
7+
target:
8+
type: primitive
9+
modifiers:
10+
- type: everyScope
11+
scopeType: {type: type}
12+
usePrePhraseSnapshot: true
13+
initialState:
14+
documentContents: |-
15+
def aaa():
16+
bbb: str
17+
ccc: str = "hello"
18+
selections:
19+
- anchor: {line: 2, character: 22}
20+
active: {line: 2, character: 22}
21+
marks: {}
22+
finalState:
23+
documentContents: |-
24+
def aaa():
25+
bbb:
26+
ccc: = "hello"
27+
selections:
28+
- anchor: {line: 1, character: 9}
29+
active: {line: 1, character: 9}
30+
- anchor: {line: 2, character: 9}
31+
active: {line: 2, character: 9}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
languageId: python
2+
command:
3+
version: 6
4+
spokenForm: change type
5+
action:
6+
name: clearAndSetSelection
7+
target:
8+
type: primitive
9+
modifiers:
10+
- type: containingScope
11+
scopeType: {type: type}
12+
usePrePhraseSnapshot: true
13+
initialState:
14+
documentContents: |-
15+
def aaa(bbb, ccc: int):
16+
pass
17+
selections:
18+
- anchor: {line: 0, character: 13}
19+
active: {line: 0, character: 13}
20+
marks: {}
21+
finalState:
22+
documentContents: |-
23+
def aaa(bbb, ccc: ):
24+
pass
25+
selections:
26+
- anchor: {line: 0, character: 18}
27+
active: {line: 0, character: 18}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
languageId: python
2+
command:
3+
version: 6
4+
spokenForm: change type
5+
action:
6+
name: clearAndSetSelection
7+
target:
8+
type: primitive
9+
modifiers:
10+
- type: containingScope
11+
scopeType: {type: type}
12+
usePrePhraseSnapshot: true
13+
initialState:
14+
documentContents: |-
15+
def aaa(bbb, ccc: int):
16+
pass
17+
selections:
18+
- anchor: {line: 0, character: 8}
19+
active: {line: 0, character: 8}
20+
marks: {}
21+
thrownError: {name: NoContainingScopeError}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
languageId: python
2+
command:
3+
version: 6
4+
spokenForm: chuck name
5+
action:
6+
name: remove
7+
target:
8+
type: primitive
9+
modifiers:
10+
- type: containingScope
11+
scopeType: {type: name}
12+
usePrePhraseSnapshot: true
13+
initialState:
14+
documentContents: |-
15+
aaa = bbb
16+
aaa: str = bbb
17+
selections:
18+
- anchor: {line: 0, character: 9}
19+
active: {line: 0, character: 9}
20+
- anchor: {line: 1, character: 9}
21+
active: {line: 1, character: 14}
22+
marks: {}
23+
finalState:
24+
documentContents: |-
25+
bbb
26+
bbb
27+
selections:
28+
- anchor: {line: 0, character: 3}
29+
active: {line: 0, character: 3}
30+
- anchor: {line: 1, character: 0}
31+
active: {line: 1, character: 3}

queries/python.scm

Lines changed: 66 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,14 +38,74 @@
3838
right: (_) @value @_.leading.end.startOf
3939
) @_.domain
4040

41+
;; value:
4142
;;!! a /= 25
4243
;;! ^^
4344
;;! xxxxxx
4445
;;! -------
46+
;; name:
47+
;;!! a /= 25
48+
;;! ^
49+
;;! xxxxx
50+
;;! -------
4551
(augmented_assignment
52+
left: (_) @name @name.trailing.start.endOf @value.leading.start.endOf
53+
right: (_) @value @value.leading.end.startOf @name.trailing.end.startOf
54+
) @_.domain
55+
56+
;;!! a = 25
57+
;;! ^
58+
;;! xxxx
59+
;;! ------
60+
;;!! a: int = 25
61+
;;! ^
62+
;;! xxxxxxxxx
63+
;;! -----------
64+
(assignment
65+
left: (_) @name @name.trailing.start.endOf
66+
right: (_)? @name.trailing.end.startOf
67+
) @_.domain
68+
69+
(_
70+
name: (_) @name
71+
) @_.domain
72+
73+
;;!! def aaa(bbb):
74+
;;! ^^^
75+
(parameters
76+
(identifier) @name
77+
)
78+
79+
;;!! def aaa(bbb: str):
80+
;;! ^^^
81+
;;! --------
82+
(typed_parameter
83+
.
84+
(_) @name
85+
) @_.domain
86+
87+
;; Matches any node at field `type` of its parent, with leading delimiter until
88+
;; previous named node. For example:
89+
;;!! aaa: str = "bbb";
90+
;;! ^^^
91+
;;! -----------------
92+
;;! xxxxx
93+
(_
4694
(_) @_.leading.start.endOf
4795
.
48-
right: (_) @value @_.leading.end.startOf
96+
type: (_) @type @_.leading.end.startOf
97+
) @_.domain
98+
99+
;;!! def aaa() -> str:
100+
;;! ^^^
101+
;;! xxxxxxx
102+
;;! [-----------------
103+
;;!! pass
104+
;;! --------]
105+
(function_definition
106+
(_) @_.leading.start.endOf
107+
.
108+
return_type: (_) @type @_.leading.end.startOf
49109
) @_.domain
50110

51111
;;!! d = {"a": 1234}
@@ -127,7 +187,7 @@
127187
) @class @className.domain
128188

129189
(module) @className.iteration @class.iteration
130-
(module) @statement.iteration
190+
(module) @statement.iteration @value.iteration @name.iteration
131191
(module) @namedFunction.iteration @functionName.iteration
132192
(class_definition) @namedFunction.iteration @functionName.iteration
133193

@@ -138,7 +198,8 @@
138198
;;! *****
139199
;;!! c = 2
140200
;;! *****>
141-
(block) @statement.iteration @value.iteration
201+
(block) @statement.iteration @value.iteration @name.iteration
202+
(block) @type.iteration
142203

143204
;;!! {"a": 1, "b": 2, "c": 3}
144205
;;! **********************
@@ -150,6 +211,6 @@
150211
;;!! def func(a=0, b=1):
151212
;;! ********
152213
(parameters
153-
"(" @value.iteration.start.endOf
154-
")" @value.iteration.end.startOf
214+
"(" @value.iteration.start.endOf @name.iteration.start.endOf @type.iteration.start.endOf
215+
")" @value.iteration.end.startOf @name.iteration.end.startOf @type.iteration.end.startOf
155216
)

0 commit comments

Comments
 (0)