Skip to content

Commit 90a5c46

Browse files
Merge #4851
4851: Add quickfix to add a struct field r=TimoFreiberg a=TimoFreiberg Related to #4563 I created a quickfix for record literals first because the NoSuchField diagnostic was already there. To offer that quickfix for FieldExprs with unknown fields I'd need to add a new diagnostic (or create a `NoSuchField` diagnostic for those cases) I think it'd make sense to make this a snippet completion (to select the generated type), but this would require changing the `Analysis` API and I'd like some feedback before I touch that. Co-authored-by: Timo Freiberg <timo.freiberg@gmail.com>
2 parents ec6df5d + f5ac313 commit 90a5c46

File tree

5 files changed

+134
-5
lines changed

5 files changed

+134
-5
lines changed

crates/ra_hir/src/semantics.rs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@ use std::{cell::RefCell, fmt, iter::successors};
66

77
use hir_def::{
88
resolver::{self, HasResolver, Resolver},
9-
AsMacroCall, TraitId,
9+
AsMacroCall, TraitId, VariantId,
1010
};
11-
use hir_expand::{hygiene::Hygiene, ExpansionInfo};
11+
use hir_expand::{diagnostics::AstDiagnostic, hygiene::Hygiene, ExpansionInfo};
1212
use hir_ty::associated_type_shorthand_candidates;
1313
use itertools::Itertools;
1414
use ra_db::{FileId, FileRange};
@@ -104,6 +104,13 @@ impl<'db, DB: HirDatabase> Semantics<'db, DB> {
104104
tree
105105
}
106106

107+
pub fn ast<T: AstDiagnostic + Diagnostic>(&self, d: &T) -> <T as AstDiagnostic>::AST {
108+
let file_id = d.source().file_id;
109+
let root = self.db.parse_or_expand(file_id).unwrap();
110+
self.cache(root, file_id);
111+
d.ast(self.db)
112+
}
113+
107114
pub fn expand(&self, macro_call: &ast::MacroCall) -> Option<SyntaxNode> {
108115
let macro_call = self.find_file(macro_call.syntax().clone()).with_value(macro_call);
109116
let sa = self.analyze2(macro_call.map(|it| it.syntax()), None);
@@ -247,6 +254,10 @@ impl<'db, DB: HirDatabase> Semantics<'db, DB> {
247254
self.analyze(path.syntax()).resolve_path(self.db, path)
248255
}
249256

257+
pub fn resolve_variant(&self, record_lit: ast::RecordLit) -> Option<VariantId> {
258+
self.analyze(record_lit.syntax()).resolve_variant(self.db, record_lit)
259+
}
260+
250261
pub fn lower_path(&self, path: &ast::Path) -> Option<Path> {
251262
let src = self.find_file(path.syntax().clone());
252263
Path::from_src(path.clone(), &Hygiene::new(self.db.upcast(), src.file_id.into()))

crates/ra_hir/src/source_analyzer.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -313,6 +313,16 @@ impl SourceAnalyzer {
313313
})?;
314314
Some(macro_call_id.as_file())
315315
}
316+
317+
pub(crate) fn resolve_variant(
318+
&self,
319+
db: &dyn HirDatabase,
320+
record_lit: ast::RecordLit,
321+
) -> Option<VariantId> {
322+
let infer = self.infer.as_ref()?;
323+
let expr_id = self.expr_id(db, &record_lit.into())?;
324+
infer.variant_resolution_for_expr(expr_id)
325+
}
316326
}
317327

318328
fn scope_for(

crates/ra_hir_ty/src/diagnostics.rs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use hir_expand::{db::AstDatabase, name::Name, HirFileId, InFile};
66
use ra_syntax::{ast, AstNode, AstPtr, SyntaxNodePtr};
77
use stdx::format_to;
88

9-
pub use hir_def::{diagnostics::UnresolvedModule, expr::MatchArm};
9+
pub use hir_def::{diagnostics::UnresolvedModule, expr::MatchArm, path::Path};
1010
pub use hir_expand::diagnostics::{AstDiagnostic, Diagnostic, DiagnosticSink};
1111

1212
#[derive(Debug)]
@@ -29,6 +29,16 @@ impl Diagnostic for NoSuchField {
2929
}
3030
}
3131

