Unnamed repository; edit this file 'description' to name the repository.
Diffstat (limited to 'crates/ide-diagnostics/src/handlers/no_such_field.rs')
-rw-r--r--crates/ide-diagnostics/src/handlers/no_such_field.rs62
1 files changed, 56 insertions, 6 deletions
diff --git a/crates/ide-diagnostics/src/handlers/no_such_field.rs b/crates/ide-diagnostics/src/handlers/no_such_field.rs
index bcfe3a8aa5..619bb2307c 100644
--- a/crates/ide-diagnostics/src/handlers/no_such_field.rs
+++ b/crates/ide-diagnostics/src/handlers/no_such_field.rs
@@ -97,25 +97,37 @@ fn missing_record_expr_field_fixes(
make::ty(&new_field_type.display_source_code(sema.db, module.into(), true).ok()?),
);
- let last_field = record_fields.fields().last()?;
- let last_field_syntax = last_field.syntax();
- let indent = IndentLevel::from_node(last_field_syntax);
+ let (indent, offset, postfix, needs_comma) =
+ if let Some(last_field) = record_fields.fields().last() {
+ let indent = IndentLevel::from_node(last_field.syntax());
+ let offset = last_field.syntax().text_range().end();
+ let needs_comma = !last_field.to_string().ends_with(',');
+ (indent, offset, String::new(), needs_comma)
+ } else {
+ let indent = IndentLevel::from_node(record_fields.syntax());
+ let offset = record_fields.l_curly_token()?.text_range().end();
+ let postfix = if record_fields.syntax().text().contains_char('\n') {
+ ",".into()
+ } else {
+ format!(",\n{indent}")
+ };
+ (indent + 1, offset, postfix, false)
+ };
let mut new_field = new_field.to_string();
// FIXME: check submodule instead of FileId
if usage_file_id != def_file_id && !matches!(def_id, hir::VariantDef::Variant(_)) {
new_field = format!("pub(crate) {new_field}");
}
- new_field = format!("\n{indent}{new_field}");
+ new_field = format!("\n{indent}{new_field}{postfix}");
- let needs_comma = !last_field_syntax.to_string().ends_with(',');
if needs_comma {
new_field = format!(",{new_field}");
}
let source_change = SourceChange::from_text_edit(
def_file_id.file_id(sema.db),
- TextEdit::insert(last_field_syntax.text_range().end(), new_field),
+ TextEdit::insert(offset, new_field),
);
return Some(vec![fix(
@@ -335,6 +347,44 @@ struct Foo {
}
#[test]
+ fn test_add_field_from_usage_with_empty_struct() {
+ check_fix(
+ r"
+fn main() {
+ Foo { bar$0: false };
+}
+struct Foo {}
+",
+ r"
+fn main() {
+ Foo { bar: false };
+}
+struct Foo {
+ bar: bool,
+}
+",
+ );
+
+ check_fix(
+ r"
+fn main() {
+ Foo { bar$0: false };
+}
+struct Foo {
+}
+",
+ r"
+fn main() {
+ Foo { bar: false };
+}
+struct Foo {
+ bar: bool,
+}
+",
+ );
+ }
+
+ #[test]
fn test_add_field_in_other_file_from_usage() {
check_fix(
r#"