Skip to content

Support "name" / "key" / "condition" for switch statements #1150

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

Closed
wants to merge 2 commits into from
Closed
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
26 changes: 25 additions & 1 deletion src/languages/java.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
trailingMatcher,
matcher,
cascadingMatcher,
patternMatcher,
} from "../util/nodeMatchers";
import { childRangeSelector } from "../util/nodeSelectors";
import { patternFinder } from "../util/nodeFinders";
Expand Down Expand Up @@ -52,6 +53,21 @@ const STATEMENT_TYPES = [
"field_declaration",
];

/**
* Tree-sitter patterns to capture the pattern in a case clause, eg
*
* ```java
* case foo:
* break
* ```
*
* This would target `foo`.
*/
const caseClausePatternPatterns = [
"switch_rule.switch_label![0]",
"switch_label.parenthesized_expression![0]",
];

const nodeMatchers: Partial<
Record<SimpleScopeTypeType, NodeMatcherAlternative>
> = {
Expand Down Expand Up @@ -88,7 +104,9 @@ const nodeMatchers: Partial<
"assignment_expression[left]",
"*[name]",
"formal_parameter.identifier!",
...caseClausePatternPatterns,
],
collectionKey: caseClausePatternPatterns,
namedFunction: ["method_declaration", "constructor_declaration"],
type: trailingMatcher([
"generic_type.type_arguments.type_identifier",
Expand All @@ -112,7 +130,13 @@ const nodeMatchers: Partial<
],
["=", "+=", "-=", "*=", "/=", "%=", "&=", "|=", "^=", "<<=", ">>="],
),
condition: conditionMatcher("*[condition]"),
condition: cascadingMatcher(
conditionMatcher("while_statement[condition]"),
conditionMatcher("if_statement[condition]"),
conditionMatcher("do_statement[condition]"),
conditionMatcher("ternary_expression[condition]"),
patternMatcher(...caseClausePatternPatterns),
),
argumentOrParameter: argumentMatcher("formal_parameters", "argument_list"),
switchStatementSubject: "switch_expression[condition][0]",
};
Expand Down
7 changes: 6 additions & 1 deletion src/languages/python.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,10 @@ const nodeMatchers: Partial<
argumentSelectionExtractor(),
),
),
collectionKey: trailingMatcher(["pair[key]"], [":"]),
collectionKey: cascadingMatcher(
trailingMatcher(["pair[key]"], [":"]),
patternMatcher("case_clause[pattern]"),
),
ifStatement: "if_statement",
anonymousFunction: "lambda?.lambda",
functionCall: "call",
Expand All @@ -107,6 +110,7 @@ const nodeMatchers: Partial<