32+
impl AstDiagnostic for NoSuchField {
33+
type AST = ast::RecordField;
34+
35+
fn ast(&self, db: &impl AstDatabase) -> Self::AST {
36+
let root = db.parse_or_expand(self.source().file_id).unwrap();
37+
let node = self.source().value.to_node(&root);
38+
ast::RecordField::cast(node).unwrap()
39+
}
40+
}
41+
3242
#[derive(Debug)]
3343
pub struct MissingFields {
3444
pub file: HirFileId,

crates/ra_ide/src/diagnostics.rs

Lines changed: 96 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,15 @@ use std::cell::RefCell;
88

99
use hir::{
1010
diagnostics::{AstDiagnostic, Diagnostic as _, DiagnosticSink},
11-
Semantics,
11+
HasSource, HirDisplay, Semantics, VariantDef,
1212
};
1313
use itertools::Itertools;
1414
use ra_db::SourceDatabase;
1515
use ra_ide_db::RootDatabase;
1616
use ra_prof::profile;
1717
use ra_syntax::{
1818
algo,
19-
ast::{self, make, AstNode},
19+
ast::{self, edit::IndentLevel, make, AstNode},
2020
SyntaxNode, TextRange, T,
2121
};
2222
use ra_text_edit::{TextEdit, TextEditBuilder};
@@ -119,14 +119,85 @@ pub(crate) fn diagnostics(db: &RootDatabase, file_id: FileId) -> Vec<Diagnostic>
119119
severity: Severity::Error,
120120
fix: Some(fix),
121121
})
122+
})
123+
.on::<hir::diagnostics::NoSuchField, _>(|d| {
124+
res.borrow_mut().push(Diagnostic {
125+
range: sema.diagnostics_range(d).range,
126+
message: d.message(),
127+
severity: Severity::Error,
128+
fix: missing_struct_field_fix(&sema, file_id, d),
129+
})
122130
});
131+
123132
if let Some(m) = sema.to_module_def(file_id) {
124133
m.diagnostics(db, &mut sink);
125134
};
126135
drop(sink);
127136
res.into_inner()
128137
}
129138

139+
fn missing_struct_field_fix(
140+
sema: &Semantics<RootDatabase>,
141+
file_id: FileId,
142+
d: &hir::diagnostics::NoSuchField,
143+
) -> Option<Fix> {
144+
let record_expr = sema.ast(d);
145+
146+
let record_lit = ast::RecordLit::cast(record_expr.syntax().parent()?.parent()?)?;
147+
let def_id = sema.resolve_variant(record_lit)?;
148+
let module;
149+
let record_fields = match VariantDef::from(def_id) {
150+
VariantDef::Struct(s) => {
151+
module = s.module(sema.db);
152+
let source = s.source(sema.db);
153+
let fields = source.value.field_def_list()?;
154+
record_field_def_list(fields)?
155+
}
156+
VariantDef::Union(u) => {
157+
module = u.module(sema.db);
158+
let source = u.source(sema.db);
159+
source.value.record_field_def_list()?
160+
}
161+
VariantDef::EnumVariant(e) => {
162+
module = e.module(sema.db);
163+
let source = e.source(sema.db);
164+
let fields = source.value.field_def_list()?;
165+
record_field_def_list(fields)?
166+
}
167+
};
168+
169+
let new_field_type = sema.type_of_expr(&record_expr.expr()?)?;
170+
let new_field = make::record_field_def(
171+
record_expr.field_name()?,
172+
make::type_ref(&new_field_type.display_source_code(sema.db, module.into()).ok()?),
173+
);
174+
175+
let last_field = record_fields.fields().last()?;
176+
let last_field_syntax = last_field.syntax();
177+
let indent = IndentLevel::from_node(last_field_syntax);
178+
179+
let mut new_field = format!("\n{}{}", indent, new_field);
180+
181+
let needs_comma = !last_field_syntax.to_string().ends_with(",");
182+
if needs_comma {
183+
new_field = format!(",{}", new_field);
184+
}
185+
186+
let source_change = SourceFileEdit {
187+
file_id,
188+
edit: TextEdit::insert(last_field_syntax.text_range().end(), new_field),
189+
};
190+
let fix = Fix::new("Create field", source_change.into());
191+
return Some(fix);
192+
193+
fn record_field_def_list(field_def_list: ast::FieldDefList) -> Option<ast::RecordFieldDefList> {
194+
match field_def_list {
195+
ast::FieldDefList::RecordFieldDefList(it) => Some(it),
196+
ast::FieldDefList::TupleFieldDefList(_) => None,
197+
}
198+
}
199+
}
200+
130201
fn check_unnecessary_braces_in_use_statement(
131202
acc: &mut Vec<Diagnostic>,
132203
file_id: FileId,
@@ -791,4 +862,27 @@ fn main() {
791862
check_struct_shorthand_initialization,
792863
);
793864
}
865+
866+
#[test]
867+
fn test_add_field_from_usage() {
868+
check_apply_diagnostic_fix(
869+
r"
870+
fn main() {
871+
Foo { bar: 3, baz: false};
872+
}
873+
struct Foo {
874+
bar: i32
875+
}
876+
",
877+
r"
878+
fn main() {
879+
Foo { bar: 3, baz: false};
880+
}
881+
struct Foo {
882+
bar: i32,
883+
baz: bool
884+
}
885+
",
886+
)
887+
}
794888
}

crates/ra_syntax/src/ast/make.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,10 @@ pub fn record_field(name: ast::NameRef, expr: Option<ast::Expr>) -> ast::RecordF
7575
}
7676
}
7777

78+
pub fn record_field_def(name: ast::NameRef, ty: ast::TypeRef) -> ast::RecordFieldDef {
79+
ast_from_text(&format!("struct S {{ {}: {}, }}", name, ty))
80+
}
81+
7882
pub fn block_expr(
7983
stmts: impl IntoIterator<Item = ast::Stmt>,
8084
tail_expr: Option<ast::Expr>,

0 commit comments

Comments
 (0)