From ecc2615ba2da0e083a8dbfbf203d1fd7fe0bcaaf Mon Sep 17 00:00:00 2001 From: Matthew Hall Date: Sat, 28 Mar 2020 20:44:12 +0000 Subject: Append new match arms rather than replacing all of them This means we now retain comments when filling in match arms. --- crates/ra_assists/src/handlers/fill_match_arms.rs | 73 +++++++++++++++++++++-- 1 file changed, 68 insertions(+), 5 deletions(-) (limited to 'crates/ra_assists/src') diff --git a/crates/ra_assists/src/handlers/fill_match_arms.rs b/crates/ra_assists/src/handlers/fill_match_arms.rs index add82e5b1..c45981c5c 100644 --- a/crates/ra_assists/src/handlers/fill_match_arms.rs +++ b/crates/ra_assists/src/handlers/fill_match_arms.rs @@ -7,7 +7,7 @@ use itertools::Itertools; use ra_ide_db::RootDatabase; use crate::{Assist, AssistCtx, AssistId}; -use ra_syntax::ast::{self, edit::IndentLevel, make, AstNode, NameOwner}; +use ra_syntax::ast::{self, make, AstNode, NameOwner}; use ast::{MatchArm, Pat}; @@ -97,10 +97,8 @@ pub(crate) fn fill_match_arms(ctx: AssistCtx) -> Option { } ctx.add_assist(AssistId("fill_match_arms"), "Fill match arms", |edit| { - arms.extend(missing_arms); - - let indent_level = IndentLevel::from_node(match_arm_list.syntax()); - let new_arm_list = indent_level.increase_indent(make::match_arm_list(arms)); + let new_arm_list = + match_arm_list.remove_placeholder().append_arms(missing_arms.into_iter()); edit.target(match_expr.syntax().text_range()); edit.set_cursor(expr.syntax().text_range().start()); @@ -655,4 +653,69 @@ mod tests { "#, ); } + + #[test] + fn fill_match_arms_preserves_comments() { + check_assist( + fill_match_arms, + r#" + enum A { + One, + Two, + } + fn foo(a: A) { + match a { + // TODO: Fill this in<|> + A::One => {} + // This is where the rest should be + } + } + "#, + r#" + enum A { + One, + Two, + } + fn foo(a: A) { + match <|>a { + // TODO: Fill this in + A::One => {} + // This is where the rest should be + A::Two => {} + } + } + "#, + ); + } + + #[test] + fn fill_match_arms_preserves_comments_empty() { + check_assist( + fill_match_arms, + r#" + enum A { + One, + Two, + } + fn foo(a: A) { + match a { + // TODO: Fill this in<|> + } + } + "#, + r#" + enum A { + One, + Two, + } + fn foo(a: A) { + match <|>a { + // TODO: Fill this in + A::One => {} + A::Two => {} + } + } + "#, + ); + } } -- cgit v1.2.3