@@ -11,7 +11,7 @@ use syntax::{
11
11
ted,
12
12
} ;
13
13
14
- use crate :: { AdjustmentHints , InlayHint , InlayHintsConfig , InlayKind } ;
14
+ use crate :: { AdjustmentHints , AdjustmentHintsMode , InlayHint , InlayHintsConfig , InlayKind } ;
15
15
16
16
pub ( super ) fn hints (
17
17
acc : & mut Vec < InlayHint > ,
@@ -40,8 +40,8 @@ pub(super) fn hints(
40
40
let desc_expr = descended. as_ref ( ) . unwrap_or ( expr) ;
41
41
let adjustments = sema. expr_adjustments ( desc_expr) . filter ( |it| !it. is_empty ( ) ) ?;
42
42
43
- let ( needs_outer_parens, needs_inner_parens) =
44
- needs_parens_for_adjustment_hints ( expr, config. adjustment_hints_postfix ) ;
43
+ let ( postfix , needs_outer_parens, needs_inner_parens) =
44
+ mode_and_needs_parens_for_adjustment_hints ( expr, config. adjustment_hints_mode ) ;
45
45
46
46
if needs_outer_parens {
47
47
acc. push ( InlayHint {
@@ -52,7 +52,7 @@ pub(super) fn hints(
52
52
} ) ;
53
53
}
54
54
55
- if config . adjustment_hints_postfix && needs_inner_parens {
55
+ if postfix && needs_inner_parens {
56
56
acc. push ( InlayHint {
57
57
range : expr. syntax ( ) . text_range ( ) ,
58
58
kind : InlayKind :: OpeningParenthesis ,
@@ -68,7 +68,7 @@ pub(super) fn hints(
68
68
}
69
69
70
70
let ( mut tmp0, mut tmp1) ;
71
- let iter: & mut dyn Iterator < Item = _ > = if config . adjustment_hints_postfix {
71
+ let iter: & mut dyn Iterator < Item = _ > = if postfix {
72
72
tmp0 = adjustments. into_iter ( ) ;
73
73
& mut tmp0
74
74
} else {
@@ -112,20 +112,16 @@ pub(super) fn hints(
112
112
} ;
113
113
acc. push ( InlayHint {
114
114
range : expr. syntax ( ) . text_range ( ) ,
115
- kind : if config . adjustment_hints_postfix {
115
+ kind : if postfix {
116
116
InlayKind :: AdjustmentHintPostfix
117
117
} else {
118
118
InlayKind :: AdjustmentHint
119
119
} ,
120
- label : if config. adjustment_hints_postfix {
121
- format ! ( ".{}" , text. trim_end( ) ) . into ( )
122
- } else {
123
- text. into ( )
124
- } ,
120
+ label : if postfix { format ! ( ".{}" , text. trim_end( ) ) . into ( ) } else { text. into ( ) } ,
125
121
tooltip : None ,
126
122
} ) ;
127
123
}
128
- if !config . adjustment_hints_postfix && needs_inner_parens {
124
+ if !postfix && needs_inner_parens {
129
125
acc. push ( InlayHint {
130
126
range : expr. syntax ( ) . text_range ( ) ,
131
127
kind : InlayKind :: OpeningParenthesis ,
@@ -150,6 +146,41 @@ pub(super) fn hints(
150
146
Some ( ( ) )
151
147
}
152
148
149
+ /// Returns whatever the hint should be postfix and if we need to add paretheses on the inside and/or outside of `expr`,
150
+ /// if we are going to add (`postfix`) adjustments hints to it.
151
+ fn mode_and_needs_parens_for_adjustment_hints (
152
+ expr : & ast:: Expr ,
153
+ mode : AdjustmentHintsMode ,
154
+ ) -> ( bool , bool , bool ) {
155
+ use { std:: cmp:: Ordering :: * , AdjustmentHintsMode :: * } ;
156
+
157
+ match mode {
158
+ Prefix | Postfix => {
159
+ let postfix = matches ! ( mode, Postfix ) ;
160
+ let ( inside, outside) = needs_parens_for_adjustment_hints ( expr, postfix) ;
161
+ ( postfix, inside, outside)
162
+ }
163
+ PreferPrefix | PreferPostfix => {
164
+ let prefer_postfix = matches ! ( mode, PreferPostfix ) ;
165
+
166
+ let ( pre_inside, pre_outside) = needs_parens_for_adjustment_hints ( expr, false ) ;
167
+ let prefix = ( false , pre_inside, pre_outside) ;
168
+ let pre_count = pre_inside as u8 + pre_outside as u8 ;
169
+
170
+ let ( post_inside, post_outside) = needs_parens_for_adjustment_hints ( expr, true ) ;
171
+ let postfix = ( true , post_inside, post_outside) ;
172
+ let post_count = post_inside as u8 + post_outside as u8 ;
173
+
174
+ match pre_count. cmp ( & post_count) {
175
+ Less => prefix,
176
+ Greater => postfix,
177
+ Equal if prefer_postfix => postfix,
178
+ Equal => prefix,
179
+ }
180
+ }
181
+ }
182
+ }
183
+
153
184
/// Returns whatever we need to add paretheses on the inside and/or outside of `expr`,
154
185
/// if we are going to add (`postfix`) adjustments hints to it.
155
186
fn needs_parens_for_adjustment_hints ( expr : & ast:: Expr , postfix : bool ) -> ( bool , bool ) {
@@ -217,7 +248,7 @@ fn needs_parens_for_adjustment_hints(expr: &ast::Expr, postfix: bool) -> (bool,
217
248
mod tests {
218
249
use crate :: {
219
250
inlay_hints:: tests:: { check_with_config, DISABLED_CONFIG } ,
220
- AdjustmentHints , InlayHintsConfig ,
251
+ AdjustmentHints , AdjustmentHintsMode , InlayHintsConfig ,
221
252
} ;
222
253
223
254
#[ test]
@@ -333,7 +364,7 @@ impl Struct {
333
364
check_with_config (
334
365
InlayHintsConfig {
335
366
adjustment_hints : AdjustmentHints :: Always ,
336
- adjustment_hints_postfix : true ,
367
+ adjustment_hints_mode : AdjustmentHintsMode :: Postfix ,
337
368
..DISABLED_CONFIG
338
369
} ,
339
370
r#"
@@ -419,6 +450,58 @@ impl Struct {
419
450
) ;
420
451
}
421
452
453
+ #[ test]
454
+ fn adjustment_hints_prefer_prefix ( ) {
455
+ check_with_config (
456
+ InlayHintsConfig {
457
+ adjustment_hints : AdjustmentHints :: Always ,
458
+ adjustment_hints_mode : AdjustmentHintsMode :: PreferPrefix ,
459
+ ..DISABLED_CONFIG
460
+ } ,
461
+ r#"
462
+ fn main() {
463
+ let _: u32 = loop {};
464
+ //^^^^^^^<never-to-any>
465
+
466
+ Struct.by_ref();
467
+ //^^^^^^.&
468
+
469
+ let (): () = return ();
470
+ //^^^^^^^^^<never-to-any>
471
+
472
+ struct Struct;
473
+ impl Struct { fn by_ref(&self) {} }
474
+ }
475
+ "# ,
476
+ )
477
+ }
478
+
479
+ #[ test]
480
+ fn adjustment_hints_prefer_postfix ( ) {
481
+ check_with_config (
482
+ InlayHintsConfig {
483
+ adjustment_hints : AdjustmentHints :: Always ,
484
+ adjustment_hints_mode : AdjustmentHintsMode :: PreferPostfix ,
485
+ ..DISABLED_CONFIG
486
+ } ,
487
+ r#"
488
+ fn main() {
489
+ let _: u32 = loop {};
490
+ //^^^^^^^.<never-to-any>
491
+
492
+ Struct.by_ref();
493
+ //^^^^^^.&
494
+
495
+ let (): () = return ();
496
+ //^^^^^^^^^<never-to-any>
497
+
498
+ struct Struct;
499
+ impl Struct { fn by_ref(&self) {} }
500
+ }
501
+ "# ,
502
+ )
503
+ }
504
+
422
505
#[ test]
423
506
fn never_to_never_is_never_shown ( ) {
424
507
check_with_config (
0 commit comments