Skip to content

Commit 5bc975d

Browse files
committed
Add pattern matchign support for scala
1 parent 944c2e5 commit 5bc975d

File tree

8 files changed

+290
-6
lines changed

8 files changed

+290
-6
lines changed

src/languages/scala.ts

Lines changed: 16 additions & 6 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

@@ -35,7 +38,7 @@ const nodeMatchers: Partial<
3538
"bindings"
3639
),
3740

38-
name: ["*[name]", "*[pattern]"],
41+
name: ["*[name]", "*[pattern]", "case_clause[pattern]"],
3942
functionName: "function_definition[name]",
4043

4144
// *[type] does not work here because while we want most of these we don't want "compound" types,
@@ -61,12 +64,19 @@ const nodeMatchers: Partial<
6164
],
6265
[":"]
6366
),
64-
value: leadingMatcher(
65-
["*[value]", "*[default_value]", "type_definition[type]"],
66-
["="]
67+
value: cascadingMatcher(
68+
patternMatcher("case_clause[body]"),
69+
leadingMatcher(
70+
["*[value]", "*[default_value]", "type_definition[type]"],
71+
["="]
72+
)
6773
),
68-
condition: conditionMatcher("*[condition]"),
69-
74+
condition: cascadingMatcher(
75+
conditionMatcher("*[condition]"),
76+
patternMatcher("case_clause[pattern]")
77+
),
78+
subject: "match_expression[value]",
79+
branch: "case_clause",
7080
// Scala features unsupported in Cursorless terminology
7181
// - Pattern matching
7282

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
languageId: scala
2+
command:
3+
spokenForm: change branch
4+
version: 2
5+
targets:
6+
- type: primitive
7+
modifiers:
8+
- type: containingScope
9+
scopeType: {type: branch}
10+
usePrePhraseSnapshot: true
11+
action: {name: clearAndSetSelection}
12+
initialState:
13+
documentContents: |
14+
def matchTest(x: Int): String = x match {
15+
case 0 => "zero"
16+
case 1 => "one"
17+
case 2 => "two"
18+
case _ => "other"
19+
}
20+
selections:
21+
- anchor: {line: 1, character: 13}
22+
active: {line: 1, character: 17}
23+
marks: {}
24+
finalState:
25+
documentContents: |
26+
def matchTest(x: Int): String = x match {
27+
case 1 => "one"
28+
case 2 => "two"
29+
case _ => "other"
30+
}
31+
selections:
32+
- anchor: {line: 1, character: 2}
33+
active: {line: 1, character: 2}
34+
thatMark:
35+
- anchor: {line: 1, character: 2}
36+
active: {line: 1, character: 2}
37+
fullTargets: [{type: primitive, mark: {type: cursor}, modifiers: [{type: containingScope, scopeType: {type: branch}}]}]
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
languageId: scala
2+
command:
3+
spokenForm: change condition
4+
version: 2
5+
targets:
6+
- type: primitive
7+
modifiers:
8+
- type: containingScope
9+
scopeType: {type: condition}
10+
usePrePhraseSnapshot: true
11+
action: {name: clearAndSetSelection}
12+
initialState:
13+
documentContents: |
14+
def matchTest(x: Int): String = x match {
15+
case 0 => "zero"
16+
case 1 => "one"
17+
case 2 => "two"
18+
case _ => "other"
19+
}
20+
selections:
21+
- anchor: {line: 1, character: 13}
22+
active: {line: 1, character: 17}
23+
marks: {}
24+
finalState:
25+
documentContents: |
26+
def matchTest(x: Int): String = x match {
27+
case => "zero"
28+
case 1 => "one"
29+
case 2 => "two"
30+
case _ => "other"
31+
}
32+
selections:
33+
- anchor: {line: 1, character: 7}
34+
active: {line: 1, character: 7}
35+
thatMark:
36+
- anchor: {line: 1, character: 7}
37+
active: {line: 1, character: 7}
38+
fullTargets: [{type: primitive, mark: {type: cursor}, modifiers: [{type: containingScope, scopeType: {type: condition}}]}]
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
languageId: scala
2+
command:
3+
spokenForm: change key
4+
version: 2
5+
targets:
6+
- type: primitive
7+
modifiers:
8+
- type: containingScope
9+
scopeType: {type: collectionKey}
10+
usePrePhraseSnapshot: true
11+
action: {name: clearAndSetSelection}
12+
initialState:
13+
documentContents: |
14+
def matchTest(x: Int): String = x match {
15+
case 0 => "zero"
16+
case 1 => "one"
17+
case 2 => "two"
18+
case _ => "other"
19+
}
20+
selections:
21+
- anchor: {line: 1, character: 13}
22+
active: {line: 1, character: 17}
23+
marks: {}
24+
finalState:
25+
documentContents: |
26+
def matchTest(x: Int): String = x match {
27+
case => "zero"
28+
case 1 => "one"
29+
case 2 => "two"
30+
case _ => "other"
31+
}
32+
selections:
33+
- anchor: {line: 1, character: 7}
34+
active: {line: 1, character: 7}
35+
thatMark:
36+
- anchor: {line: 1, character: 7}
37+
active: {line: 1, character: 7}
38+
fullTargets: [{type: primitive, mark: {type: cursor}, modifiers: [{type: containingScope, scopeType: {type: collectionKey}}]}]
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
languageId: scala
2+
command:
3+
spokenForm: change name
4+
version: 2
5+
targets:
6+
- type: primitive
7+
modifiers:
8+
- type: containingScope
9+
scopeType: {type: name}
10+
usePrePhraseSnapshot: true
11+
action: {name: clearAndSetSelection}
12+
initialState:
13+
documentContents: |
14+
def matchTest(x: Int): String = x match {
15+
case 0 => "zero"
16+
case 1 => "one"
17+
case 2 => "two"
18+
case _ => "other"
19+
}
20+
selections:
21+
- anchor: {line: 1, character: 13}
22+
active: {line: 1, character: 17}
23+
marks: {}
24+
finalState:
25+
documentContents: |
26+
def matchTest(x: Int): String = x match {
27+
case => "zero"
28+
case 1 => "one"
29+
case 2 => "two"
30+
case _ => "other"
31+
}
32+
selections:
33+
- anchor: {line: 1, character: 7}
34+
active: {line: 1, character: 7}
35+
thatMark:
36+
- anchor: {line: 1, character: 7}
37+
active: {line: 1, character: 7}
38+
fullTargets: [{type: primitive, mark: {type: cursor}, modifiers: [{type: containingScope, scopeType: {type: name}}]}]
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
languageId: scala
2+
command:
3+
spokenForm: change subject trap
4+
version: 2
5+
targets:
6+
- type: primitive
7+
mark: {type: decoratedSymbol, symbolColor: default, character: t}
8+
modifiers:
9+
- type: containingScope
10+
scopeType: {type: subject}
11+
usePrePhraseSnapshot: true
12+
action: {name: clearAndSetSelection}
13+
initialState:
14+
documentContents: |
15+
def matchTest(x: Int): String = x match {
16+
case 0 => "zero"
17+
case 1 => "one"
18+
case 2 => "two"
19+
case _ => "other"
20+
}
21+
selections:
22+
- anchor: {line: 6, character: 0}
23+
active: {line: 6, character: 0}
24+
marks:
25+
default.t:
26+
start: {line: 0, character: 34}
27+
end: {line: 0, character: 39}
28+
finalState:
29+
documentContents: |
30+
def matchTest(x: Int): String = match {
31+
case 0 => "zero"
32+
case 1 => "one"
33+
case 2 => "two"
34+
case _ => "other"
35+
}
36+
selections:
37+
- anchor: {line: 0, character: 32}
38+
active: {line: 0, character: 32}
39+
thatMark:
40+
- anchor: {line: 0, character: 32}
41+
active: {line: 0, character: 32}
42+
fullTargets: [{type: primitive, mark: {type: decoratedSymbol, symbolColor: default, character: t}, modifiers: [{type: containingScope, scopeType: {type: subject}}]}]
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
languageId: scala
2+
command:
3+
spokenForm: change value zero
4+
version: 2
5+
targets:
6+
- type: primitive
7+
mark: {type: decoratedSymbol, symbolColor: default, character: '0'}
8+
modifiers:
9+
- type: containingScope
10+
scopeType: {type: value}
11+
usePrePhraseSnapshot: true
12+
action: {name: clearAndSetSelection}
13+
initialState:
14+
documentContents: |
15+
def matchTest(x: Int): String = x match {
16+
case 0 => "zero"
17+
case 1 => "one"
18+
case 2 => "two"
19+
case _ => "other"
20+
}
21+
selections:
22+
- anchor: {line: 6, character: 0}
23+
active: {line: 6, character: 0}
24+
marks:
25+
default.0:
26+
start: {line: 1, character: 7}
27+
end: {line: 1, character: 8}
28+
finalState:
29+
documentContents: |
30+
def matchTest(x: Int): String = x match {
31+
case 0 =>
32+
case 1 => "one"
33+
case 2 => "two"
34+
case _ => "other"
35+
}
36+
selections:
37+
- anchor: {line: 1, character: 12}
38+
active: {line: 1, character: 12}
39+
thatMark:
40+
- anchor: {line: 1, character: 12}
41+
active: {line: 1, character: 12}
42+
fullTargets: [{type: primitive, mark: {type: decoratedSymbol, symbolColor: default, character: '0'}, modifiers: [{type: containingScope, scopeType: {type: value}}]}]
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
languageId: scala
2+
command:
3+
spokenForm: take zip
4+
version: 2
5+
targets:
6+
- type: primitive
7+
mark: {type: decoratedSymbol, symbolColor: default, character: z}
8+
usePrePhraseSnapshot: true
9+
action: {name: setSelection}
10+
initialState:
11+
documentContents: |
12+
def matchTest(x: Int): String = x match {
13+
case 0 => "zero"
14+
case 1 => "one"
15+
case 2 => "two"
16+
case _ => "other"
17+
}
18+
selections:
19+
- anchor: {line: 6, character: 0}
20+
active: {line: 6, character: 0}
21+
marks:
22+
default.z:
23+
start: {line: 1, character: 13}
24+
end: {line: 1, character: 17}
25+
finalState:
26+
documentContents: |
27+
def matchTest(x: Int): String = x match {
28+
case 0 => "zero"
29+
case 1 => "one"
30+
case 2 => "two"
31+
case _ => "other"
32+
}
33+
selections:
34+
- anchor: {line: 1, character: 13}
35+
active: {line: 1, character: 17}
36+
thatMark:
37+
- anchor: {line: 1, character: 13}
38+
active: {line: 1, character: 17}
39+
fullTargets: [{type: primitive, mark: {type: decoratedSymbol, symbolColor: default, character: z}, modifiers: []}]

0 commit comments

Comments
 (0)