@@ -23,6 +23,7 @@ use crate::{AssistContext, AssistId, Assists};
23
23
// ```
24
24
pub ( crate ) fn add_explicit_type ( acc : & mut Assists , ctx : & AssistContext ) -> Option < ( ) > {
25
25
let stmt = ctx. find_node_at_offset :: < LetStmt > ( ) ?;
26
+ let module = ctx. sema . scope ( stmt. syntax ( ) ) . module ( ) ?;
26
27
let expr = stmt. initializer ( ) ?;
27
28
let pat = stmt. pat ( ) ?;
28
29
// Must be a binding
@@ -57,17 +58,17 @@ pub(crate) fn add_explicit_type(acc: &mut Assists, ctx: &AssistContext) -> Optio
57
58
return None ;
58
59
}
59
60
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 ( ) ?;
62
62
acc. add (
63
63
AssistId ( "add_explicit_type" ) ,
64
- format ! ( "Insert explicit type '{}'" , new_type_string ) ,
64
+ format ! ( "Insert explicit type '{}'" , inferred_type ) ,
65
65
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) ) ;
71
72
}
72
73
} ,
73
74
)
@@ -208,7 +209,7 @@ struct Test<K, T = u8> {
208
209
}
209
210
210
211
fn main() {
211
- let test<|>: Test<i32> = Test { t: 23, k: 33 };
212
+ let test<|>: Test<i32, u8 > = Test { t: 23, k: 33 };
212
213
}"# ,
213
214
) ;
214
215
}
0 commit comments