Skip to content

Commit b105e9b

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

File tree

3 files changed

+104
-30
lines changed

3 files changed

+104
-30
lines changed

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

Lines changed: 6 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,10 @@ use syntax::{
2020
};
2121
use text_edit::TextRange;
2222

23-
use crate::assist_context::{AssistContext, Assists};
23+
use crate::{
24+
assist_context::{AssistContext, Assists},
25+
utils,
26+
};
2427

2528
// Assist: bool_to_enum
2629
//
@@ -238,7 +241,7 @@ fn replace_usages(
238241
cov_mark::hit!(replaces_record_expr);
239242

240243
let enum_expr = bool_expr_to_enum_expr(initializer);
241-
replace_record_field_expr(ctx, edit, record_field, enum_expr);
244+
utils::replace_record_field_expr(ctx, edit, record_field, enum_expr);
242245
} else if let Some(pat) = find_record_pat_field_usage(&name) {
243246
match pat {
244247
ast::Pat::IdentPat(ident_pat) => {
@@ -281,7 +284,7 @@ fn replace_usages(
281284
|record_field| record_field.expr().map(|expr| (record_field, expr)),
282285
)
283286
{
284-
replace_record_field_expr(
287+
utils::replace_record_field_expr(
285288
ctx,
286289
edit,
287290
record_field,
@@ -310,24 +313,6 @@ fn replace_usages(
310313
}
311314
}
312315

313-
/// Replaces the record expression, handling field shorthands.
314-
fn replace_record_field_expr(
315-
ctx: &AssistContext<'_>,
316-
edit: &mut SourceChangeBuilder,
317-
record_field: ast::RecordExprField,
318-
initializer: ast::Expr,
319-
) {
320-
if let Some(ast::Expr::PathExpr(path_expr)) = record_field.expr() {
321-
// replace field shorthand
322-
let file_range = ctx.sema.original_range(path_expr.syntax());
323-
edit.insert(file_range.range.end(), format!(": {}", initializer.syntax().text()))
324-
} else if let Some(expr) = record_field.expr() {
325-
// just replace expr
326-
let file_range = ctx.sema.original_range(expr.syntax());
327-
edit.replace(file_range.range, initializer.syntax().text());
328-
}
329-
}
330-
331316
struct FileReferenceWithImport {
332317
range: TextRange,
333318
name: ast::NameLike,

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

Lines changed: 80 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,10 @@ use syntax::{
1111
ted, AstNode, WalkEvent,
1212
};
1313

14-
use crate::assist_context::{AssistContext, Assists};
14+
use crate::{
15+
assist_context::{AssistContext, Assists},
16+
utils,
17+
};
1518

1619
// Assist: promote_local_to_const
1720
//
@@ -79,15 +82,13 @@ pub(crate) fn promote_local_to_const(acc: &mut Assists, ctx: &AssistContext<'_>)
7982
let name_ref = make::name_ref(&name);
8083

8184
for usage in usages {
82-
let Some(usage) = usage.name.as_name_ref().cloned() else { continue };
83-
if let Some(record_field) = ast::RecordExprField::for_name_ref(&usage) {
84-
let record_field = edit.make_mut(record_field);
85-
let name_expr =
86-
make::expr_path(make::path_from_text(&name)).clone_for_update();
87-
record_field.replace_expr(name_expr);
85+
let Some(usage_name) = usage.name.as_name_ref().cloned() else { continue };
86+
if let Some(record_field) = ast::RecordExprField::for_name_ref(&usage_name) {
87+
let name_expr = make::expr_path(make::path_from_text(&name));
88+
utils::replace_record_field_expr(ctx, edit, record_field, name_expr);
8889
} else {
89-
let usage = edit.make_mut(usage);
90-
ted::replace(usage.syntax(), name_ref.clone_for_update().syntax());
90+
let usage_range = usage.range;
91+
edit.replace(usage_range, name_ref.syntax().text());
9192
}
9293
}
9394
}
@@ -212,6 +213,76 @@ fn main() {
212213
)
213214
}
214215

216+
#[test]
217+
fn usage_in_macro() {
218+
check_assist(
219+
promote_local_to_const,
220+
r"
221+
macro_rules! identity {
222+
($body:expr) => {
223+
$body
224+
}
225+
}
226+
227+
fn baz() -> usize {
228+
let $0foo = 2;
229+
identity![foo]
230+
}
231+
",
232+
r"
233+
macro_rules! identity {
234+
($body:expr) => {
235+
$body
236+
}
237+
}
238+
239+
fn baz() -> usize {
240+
const $0FOO: usize = 2;
241+
identity![FOO]
242+
}
243+
",
244+
)
245+
}
246+
247+
#[test]
248+
fn usage_shorthand_in_macro() {
249+
check_assist(
250+
promote_local_to_const,
251+
r"
252+
struct Foo {
253+
foo: usize,
254+
}
255+
256+
macro_rules! identity {
257+
($body:expr) => {
258+
$body
259+
};
260+
}
261+
262+
fn baz() -> Foo {
263+
let $0foo = 2;
264+
identity![Foo { foo }]
265+
}
266+
",
267+
r"
268+
struct Foo {
269+
foo: usize,
270+
}
271+
272+
macro_rules! identity {
273+
($body:expr) => {
274+
$body
275+
};
276+
}
277+
278+
fn baz() -> Foo {
279+
const $0FOO: usize = 2;
280+
identity![Foo { foo: FOO }]
281+
}
282+
",
283+
)
284+
}
285+
215286
#[test]
216287
fn not_applicable_non_const_meth_call() {
217288
cov_mark::check!(promote_local_non_const);

crates/ide-assists/src/utils.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -813,3 +813,21 @@ fn test_required_hashes() {
813813
assert_eq!(3, required_hashes("#ab\"##c"));
814814
assert_eq!(5, required_hashes("#ab\"##\"####c"));
815815
}
816+
817+
/// Replaces the record expression, handling field shorthands including inside macros.
818+
pub(crate) fn replace_record_field_expr(
819+
ctx: &AssistContext<'_>,
820+
edit: &mut SourceChangeBuilder,
821+
record_field: ast::RecordExprField,
822+
initializer: ast::Expr,
823+
) {
824+
if let Some(ast::Expr::PathExpr(path_expr)) = record_field.expr() {
825+
// replace field shorthand
826+
let file_range = ctx.sema.original_range(path_expr.syntax());
827+
edit.insert(file_range.range.end(), format!(": {}", initializer.syntax().text()))
828+
} else if let Some(expr) = record_field.expr() {
829+
// just replace expr
830+
let file_range = ctx.sema.original_range(expr.syntax());
831+
edit.replace(file_range.range, initializer.syntax().text());
832+
}
833+
}

0 commit comments

Comments
 (0)