@@ -3,7 +3,6 @@ use ide_db::helpers::{import_assets::NameToImport, mod_path_to_ast};
3
3
use ide_db:: items_locator;
4
4
use itertools:: Itertools ;
5
5
use syntax:: ast:: edit:: AstNodeEdit ;
6
- use syntax:: ast:: Pat ;
7
6
use syntax:: ted;
8
7
use syntax:: {
9
8
ast:: { self , make, AstNode , NameOwner } ,
@@ -203,7 +202,22 @@ fn gen_debug_impl(adt: &ast::Adt, fn_: &ast::Fn, annotated_name: &ast::Name) {
203
202
let body = make:: block_expr ( None , Some ( expr) ) . indent ( ast:: edit:: IndentLevel ( 1 ) ) ;
204
203
ted:: replace ( fn_. body ( ) . unwrap ( ) . syntax ( ) , body. clone_for_update ( ) . syntax ( ) ) ;
205
204
}
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
+ }
207
221
None => {
208
222
let name = format ! ( "\" {}\" " , annotated_name) ;
209
223
let args = make:: arg_list ( Some ( make:: expr_literal ( & name) . into ( ) ) ) ;
@@ -258,7 +272,7 @@ mod tests {
258
272
use super :: * ;
259
273
260
274
#[ test]
261
- fn add_custom_impl_debug ( ) {
275
+ fn add_custom_impl_debug_record_struct ( ) {
262
276
check_assist (
263
277
replace_derive_with_manual_impl,
264
278
r#"
@@ -295,6 +309,80 @@ impl fmt::Debug for Foo {
295
309
f.debug_struct("Foo").field("bar", &self.bar).finish()
296
310
}
297
311
}
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
+ }
298
386
"# ,
299
387
)
300
388
}
0 commit comments