Skip to content

Commit ec96819

Browse files
committed
Auto merge of rust-lang#13816 - WaffleLapkin:postfix_adjustment_hints, r=Veykril
Postfix adjustment hints # Basic Description This PR implements "postfix" adjustment hints: ![2022-12-21_19-27](https://user-images.githubusercontent.com/38225716/208941721-d48d316f-a918-408a-9757-8d4e2b402a66.png) They are identical to normal adjustment hints, but are rendered _after_ the expression. E.g. `expr.*` instead of `*expr`. ~~This mirrors "postfix deref" feature that I'm planning to eventually propose to the compiler.~~ # Motivation The advantage of being postfix is that you need to add parentheses less often: ![2022-12-21_19-38](https://user-images.githubusercontent.com/38225716/208944302-16718112-14a4-4438-8aed-797766391c63.png) ![2022-12-21_19-37](https://user-images.githubusercontent.com/38225716/208944281-d9614888-6597-41ee-bf5d-a081d8048f94.png) This is because a lot of "reborrow" hints are caused by field access or method calls, both of which are postfix and have higher "precedence" than prefix `&` and `*`. Also IMHO it just looks nicer and it's more clear what is happening (order of operations). # Modes However, there are some cases where postfix hints need parentheses but prefix don't (for example `&x` being turned into `(&x).*.*.&` or `&**&x`). This PR allows users to choose which look they like more. There are 4 options (`rust-analyzer.inlayHints.expressionAdjustmentHints.mode` setting): - `prefix` — always use prefix hints (default, what was used before that PR) - `postfix` — always use postfix hints - `prefer_prefix` — try to minimize number of parentheses, breaking ties in favor of prefix - `prefer_postfix` — try to minimize number of parentheses, breaking ties in favor of postfix Comparison of all modes: ![2022-12-21_19-53](https://user-images.githubusercontent.com/38225716/208947482-26357c82-2b42-47d9-acec-835f5f03f6b4.png) ![2022-12-21_19-49](https://user-images.githubusercontent.com/38225716/208946731-fe566d3b-52b2-4846-994d-c2cecc769e0f.png) ![2022-12-21_19-48](https://user-images.githubusercontent.com/38225716/208946742-6e237f44-805e-469a-a3db-03d8f76e1317.png) ![2022-12-21_19-47](https://user-images.githubusercontent.com/38225716/208946747-79f25fae-e3ea-47d2-8d27-cb4eeac034fe.png) # Edge cases Where are some rare cases where chain hints weirdly interact with adjustment hints, for example (note `SourceAnalyzer.&`): ![image](https://user-images.githubusercontent.com/38225716/208947958-41c12971-f1f0-4a41-a930-47939cce9f58.png) This is pre-existing, you can get the same effect with prefix hints (`SourceAnalyzer)`). ---- Another weird thing is this: ![2022-12-21_20-00](https://user-images.githubusercontent.com/38225716/208948590-ea26d325-2108-4b35-abaa-716a65a1ae99.png) Here `.&` is a hint and `?` is written in the source code. It looks like `?` is part of the hint because `?.` is ligature in my font. IMO this is a bug in vscode, but still worth mentioning (I'm also too lazy to report it there...). # Fixed bugs I've used the "needs parens" API and this accidentally fixed a bug with parens around `as`, see the test diff: ```diff,rust let _: *const u32 = &mut 0u32 as *mut u32; //^^^^^^^^^^^^^^^^^^^^^<mut-ptr-to-const-ptr> + //^^^^^^^^^^^^^^^^^^^^^( + //^^^^^^^^^^^^^^^^^^^^^) ... let _: *const u32 = &mut 0u32 as *mut u32; //^^^^^^^^^^^^^^^^^^^^^<mut-ptr-to-const-ptr> + //^^^^^^^^^^^^^^^^^^^^^( + //^^^^^^^^^^^^^^^^^^^^^) ``` # Changelog changelog feature Add an option to make adjustment hints (aka reborrow hints) postfix changelog fix Fix placement of parentheses around `as` casts for adjustment hints
2 parents b0214d8 + b6169c2 commit ec96819

File tree

8 files changed

+402
-32
lines changed

8 files changed

+402
-32
lines changed

crates/ide/src/inlay_hints.rs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ pub struct InlayHintsConfig {
3535
pub parameter_hints: bool,
3636
pub chaining_hints: bool,
3737
pub adjustment_hints: AdjustmentHints,
38+
pub adjustment_hints_mode: AdjustmentHintsMode,
3839
pub adjustment_hints_hide_outside_unsafe: bool,
3940
pub closure_return_type_hints: ClosureReturnTypeHints,
4041
pub binding_mode_hints: bool,
@@ -74,6 +75,14 @@ pub enum AdjustmentHints {
7475
Never,
7576
}
7677

78+
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
79+
pub enum AdjustmentHintsMode {
80+
Prefix,
81+
Postfix,
82+
PreferPrefix,
83+
PreferPostfix,
84+
}
85+
7786
#[derive(Clone, Debug, PartialEq, Eq)]
7887
pub enum InlayKind {
7988
BindingModeHint,
@@ -82,6 +91,7 @@ pub enum InlayKind {
8291
ClosureReturnTypeHint,
8392
GenericParamListHint,
8493
AdjustmentHint,
94+
AdjustmentHintPostfix,
8595
LifetimeHint,
8696
ParameterHint,
8797
TypeHint,
@@ -430,7 +440,7 @@ mod tests {
430440
use itertools::Itertools;
431441
use test_utils::extract_annotations;
432442

433-
use crate::inlay_hints::AdjustmentHints;
443+
use crate::inlay_hints::{AdjustmentHints, AdjustmentHintsMode};
434444
use crate::DiscriminantHints;
435445
use crate::{fixture, inlay_hints::InlayHintsConfig, LifetimeElisionHints};
436446

@@ -446,6 +456,7 @@ mod tests {
446456
lifetime_elision_hints: LifetimeElisionHints::Never,
447457
closure_return_type_hints: ClosureReturnTypeHints::Never,
448458
adjustment_hints: AdjustmentHints::Never,
459+
adjustment_hints_mode: AdjustmentHintsMode::Prefix,
449460
adjustment_hints_hide_outside_unsafe: false,
450461
binding_mode_hints: false,
451462
hide_named_constructor_hints: false,

0 commit comments

Comments
 (0)