// Ternaries
patternMatcher("conditional_expression[1]"),
patternMatcher("case_clause[pattern]"),
),
type: leadingMatcher(
["function_definition[return_type]", "*[type]"],
Expand All @@ -118,6 +122,7 @@ const nodeMatchers: Partial<
"typed_parameter.identifier!",
"parameters.identifier!",
"*[name]",
"case_clause[pattern]",
],
value: cascadingMatcher(
leadingMatcher(
Expand Down
8 changes: 7 additions & 1 deletion src/languages/rust.ts
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,11 @@ const nodeMatchers: Partial<
string: ["raw_string_literal", "string_literal"],
ifStatement: ["if_expression", "if_let_expression"],
condition: cascadingMatcher(
patternMatcher("while_expression[condition]", "if_expression[condition]"),
patternMatcher(
"while_expression[condition]",
"if_expression[condition]",
"match_pattern",
),
matcher(
patternFinder("while_let_expression", "if_let_expression"),
childRangeSelector(["while", "if", "block"], [], {
Expand Down Expand Up @@ -204,6 +208,7 @@ const nodeMatchers: Partial<
),
collectionKey: cascadingMatcher(
trailingMatcher(["field_initializer[name]", "field_pattern[name]"], [":"]),
patternMatcher("match_pattern"),
),
name: cascadingMatcher(
patternMatcher(
Expand All @@ -219,6 +224,7 @@ const nodeMatchers: Partial<
"let_declaration[pattern]",
"constrained_type_parameter[left]",
"where_predicate[left]",
"match_pattern",
"field_declaration[name]",
),
trailingMatcher(["field_initializer[name]", "field_pattern[name]"], [":"]),
Expand Down
10 changes: 8 additions & 2 deletions src/languages/scala.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import {
argumentMatcher,
leadingMatcher,
conditionMatcher,
cascadingMatcher,
patternMatcher,
} from "../util/nodeMatchers";
import { NodeMatcherAlternative } from "../typings/Types";
import { SimpleScopeTypeType } from "../core/commandRunner/typings/PartialTargetDescriptor.types";
Expand All @@ -17,6 +19,7 @@ const nodeMatchers: Partial<
"object_definition[name]",
"trait_definition[name]",
],
collectionKey: "case_clause[pattern]",

ifStatement: "if_expression",

Expand All @@ -36,7 +39,7 @@ const nodeMatchers: Partial<
),

switchStatementSubject: "match_expression[value]",
name: ["*[name]", "*[pattern]"],
name: ["*[name]", "*[pattern]", "case_clause[pattern]"],
functionName: "function_definition[name]",

// *[type] does not work here because while we want most of these we don't want "compound" types,
Expand Down Expand Up @@ -66,7 +69,10 @@ const nodeMatchers: Partial<
["*[value]", "*[default_value]", "type_definition[type]"],
["="],
),
condition: conditionMatcher("*[condition]"),
condition: cascadingMatcher(
conditionMatcher("*[condition]"),
patternMatcher("case_clause[pattern]"),
),

// Scala features unsupported in Cursorless terminology
// - Pattern matching
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
languageId: java
command:
version: 4
spokenForm: clear condition
action: {name: clearAndSetSelection}
targets:
- type: primitive
modifiers:
- type: containingScope
scopeType: {type: condition}
usePrePhraseSnapshot: true
initialState:
documentContents: |-
class Aaa {
static void bbb() {
var s =
switch ("0") {
case "0" -> "zero";
case "1" -> "one";
};
}
}
selections:
- anchor: {line: 4, character: 24}
active: {line: 4, character: 24}
marks: {}
finalState:
documentContents: |-
class Aaa {
static void bbb() {
var s =
switch ("0") {
case -> "zero";
case "1" -> "one";
};
}
}
selections:
- anchor: {line: 4, character: 17}
active: {line: 4, character: 17}
fullTargets: [{type: primitive, mark: {type: cursor}, modifiers: [{type: containingScope, scopeType: {type: condition}}]}]
41 changes: 41 additions & 0 deletions src/test/suite/fixtures/recorded/languages/java/clearKey.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
languageId: java
command:
version: 4
spokenForm: clear key
action: {name: clearAndSetSelection}
targets:
- type: primitive
modifiers:
- type: containingScope
scopeType: {type: collectionKey}
usePrePhraseSnapshot: true
initialState:
documentContents: |-
class Aaa {
static void bbb() {
var s =
switch ("0") {
case "0" -> "zero";
case "1" -> "one";
};
}
}
selections:
- anchor: {line: 4, character: 24}
active: {line: 4, character: 24}
marks: {}
finalState:
documentContents: |-
class Aaa {
static void bbb() {
var s =
switch ("0") {
case -> "zero";
case "1" -> "one";
};
}
}
selections:
- anchor: {line: 4, character: 17}
active: {line: 4, character: 17}
fullTargets: [{type: primitive, mark: {type: cursor}, modifiers: [{type: containingScope, scopeType: {type: collectionKey}}]}]
41 changes: 41 additions & 0 deletions src/test/suite/fixtures/recorded/languages/java/clearName2.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
languageId: java
command:
version: 4
spokenForm: clear name
action: {name: clearAndSetSelection}
targets:
- type: primitive
modifiers:
- type: containingScope
scopeType: {type: name}
usePrePhraseSnapshot: true
initialState:
documentContents: |-
class Aaa {
static void bbb() {
var s =
switch ("0") {
case "0" -> "zero";
case "1" -> "one";
};
}
}
selections:
- anchor: {line: 4, character: 24}
active: {line: 4, character: 24}
marks: {}
finalState:
documentContents: |-
class Aaa {
static void bbb() {
var s =
switch ("0") {
case -> "zero";
case "1" -> "one";
};
}
}
selections:
- anchor: {line: 4, character: 17}
active: {line: 4, character: 17}
fullTargets: [{type: primitive, mark: {type: cursor}, modifiers: [{type: containingScope, scopeType: {type: name}}]}]
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
languageId: python
command:
spokenForm: change condition air
version: 2
targets:
- type: primitive
mark: {type: decoratedSymbol, symbolColor: default, character: a}
modifiers:
- type: containingScope
scopeType: {type: condition}
usePrePhraseSnapshot: true
action: {name: clearAndSetSelection}
initialState:
documentContents: |
match command.split():
case ["quit"]:
print("quit")
case ["go", direction] | []:
print("going", direction)
case ["drop", *objects]:
print("drop", objects)
case _:
print(f"Sorry, I couldn't understand {command!r}")
selections:
- anchor: {line: 3, character: 18}
active: {line: 3, character: 21}
marks:
default.a:
start: {line: 3, character: 4}
end: {line: 3, character: 8}
finalState:
documentContents: |
match command.split():
case ["quit"]:
print("quit")
case :
print("going", direction)
case ["drop", *objects]:
print("drop", objects)
case _:
print(f"Sorry, I couldn't understand {command!r}")
selections:
- anchor: {line: 3, character: 9}
active: {line: 3, character: 9}
thatMark:
- anchor: {line: 3, character: 9}
active: {line: 3, character: 9}
fullTargets: [{type: primitive, mark: {type: decoratedSymbol, symbolColor: default, character: a}, modifiers: [{type: containingScope, scopeType: {type: condition}}]}]
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
languageId: python
command:
spokenForm: change condition sun
version: 2
targets:
- type: primitive
mark: {type: decoratedSymbol, symbolColor: default, character: s}
modifiers:
- type: containingScope
scopeType: {type: condition}
usePrePhraseSnapshot: true
action: {name: clearAndSetSelection}
initialState:
documentContents: |
match command.split():
case ["quit"]:
print("quit")
case ["go", direction] | []:
print("going", direction)
case ["drop", *objects]:
print("drop", objects)
case _:
print(f"Sorry, I couldn't understand {command!r}")
selections:
- anchor: {line: 9, character: 0}
active: {line: 9, character: 0}
marks:
default.s:
start: {line: 3, character: 4}
end: {line: 3, character: 8}
finalState:
documentContents: |
match command.split():
case ["quit"]:
print("quit")
case :
print("going", direction)
case ["drop", *objects]:
print("drop", objects)
case _:
print(f"Sorry, I couldn't understand {command!r}")
selections:
- anchor: {line: 3, character: 9}
active: {line: 3, character: 9}
thatMark:
- anchor: {line: 3, character: 9}
active: {line: 3, character: 9}
fullTargets: [{type: primitive, mark: {type: decoratedSymbol, symbolColor: default, character: s}, modifiers: [{type: containingScope, scopeType: {type: condition}}]}]
Loading