3
3
//! let _: u32 = /* <never-to-any> */ loop {};
4
4
//! let _: &u32 = /* &* */ &mut 0;
5
5
//! ```
6
+ use std:: ops:: Not ;
7
+
6
8
use either:: Either ;
7
9
use hir:: {
8
10
Adjust , Adjustment , AutoBorrow , HirDisplay , Mutability , OverloadedDeref , PointerCast , Safety ,
@@ -15,6 +17,7 @@ use syntax::{
15
17
ast:: { self , make, AstNode } ,
16
18
ted,
17
19
} ;
20
+ use text_edit:: TextEditBuilder ;
18
21
19
22
use crate :: {
20
23
AdjustmentHints , AdjustmentHintsMode , InlayHint , InlayHintLabel , InlayHintLabelPart ,
@@ -51,13 +54,13 @@ pub(super) fn hints(
51
54
let adjustments = sema. expr_adjustments ( desc_expr) . filter ( |it| !it. is_empty ( ) ) ?;
52
55
53
56
if let ast:: Expr :: BlockExpr ( _) | ast:: Expr :: IfExpr ( _) | ast:: Expr :: MatchExpr ( _) = desc_expr {
54
- if let [ Adjustment { kind : Adjust :: Deref ( _ ) , source , .. } , Adjustment { kind : Adjust :: Borrow ( _ ) , source : _ , target } ] =
55
- & * adjustments
56
- {
57
- // Don't show unnecessary reborrows for these, they will just repeat the inner ones again
58
- if source == target {
59
- return None ;
60
- }
57
+ // Don't show unnecessary reborrows for these, they will just repeat the inner ones again
58
+ if matches ! (
59
+ & * adjustments ,
60
+ [ Adjustment { kind : Adjust :: Deref ( _ ) , source , .. } , Adjustment { kind : Adjust :: Borrow ( _ ) , target , .. } ]
61
+ if source == target
62
+ ) {
63
+ return None ;
61
64
}
62
65
}
63
66
@@ -101,6 +104,7 @@ pub(super) fn hints(
101
104
} ;
102
105
let iter: & mut dyn Iterator < Item = _ > = iter. as_mut ( ) . either ( |it| it as _ , |it| it as _ ) ;
103
106
107
+ let mut allow_edit = !postfix;
104
108
for Adjustment { source, target, kind } in iter {
105
109
if source == target {
106
110
cov_mark:: hit!( same_type_adjustment) ;
@@ -110,6 +114,7 @@ pub(super) fn hints(
110
114
// FIXME: Add some nicer tooltips to each of these
111
115
let ( text, coercion) = match kind {
112
116
Adjust :: NeverToAny if config. adjustment_hints == AdjustmentHints :: Always => {
117
+ allow_edit = false ;
113
118
( "<never-to-any>" , "never to any" )
114
119
}
115
120
Adjust :: Deref ( None ) => ( "*" , "dereference" ) ,
@@ -130,6 +135,7 @@ pub(super) fn hints(
130
135
// some of these could be represented via `as` casts, but that's not too nice and
131
136
// handling everything as a prefix expr makes the `(` and `)` insertion easier
132
137
Adjust :: Pointer ( cast) if config. adjustment_hints == AdjustmentHints :: Always => {
138
+ allow_edit = false ;
133
139
match cast {
134
140
PointerCast :: ReifyFnPointer => {
135
141
( "<fn-item-to-fn-pointer>" , "fn item to fn pointer" )
@@ -170,12 +176,41 @@ pub(super) fn hints(
170
176
if needs_outer_parens || ( !postfix && needs_inner_parens) {
171
177
post. label . append_str ( ")" ) ;
172
178
}
173
- if !pre. label . parts . is_empty ( ) {
174
- acc. push ( pre) ;
179
+
180
+ let mut pre = pre. label . parts . is_empty ( ) . not ( ) . then_some ( pre) ;
181
+ let mut post = post. label . parts . is_empty ( ) . not ( ) . then_some ( post) ;
182
+ if pre. is_none ( ) && post. is_none ( ) {
183
+ return None ;
175
184
}
176
- if !post. label . parts . is_empty ( ) {
177
- acc. push ( post) ;
185
+ if allow_edit {
186
+ let edit = {
187
+ let mut b = TextEditBuilder :: default ( ) ;
188
+ if let Some ( pre) = & pre {
189
+ b. insert (
190
+ pre. range . start ( ) ,
191
+ pre. label . parts . iter ( ) . map ( |part| & * part. text ) . collect :: < String > ( ) ,
192
+ ) ;
193
+ }
194
+ if let Some ( post) = & post {
195
+ b. insert (
196
+ post. range . end ( ) ,
197
+ post. label . parts . iter ( ) . map ( |part| & * part. text ) . collect :: < String > ( ) ,
198
+ ) ;
199
+ }
200
+ b. finish ( )
201
+ } ;
202
+ match ( & mut pre, & mut post) {
203
+ ( Some ( pre) , Some ( post) ) => {
204
+ pre. text_edit = Some ( edit. clone ( ) ) ;
205
+ post. text_edit = Some ( edit) ;
206
+ }
207
+ ( Some ( pre) , None ) => pre. text_edit = Some ( edit) ,
208
+ ( None , Some ( post) ) => post. text_edit = Some ( edit) ,
209
+ ( None , None ) => ( ) ,
210
+ }
178
211
}
212
+ acc. extend ( pre) ;
213
+ acc. extend ( post) ;
179
214
Some ( ( ) )
180
215
}
181
216
0 commit comments