Skip to content

Pattern matching support #831

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 9 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
2 changes: 2 additions & 0 deletions cursorless-talon/src/modifiers/scopes.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@
"selector": "selector",
"state": "statement",
"string": "string",
"subject": "subject",
"branch": "branch",
"type": "type",
"value": "value",
"condition": "condition",
Expand Down
3 changes: 3 additions & 0 deletions schemas/cursorless-snippets.json
Original file line number Diff line number Diff line change
Expand Up @@ -73,11 +73,13 @@
"argumentOrParameter",
"anonymousFunction",
"attribute",
"branch",
"class",
"className",
"collectionItem",
"collectionKey",
"comment",
"condition",
"functionCall",
"functionName",
"ifStatement",
Expand All @@ -88,6 +90,7 @@
"regularExpression",
"statement",
"string",
"subject",
"type",
"value",
"condition",
Expand Down
27 changes: 26 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 @@ -88,6 +89,12 @@ const nodeMatchers: Partial<
"assignment_expression[left]",
"*[name]",
"formal_parameter.identifier!",
"switch_label.parenthesized_expression![0]",
"switch_rule.switch_label![0]",
],
collectionKey: [
"switch_label.parenthesized_expression![0]",
"switch_rule.switch_label![0]",
],
namedFunction: ["method_declaration", "constructor_declaration"],
type: trailingMatcher([
Expand All @@ -112,8 +119,26 @@ 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("switch_label.parenthesized_expression![0]"),
patternMatcher("switch_rule.switch_label![0]"),
),
argumentOrParameter: argumentMatcher("formal_parameters", "argument_list"),
subject: "switch_expression[condition][0]",
branch: cascadingMatcher(
matcher(
patternFinder("switch_block_statement_group"),
childRangeSelector(["switch_label"], []),
),
matcher(
patternFinder("switch_rule"),
childRangeSelector(["switch_label"], []),
),
),
};

export default createPatternMatchers(nodeMatchers);
14 changes: 12 additions & 2 deletions 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 @@ -99,7 +102,10 @@ const nodeMatchers: Partial<
className: "class_definition[name]",
namedFunction: "decorated_definition?.function_definition",
functionName: "function_definition[name]",
condition: conditionMatcher("*[condition]"),
condition: cascadingMatcher(
conditionMatcher("*[condition]"),
patternMatcher("case_clause[pattern]"),
),
type: leadingMatcher(
["function_definition[return_type]", "*[type]"],
[":", "->"],
Expand All @@ -110,6 +116,7 @@ const nodeMatchers: Partial<
"typed_parameter.identifier!",
"parameters.identifier!",
"*[name]",
"case_clause[pattern]",
],
value: cascadingMatcher(
leadingMatcher(
Expand All @@ -132,11 +139,14 @@ const nodeMatchers: Partial<
],
),
patternMatcher("return_statement.~return!"),
patternMatcher("case_clause[consequence]"),
),
argumentOrParameter: cascadingMatcher(
argumentMatcher("parameters", "argument_list"),
matcher(patternFinder("call.generator_expression!"), childRangeSelector()),
),
subject: "match_statement[subject]",
branch: "case_clause",
};

export default createPatternMatchers(nodeMatchers);
10 changes: 9 additions & 1 deletion src/languages/rust.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,10 @@ const nodeMatchers: Partial<
1,
),
string: ["raw_string_literal", "string_literal"],
ifStatement: ["if_expression", "if_let_expression"],
ifStatement: cascadingMatcher(
patternMatcher("if_expression", "if_let_expression"),
leadingMatcher(["match_pattern[condition]"], ["if"]),
),
functionCall: ["call_expression", "macro_invocation", "struct_expression"],
functionCallee: "call_expression[function]",
comment: ["line_comment", "block_comment"],
Expand Down Expand Up @@ -188,7 +191,9 @@ const nodeMatchers: Partial<
),
collectionKey: cascadingMatcher(
trailingMatcher(["field_initializer[name]", "field_pattern[name]"], [":"]),
patternMatcher("match_pattern"),
),
condition: ["match_pattern"],
name: cascadingMatcher(
patternMatcher(
"let_declaration.identifier!",
Expand All @@ -202,6 +207,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 All @@ -218,6 +224,8 @@ const nodeMatchers: Partial<
matcher(returnValueFinder),
),
attribute: trailingMatcher(["mutable_specifier", "attribute_item"]),
subject: "match_expression[value]",
branch: ["match_arm", "if_expression[consequence]", "else_clause.block!"],
};

