Skip to content

Commit bc54775

Browse files
committed
Preserve comments for extracted block expr in 'extract_function' assist
1 parent ae6e737 commit bc54775

File tree

1 file changed

+18
-3
lines changed

1 file changed

+18
-3
lines changed

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

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ use syntax::{
2525
edit::{AstNodeEdit, IndentLevel},
2626
AstNode, HasGenericParams,
2727
},
28-
match_ast, ted, SyntaxElement,
28+
match_ast, ted, AstToken, SyntaxElement,
2929
SyntaxKind::{self, COMMENT},
3030
SyntaxNode, SyntaxToken, TextRange, TextSize, TokenAtOffset, WalkEvent, T,
3131
};
@@ -1733,8 +1733,23 @@ fn make_body(
17331733
ast::Expr::BlockExpr(block) => {
17341734
// If the extracted expression is itself a block, there is no need to wrap it inside another block.
17351735
let block = block.dedent(old_indent);
1736-
// Recreate the block for formatting consistency with other extracted functions.
1737-
make::block_expr(block.statements(), block.tail_expr())
1736+
let elements = block.stmt_list().map_or_else(
1737+
|| Either::Left(iter::empty()),
1738+
|stmt_list| {
1739+
let elements = stmt_list.syntax().children_with_tokens().filter_map(
1740+
|node_or_token| match &node_or_token {
1741+
syntax::NodeOrToken::Node(node) => {
1742+
ast::Stmt::cast(node.clone()).map(|_| node_or_token)
1743+
}
1744+
syntax::NodeOrToken::Token(token) => {
1745+
ast::Comment::cast(token.clone()).map(|_| node_or_token)
1746+
}
1747+
},
1748+
);
1749+
Either::Right(elements)
1750+
},
1751+
);
1752+
make::hacky_block_expr(elements, block.tail_expr())
17381753
}
17391754
_ => {
17401755
let expr = expr.dedent(old_indent).indent(IndentLevel(1));

0 commit comments

Comments
 (0)