Skip to content

Commit 0b50c4a

Browse files
saidelikeCedric Halbronnpokeypre-commit-ci-lite[bot]
authored
Migrate Python to use next-gen scope handlers for text fragment extractors (#1862)
- Depends on #1863 ## Checklist - [x] 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 - [x] merge #1858 first as seems required - [x] @pokey to fix a bug in the "processSurroundingPairCore" function atm that makes it not working atm (see commit message). It will be handled in a different commit before we can take this PR into account - [x] record a test for the fix by @pokey by saying "cursorless record" and using "change string" This PR also includes a few additional unit tests for Python strings. --------- Co-authored-by: Cedric Halbronn <[email protected]> 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 162bcf7 commit 0b50c4a

File tree

8 files changed

+125
-3
lines changed

8 files changed

+125
-3
lines changed

data/playground/python/statements.py

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import sys
2+
3+
4+
def statements():
5+
print(sys.path[0]) # comment 1
6+
print("hello") # comment 2
7+
8+
# the below statement has additional spaces after it
9+
val = 1 == 2
10+
if val is True:
11+
return
12+
13+
# also the below has non-empty indentation
14+
15+
c = range(10)
16+
c.append(100)
17+
x = 1
18+
x /= 2
19+
for i in range(10):
20+
if i == 0:
21+
continue
22+
print(i)
23+
break
24+
25+
age = 120
26+
if age > 90:
27+
print("You are too old to party, granny.")
28+
elif age < 0:
29+
print("You're yet to be born")
30+
elif age >= 18:
31+
print("You are allowed to party")
32+
else:
33+
print("You're too young to party")
34+
35+
36+
statements()

data/playground/python/strings.py

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
value = 3
2+
3+
a = "single quote string"
4+
b = "double quote string"
5+
c = """triple single quote string"""
6+
d = """triple double quote string"""
7+
e = r"literal string"
8+
f = f"format string {value}"

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

-1
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,6 @@ const textFragmentExtractors: Record<
159159
"php",
160160
phpStringTextFragmentExtractor,
161161
),
162-
python: constructDefaultTextFragmentExtractor("python"),
163162
ruby: constructDefaultTextFragmentExtractor(
164163
"ruby",
165164
rubyStringTextFragmentExtractor,

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

-2
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,6 @@ function itemNodeFinder(
4545
const nodeMatchers: Partial<
4646
Record<SimpleScopeTypeType, NodeMatcherAlternative>
4747
> = {
48-
string: "string",
4948
collectionItem: cascadingMatcher(
5049
matcher(
5150
itemNodeFinder("import_from_statement", "dotted_name", true),
@@ -61,7 +60,6 @@ const nodeMatchers: Partial<
6160
anonymousFunction: "lambda?.lambda",
6261
functionCall: "call",
6362
functionCallee: "call[function]",
64-
comment: "comment",
6563
condition: cascadingMatcher(
6664
conditionMatcher("*[condition]"),
6765

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
languageId: python
2+
command:
3+
version: 1
4+
spokenForm: take string
5+
action: setSelection
6+
targets:
7+
- type: primitive
8+
modifier: {type: containingScope, scopeType: string}
9+
spokenFormError: Scope type 'string'
10+
initialState:
11+
documentContents: |
12+
13+
value = """hello world"""
14+
selections:
15+
- anchor: {line: 1, character: 17}
16+
active: {line: 1, character: 17}
17+
marks: {}
18+
finalState:
19+
documentContents: |
20+
21+
value = """hello world"""
22+
selections:
23+
- anchor: {line: 1, character: 8}
24+
active: {line: 1, character: 25}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
languageId: python
2+
command:
3+
version: 1
4+
spokenForm: take string
5+
action: setSelection
6+
targets:
7+
- type: primitive
8+
modifier: {type: containingScope, scopeType: string}
9+
spokenFormError: Scope type 'string'
10+
initialState:
11+
documentContents: |
12+
13+
value = r"hello world"
14+
selections:
15+
- anchor: {line: 1, character: 16}
16+
active: {line: 1, character: 16}
17+
marks: {}
18+
finalState:
19+
documentContents: |
20+
21+
value = r"hello world"
22+
selections:
23+
- anchor: {line: 1, character: 8}
24+
active: {line: 1, character: 22}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
languageId: python
2+
command:
3+
version: 1
4+
spokenForm: take string
5+
action: setSelection
6+
targets:
7+
- type: primitive
8+
modifier: {type: containingScope, scopeType: string}
9+
spokenFormError: Scope type 'string'
10+
initialState:
11+
documentContents: |
12+
13+
w = "world"
14+
value = f"hello {w}"
15+
selections:
16+
- anchor: {line: 2, character: 16}
17+
active: {line: 2, character: 16}
18+
marks: {}
19+
finalState:
20+
documentContents: |
21+
22+
w = "world"
23+
value = f"hello {w}"
24+
selections:
25+
- anchor: {line: 2, character: 8}
26+
active: {line: 2, character: 20}

queries/python.scm

+7
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,13 @@
2727
(with_statement)
2828
] @statement
2929

30+
(comment) @comment @textFragment
31+
32+
(string
33+
_ @textFragment.start.endOf
34+
_ @textFragment.end.startOf
35+
) @string
36+
3037
[
3138
(dictionary)
3239
(dictionary_comprehension)

0 commit comments

Comments
 (0)