Skip to content

Commit

Permalink
Auto merge of rust-lang#13891 - bvanjoi:reverse-whitespace-in-assists…
Browse files Browse the repository at this point in the history
…, r=Veykril

fix: keep whitespace in extract function handler

Fixed rust-lang#13874
  • Loading branch information
bors committed Jan 9, 2023
2 parents ec96819 + ae73628 commit 938a39a
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 9 deletions.
9 changes: 5 additions & 4 deletions crates/ide-assists/src/handlers/extract_function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1799,7 +1799,8 @@ fn make_body(
})
.collect::<Vec<SyntaxElement>>();
let tail_expr = tail_expr.map(|expr| expr.dedent(old_indent).indent(body_indent));
make::hacky_block_expr_with_comments(elements, tail_expr)

make::hacky_block_expr(elements, tail_expr)
}
};

Expand Down Expand Up @@ -1881,7 +1882,7 @@ fn with_tail_expr(block: ast::BlockExpr, tail_expr: ast::Expr) -> ast::BlockExpr
elements.push(syntax::NodeOrToken::Node(stmt_tail.syntax().clone()));
}

make::hacky_block_expr_with_comments(elements, Some(tail_expr))
make::hacky_block_expr(elements, Some(tail_expr))
}

fn format_type(ty: &hir::Type, ctx: &AssistContext<'_>, module: hir::Module) -> String {
Expand Down Expand Up @@ -4978,9 +4979,8 @@ fn $0fun_name() {
);
}

// FIXME: we do want to preserve whitespace
#[test]
fn extract_function_does_not_preserve_whitespace() {
fn extract_function_does_preserve_whitespace() {
check_assist(
extract_function,
r#"
Expand All @@ -4999,6 +4999,7 @@ fn func() {
fn $0fun_name() {
let a = 0;
let x = 0;
}
"#,
Expand Down
17 changes: 12 additions & 5 deletions crates/syntax/src/ast/make.rs
Original file line number Diff line number Diff line change
Expand Up @@ -339,21 +339,28 @@ pub fn tail_only_block_expr(tail_expr: ast::Expr) -> ast::BlockExpr {
}

/// Ideally this function wouldn't exist since it involves manual indenting.
/// It differs from `make::block_expr` by also supporting comments.
/// It differs from `make::block_expr` by also supporting comments and whitespace.
///
/// FIXME: replace usages of this with the mutable syntax tree API
pub fn hacky_block_expr_with_comments(
pub fn hacky_block_expr(
elements: impl IntoIterator<Item = crate::SyntaxElement>,
tail_expr: Option<ast::Expr>,
) -> ast::BlockExpr {
let mut buf = "{\n".to_string();
for node_or_token in elements.into_iter() {
match node_or_token {
rowan::NodeOrToken::Node(n) => format_to!(buf, " {n}\n"),
rowan::NodeOrToken::Token(t) if t.kind() == SyntaxKind::COMMENT => {
format_to!(buf, " {t}\n")
rowan::NodeOrToken::Token(t) => {
let kind = t.kind();
if kind == SyntaxKind::COMMENT {
format_to!(buf, " {t}\n")
} else if kind == SyntaxKind::WHITESPACE {
let content = t.text().trim_matches(|c| c != '\n');
if content.len() >= 1 {
format_to!(buf, "{}", &content[1..])
}
}
}
_ => (),
}
}
if let Some(tail_expr) = tail_expr {
Expand Down

0 comments on commit 938a39a

Please sign in to comment.