Skip to content

Commit 4de447b

Browse files
committed
Attach comma token to MATCH_ARM instead of MATCH_ARM_LIST
1 parent 8d3b294 commit 4de447b

15 files changed

+113
-121
lines changed

crates/ide/src/join_lines.rs

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,10 @@ use std::convert::TryFrom;
33
use ide_assists::utils::extract_trivial_expression;
44
use itertools::Itertools;
55
use syntax::{
6-
algo::non_trivia_sibling,
76
ast::{self, AstNode, AstToken, IsString},
8-
Direction, NodeOrToken, SourceFile, SyntaxElement,
7+
NodeOrToken, SourceFile, SyntaxElement,
98
SyntaxKind::{self, USE_TREE, WHITESPACE},
10-
SyntaxNode, SyntaxToken, TextRange, TextSize, T,
9+
SyntaxToken, TextRange, TextSize, T,
1110
};
1211

1312
use text_edit::{TextEdit, TextEditBuilder};
@@ -204,13 +203,6 @@ fn remove_newline(
204203
edit.replace(token.text_range(), compute_ws(prev.kind(), next.kind()).to_string());
205204
}
206205

207-
fn has_comma_after(node: &SyntaxNode) -> bool {
208-
match non_trivia_sibling(node.clone().into(), Direction::Next) {
209-
Some(n) => n.kind() == T![,],
210-
_ => false,
211-
}
212-
}
213-
214206
fn join_single_expr_block(edit: &mut TextEditBuilder, token: &SyntaxToken) -> Option<()> {
215207
let block_expr = ast::BlockExpr::cast(token.parent()?)?;
216208
if !block_expr.is_standalone() {
@@ -223,7 +215,7 @@ fn join_single_expr_block(edit: &mut TextEditBuilder, token: &SyntaxToken) -> Op
223215

224216
// Match block needs to have a comma after the block
225217
if let Some(match_arm) = block_expr.syntax().parent().and_then(ast::MatchArm::cast) {
226-
if !has_comma_after(match_arm.syntax()) {
218+
if match_arm.comma_token().is_none() {
227219
buf.push(',');
228220
}
229221
}

crates/ide/src/move_item.rs

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -205,16 +205,16 @@ fn main() {
205205
}
206206
"#,
207207
expect![[r#"
208-
fn main() {
209-
match true {
210-
false =>$0 {
211-
println!("Test");
212-
},
213-
true => {
214-
println!("Hello, world");
215-
}
216-
};
217-
}
208+
fn main() {
209+
match true {
210+
false =>$0 {
211+
println!("Test");
212+
}
213+
true => {
214+
println!("Hello, world");
215+
},
216+
};
217+
}
218218
"#]],
219219
Direction::Up,
220220
);
@@ -236,16 +236,16 @@ fn main() {
236236
}
237237
"#,
238238
expect![[r#"
239-
fn main() {
240-
match true {
241-
false => {
242-
println!("Test");
243-
},
244-
true =>$0 {
245-
println!("Hello, world");
246-
}
247-
};
248-
}
239+
fn main() {
240+
match true {
241+
false => {
242+
println!("Test");
243+
}
244+
true =>$0 {
245+
println!("Hello, world");
246+
},
247+
};
248+
}
249249
"#]],
250250
Direction::Down,
251251
);

crates/parser/src/grammar/expressions/atom.rs

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -400,20 +400,7 @@ pub(crate) fn match_arm_list(p: &mut Parser) {
400400
error_block(p, "expected match arm");
401401
continue;
402402
}
403-
404-
// test match_arms_commas
405-
// fn foo() {
406-
// match () {
407-
// _ => (),
408-
// _ => {}
409-
// _ => ()
410-
// }
411-
// }
412-
if match_arm(p).is_block() {
413-
p.eat(T![,]);
414-
} else if !p.at(T!['}']) {
415-
p.expect(T![,]);
416-
}
403+
match_arm(p);
417404
}
418405
p.expect(T!['}']);
419406
m.complete(p, MATCH_ARM_LIST);
@@ -429,7 +416,7 @@ pub(crate) fn match_arm_list(p: &mut Parser) {
429416
// | X => (),
430417
// };
431418
// }
432-
fn match_arm(p: &mut Parser) -> BlockLike {
419+
fn match_arm(p: &mut Parser) {
433420
let m = p.start();
434421
// test match_arms_outer_attributes
435422
// fn foo() {
@@ -452,8 +439,21 @@ fn match_arm(p: &mut Parser) -> BlockLike {
452439
}
453440
p.expect(T![=>]);
454441
let blocklike = expr_stmt(p).1;
442+
443+
// test match_arms_commas
444+
// fn foo() {
445+
// match () {
446+
// _ => (),
447+
// _ => {}
448+
// _ => ()
449+
// }
450+
// }
451+
if blocklike.is_block() {
452+
p.eat(T![,]);
453+
} else if !p.at(T!['}']) {
454+
p.expect(T![,]);
455+
}
455456
m.complete(p, MATCH_ARM);
456-
blocklike
457457
}
458458

459459
// test match_guard

crates/syntax/test_data/parser/err/0032_match_arms_inner_attrs.rast

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ [email protected]
2222
2323
2424
25-
MATCH_ARM@34..41
25+
MATCH_ARM@34..42
2626
2727
2828
@@ -31,7 +31,7 @@ [email protected]
3131
3232
3333
34-
34+
3535
3636
3737
@@ -53,7 +53,7 @@ [email protected]
5353
5454
5555
56-
MATCH_ARM@87..94
56+
MATCH_ARM@87..95
5757
5858
5959
@@ -62,7 +62,7 @@ [email protected]
6262
6363
6464
65-
65+
6666
6767
6868
@@ -77,7 +77,7 @@ [email protected]
7777
7878
7979
80-
MATCH_ARM@126..133
80+
MATCH_ARM@126..134
8181
8282
8383
@@ -86,9 +86,9 @@ [email protected]
8686
8787
8888
89-
89+
9090
91-
MATCH_ARM@143..150
91+
MATCH_ARM@143..151
9292
9393
9494
@@ -97,7 +97,7 @@ [email protected]
9797
9898
9999
100-
100+
101101
102102
103103
@@ -165,7 +165,7 @@ [email protected]
165165
166166
167167
168-
MATCH_ARM@259..266
168+
MATCH_ARM@259..267
169169
170170
171171
@@ -174,9 +174,9 @@ [email protected]
174174
175175
176176
177-
177+
178178
179-
MATCH_ARM@276..283
179+
MATCH_ARM@276..284
180180
181181
182182
@@ -185,7 +185,7 @@ [email protected]
185185
186186
187187
188-
188+
189189
190190
191191

crates/syntax/test_data/parser/err/0033_match_arms_outer_attrs.rast

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ [email protected]
2121
2222
2323
24-
MATCH_ARM@34..41
24+
MATCH_ARM@34..42
2525
2626
2727
@@ -30,9 +30,9 @@ [email protected]
3030
3131
3232
33-
33+
3434
35-
MATCH_ARM@51..58
35+
MATCH_ARM@51..59
3636
3737
3838
@@ -41,7 +41,7 @@ [email protected]
4141
4242
4343
44-
44+
4545
4646
4747

crates/syntax/test_data/parser/inline/ok/0055_literal_pattern.rast

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ [email protected]
2121
2222
2323
24-
MATCH_ARM@35..43
24+
MATCH_ARM@35..44
2525
2626
2727
@@ -32,9 +32,9 @@ [email protected]
3232
3333
3434
35-
35+
3636
37-
MATCH_ARM@53..61
37+
MATCH_ARM@53..62
3838
3939
4040
@@ -44,9 +44,9 @@ [email protected]
4444
4545
4646
47-
47+
4848
49-
MATCH_ARM@71..80
49+
MATCH_ARM@71..81
5050
5151
5252
@@ -56,9 +56,9 @@ [email protected]
5656
5757
5858
59-
59+
6060
61-
MATCH_ARM@90..103
61+
MATCH_ARM@90..104
6262
6363
6464
[email protected] "\"hello\""
@@ -68,7 +68,7 @@ [email protected]
6868
6969
7070
71-
71+
7272
7373
7474

crates/syntax/test_data/parser/inline/ok/0058_range_pat.rast

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ [email protected]
2020
2121
2222
23-
MATCH_ARM@35..50
23+
MATCH_ARM@35..51
2424
2525
2626
@@ -37,9 +37,9 @@ [email protected]
3737
3838
3939
40-
40+
4141
42-
MATCH_ARM@60..77
42+
MATCH_ARM@60..78
4343
4444
4545
@@ -56,9 +56,9 @@ [email protected]
5656
5757
5858
59-
59+
6060
61-
MATCH_ARM@87..102
61+
MATCH_ARM@87..103
6262
6363
6464
@@ -74,7 +74,7 @@ [email protected]
7474
7575
7676
77-
77+
7878
7979
8080

crates/syntax/test_data/parser/inline/ok/0059_match_arms_commas.rast

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ [email protected]
2121
2222
2323
24-
MATCH_ARM@34..41
24+
MATCH_ARM@34..42
2525
2626
2727
@@ -30,7 +30,7 @@ [email protected]
3030
3131
3232
33-
33+
3434
3535
3636

0 commit comments

Comments
 (0)