Skip to content

Commit fd7236c

Browse files
committed
debug for record field structs
1 parent e2ab2e1 commit fd7236c

File tree

3 files changed

+32
-17
lines changed

3 files changed

+32
-17
lines changed

crates/ide_assists/src/handlers/replace_derive_with_manual_impl.rs

Lines changed: 28 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ use crate::{
3636
//
3737
// impl Debug for S {
3838
// $0fn fmt(&self, f: &mut Formatter) -> Result<()> {
39-
// f.debug_struct(S)
39+
// f.debug_struct("S").finish()
4040
// }
4141
// }
4242
// ```
@@ -183,25 +183,37 @@ fn gen_debug_impl(adt: &ast::Adt, fn_: &ast::Fn, annotated_name: &ast::Name) {
183183
match adt {
184184
ast::Adt::Union(_) => {} // `Debug` cannot be derived for unions, so no default impl can be provided.
185185
ast::Adt::Enum(_) => {} // TODO
186-
ast::Adt::Struct(strukt) => {
187-
match strukt.field_list() {
188-
Some(ast::FieldList::RecordFieldList(field_list)) => {
189-
let name = format!("\"{}\"", annotated_name);
190-
let args = make::arg_list(Some(make::expr_literal(&name).into()));
191-
let target = make::expr_path(make::ext::ident_path("f"));
192-
let mut expr = make::expr_method_call(target, "debug_struct", args);
193-
for field in field_list.fields() {
194-
let args = make::arg_list(Some(make::expr_path(&name).into()));
186+
ast::Adt::Struct(strukt) => match strukt.field_list() {
187+
Some(ast::FieldList::RecordFieldList(field_list)) => {
188+
let name = format!("\"{}\"", annotated_name);
189+
let args = make::arg_list(Some(make::expr_literal(&name).into()));
190+
let target = make::expr_path(make::ext::ident_path("f"));
191+
let mut expr = make::expr_method_call(target, "debug_struct", args);
192+
for field in field_list.fields() {
193+
if let Some(name) = field.name() {
194+
let f_name = make::expr_literal(&(format!("\"{}\"", name))).into();
195+
let f_path = make::expr_path(make::ext::ident_path("self"));
196+
let f_path = make::expr_ref(f_path, false);
197+
let f_path = make::expr_field(f_path, &format!("{}", name)).into();
198+
let args = make::arg_list(vec![f_name, f_path]);
195199
expr = make::expr_method_call(expr, "field", args);
196200
}
197-
let expr = make::expr_method_call(expr, "finish", make::arg_list(None));
198-
let body = make::block_expr(None, Some(expr)).indent(ast::edit::IndentLevel(1));
199-
ted::replace(fn_.body().unwrap().syntax(), body.clone_for_update().syntax());
200201
}
201-
Some(ast::FieldList::TupleFieldList(field_list)) => {}
202-
None => {} // `Debug` cannot be implemented for an incomplete struct.
202+
let expr = make::expr_method_call(expr, "finish", make::arg_list(None));
203+
let body = make::block_expr(None, Some(expr)).indent(ast::edit::IndentLevel(1));
204+
ted::replace(fn_.body().unwrap().syntax(), body.clone_for_update().syntax());
203205
}
204-
}
206+
Some(ast::FieldList::TupleFieldList(field_list)) => {}
207+
None => {
208+
let name = format!("\"{}\"", annotated_name);
209+
let args = make::arg_list(Some(make::expr_literal(&name).into()));
210+
let target = make::expr_path(make::ext::ident_path("f"));
211+
let expr = make::expr_method_call(target, "debug_struct", args);
212+
let expr = make::expr_method_call(expr, "finish", make::arg_list(None));
213+
let body = make::block_expr(None, Some(expr)).indent(ast::edit::IndentLevel(1));
214+
ted::replace(fn_.body().unwrap().syntax(), body.clone_for_update().syntax());
215+
}
216+
},
205217
}
206218
}
207219

crates/ide_assists/src/tests/generated.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1364,7 +1364,7 @@ struct S;
13641364
13651365
impl Debug for S {
13661366
$0fn fmt(&self, f: &mut Formatter) -> Result<()> {
1367-
f.debug_struct(S)
1367+
f.debug_struct("S").finish()
13681368
}
13691369
}
13701370
"#####,

crates/syntax/src/ast/make.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -318,6 +318,9 @@ pub fn expr_closure(pats: impl IntoIterator<Item = ast::Param>, expr: ast::Expr)
318318
let params = pats.into_iter().join(", ");
319319
expr_from_text(&format!("|{}| {}", params, expr))
320320
}
321+
pub fn expr_field(receiver: ast::Expr, field: &str) -> ast::Expr {
322+
expr_from_text(&format!("{}.{}", receiver, field))
323+
}
321324
pub fn expr_paren(expr: ast::Expr) -> ast::Expr {
322325
expr_from_text(&format!("({})", expr))
323326
}

0 commit comments

Comments
 (0)