fn format_arm(block: &ast::BlockExpr) -> String {
match extract_trivial_expression(block) {
- None => block.syntax().text().to_string(),
- Some(e) => format!("{},", e.syntax().text()),
+ Some(e) if !e.syntax().text().contains_char('\n') => format!("{},", e.syntax().text()),
+ _ => block.syntax().text().to_string(),
}
}
)
}
+ #[test]
+ fn test_replace_if_let_with_match_doesnt_unwrap_multiline_expressions() {
+ check_assist(
+ replace_if_let_with_match,
+ "
+fn foo() {
+ if <|>let VariantData::Struct(..) = a {
+ bar(
+ 123
+ )
+ } else {
+ false
+ }
+} ",
+ "
+fn foo() {
+ <|>match a {
+ VariantData::Struct(..) => {
+ bar(
+ 123
+ )
+ }
+ _ => false,
+ }
+} ",
+ )
+ }
+
#[test]
fn replace_if_let_with_match_target() {
check_assist_target(
pub fn extract_trivial_expression(expr: &ast::BlockExpr) -> Option<ast::Expr> {
let block = expr.block()?;
let expr = block.expr()?;
- if expr.syntax().text().contains_char('\n') {
- return None;
- }
let non_trivial_children = block.syntax().children().filter(|it| match it.kind() {
WHITESPACE | T!['{'] | T!['}'] => false,
_ => it != expr.syntax(),
);
}
+ #[test]
+ fn join_lines_multiline_in_block() {
+ check_join_lines(
+ r"
+fn foo() {
+ match ty {
+ <|> Some(ty) => {
+ match ty {
+ _ => false,
+ }
+ }
+ _ => true,
+ }
+}
+",
+ r"
+fn foo() {
+ match ty {
+ <|> Some(ty) => match ty {
+ _ => false,
+ },
+ _ => true,
+ }
+}
+",
+ );
+ }
+
#[test]
fn join_lines_keeps_comma_for_block_in_match_arm() {
// We already have a comma