@@ -23,6 +23,7 @@ use crate::{AssistContext, AssistId, Assists};
2323// ```
2424pub ( crate ) fn add_explicit_type ( acc : & mut Assists , ctx : & AssistContext ) -> Option < ( ) > {
2525 let stmt = ctx. find_node_at_offset :: < LetStmt > ( ) ?;
26+ let module = ctx. sema . scope ( stmt. syntax ( ) ) . module ( ) ?;
2627 let expr = stmt. initializer ( ) ?;
2728 let pat = stmt. pat ( ) ?;
2829 // Must be a binding
@@ -57,17 +58,17 @@ pub(crate) fn add_explicit_type(acc: &mut Assists, ctx: &AssistContext) -> Optio
5758 return None ;
5859 }
5960
60- let db = ctx. db ;
61- let new_type_string = ty. display_truncated ( db, None ) . to_string ( ) ;
61+ let inferred_type = ty. display_source_code ( ctx. db , module. into ( ) ) . ok ( ) ?;
6262 acc. add (
6363 AssistId ( "add_explicit_type" ) ,
64- format ! ( "Insert explicit type '{}'" , new_type_string ) ,
64+ format ! ( "Insert explicit type '{}'" , inferred_type ) ,
6565 pat_range,
66- |edit| {
67- if let Some ( ascribed_ty) = ascribed_ty {
68- edit. replace ( ascribed_ty. syntax ( ) . text_range ( ) , new_type_string) ;
69- } else {
70- edit. insert ( name_range. end ( ) , format ! ( ": {}" , new_type_string) ) ;
66+ |builder| match ascribed_ty {
67+ Some ( ascribed_ty) => {
68+ builder. replace ( ascribed_ty. syntax ( ) . text_range ( ) , inferred_type) ;
69+ }
70+ None => {
71+ builder. insert ( name_range. end ( ) , format ! ( ": {}" , inferred_type) ) ;
7172 }
7273 } ,
7374 )
@@ -208,7 +209,7 @@ struct Test<K, T = u8> {
208209}
209210
210211fn main() {
211- let test<|>: Test<i32> = Test { t: 23, k: 33 };
212+ let test<|>: Test<i32, u8 > = Test { t: 23, k: 33 };
212213}"# ,
213214 ) ;
214215 }
0 commit comments