Skip to content

Improve condition support for Python and Rust #1147

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 13 commits into from
Dec 22, 2022
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
10 changes: 9 additions & 1 deletion src/languages/python.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,15 @@ const nodeMatchers: Partial<
className: "class_definition[name]",
namedFunction: "decorated_definition?.function_definition",
functionName: "function_definition[name]",
condition: conditionMatcher("*[condition]"),
condition: cascadingMatcher(
conditionMatcher("*[condition]"),

// Comprehensions and match statements
leadingMatcher(["*.if_clause![0]"], ["if"]),

// Ternaries
patternMatcher("conditional_expression[1]"),
),
type: leadingMatcher(
["function_definition[return_type]", "*[type]"],
[":", "->"],
Expand Down
11 changes: 11 additions & 0 deletions src/languages/rust.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
trailingMatcher,
} from "../util/nodeMatchers";
import {
childRangeSelector,
makeNodePairSelection,
makeRangeFromPositions,
} from "../util/nodeSelectors";
Expand Down Expand Up @@ -146,6 +147,16 @@ const nodeMatchers: Partial<
),
string: ["raw_string_literal", "string_literal"],
ifStatement: ["if_expression", "if_let_expression"],
condition: cascadingMatcher(
patternMatcher("while_expression[condition]", "if_expression[condition]"),
matcher(
patternFinder("while_let_expression", "if_let_expression"),
childRangeSelector(["while", "if", "block"], [], {
includeUnnamedChildren: true,
}),
),
leadingMatcher(["*.match_pattern![condition]"], ["if"]),
),
functionCall: ["call_expression", "macro_invocation", "struct_expression"],
functionCallee: "call_expression[function]",
comment: ["line_comment", "block_comment"],
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
languageId: python
command:
spokenForm: clear condition
version: 3
targets:
- type: primitive
modifiers:
- type: containingScope
scopeType: {type: condition}
usePrePhraseSnapshot: true
action: {name: clearAndSetSelection}
initialState:
documentContents: aaa if bbb else ccc
selections:
- anchor: {line: 0, character: 16}
active: {line: 0, character: 16}
marks: {}
finalState:
documentContents: aaa if else ccc
selections:
- anchor: {line: 0, character: 7}
active: {line: 0, character: 7}
fullTargets: [{type: primitive, mark: {type: cursor}, modifiers: [{type: containingScope, scopeType: {type: condition}}]}]
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
languageId: python
command:
spokenForm: clear condition
version: 3
targets:
- type: primitive
modifiers:
- type: containingScope
scopeType: {type: condition}
usePrePhraseSnapshot: true
action: {name: clearAndSetSelection}
initialState:
documentContents: "[aaa for aaa in bbb if ccc]"
selections:
- anchor: {line: 0, character: 1}
active: {line: 0, character: 1}
marks: {}
finalState:
documentContents: "[aaa for aaa in bbb if ]"
selections:
- anchor: {line: 0, character: 23}
active: {line: 0, character: 23}
fullTargets: [{type: primitive, mark: {type: cursor}, modifiers: [{type: containingScope, scopeType: {type: condition}}]}]
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
languageId: python
command:
spokenForm: clear condition
version: 3
targets:
- type: primitive
modifiers:
- type: containingScope
scopeType: {type: condition}
usePrePhraseSnapshot: true
action: {name: clearAndSetSelection}
initialState:
documentContents: (aaa for aaa in bbb if ccc)
selections:
- anchor: {line: 0, character: 1}
active: {line: 0, character: 1}
marks: {}
finalState:
documentContents: (aaa for aaa in bbb if )
selections:
- anchor: {line: 0, character: 23}
active: {line: 0, character: 23}
fullTargets: [{type: primitive, mark: {type: cursor}, modifiers: [{type: containingScope, scopeType: {type: condition}}]}]
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
languageId: python
command:
spokenForm: clear condition
version: 3
targets:
- type: primitive
modifiers:
- type: containingScope
scopeType: {type: condition}
usePrePhraseSnapshot: false
action: {name: clearAndSetSelection}
initialState:
documentContents: "{aaa for aaa in bbb if ccc}"
selections:
- anchor: {line: 0, character: 1}
active: {line: 0, character: 1}
marks: {}
finalState:
documentContents: "{aaa for aaa in bbb if }"
selections:
- anchor: {line: 0, character: 23}
active: {line: 0, character: 23}
fullTargets: [{type: primitive, mark: {type: cursor}, modifiers: [{type: containingScope, scopeType: {type: condition}}]}]
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
languageId: python
command:
spokenForm: clear condition
version: 3
targets:
- type: primitive
modifiers:
- type: containingScope
scopeType: {type: condition}
usePrePhraseSnapshot: false
action: {name: clearAndSetSelection}
initialState:
documentContents: "{aaa: aaa for aaa in bbb if ccc}"
selections:
- anchor: {line: 0, character: 1}
active: {line: 0, character: 1}
marks: {}
finalState:
documentContents: "{aaa: aaa for aaa in bbb if }"
selections:
- anchor: {line: 0, character: 28}
active: {line: 0, character: 28}
fullTargets: [{type: primitive, mark: {type: cursor}, modifiers: [{type: containingScope, scopeType: {type: condition}}]}]
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
languageId: python
command:
spokenForm: clear condition
version: 3
targets:
- type: primitive
modifiers:
- type: containingScope
scopeType: {type: condition}
usePrePhraseSnapshot: true
action: {name: clearAndSetSelection}
initialState:
documentContents: |-
match 0:
case a if a > 1:
pass
selections:
- anchor: {line: 2, character: 8}
active: {line: 2, character: 8}
marks: {}
finalState:
documentContents: |-
match 0:
case a if :
pass
selections:
- anchor: {line: 1, character: 14}
active: {line: 1, character: 14}
fullTargets: [{type: primitive, mark: {type: cursor}, modifiers: [{type: containingScope, scopeType: {type: condition}}]}]
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
languageId: python
command:
spokenForm: ditch condition
version: 3
targets:
- type: primitive
modifiers:
- type: containingScope
scopeType: {type: condition}
usePrePhraseSnapshot: true
action: {name: remove}
initialState:
documentContents: "[aaa for aaa in bbb if ccc]"
selections:
- anchor: {line: 0, character: 1}
active: {line: 0, character: 1}
marks: {}
finalState:
documentContents: "[aaa for aaa in bbb ]"
selections:
- anchor: {line: 0, character: 1}
active: {line: 0, character: 1}
fullTargets: [{type: primitive, mark: {type: cursor}, modifiers: [{type: containingScope, scopeType: {type: condition}}]}]
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
languageId: python
command:
spokenForm: ditch condition
version: 3
targets:
- type: primitive
modifiers:
- type: containingScope
scopeType: {type: condition}
usePrePhraseSnapshot: true
action: {name: remove}
initialState:
documentContents: |-
match 0:
case a if a > 1:
pass
selections:
- anchor: {line: 2, character: 8}
active: {line: 2, character: 8}
marks: {}
finalState:
documentContents: |-
match 0:
case a :
pass
selections:
- anchor: {line: 2, character: 8}
active: {line: 2, character: 8}
fullTargets: [{type: primitive, mark: {type: cursor}, modifiers: [{type: containingScope, scopeType: {type: condition}}]}]
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
languageId: rust
command:
spokenForm: chuck condition
version: 2
targets:
- type: primitive
modifiers:
- type: containingScope
scopeType: {type: condition}
usePrePhraseSnapshot: true
action: {name: remove}
initialState:
documentContents: |
match user {
User { first_name: "John" } => {},
User { first_name } if first_name.starts_with("P") => {}
}
selections:
- anchor: {line: 2, character: 27}
active: {line: 2, character: 54}
marks: {}
finalState:
documentContents: |
match user {
User { first_name: "John" } => {},
User { first_name } => {}
}
selections:
- anchor: {line: 2, character: 23}
active: {line: 2, character: 23}
fullTargets: [{type: primitive, mark: {type: cursor}, modifiers: [{type: containingScope, scopeType: {type: ifStatement}}]}]
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
languageId: rust
command:
spokenForm: clear condition
version: 3
targets:
- type: primitive
modifiers:
- type: containingScope
scopeType: {type: condition}
usePrePhraseSnapshot: true
action: {name: clearAndSetSelection}
initialState:
documentContents: |-
match user {
User { first_name: "John" } => {},
User { first_name } if first_name.starts_with("P") => {}
}
selections:
- anchor: {line: 2, character: 57}
active: {line: 2, character: 57}
marks: {}
finalState:
documentContents: |-
match user {
User { first_name: "John" } => {},
User { first_name } if => {}
}
selections:
- anchor: {line: 2, character: 25}
active: {line: 2, character: 25}
fullTargets: [{type: primitive, mark: {type: cursor}, modifiers: [{type: containingScope, scopeType: {type: condition}}]}]
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
languageId: rust
command:
version: 3
spokenForm: clear condition
action: {name: clearAndSetSelection}
targets:
- type: primitive
modifiers:
- type: containingScope
scopeType: {type: condition}
usePrePhraseSnapshot: true
initialState:
documentContents: |-
if n < 0 {
print!("{} is negative", n);
} else if n > 0 {
print!("{} is positive", n);
}
selections:
- anchor: {line: 1, character: 2}
active: {line: 1, character: 2}
marks: {}
finalState:
documentContents: |-
if {
print!("{} is negative", n);
} else if n > 0 {
print!("{} is positive", n);
}
selections:
- anchor: {line: 0, character: 3}
active: {line: 0, character: 3}
fullTargets: [{type: primitive, mark: {type: cursor}, modifiers: [{type: containingScope, scopeType: {type: condition}}]}]
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
languageId: rust
command:
version: 3
spokenForm: clear condition
action: {name: clearAndSetSelection}
targets:
- type: primitive
modifiers:
- type: containingScope
scopeType: {type: condition}
usePrePhraseSnapshot: false
initialState:
documentContents: |-
if n < 0 {
print!("{} is negative", n);
} else if n > 0 {
print!("{} is positive", n);
}
selections:
- anchor: {line: 3, character: 2}
active: {line: 3, character: 2}
marks: {}
finalState:
documentContents: |-
if n < 0 {
print!("{} is negative", n);
} else if {
print!("{} is positive", n);
}
selections:
- anchor: {line: 2, character: 10}
active: {line: 2, character: 10}
fullTargets: [{type: primitive, mark: {type: cursor}, modifiers: [{type: containingScope, scopeType: {type: condition}}]}]
Loading