aboutsummaryrefslogtreecommitdiff
path: root/crates/syntax/src
diff options
context:
space:
mode:
authorbors[bot] <26634292+bors[bot]@users.noreply.github.com>2021-05-24 20:59:51 +0100
committerGitHub <[email protected]>2021-05-24 20:59:51 +0100
commit86ca1764b66e65fcb986b7188b6a77e8b53f9758 (patch)
tree9f30e5c52c33482b37bdcdff595aa38bf3a0a263 /crates/syntax/src
parentf5f24a9a2c2e9d5d5ff155e700b2bf647f926d47 (diff)
parent3641abc0c3e3ffaa1f6b5bf9b280b4217ea93e67 (diff)
Merge #8975
8975: Use todo!() as placeholder body for generated match arms r=matklad a=jDomantas `todo!()` seems to be a better fit for this than `{}`. Seeing that this assist predates stabilization of `todo` my guess is that simply no one bothered to change it yet. Also fixed the issue where if the last arm was not block-like, rust-analyzer would not add a comma after it and would generate invalid code. Co-authored-by: Domantas Jadenkus <[email protected]>
Diffstat (limited to 'crates/syntax/src')
-rw-r--r--crates/syntax/src/ast/edit_in_place.rs21
1 files changed, 15 insertions, 6 deletions
diff --git a/crates/syntax/src/ast/edit_in_place.rs b/crates/syntax/src/ast/edit_in_place.rs
index ca8103668..f7ee29d14 100644
--- a/crates/syntax/src/ast/edit_in_place.rs
+++ b/crates/syntax/src/ast/edit_in_place.rs
@@ -356,13 +356,17 @@ impl ast::MatchArm {
356impl ast::MatchArmList { 356impl ast::MatchArmList {
357 pub fn add_arm(&self, arm: ast::MatchArm) { 357 pub fn add_arm(&self, arm: ast::MatchArm) {
358 normalize_ws_between_braces(self.syntax()); 358 normalize_ws_between_braces(self.syntax());
359 let mut elements = Vec::new();
359 let position = match self.arms().last() { 360 let position = match self.arms().last() {
360 Some(last_arm) => { 361 Some(last_arm) => {
361 let curly = last_arm 362 let comma = last_arm
362 .syntax() 363 .syntax()
363 .siblings_with_tokens(Direction::Next) 364 .siblings_with_tokens(Direction::Next)
364 .find(|it| it.kind() == T![,]); 365 .find(|it| it.kind() == T![,]);
365 Position::after(curly.unwrap_or_else(|| last_arm.syntax().clone().into())) 366 if needs_comma(&last_arm) && comma.is_none() {
367 elements.push(make::token(SyntaxKind::COMMA).into());
368 }
369 Position::after(comma.unwrap_or_else(|| last_arm.syntax().clone().into()))
366 } 370 }
367 None => match self.l_curly_token() { 371 None => match self.l_curly_token() {
368 Some(it) => Position::after(it), 372 Some(it) => Position::after(it),
@@ -370,11 +374,16 @@ impl ast::MatchArmList {
370 }, 374 },
371 }; 375 };
372 let indent = IndentLevel::from_node(self.syntax()) + 1; 376 let indent = IndentLevel::from_node(self.syntax()) + 1;
373 let elements = vec![ 377 elements.push(make::tokens::whitespace(&format!("\n{}", indent)).into());
374 make::tokens::whitespace(&format!("\n{}", indent)).into(), 378 elements.push(arm.syntax().clone().into());
375 arm.syntax().clone().into(), 379 if needs_comma(&arm) {
376 ]; 380 elements.push(make::token(SyntaxKind::COMMA).into());
381 }
377 ted::insert_all(position, elements); 382 ted::insert_all(position, elements);
383
384 fn needs_comma(arm: &ast::MatchArm) -> bool {
385 arm.expr().map_or(false, |e| !e.is_block_like())
386 }
378 } 387 }
379} 388}
380 389