Skip to content

Commit 4596847

Browse files
committed
Auto merge of #13746 - feniljain:fix_extract_function, r=jonas-schievink
fix: make make_body respect comments in extract_function Possible fix for #13621 ### Points to help in review: - Earlier we were only considering statements in a block expr and hence comments were being ignored, now we handle tokens hence making it aware of comments and then preserving them using `hacky_block_expr_with_comments` Seems like I am not able to attach output video, github is glitching for it :(
2 parents 15ff8a5 + d7183fb commit 4596847

File tree

1 file changed

+63
-4
lines changed

1 file changed

+63
-4
lines changed

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

+63-4
Original file line numberDiff line numberDiff line change
@@ -1799,7 +1799,6 @@ fn make_body(
17991799
})
18001800
.collect::<Vec<SyntaxElement>>();
18011801
let tail_expr = tail_expr.map(|expr| expr.dedent(old_indent).indent(body_indent));
1802-
18031802
make::hacky_block_expr_with_comments(elements, tail_expr)
18041803
}
18051804
};
@@ -1860,9 +1859,29 @@ fn with_default_tail_expr(block: ast::BlockExpr, tail_expr: ast::Expr) -> ast::B
18601859
}
18611860

18621861
fn with_tail_expr(block: ast::BlockExpr, tail_expr: ast::Expr) -> ast::BlockExpr {
1863-
let stmt_tail = block.tail_expr().map(|expr| make::expr_stmt(expr).into());
1864-
let stmts = block.statements().chain(stmt_tail);
1865-
make::block_expr(stmts, Some(tail_expr))
1862+
let stmt_tail_opt: Option<ast::Stmt> =
1863+
block.tail_expr().map(|expr| make::expr_stmt(expr).into());
1864+
1865+
let mut elements: Vec<SyntaxElement> = vec![];
1866+
1867+
block.statements().for_each(|stmt| {
1868+
elements.push(syntax::NodeOrToken::Node(stmt.syntax().clone()));
1869+
});
1870+
1871+
if let Some(stmt_list) = block.stmt_list() {
1872+
stmt_list.syntax().children_with_tokens().for_each(|node_or_token| {
1873+
match &node_or_token {
1874+
syntax::NodeOrToken::Token(_) => elements.push(node_or_token),
1875+
_ => (),
1876+
};
1877+
});
1878+
}
1879+
1880+
if let Some(stmt_tail) = stmt_tail_opt {
1881+
elements.push(syntax::NodeOrToken::Node(stmt_tail.syntax().clone()));
1882+
}
1883+
1884+
make::hacky_block_expr_with_comments(elements, Some(tail_expr))
18661885
}
18671886

18681887
fn format_type(ty: &hir::Type, ctx: &AssistContext<'_>, module: hir::Module) -> String {
@@ -5744,6 +5763,46 @@ fn $0fun_name() -> Option<()> {
57445763
};
57455764
Some(a)
57465765
}
5766+
"#,
5767+
);
5768+
}
5769+
5770+
#[test]
5771+
fn non_tail_expr_with_comment_of_tail_expr_loop() {
5772+
check_assist(
5773+
extract_function,
5774+
r#"
5775+
pub fn f() {
5776+
loop {
5777+
$0// A comment
5778+
if true {
5779+
continue;
5780+
}$0
5781+
if false {
5782+
break;
5783+
}
5784+
}
5785+
}
5786+
"#,
5787+
r#"
5788+
pub fn f() {
5789+
loop {
5790+
if let ControlFlow::Break(_) = fun_name() {
5791+
continue;
5792+
}
5793+
if false {
5794+
break;
5795+
}
5796+
}
5797+
}
5798+
5799+
fn $0fun_name() -> ControlFlow<()> {
5800+
// A comment
5801+
if true {
5802+
return ControlFlow::Break(());
5803+
}
5804+
ControlFlow::Continue(())
5805+
}
57475806
"#,
57485807
);
57495808
}

0 commit comments

Comments
 (0)