Skip to content

Migrate Python to use next-gen scope handlers for text fragment extractors #1862

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 9 commits into from
Sep 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions data/playground/python/statements.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import sys


def statements():
print(sys.path[0]) # comment 1
print("hello") # comment 2

# the below statement has additional spaces after it
val = 1 == 2
if val is True:
return

# also the below has non-empty indentation

c = range(10)
c.append(100)
x = 1
x /= 2
for i in range(10):
if i == 0:
continue
print(i)
break

age = 120
if age > 90:
print("You are too old to party, granny.")
elif age < 0:
print("You're yet to be born")
elif age >= 18:
print("You are allowed to party")
else:
print("You're too young to party")


statements()
8 changes: 8 additions & 0 deletions data/playground/python/strings.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
value = 3

a = "single quote string"
b = "double quote string"
c = """triple single quote string"""
d = """triple double quote string"""
e = r"literal string"
f = f"format string {value}"
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,6 @@ const textFragmentExtractors: Record<
"php",
phpStringTextFragmentExtractor,
),
python: constructDefaultTextFragmentExtractor("python"),
ruby: constructDefaultTextFragmentExtractor(
"ruby",
rubyStringTextFragmentExtractor,
Expand Down
2 changes: 0 additions & 2 deletions packages/cursorless-engine/src/languages/python.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ function itemNodeFinder(
const nodeMatchers: Partial<
Record<SimpleScopeTypeType, NodeMatcherAlternative>
> = {
string: "string",
collectionItem: cascadingMatcher(
matcher(
itemNodeFinder("import_from_statement", "dotted_name", true),
Expand All @@ -61,7 +60,6 @@ const nodeMatchers: Partial<
anonymousFunction: "lambda?.lambda",
functionCall: "call",
functionCallee: "call[function]",
comment: "comment",
condition: cascadingMatcher(
conditionMatcher("*[condition]"),

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
languageId: python
command:
version: 1
spokenForm: take string
action: setSelection
targets:
- type: primitive
modifier: {type: containingScope, scopeType: string}
spokenFormError: Scope type 'string'
initialState:
documentContents: |

value = """hello world"""
selections:
- anchor: {line: 1, character: 17}
active: {line: 1, character: 17}
marks: {}
finalState:
documentContents: |

value = """hello world"""
selections:
- anchor: {line: 1, character: 8}
active: {line: 1, character: 25}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
languageId: python
command:
version: 1
spokenForm: take string
action: setSelection
targets:
- type: primitive
modifier: {type: containingScope, scopeType: string}
spokenFormError: Scope type 'string'
initialState:
documentContents: |

value = r"hello world"
selections:
- anchor: {line: 1, character: 16}
active: {line: 1, character: 16}
marks: {}
finalState:
documentContents: |

value = r"hello world"
selections:
- anchor: {line: 1, character: 8}
active: {line: 1, character: 22}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
languageId: python
command:
version: 1
spokenForm: take string
action: setSelection
targets:
- type: primitive
modifier: {type: containingScope, scopeType: string}
spokenFormError: Scope type 'string'
initialState:
documentContents: |

w = "world"
value = f"hello {w}"
selections:
- anchor: {line: 2, character: 16}
active: {line: 2, character: 16}
marks: {}
finalState:
documentContents: |

w = "world"
value = f"hello {w}"
selections:
- anchor: {line: 2, character: 8}
active: {line: 2, character: 20}
7 changes: 7 additions & 0 deletions queries/python.scm
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,13 @@
(with_statement)
] @statement

(comment) @comment @textFragment

(string
_ @textFragment.start.endOf
_ @textFragment.end.startOf
) @string

[
(dictionary)
(dictionary_comprehension)
Expand Down