export default createPatternMatchers(nodeMatchers);
22 changes: 16 additions & 6 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 "../typings/targetDescriptor.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 @@ -35,7 +38,7 @@ const nodeMatchers: Partial<
"bindings",
),

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 All @@ -61,12 +64,19 @@ const nodeMatchers: Partial<
],
[":"],
),
value: leadingMatcher(
["*[value]", "*[default_value]", "type_definition[type]"],
["="],
value: cascadingMatcher(
patternMatcher("case_clause[body]"),
leadingMatcher(
["*[value]", "*[default_value]", "type_definition[type]"],
["="],
),
),
condition: conditionMatcher("*[condition]"),

condition: cascadingMatcher(
conditionMatcher("*[condition]"),
patternMatcher("case_clause[pattern]"),
),
subject: "match_expression[value]",
branch: "case_clause",
// Scala features unsupported in Cursorless terminology
// - Pattern matching

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
languageId: java
command:
spokenForm: change branch air
version: 2
targets:
- type: primitive
mark: {type: decoratedSymbol, symbolColor: default, character: a}
modifiers:
- type: containingScope
scopeType: {type: branch}
usePrePhraseSnapshot: true
action: {name: clearAndSetSelection}
initialState:
documentContents: |
class C {
static void main(String args[]) {
switch (arg[0]) {
case("0"):
System.out.println("zero");
break;
case("1"):
System.out.println("one");
break;
default:
System.out.println("other");
break;
};

var s = switch (arg[0]) {
case "0" -> "zero";
case "1" -> "one";
default -> "other";
};
System.out.println(s);
}
}
selections:
- anchor: {line: 22, character: 0}
active: {line: 22, character: 0}
marks:
default.a:
start: {line: 15, character: 12}
end: {line: 15, character: 16}
finalState:
documentContents: |
class C {
static void main(String args[]) {
switch (arg[0]) {
case("0"):
System.out.println("zero");
break;
case("1"):
System.out.println("one");
break;
default:
System.out.println("other");
break;
};

var s = switch (arg[0]) {
case "0" ->
case "1" -> "one";
default -> "other";
};
System.out.println(s);
}
}
selections:
- anchor: {line: 15, character: 24}
active: {line: 15, character: 24}
thatMark:
- anchor: {line: 15, character: 24}
active: {line: 15, character: 24}
fullTargets: [{type: primitive, mark: {type: decoratedSymbol, symbolColor: default, character: a}, modifiers: [{type: containingScope, scopeType: {type: branch}}]}]
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
languageId: java
command:
spokenForm: change branch blue each
version: 2
targets:
- type: primitive
mark: {type: decoratedSymbol, symbolColor: blue, character: e}
modifiers:
- type: containingScope
scopeType: {type: branch}
usePrePhraseSnapshot: true
action: {name: clearAndSetSelection}
initialState:
documentContents: |
class C {
static void main(String args[]) {
switch (arg[0]) {
case("0"):
System.out.println("zero");
break;
case("1"):
System.out.println("one");
break;
default:
System.out.println("other");
break;
};

var s = switch (arg[0]) {
case "0" -> "zero";
case "1" -> "one";
default -> "other";
};
System.out.println(s);
}
}
selections:
- anchor: {line: 22, character: 0}
active: {line: 22, character: 0}
marks:
blue.e:
start: {line: 3, character: 12}
end: {line: 3, character: 16}
finalState:
documentContents: |
class C {
static void main(String args[]) {
switch (arg[0]) {
case("0"):

case("1"):
System.out.println("one");
break;
default:
System.out.println("other");
break;
};

var s = switch (arg[0]) {
case "0" -> "zero";
case "1" -> "one";
default -> "other";
};
System.out.println(s);
}
}
selections:
- anchor: {line: 4, character: 16}
active: {line: 4, character: 16}
thatMark:
- anchor: {line: 4, character: 16}
active: {line: 4, character: 16}
fullTargets: [{type: primitive, mark: {type: decoratedSymbol, symbolColor: blue, character: e}, modifiers: [{type: containingScope, scopeType: {type: branch}}]}]
Loading