@@ -169,8 +169,8 @@ fn replace_bool_expr(edit: &mut SourceChangeBuilder, expr: ast::Expr) {
169
169
170
170
/// Converts an expression of type `bool` to one of the new enum type.
171
171
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" ) ) ;
174
174
175
175
if let ast:: Expr :: Literal ( literal) = & expr {
176
176
match literal. kind ( ) {
@@ -184,7 +184,6 @@ fn bool_expr_to_enum_expr(expr: ast::Expr) -> ast::Expr {
184
184
make:: tail_only_block_expr ( true_expr) ,
185
185
Some ( ast:: ElseBranch :: Block ( make:: tail_only_block_expr ( false_expr) ) ) ,
186
186
)
187
- . clone_for_update ( )
188
187
}
189
188
}
190
189
@@ -239,7 +238,7 @@ fn replace_usages(
239
238
cov_mark:: hit!( replaces_record_expr) ;
240
239
241
240
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) ;
243
242
} else if let Some ( pat) = find_record_pat_field_usage ( & name) {
244
243
match pat {
245
244
ast:: Pat :: IdentPat ( ident_pat) => {
@@ -283,6 +282,7 @@ fn replace_usages(
283
282
)
284
283
{
285
284
replace_record_field_expr (
285
+ ctx,
286
286
edit,
287
287
record_field,
288
288
make:: expr_bin_op (
@@ -312,19 +312,19 @@ fn replace_usages(
312
312
313
313
/// Replaces the record expression, handling field shorthands.
314
314
fn replace_record_field_expr (
315
+ ctx : & AssistContext < ' _ > ,
315
316
edit : & mut SourceChangeBuilder ,
316
317
record_field : ast:: RecordExprField ,
317
318
initializer : ast:: Expr ,
318
319
) {
319
320
if let Some ( ast:: Expr :: PathExpr ( path_expr) ) = record_field. expr ( ) {
320
321
// 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( ) ) )
325
324
} else if let Some ( expr) = record_field. expr ( ) {
326
325
// 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 ( ) ) ;
328
328
}
329
329
}
330
330
@@ -838,6 +838,48 @@ fn main() {
838
838
)
839
839
}
840
840
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
+
841
883
#[ test]
842
884
fn field_struct_basic ( ) {
843
885
cov_mark:: check!( replaces_record_expr) ;
@@ -1359,6 +1401,46 @@ fn main() {
1359
1401
)
1360
1402
}
1361
1403
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
+
1362
1444
#[ test]
1363
1445
fn field_non_bool ( ) {
1364
1446
cov_mark:: check!( not_applicable_non_bool_field) ;
0 commit comments