Skip to content

Commit 55ebccb

Browse files
committed
Add pattern matchign support for python
1 parent 5bc975d commit 55ebccb

21 files changed

+1186
-8
lines changed

src/languages/java.ts

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -86,9 +86,13 @@ const nodeMatchers: Partial<
8686
"assignment_expression[left]",
8787
"*[name]",
8888
"formal_parameter.identifier!",
89-
"switch_label.parenthesized_expression!"
89+
"switch_label.parenthesized_expression![0]",
90+
"switch_rule.switch_label![0]",
91+
],
92+
collectionKey: [
93+
"switch_label.parenthesized_expression![0]",
94+
"switch_rule.switch_label![0]",
9095
],
91-
collectionKey: ["switch_label.parenthesized_expression!"],
9296
namedFunction: ["method_declaration", "constructor_declaration"],
9397
type: trailingMatcher([
9498
"generic_type.type_arguments.type_identifier",
@@ -113,11 +117,25 @@ const nodeMatchers: Partial<
113117
["=", "+=", "-=", "*=", "/=", "%=", "&=", "|=", "^=", "<<=", ">>="]
114118
),
115119
condition: cascadingMatcher(
116-
conditionMatcher("*[condition]"),
120+
conditionMatcher("while_statement[condition]"),
121+
conditionMatcher("if_statement[condition]"),
122+
conditionMatcher("do_statement[condition]"),
123+
conditionMatcher("ternary_expression[condition]"),
124+
patternMatcher("switch_label.parenthesized_expression![0]"),
125+
patternMatcher("switch_rule.switch_label![0]")
117126
),
118127
argumentOrParameter: argumentMatcher("formal_parameters", "argument_list"),
119-
subject: patternMatcher("switch_expression[condition]"),
120-
branch: ["switch_block_statement_group", "switch_rule"],
128+
subject: patternMatcher("switch_expression[condition][0]"),
129+
branch: cascadingMatcher(
130+
matcher(
131+
patternFinder("switch_block_statement_group"),
132+
childRangeSelector(["switch_label"], [])
133+
),
134+
matcher(
135+
patternFinder("switch_rule"),
136+
childRangeSelector(["switch_label"], [])
137+
)
138+
),
121139
};
122140

123141
export default createPatternMatchers(nodeMatchers);

src/languages/python.ts

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,10 @@ const nodeMatchers: Partial<
7676
statement: STATEMENT_TYPES,
7777
string: "string",
7878
collectionItem: matcher(importNodeFinder(), argumentSelectionExtractor()),
79-
collectionKey: trailingMatcher(["pair[key]"], [":"]),
79+
collectionKey: cascadingMatcher(
80+
trailingMatcher(["pair[key]"], [":"]),
81+
patternMatcher("case_clause[pattern]")
82+
),
8083
ifStatement: "if_statement",
8184
anonymousFunction: "lambda?.lambda",
8285
functionCall: "call",
@@ -86,7 +89,10 @@ const nodeMatchers: Partial<
8689
className: "class_definition[name]",
8790
namedFunction: "decorated_definition?.function_definition",
8891
functionName: "function_definition[name]",
89-
condition: conditionMatcher("*[condition]"),
92+
condition: cascadingMatcher(
93+
conditionMatcher("*[condition]"),
94+
patternMatcher("case_clause[pattern]")
95+
),
9096
type: leadingMatcher(
9197
["function_definition[return_type]", "*[type]"],
9298
[":", "->"]
@@ -97,6 +103,7 @@ const nodeMatchers: Partial<
97103
"typed_parameter.identifier!",
98104
"parameters.identifier!",
99105
"*[name]",
106+
"case_clause[pattern]",
100107
],
101108
value: cascadingMatcher(
102109
leadingMatcher(
@@ -118,12 +125,15 @@ const nodeMatchers: Partial<
118125
">>=",
119126
]
120127
),
121-
patternMatcher("return_statement.~return!")
128+
patternMatcher("return_statement.~return!"),
129+
patternMatcher("case_clause[consequence]")
122130
),
123131
argumentOrParameter: cascadingMatcher(
124132
argumentMatcher("parameters", "argument_list"),
125133
matcher(patternFinder("call.generator_expression!"), childRangeSelector())
126134
),
135+
subject: "match_statement[subject]",
136+
branch: "case_clause",
127137
};
128138

