Skip to content

Commit 289fd08

Browse files
committed
Add condition for case statements
1 parent 90619bc commit 289fd08

22 files changed

+987
-5
lines changed

src/languages/java.ts

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import {
66
trailingMatcher,
77
matcher,
88
cascadingMatcher,
9+
patternMatcher,
910
} from "../util/nodeMatchers";
1011
import { childRangeSelector } from "../util/nodeSelectors";
1112
import { patternFinder } from "../util/nodeFinders";
@@ -88,6 +89,12 @@ const nodeMatchers: Partial<
8889
"assignment_expression[left]",
8990
"*[name]",
9091
"formal_parameter.identifier!",
92+
"switch_label.parenthesized_expression![0]",
93+
"switch_rule.switch_label![0]",
94+
],
95+
collectionKey: [
96+
"switch_label.parenthesized_expression![0]",
97+
"switch_rule.switch_label![0]",
9198
],
9299
namedFunction: ["method_declaration", "constructor_declaration"],
93100
type: trailingMatcher([
@@ -112,7 +119,14 @@ const nodeMatchers: Partial<
112119
],
113120
["=", "+=", "-=", "*=", "/=", "%=", "&=", "|=", "^=", "<<=", ">>="],
114121
),
115-
condition: conditionMatcher("*[condition]"),
122+
condition: cascadingMatcher(
123+
conditionMatcher("while_statement[condition]"),
124+
conditionMatcher("if_statement[condition]"),
125+
conditionMatcher("do_statement[condition]"),
126+
conditionMatcher("ternary_expression[condition]"),
127+
patternMatcher("switch_label.parenthesized_expression![0]"),
128+
patternMatcher("switch_rule.switch_label![0]"),
129+
),
116130
argumentOrParameter: argumentMatcher("formal_parameters", "argument_list"),
117131
branch: cascadingMatcher(
118132
matcher(

src/languages/python.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,10 @@ const nodeMatchers: Partial<
8989
argumentSelectionExtractor(),
9090
),
9191
),
92-
collectionKey: trailingMatcher(["pair[key]"], [":"]),
92+
collectionKey: cascadingMatcher(
93+
trailingMatcher(["pair[key]"], [":"]),
94+
patternMatcher("case_clause[pattern]"),
95+
),
9396
ifStatement: "if_statement",
9497
anonymousFunction: "lambda?.lambda",
9598
functionCall: "call",
@@ -99,7 +102,10 @@ const nodeMatchers: Partial<
99102
className: "class_definition[name]",
100103
namedFunction: "decorated_definition?.function_definition",
101104
functionName: "function_definition[name]",
102-
condition: conditionMatcher("*[condition]"),
105+
condition: cascadingMatcher(
106+
conditionMatcher("*[condition]"),
107+
patternMatcher("case_clause[pattern]"),
108+
),
103109
type: leadingMatcher(
104110
["function_definition[return_type]", "*[type]"],
105111
[":", "->"],
@@ -110,6 +116,7 @@ const nodeMatchers: Partial<
110116
"typed_parameter.identifier!",
111117
"parameters.identifier!",
112118
"*[name]",
119+
"case_clause[pattern]",
113120
],
114121
value: cascadingMatcher(
115122
leadingMatcher(

src/languages/rust.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,9 @@ const nodeMatchers: Partial<
188188
),
189189
collectionKey: cascadingMatcher(
190190
trailingMatcher(["field_initializer[name]", "field_pattern[name]"], [":"]),
191+
patternMatcher("match_pattern"),
191192
),
193+
condition: ["match_pattern"],
192194
name: cascadingMatcher(
193195
patternMatcher(
194196
"let_declaration.identifier!",
@@ -202,6 +204,7 @@ const nodeMatchers: Partial<
202204
"let_declaration[pattern]",
203205
"constrained_type_parameter[left]",
204206
"where_predicate[left]",
207+
"match_pattern",
205208
"field_declaration[name]",
206209
),
207210
trailingMatcher(["field_initializer[name]", "field_pattern[name]"], [":"]),

src/languages/scala.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ import {
33
argumentMatcher,
44
leadingMatcher,
55
conditionMatcher,
6+
cascadingMatcher,
7+
patternMatcher,
68
} from "../util/nodeMatchers";
79
import { NodeMatcherAlternative } from "../typings/Types";
810
import { SimpleScopeTypeType } from "../typings/targetDescriptor.types";
@@ -17,6 +19,7 @@ const nodeMatchers: Partial<
1719
"object_definition[name]",
1820
"trait_definition[name]",
1921
],
22+
collectionKey: "case_clause[pattern]",
2023

2124
ifStatement: "if_expression",
2225

@@ -36,7 +39,7 @@ const nodeMatchers: Partial<
3639
),
3740
branch: "case_clause",
3841

39-
name: ["*[name]", "*[pattern]"],
42+
name: ["*[name]", "*[pattern]", "case_clause[pattern]"],
4043
functionName: "function_definition[name]",
4144

4245
// *[type] does not work here because while we want most of these we don't want "compound" types,
@@ -66,7 +69,10 @@ const nodeMatchers: Partial<
6669
["*[value]", "*[default_value]", "type_definition[type]"],
6770
["="],
6871
),
69-
condition: conditionMatcher("*[condition]"),
72+
condition: cascadingMatcher(
73+
conditionMatcher("*[condition]"),
74+
patternMatcher("case_clause[pattern]"),
75+
),
7076

7177
// Scala features unsupported in Cursorless terminology
7278
// - Pattern matching
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}}]}]
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 key 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: collectionKey}
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: collectionKey}}]}]

0 commit comments

Comments
 (0)