diff options
Diffstat (limited to 'crates/syntax')
-rw-r--r-- | crates/syntax/src/ast/edit_in_place.rs | 21 | ||||
-rw-r--r-- | crates/syntax/src/ast/node_ext.rs | 7 |
2 files changed, 22 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 { | |||
356 | impl ast::MatchArmList { | 356 | impl 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 | ||
diff --git a/crates/syntax/src/ast/node_ext.rs b/crates/syntax/src/ast/node_ext.rs index df8f98b5b..884fe0739 100644 --- a/crates/syntax/src/ast/node_ext.rs +++ b/crates/syntax/src/ast/node_ext.rs | |||
@@ -243,6 +243,13 @@ impl ast::Path { | |||
243 | } | 243 | } |
244 | } | 244 | } |
245 | 245 | ||
246 | pub fn as_single_name_ref(&self) -> Option<ast::NameRef> { | ||
247 | match self.qualifier() { | ||
248 | Some(_) => None, | ||
249 | None => self.segment()?.name_ref(), | ||
250 | } | ||
251 | } | ||
252 | |||
246 | pub fn first_qualifier_or_self(&self) -> ast::Path { | 253 | pub fn first_qualifier_or_self(&self) -> ast::Path { |
247 | successors(Some(self.clone()), ast::Path::qualifier).last().unwrap() | 254 | successors(Some(self.clone()), ast::Path::qualifier).last().unwrap() |
248 | } | 255 | } |