129139
export default createPatternMatchers(nodeMatchers);
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
languageId: java
2+
command:
3+
spokenForm: change branch air
4+
version: 2
5+
targets:
6+
- type: primitive
7+
mark: {type: decoratedSymbol, symbolColor: default, character: a}
8+
modifiers:
9+
- type: containingScope
10+
scopeType: {type: branch}
11+
usePrePhraseSnapshot: true
12+
action: {name: clearAndSetSelection}
13+
initialState:
14+
documentContents: |
15+
class C {
16+
static void main(String args[]) {
17+
switch (arg[0]) {
18+
case("0"):
19+
System.out.println("zero");
20+
break;
21+
case("1"):
22+
System.out.println("one");
23+
break;
24+
default:
25+
System.out.println("other");
26+
break;
27+
};
28+
29+
var s = switch (arg[0]) {
30+
case "0" -> "zero";
31+
case "1" -> "one";
32+
default -> "other";
33+
};
34+
System.out.println(s);
35+
}
36+
}
37+
selections:
38+
- anchor: {line: 22, character: 0}
39+
active: {line: 22, character: 0}
40+
marks:
41+
default.a:
42+
start: {line: 15, character: 12}
43+
end: {line: 15, character: 16}
44+
finalState:
45+
documentContents: |
46+
class C {
47+
static void main(String args[]) {
48+
switch (arg[0]) {
49+
case("0"):
50+
System.out.println("zero");
51+
break;
52+
case("1"):
53+
System.out.println("one");
54+
break;
55+
default:
56+
System.out.println("other");
57+
break;
58+
};
59+
60+
var s = switch (arg[0]) {
61+
case "0" ->
62+
case "1" -> "one";
63+
default -> "other";
64+
};
65+
System.out.println(s);
66+
}
67+
}
68+
selections:
69+
- anchor: {line: 15, character: 24}
70+
active: {line: 15, character: 24}
71+
thatMark:
72+
- anchor: {line: 15, character: 24}
73+
active: {line: 15, character: 24}
74+
fullTargets: [{type: primitive, mark: {type: decoratedSymbol, symbolColor: default, character: a}, modifiers: [{type: containingScope, scopeType: {type: branch}}]}]
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
languageId: java
2+
command:
3+
spokenForm: change branch blue each
4+
version: 2
5+
targets:
6+
- type: primitive
7+
mark: {type: decoratedSymbol, symbolColor: blue, character: e}
8+
modifiers:
9+
- type: containingScope
10+
scopeType: {type: branch}
11+
usePrePhraseSnapshot: true
12+
action: {name: clearAndSetSelection}
13+
initialState:
14+
documentContents: |
15+
class C {
16+
static void main(String args[]) {
17+
switch (arg[0]) {
18+
case("0"):
19+
System.out.println("zero");
20+
break;
21+
case("1"):
22+
System.out.println("one");
23+
break;
24+
default:
25+
System.out.println("other");
26+
break;
27+
};
28+
29+
var s = switch (arg[0]) {
30+
case "0" -> "zero";
31+
case "1" -> "one";
32+
default -> "other";
33+
};
34+
System.out.println(s);
35+
}
36+
}
37+
selections:
38+
- anchor: {line: 22, character: 0}
39+
active: {line: 22, character: 0}
40+
marks:
41+
blue.e:
42+
start: {line: 3, character: 12}
43+
end: {line: 3, character: 16}
44+
finalState:
45+
documentContents: |
46+
class C {
47+
static void main(String args[]) {
48+
switch (arg[0]) {
49+
case("0"):
50+
51+
case("1"):
52+
System.out.println("one");
53+
break;
54+
default:
55+
System.out.println("other");
56+
break;
57+
};
58+
59+
var s = switch (arg[0]) {
60+
case "0" -> "zero";
61+
case "1" -> "one";
62+
default -> "other";
63+
};
64+
System.out.println(s);
65+
}
66+
}
67+
selections:
68+
- anchor: {line: 4, character: 16}
69+
active: {line: 4, character: 16}
70+
thatMark:
71+
- anchor: {line: 4, character: 16}
72+
active: {line: 4, character: 16}
73+
fullTargets: [{type: primitive, mark: {type: decoratedSymbol, symbolColor: blue, character: e}, modifiers: [{type: containingScope, scopeType: {type: branch}}]}]
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
languageId: java
2+
command:
3+
spokenForm: change condition air
4+
version: 2
5+
targets:
6+
- type: primitive
7+
mark: {type: decoratedSymbol, symbolColor: default, character: a}
8+
modifiers:
9+
- type: containingScope
10+
scopeType: {type: condition}
11+
usePrePhraseSnapshot: true
12+
action: {name: clearAndSetSelection}
13+
initialState:
14+
documentContents: |
15+
class C {
16+
static void main(String args[]) {
17+
switch (arg[0]) {
18+
case("0"):
19+
System.out.println("zero");
20+
break;
21+
case("1"):
22+
System.out.println("one");
23+
break;
24+
default:
25+
System.out.println("other");
26+
break;
27+
};
28+
29+
var s = switch (arg[0]) {
30+
case "0" -> "zero";
31+
case "1" -> "one";
32+
default -> "other";
33+
};
34+
System.out.println(s);
35+
}
36+
}
37+
selections:
38+
- anchor: {line: 22, character: 0}
39+
active: {line: 22, character: 0}
40+
marks:
41+
default.a:
42+
start: {line: 15, character: 12}
43+
end: {line: 15, character: 16}
44+
finalState:
45+
documentContents: |
46+
class C {
47+
static void main(String args[]) {
48+
switch (arg[0]) {
49+
case("0"):
50+
System.out.println("zero");
51+
break;
52+
case("1"):
53+
System.out.println("one");
54+
break;
55+
default:
56+
System.out.println("other");
57+
break;
58+
};
59+
60+
var s = switch (arg[0]) {
61+
case -> "zero";
62+
case "1" -> "one";
63+
default -> "other";
64+
};
65+
System.out.println(s);
66+
}
67+
}
68+
selections:
69+
- anchor: {line: 15, character: 17}
70+
active: {line: 15, character: 17}
71+
thatMark:
72+
- anchor: {line: 15, character: 17}
73+
active: {line: 15, character: 17}
74+
fullTargets: [{type: primitive, mark: {type: decoratedSymbol, symbolColor: default, character: a}, modifiers: [{type: containingScope, scopeType: {type: condition}}]}]
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
languageId: java
2+
command:
3+
spokenForm: change condition blue each
4+
version: 2
5+
targets:
6+
- type: primitive
7+
mark: {type: decoratedSymbol, symbolColor: blue, character: e}
8+
modifiers:
9+
- type: containingScope
10+
scopeType: {type: condition}
11+
usePrePhraseSnapshot: true
12+
action: {name: clearAndSetSelection}
13+
initialState:
14+
documentContents: |
15+
class C {
16+
static void main(String args[]) {
17+
switch (arg[0]) {
18+
case("0"):
19+
System.out.println("zero");
20+
break;
21+
case("1"):
22+
System.out.println("one");
23+
break;
24+
default:
25+
System.out.println("other");
26+
break;
27+
};
28+
29+
var s = switch (arg[0]) {
30+
case "0" -> "zero";
31+
case "1" -> "one";
32+
default -> "other";
33+
};
34+
System.out.println(s);
35+
}
36+
}
37+
selections:
38+
- anchor: {line: 22, character: 0}
39+
active: {line: 22, character: 0}
40+
marks:
41+
blue.e:
42+
start: {line: 3, character: 12}
43+
end: {line: 3, character: 16}
44+
finalState:
45+
documentContents: |
46+
class C {
47+
static void main(String args[]) {
48+
switch (arg[0]) {
49+
case():
50+
System.out.println("zero");
51+
break;
52+
case("1"):
53+
System.out.println("one");
54+
break;
55+
default:
56+
System.out.println("other");
57+
break;
58+
};
59+
60+
var s = switch (arg[0]) {
61+
case "0" -> "zero";
62+
case "1" -> "one";
63+
default -> "other";
64+
};
65+
System.out.println(s);
66+
}
67+
}
68+
selections:
69+
- anchor: {line: 3, character: 17}
70+
active: {line: 3, character: 17}
71+
thatMark:
72+
- anchor: {line: 3, character: 17}
73+
active: {line: 3, character: 17}
74+
fullTargets: [{type: primitive, mark: {type: decoratedSymbol, symbolColor: blue, character: e}, modifiers: [{type: containingScope, scopeType: {type: condition}}]}]

0 commit comments

Comments
 (0)