Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit 9f6a2c4

Browse files
rmehri01Veykril
authored andcommitted
fix: use original range to deal with macros in bool_to_enum
1 parent b5e0edf commit 9f6a2c4

File tree

1 file changed

+91
-9
lines changed

1 file changed

+91
-9
lines changed

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

Lines changed: 91 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -169,8 +169,8 @@ fn replace_bool_expr(edit: &mut SourceChangeBuilder, expr: ast::Expr) {
169169

170170
/// Converts an expression of type `bool` to one of the new enum type.
171171
fn bool_expr_to_enum_expr(expr: ast::Expr) -> ast::Expr {
172-
let true_expr = make::expr_path(make::path_from_text("Bool::True")).clone_for_update();
173-
let false_expr = make::expr_path(make::path_from_text("Bool::False")).clone_for_update();
172+
let true_expr = make::expr_path(make::path_from_text("Bool::True"));
173+
let false_expr = make::expr_path(make::path_from_text("Bool::False"));
174174

175175
if let ast::Expr::Literal(literal) = &expr {
176176
match literal.kind() {
@@ -184,7 +184,6 @@ fn bool_expr_to_enum_expr(expr: ast::Expr) -> ast::Expr {
184184
make::tail_only_block_expr(true_expr),
185185
Some(ast::ElseBranch::Block(make::tail_only_block_expr(false_expr))),
186186
)
187-
.clone_for_update()
188187
}
189188
}
190189

@@ -239,7 +238,7 @@ fn replace_usages(
239238
cov_mark::hit!(replaces_record_expr);
240239

241240
let enum_expr = bool_expr_to_enum_expr(initializer);
242-
replace_record_field_expr(edit, record_field, enum_expr);
241+
replace_record_field_expr(ctx, edit, record_field, enum_expr);
243242
} else if let Some(pat) = find_record_pat_field_usage(&name) {
244243
match pat {
245244
ast::Pat::IdentPat(ident_pat) => {
@@ -283,6 +282,7 @@ fn replace_usages(
283282
)
284283
{
285284
replace_record_field_expr(
285+
ctx,
286286
edit,
287287
record_field,
288288
make::expr_bin_op(
@@ -312,19 +312,19 @@ fn replace_usages(
312312

313313
/// Replaces the record expression, handling field shorthands.
314314
fn replace_record_field_expr(
315+
ctx: &AssistContext<'_>,
315316
edit: &mut SourceChangeBuilder,
316317
record_field: ast::RecordExprField,
317318
initializer: ast::Expr,
318319
) {
319320
if let Some(ast::Expr::PathExpr(path_expr)) = record_field.expr() {
320321
// replace field shorthand
321-
edit.insert(
322-
path_expr.syntax().text_range().end(),
323-
format!(": {}", initializer.syntax().text()),
324-
)
322+
let file_range = ctx.sema.original_range(path_expr.syntax());
323+
edit.insert(file_range.range.end(), format!(": {}", initializer.syntax().text()))
325324
} else if let Some(expr) = record_field.expr() {
326325
// just replace expr
327-
edit.replace_ast(expr, initializer);
326+
let file_range = ctx.sema.original_range(expr.syntax());
327+
edit.replace(file_range.range, initializer.syntax().text());
328328
}
329329
}
330330

@@ -838,6 +838,48 @@ fn main() {
838838
)
839839
}
840840

841+
#[test]
842+
fn local_var_init_struct_usage_in_macro() {
843+
check_assist(
844+
bool_to_enum,
845+
r#"
846+
struct Struct {
847+
boolean: bool,
848+
}
849+
850+
macro_rules! identity {
851+
($body:expr) => {
852+
$body
853+
}
854+
}
855+
856+
fn new() -> Struct {
857+
let $0boolean = true;
858+
identity![Struct { boolean }]
859+
}
860+
"#,
861+
r#"
862+
struct Struct {
863+
boolean: bool,
864+
}
865+
866+
macro_rules! identity {
867+
($body:expr) => {
868+
$body
869+
}
870+
}
871+
872+
#[derive(PartialEq, Eq)]
873+
enum Bool { True, False }
874+
875+
fn new() -> Struct {
876+
let boolean = Bool::True;
877+
identity![Struct { boolean: boolean == Bool::True }]
878+
}
879+
"#,
880+
)
881+
}
882+
841883
#[test]
842884
fn field_struct_basic() {
843885
cov_mark::check!(replaces_record_expr);
@@ -1359,6 +1401,46 @@ fn main() {
13591401
)
13601402
}
13611403

1404+
#[test]
1405+
fn field_in_macro() {
1406+
check_assist(
1407+
bool_to_enum,
1408+
r#"
1409+
struct Struct {
1410+
$0boolean: bool,
1411+
}
1412+
1413+
fn boolean(x: Struct) {
1414+
let Struct { boolean } = x;
1415+
}
1416+
1417+
macro_rules! identity { ($body:expr) => { $body } }
1418+
1419+
fn new() -> Struct {
1420+
identity!(Struct { boolean: true })
1421+
}
1422+
"#,
1423+
r#"
1424+
#[derive(PartialEq, Eq)]
1425+
enum Bool { True, False }
1426+
1427+
struct Struct {
1428+
boolean: Bool,
1429+
}
1430+
1431+
fn boolean(x: Struct) {
1432+
let Struct { boolean } = x;
1433+
}
1434+
1435+
macro_rules! identity { ($body:expr) => { $body } }
1436+
1437+
fn new() -> Struct {
1438+
identity!(Struct { boolean: Bool::True })
1439+
}
1440+
"#,
1441+
)
1442+
}
1443+
13621444
#[test]
13631445
fn field_non_bool() {
13641446
cov_mark::check!(not_applicable_non_bool_field);

0 commit comments

Comments
 (0)