Skip to content

Commit 938a39a

Browse files
committed
Auto merge of rust-lang#13891 - bvanjoi:reverse-whitespace-in-assists, r=Veykril
fix: keep whitespace in extract function handler Fixed rust-lang#13874
2 parents ec96819 + ae73628 commit 938a39a

File tree

2 files changed

+17
-9
lines changed

2 files changed

+17
-9
lines changed

crates/ide-assists/src/handlers/extract_function.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1799,7 +1799,8 @@ fn make_body(
17991799
})
18001800
.collect::<Vec<SyntaxElement>>();
18011801
let tail_expr = tail_expr.map(|expr| expr.dedent(old_indent).indent(body_indent));
1802-
make::hacky_block_expr_with_comments(elements, tail_expr)
1802+
1803+
make::hacky_block_expr(elements, tail_expr)
18031804
}
18041805
};
18051806

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

1884-
make::hacky_block_expr_with_comments(elements, Some(tail_expr))
1885+
make::hacky_block_expr(elements, Some(tail_expr))
18851886
}
18861887

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

4981-
// FIXME: we do want to preserve whitespace
49824982
#[test]
4983-
fn extract_function_does_not_preserve_whitespace() {
4983+
fn extract_function_does_preserve_whitespace() {
49844984
check_assist(
49854985
extract_function,
49864986
r#"
@@ -4999,6 +4999,7 @@ fn func() {
49994999
50005000
fn $0fun_name() {
50015001
let a = 0;
5002+
50025003
let x = 0;
50035004
}
50045005
"#,

crates/syntax/src/ast/make.rs

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -339,21 +339,28 @@ pub fn tail_only_block_expr(tail_expr: ast::Expr) -> ast::BlockExpr {
339339
}
340340

341341
/// Ideally this function wouldn't exist since it involves manual indenting.
342-
/// It differs from `make::block_expr` by also supporting comments.
342+
/// It differs from `make::block_expr` by also supporting comments and whitespace.
343343
///
344344
/// FIXME: replace usages of this with the mutable syntax tree API
345-
pub fn hacky_block_expr_with_comments(
345+
pub fn hacky_block_expr(
346346
elements: impl IntoIterator<Item = crate::SyntaxElement>,
347347
tail_expr: Option<ast::Expr>,
348348
) -> ast::BlockExpr {
349349
let mut buf = "{\n".to_string();
350350
for node_or_token in elements.into_iter() {
351351
match node_or_token {
352352
rowan::NodeOrToken::Node(n) => format_to!(buf, " {n}\n"),
353-
rowan::NodeOrToken::Token(t) if t.kind() == SyntaxKind::COMMENT => {
354-
format_to!(buf, " {t}\n")
353+
rowan::NodeOrToken::Token(t) => {
354+
let kind = t.kind();
355+
if kind == SyntaxKind::COMMENT {
356+
format_to!(buf, " {t}\n")
357+
} else if kind == SyntaxKind::WHITESPACE {
358+
let content = t.text().trim_matches(|c| c != '\n');
359+
if content.len() >= 1 {
360+
format_to!(buf, "{}", &content[1..])
361+
}
362+
}
355363
}
356-
_ => (),
357364
}
358365
}
359366
if let Some(tail_expr) = tail_expr {

0 commit comments

Comments
 (0)