Skip to content

Commit aa09141

Browse files
committed
finish debug_struct impls
1 parent fd7236c commit aa09141

File tree

1 file changed

+91
-3
lines changed

1 file changed

+91
-3
lines changed

crates/ide_assists/src/handlers/replace_derive_with_manual_impl.rs

Lines changed: 91 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ use ide_db::helpers::{import_assets::NameToImport, mod_path_to_ast};
33
use ide_db::items_locator;
44
use itertools::Itertools;
55
use syntax::ast::edit::AstNodeEdit;
6-
use syntax::ast::Pat;
76
use syntax::ted;
87
use syntax::{
98
ast::{self, make, AstNode, NameOwner},
@@ -203,7 +202,22 @@ fn gen_debug_impl(adt: &ast::Adt, fn_: &ast::Fn, annotated_name: &ast::Name) {
203202
let body = make::block_expr(None, Some(expr)).indent(ast::edit::IndentLevel(1));
204203
ted::replace(fn_.body().unwrap().syntax(), body.clone_for_update().syntax());
205204
}
206-
Some(ast::FieldList::TupleFieldList(field_list)) => {}
205+
Some(ast::FieldList::TupleFieldList(field_list)) => {
206+
let name = format!("\"{}\"", annotated_name);
207+
let args = make::arg_list(Some(make::expr_literal(&name).into()));
208+
let target = make::expr_path(make::ext::ident_path("f"));
209+
let mut expr = make::expr_method_call(target, "debug_tuple", args);
210+
for (idx, _) in field_list.fields().enumerate() {
211+
let f_path = make::expr_path(make::ext::ident_path("self"));
212+
let f_path = make::expr_ref(f_path, false);
213+
let f_path = make::expr_field(f_path, &format!("{}", idx)).into();
214+
let args = make::arg_list(Some(f_path));
215+
expr = make::expr_method_call(expr, "field", args);
216+
}
217+
let expr = make::expr_method_call(expr, "finish", make::arg_list(None));
218+
let body = make::block_expr(None, Some(expr)).indent(ast::edit::IndentLevel(1));
219+
ted::replace(fn_.body().unwrap().syntax(), body.clone_for_update().syntax());
220+
}
207221
None => {
208222
let name = format!("\"{}\"", annotated_name);
209223
let args = make::arg_list(Some(make::expr_literal(&name).into()));
@@ -258,7 +272,7 @@ mod tests {
258272
use super::*;
259273

260274
#[test]
261-
fn add_custom_impl_debug() {
275+
fn add_custom_impl_debug_record_struct() {
262276
check_assist(
263277
replace_derive_with_manual_impl,
264278
r#"
@@ -295,6 +309,80 @@ impl fmt::Debug for Foo {
295309
f.debug_struct("Foo").field("bar", &self.bar).finish()
296310
}
297311
}
312+
"#,
313+
)
314+
}
315+
#[test]
316+
fn add_custom_impl_debug_tuple_struct() {
317+
check_assist(
318+
replace_derive_with_manual_impl,
319+
r#"
320+
mod fmt {
321+
pub struct Error;
322+
pub type Result = Result<(), Error>;
323+
pub struct Formatter<'a>;
324+
pub trait Debug {
325+
fn fmt(&self, f: &mut Formatter<'_>) -> Result;
326+
}
327+
}
328+
329+
#[derive(Debu$0g)]
330+
struct Foo(String, usize);
331+
"#,
332+
r#"
333+
mod fmt {
334+
pub struct Error;
335+
pub type Result = Result<(), Error>;
336+
pub struct Formatter<'a>;
337+
pub trait Debug {
338+
fn fmt(&self, f: &mut Formatter<'_>) -> Result;
339+
}
340+
}
341+
342+
struct Foo(String, usize);
343+
344+
impl fmt::Debug for Foo {
345+
$0fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
346+
f.debug_tuple("Foo").field(&self.0).field(&self.1).finish()
347+
}
348+
}
349+
"#,
350+
)
351+
}
352+
#[test]
353+
fn add_custom_impl_debug_empty_struct() {
354+
check_assist(
355+
replace_derive_with_manual_impl,
356+
r#"
357+
mod fmt {
358+
pub struct Error;
359+
pub type Result = Result<(), Error>;
360+
pub struct Formatter<'a>;
361+
pub trait Debug {
362+
fn fmt(&self, f: &mut Formatter<'_>) -> Result;
363+
}
364+
}
365+
366+
#[derive(Debu$0g)]
367+
struct Foo;
368+
"#,
369+
r#"
370+
mod fmt {
371+
pub struct Error;
372+
pub type Result = Result<(), Error>;
373+
pub struct Formatter<'a>;
374+
pub trait Debug {
375+
fn fmt(&self, f: &mut Formatter<'_>) -> Result;
376+
}
377+
}
378+
379+
struct Foo;
380+
381+
impl fmt::Debug for Foo {
382+
$0fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
383+
f.debug_struct("Foo").finish()
384+
}
385+
}
298386
"#,
299387
)
300388
}

0 commit comments

Comments
 (0)