Skip to content

Commit 14e2da3

Browse files
authored
tolk: add else branch completion for match (#3)
1 parent 3c8e186 commit 14e2da3

File tree

4 files changed

+109
-5
lines changed

4 files changed

+109
-5
lines changed

server/src/e2e/tolk/testcases/completion-select/match.test

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,7 @@ fun main(msg: AllowedMessageToMinter) {
251251
RequestWalletAddress => {},
252252
ChangeMinterAdmin => {},
253253
ChangeMinterContent => {},
254-
AllowedMessageToMinter<caret>
254+
else => {<caret>},
255255
}
256256
}
257257

@@ -273,3 +273,33 @@ fun foo(value: int) {
273273
FOO<caret> => {}
274274
}
275275
}
276+
277+
========================================================================
278+
Match over type else completion
279+
========================================================================
280+
fun foo(a: int | slice) {
281+
match (a) {
282+
els<caret>
283+
}
284+
}
285+
------------------------------------------------------------------------
286+
fun foo(a: int | slice) {
287+
match (a) {
288+
else => {<caret>},
289+
}
290+
}
291+
292+
========================================================================
293+
Match over value else completion
294+
========================================================================
295+
fun foo() {
296+
match (10) {
297+
els<caret>
298+
}
299+
}
300+
------------------------------------------------------------------------
301+
fun foo() {
302+
match (10) {
303+
else => {<caret>},
304+
}
305+
}

server/src/e2e/tolk/testcases/completion/match.test

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ fun foo(value: int | slice) {
2424
------------------------------------------------------------------------
2525
22 int => {}
2626
22 slice => {}
27+
22 else => {}
2728

2829
========================================================================
2930
Match over type completion, union type 2
@@ -52,6 +53,7 @@ fun main(msg: AllowedMessageToMinter) {
5253
22 ChangeMinterContent => {}
5354
22 MintNewJettons => {}
5455
22 RequestWalletAddress => {}
56+
22 else => {}
5557

5658
========================================================================
5759
Match over type completion, union type, match with single arm
@@ -80,6 +82,7 @@ fun main(msg: AllowedMessageToMinter) {
8082
22 ChangeMinterAdmin => {}
8183
22 ChangeMinterContent => {}
8284
22 RequestWalletAddress => {}
85+
22 else => {}
8386

8487
========================================================================
8588
Match over type completion, union type, match with single arm, cursor before arm
@@ -109,6 +112,7 @@ fun main(msg: AllowedMessageToMinter) {
109112
22 ChangeMinterContent => {}
110113
22 MintNewJettons => {}
111114
22 RequestWalletAddress => {}
115+
22 else => {}
112116

113117
========================================================================
114118
Match over type completion, union type, match with several arms
@@ -137,6 +141,7 @@ fun main(msg: AllowedMessageToMinter) {
137141
}
138142
------------------------------------------------------------------------
139143
22 ChangeMinterContent => {}
144+
22 else => {}
140145

141146
========================================================================
142147
Match over type completion, union type, match with all arms
@@ -165,6 +170,21 @@ fun main(msg: AllowedMessageToMinter) {
165170
}
166171
}
167172
------------------------------------------------------------------------
173+
22 else => {}
174+
175+
========================================================================
176+
Match over type with else completion
177+
========================================================================
178+
const FOO = 100
179+
180+
fun foo(value: int | slice) {
181+
match (value) {
182+
int => {},
183+
else => {},
184+
els<caret>
185+
}
186+
}
187+
------------------------------------------------------------------------
168188
No completion items
169189

170190
========================================================================
@@ -179,3 +199,18 @@ fun foo(value: int) {
179199
}
180200
------------------------------------------------------------------------
181201
20 FOO: int = 100
202+
203+
========================================================================
204+
Match over value with else completion
205+
========================================================================
206+
const FOO = 100
207+
208+
fun foo(value: int) {
209+
match (value) {
210+
FOO => {},
211+
else => {},
212+
els<caret>
213+
}
214+
}
215+
------------------------------------------------------------------------
216+
No completion items

server/src/e2e/tolk/testcases/completion/struct-instance.test

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -92,12 +92,10 @@ struct Foo {
9292
}
9393

9494
fun test(value: int) {
95-
Foo{ val<caret> value: 10 };
95+
Foo{ a<caret>, value: 10 };
9696
}
9797
------------------------------------------------------------------------
98-
5 value int
99-
14 val
100-
14 valt
98+
9 age : int of Foo
10199

102100
========================================================================
103101
Second field in struct init with variable

server/src/languages/tolk/completion/providers/MatchArmsCompletionProvider.ts

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,16 +49,44 @@ export class MatchArmsCompletionProvider implements CompletionProvider {
4949
insertText: value.insertText + "$1 => {$0}",
5050
})
5151
}
52+
53+
let seenElse = false
54+
55+
const arms = body.namedChildren.filter(it => it?.type === "match_arm")
56+
for (const arm of arms) {
57+
if (arm?.childForFieldName("pattern_else")) {
58+
seenElse = true
59+
}
60+
}
61+
62+
if (!seenElse) {
63+
result.add({
64+
label: "else",
65+
labelDetails: {
66+
detail: " => {}",
67+
},
68+
kind: CompletionItemKind.Event,
69+
insertTextFormat: InsertTextFormat.Snippet,
70+
insertText: "else => {$0},",
71+
weight: CompletionWeight.SNIPPET + 10,
72+
})
73+
}
5274
return
5375
}
5476

5577
const arms = body.namedChildren.filter(it => it?.type === "match_arm")
5678

79+
let seenElse = false
5780
const handledTypes: Set<string> = new Set()
5881

5982
for (const arm of arms) {
6083
if (!arm) continue
6184

85+
if (arm.childForFieldName("pattern_else")) {
86+
seenElse = true
87+
continue
88+
}
89+
6290
const patternType = arm.childForFieldName("pattern_type")
6391
if (!patternType) continue
6492

@@ -83,5 +111,18 @@ export class MatchArmsCompletionProvider implements CompletionProvider {
83111
weight: CompletionWeight.SNIPPET,
84112
})
85113
}
114+
115+
if (!seenElse) {
116+
result.add({
117+
label: "else",
118+
labelDetails: {
119+
detail: " => {}",
120+
},
121+
kind: CompletionItemKind.Event,
122+
insertTextFormat: InsertTextFormat.Snippet,
123+
insertText: "else => {$0},",
124+
weight: CompletionWeight.SNIPPET + 10,
125+
})
126+
}
86127
}
87128
}

0 commit comments

Comments
 (0)