@@ -11,7 +11,7 @@ use syntax::{
1111 ted,
1212} ;
1313
14- use crate :: { AdjustmentHints , InlayHint , InlayHintsConfig , InlayKind } ;
14+ use crate :: { AdjustmentHints , AdjustmentHintsMode , InlayHint , InlayHintsConfig , InlayKind } ;
1515
1616pub ( super ) fn hints (
1717 acc : & mut Vec < InlayHint > ,
@@ -40,8 +40,8 @@ pub(super) fn hints(
4040 let desc_expr = descended. as_ref ( ) . unwrap_or ( expr) ;
4141 let adjustments = sema. expr_adjustments ( desc_expr) . filter ( |it| !it. is_empty ( ) ) ?;
4242
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 ) ;
4545
4646 if needs_outer_parens {
4747 acc. push ( InlayHint {
@@ -52,7 +52,7 @@ pub(super) fn hints(
5252 } ) ;
5353 }
5454
55- if config . adjustment_hints_postfix && needs_inner_parens {
55+ if postfix && needs_inner_parens {
5656 acc. push ( InlayHint {
5757 range : expr. syntax ( ) . text_range ( ) ,
5858 kind : InlayKind :: OpeningParenthesis ,
@@ -68,7 +68,7 @@ pub(super) fn hints(
6868 }
6969
7070 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 {
7272 tmp0 = adjustments. into_iter ( ) ;
7373 & mut tmp0
7474 } else {
@@ -112,20 +112,16 @@ pub(super) fn hints(
112112 } ;
113113 acc. push ( InlayHint {
114114 range : expr. syntax ( ) . text_range ( ) ,
115- kind : if config . adjustment_hints_postfix {
115+ kind : if postfix {
116116 InlayKind :: AdjustmentHintPostfix
117117 } else {
118118 InlayKind :: AdjustmentHint
119119 } ,
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 ( ) } ,
125121 tooltip : None ,
126122 } ) ;
127123 }
128- if !config . adjustment_hints_postfix && needs_inner_parens {
124+ if !postfix && needs_inner_parens {
129125 acc. push ( InlayHint {
130126 range : expr. syntax ( ) . text_range ( ) ,
131127 kind : InlayKind :: OpeningParenthesis ,
@@ -150,6 +146,41 @@ pub(super) fn hints(
150146 Some ( ( ) )
151147}
152148
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+
153184/// Returns whatever we need to add paretheses on the inside and/or outside of `expr`,
154185/// if we are going to add (`postfix`) adjustments hints to it.
155186fn 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,
217248mod tests {
218249 use crate :: {
219250 inlay_hints:: tests:: { check_with_config, DISABLED_CONFIG } ,
220- AdjustmentHints , InlayHintsConfig ,
251+ AdjustmentHints , AdjustmentHintsMode , InlayHintsConfig ,
221252 } ;
222253
223254 #[ test]
@@ -333,7 +364,7 @@ impl Struct {
333364 check_with_config (
334365 InlayHintsConfig {
335366 adjustment_hints : AdjustmentHints :: Always ,
336- adjustment_hints_postfix : true ,
367+ adjustment_hints_mode : AdjustmentHintsMode :: Postfix ,
337368 ..DISABLED_CONFIG
338369 } ,
339370 r#"
@@ -419,6 +450,58 @@ impl Struct {
419450 ) ;
420451 }
421452
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+
422505 #[ test]
423506 fn never_to_never_is_never_shown ( ) {
424507 check_with_config (
0 commit comments