Skip to content

Commit e45a250

Browse files
committed
fix: Insert spaces when inlining a function defined in a macro.
1 parent 0b1ed70 commit e45a250

File tree

1 file changed

+48
-2
lines changed

1 file changed

+48
-2
lines changed

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

+48-2
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use ide_db::{
77
imports::insert_use::remove_path_if_in_use_stmt,
88
path_transform::PathTransform,
99
search::{FileReference, SearchScope},
10-
syntax_helpers::node_ext::expr_as_name_ref,
10+
syntax_helpers::{insert_whitespace_into_node::insert_ws_into, node_ext::expr_as_name_ref},
1111
RootDatabase,
1212
};
1313
use itertools::{izip, Itertools};
@@ -301,7 +301,18 @@ fn inline(
301301
params: &[(ast::Pat, Option<ast::Type>, hir::Param)],
302302
CallInfo { node, arguments, generic_arg_list }: &CallInfo,
303303
) -> ast::Expr {
304-
let body = fn_body.clone_for_update();
304+
let body = if sema.hir_file_for(fn_body.syntax()).is_macro() {
305+
cov_mark::hit!(inline_call_defined_in_macro);
306+
if let Some(body) = ast::BlockExpr::cast(insert_ws_into(fn_body.syntax().clone())) {
307+
body
308+
} else {
309+
// FIXME(zachs18): I believe this should be unreachable,
310+
// since insert_ws_into shouldn't change the kind of the SyntaxNode.
311+
fn_body.clone_for_update()
312+
}
313+
} else {
314+
fn_body.clone_for_update()
315+
};
305316
let usages_for_locals = |local| {
306317
Definition::Local(local)
307318
.usages(sema)
@@ -1144,6 +1155,41 @@ fn bar() -> u32 {
11441155
x
11451156
}) + foo()
11461157
}
1158+
"#,
1159+
)
1160+
}
1161+
1162+
#[test]
1163+
fn inline_call_defined_in_macro() {
1164+
cov_mark::check!(inline_call_defined_in_macro);
1165+
check_assist(
1166+
inline_call,
1167+
r#"
1168+
macro_rules! define_foo {
1169+
() => { fn foo() -> u32 {
1170+
let x = 0;
1171+
x
1172+
} };
1173+
}
1174+
define_foo!();
1175+
fn bar() -> u32 {
1176+
foo$0()
1177+
}
1178+
"#,
1179+
r#"
1180+
macro_rules! define_foo {
1181+
() => { fn foo() -> u32 {
1182+
let x = 0;
1183+
x
1184+
} };
1185+
}
1186+
define_foo!();
1187+
fn bar() -> u32 {
1188+
{
1189+
let x = 0;
1190+
x
1191+
}
1192+
}
11471193
"#,
11481194
)
11491195
}

0 commit comments

Comments
 (0)