Skip to content

Commit 6c7526d

Browse files
bors[bot]Veykril
andauthored
Merge #10552
10552: fix: Fix missing_fields diagnostic fix replacing wrong text ranges r=Veykril a=Veykril Fixes #5393 by replacing the problematic behaviour there with a new "problem" It replaces the correct range now, but it potentially discards the whitespace in the macro input. This is the best we can do currently until we get a formatter. bors r+ Co-authored-by: Lukas Wirth <[email protected]>
2 parents c67db1b + ac505b2 commit 6c7526d

File tree

1 file changed

+15
-7
lines changed

1 file changed

+15
-7
lines changed

crates/ide_diagnostics/src/handlers/missing_fields.rs

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ fn fixes(ctx: &DiagnosticsContext<'_>, d: &hir::MissingFields) -> Option<Vec<Ass
5454
Either::Right(_) => return None,
5555
};
5656
let old_field_list = field_list_parent.record_expr_field_list()?;
57+
5758
let new_field_list = old_field_list.clone_for_update();
5859
let mut locals = FxHashMap::default();
5960
ctx.sema.scope(field_list_parent.syntax()).process_all_names(&mut |name, def| {
@@ -80,11 +81,19 @@ fn fixes(ctx: &DiagnosticsContext<'_>, d: &hir::MissingFields) -> Option<Vec<Ass
8081
new_field_list.add_field(field);
8182
}
8283

83-
let edit = {
84-
let mut builder = TextEdit::builder();
84+
let mut builder = TextEdit::builder();
85+
if d.file.is_macro() {
86+
// we can't map the diff up into the macro input unfortunately, as the macro loses all
87+
// whitespace information so the diff wouldn't be applicable no matter what
88+
// This has the downside that the cursor will be moved in macros by doing it without a diff
89+
// but that is a trade off we can make.
90+
// FIXE: this also currently discards a lot of whitespace in the input... we really need a formatter here
91+
let range = ctx.sema.original_range_opt(old_field_list.syntax())?;
92+
builder.replace(range.range, new_field_list.to_string());
93+
} else {
8594
algo::diff(old_field_list.syntax(), new_field_list.syntax()).into_text_edit(&mut builder);
86-
builder.finish()
87-
};
95+
}
96+
let edit = builder.finish();
8897
Some(vec![fix(
8998
"fill_missing_fields",
9099
"Fill struct fields",
@@ -151,7 +160,6 @@ fn x(a: S) {
151160

152161
#[test]
153162
fn range_mapping_out_of_macros() {
154-
// FIXME: this is very wrong, but somewhat tricky to fix.
155163
check_fix(
156164
r#"
157165
fn some() {}
@@ -167,14 +175,14 @@ fn main() {
167175
pub struct Foo { pub a: i32, pub b: i32 }
168176
"#,
169177
r#"
170-
fn some(, b: todo!() ) {}
178+
fn some() {}
171179
fn items() {}
172180
fn here() {}
173181
174182
macro_rules! id { ($($tt:tt)*) => { $($tt)*}; }
175183
176184
fn main() {
177-
let _x = id![Foo { a: 42 }];
185+
let _x = id![Foo {a:42, b: todo!() }];
178186
}
179187
180188
pub struct Foo { pub a: i32, pub b: i32 }

0 commit comments

Comments
 (0)