@@ -37,6 +37,7 @@ use ide_db::{
37
37
SymbolKind , documentation:: HasDocs , path_transform:: PathTransform ,
38
38
syntax_helpers:: prettify_macro_expansion, traits:: get_missing_assoc_items,
39
39
} ;
40
+ use syntax:: ast:: HasGenericParams ;
40
41
use syntax:: {
41
42
AstNode , SmolStr , SyntaxElement , SyntaxKind , T , TextRange , ToSmolStr ,
42
43
ast:: { self , HasGenericArgs , HasTypeBounds , edit_in_place:: AttrsOwnerEdit , make} ,
@@ -388,6 +389,12 @@ fn add_type_alias_impl(
388
389
{
389
390
end
390
391
} else if let Some ( end) = transformed_ty. eq_token ( ) . map ( |tok| tok. text_range ( ) . start ( ) )
392
+ {
393
+ end
394
+ } else if let Some ( end) = transformed_ty
395
+ . where_clause ( )
396
+ . and_then ( |wc| wc. where_token ( ) )
397
+ . map ( |tok| tok. text_range ( ) . start ( ) )
391
398
{
392
399
end
393
400
} else if let Some ( end) =
@@ -400,17 +407,29 @@ fn add_type_alias_impl(
400
407
401
408
let len = end - start;
402
409
let mut decl = transformed_ty. syntax ( ) . text ( ) . slice ( ..len) . to_string ( ) ;
403
- if !decl. ends_with ( ' ' ) {
404
- decl. push ( ' ' ) ;
405
- }
406
- decl. push_str ( "= " ) ;
410
+ decl. truncate ( decl. trim_end ( ) . len ( ) ) ;
411
+ decl. push_str ( " = " ) ;
412
+
413
+ let wc = transformed_ty
414
+ . where_clause ( )
415
+ . map ( |wc| {
416
+ let ws = wc
417
+ . where_token ( )
418
+ . and_then ( |it| it. prev_token ( ) )
419
+ . filter ( |token| token. kind ( ) == SyntaxKind :: WHITESPACE )
420
+ . map ( |token| token. to_string ( ) )
421
+ . unwrap_or_else ( || " " . into ( ) ) ;
422
+ format ! ( "{ws}{wc}" )
423
+ } )
424
+ . unwrap_or_default ( ) ;
407
425
408
426
match ctx. config . snippet_cap {
409
427
Some ( cap) => {
410
- let snippet = format ! ( "{decl}$0;" ) ;
428
+ let snippet = format ! ( "{decl}$0{wc} ;" ) ;
411
429
item. snippet_edit ( cap, TextEdit :: replace ( replacement_range, snippet) ) ;
412
430
}
413
431
None => {
432
+ decl. push_str ( & wc) ;
414
433
item. text_edit ( TextEdit :: replace ( replacement_range, decl) ) ;
415
434
}
416
435
} ;
@@ -1437,6 +1456,30 @@ trait Tr<'b> {
1437
1456
impl<'b> Tr<'b> for () {
1438
1457
type Ty<'a: 'b, T: Copy, const C: usize> = $0;
1439
1458
}
1459
+ "# ,
1460
+ ) ;
1461
+ }
1462
+ #[ test]
1463
+ fn includes_where_clause ( ) {
1464
+ check_edit (
1465
+ "type Ty" ,
1466
+ r#"
1467
+ trait Tr {
1468
+ type Ty where Self: Copy;
1469
+ }
1470
+
1471
+ impl Tr for () {
1472
+ $0
1473
+ }
1474
+ "# ,
1475
+ r#"
1476
+ trait Tr {
1477
+ type Ty where Self: Copy;
1478
+ }
1479
+
1480
+ impl Tr for () {
1481
+ type Ty = $0 where Self: Copy;
1482
+ }
1440
1483
"# ,
1441
1484
) ;
1442
1485
}
0 commit comments