Skip to content

Commit 730286b

Browse files
committed
Restructure InlayHint, no longer derive properties from its kind
1 parent 4c5fd19 commit 730286b

File tree

15 files changed

+416
-348
lines changed

15 files changed

+416
-348
lines changed

crates/ide/src/inlay_hints.rs

Lines changed: 28 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -90,32 +90,34 @@ pub enum AdjustmentHintsMode {
9090
PreferPostfix,
9191
}
9292

93-
// FIXME: Clean up this mess, the kinds are mainly used for setting different rendering properties in the lsp layer
94-
// We should probably turns this into such a property holding struct. Or clean this up in some other form.
9593
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
9694
pub enum InlayKind {
95+
Adjustment,
9796
BindingMode,
9897
Chaining,
9998
ClosingBrace,
100-
ClosureReturnType,
99+
ClosureCapture,
100+
Discriminant,
101101
GenericParamList,
102-
Adjustment,
103-
AdjustmentPostfix,
104102
Lifetime,
105-
ClosureCapture,
106103
Parameter,
107104
Type,
108-
Discriminant,
109-
OpeningParenthesis,
110-
ClosingParenthesis,
105+
}
106+
107+
#[derive(Debug)]
108+
pub enum InlayHintPosition {
109+
Before,
110+
After,
111111
}
112112

113113
#[derive(Debug)]
114114
pub struct InlayHint {
115115
/// The text range this inlay hint applies to.
116116
pub range: TextRange,
117-
/// The kind of this inlay hint. This is used to determine side and padding of the hint for
118-
/// rendering purposes.
117+
pub position: InlayHintPosition,
118+
pub pad_left: bool,
119+
pub pad_right: bool,
120+
/// The kind of this inlay hint.
119121
pub kind: InlayKind,
120122
/// The actual label to show in the inlay hint.
121123
pub label: InlayHintLabel,
@@ -124,20 +126,26 @@ pub struct InlayHint {
124126
}
125127

126128
impl InlayHint {
127-
fn closing_paren(range: TextRange) -> InlayHint {
129+
fn closing_paren_after(kind: InlayKind, range: TextRange) -> InlayHint {
128130
InlayHint {
129131
range,
130-
kind: InlayKind::ClosingParenthesis,
132+
kind,
131133
label: InlayHintLabel::from(")"),
132134
text_edit: None,
135+
position: InlayHintPosition::After,
136+
pad_left: false,
137+
pad_right: false,
133138
}
134139
}
135-
fn opening_paren(range: TextRange) -> InlayHint {
140+
fn opening_paren_before(kind: InlayKind, range: TextRange) -> InlayHint {
136141
InlayHint {
137142
range,
138-
kind: InlayKind::OpeningParenthesis,
143+
kind,
139144
label: InlayHintLabel::from("("),
140145
text_edit: None,
146+
position: InlayHintPosition::Before,
147+
pad_left: false,
148+
pad_right: false,
141149
}
142150
}
143151
}
@@ -303,13 +311,13 @@ impl InlayHintLabelBuilder<'_> {
303311
fn label_of_ty(
304312
famous_defs @ FamousDefs(sema, _): &FamousDefs<'_, '_>,
305313
config: &InlayHintsConfig,
306-
ty: hir::Type,
314+
ty: &hir::Type,
307315
) -> Option<InlayHintLabel> {
308316
fn rec(
309317
sema: &Semantics<'_, RootDatabase>,
310318
famous_defs: &FamousDefs<'_, '_>,
311319
mut max_length: Option<usize>,
312-
ty: hir::Type,
320+
ty: &hir::Type,
313321
label_builder: &mut InlayHintLabelBuilder<'_>,
314322
config: &InlayHintsConfig,
315323
) -> Result<(), HirDisplayError> {
@@ -342,7 +350,7 @@ fn label_of_ty(
342350
label_builder.write_str(LABEL_ITEM)?;
343351
label_builder.end_location_link();
344352
label_builder.write_str(LABEL_MIDDLE2)?;
345-
rec(sema, famous_defs, max_length, ty, label_builder, config)?;
353+
rec(sema, famous_defs, max_length, &ty, label_builder, config)?;
346354
label_builder.write_str(LABEL_END)?;
347355
Ok(())
348356
}
@@ -574,7 +582,8 @@ mod tests {
574582
let inlay_hints = analysis.inlay_hints(&config, file_id, None).unwrap();
575583
let actual = inlay_hints
576584
.into_iter()
577-
.map(|it| (it.range, it.label.to_string()))
585+
// FIXME: We trim the start because some inlay produces leading whitespace which is not properly supported by our annotation extraction
586+
.map(|it| (it.range, it.label.to_string().trim_start().to_owned()))
578587
.sorted_by_key(|(range, _)| range.start())
579588
.collect::<Vec<_>>();
580589
expected.sort_by_key(|(range, _)| range.start());

crates/ide/src/inlay_hints/adjustment.rs

Lines changed: 26 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
//! let _: u32 = /* <never-to-any> */ loop {};
44
//! let _: &u32 = /* &* */ &mut 0;
55
//! ```
6+
use either::Either;
67
use hir::{
78
Adjust, Adjustment, AutoBorrow, HirDisplay, Mutability, OverloadedDeref, PointerCast, Safety,
89
Semantics,
@@ -16,8 +17,8 @@ use syntax::{
1617
};
1718

1819
use crate::{
19-
AdjustmentHints, AdjustmentHintsMode, InlayHint, InlayHintLabel, InlayHintsConfig, InlayKind,
20-
InlayTooltip,
20+
AdjustmentHints, AdjustmentHintsMode, InlayHint, InlayHintLabel, InlayHintPosition,
21+
InlayHintsConfig, InlayKind, InlayTooltip,
2122
};
2223

2324
pub(super) fn hints(
@@ -63,22 +64,26 @@ pub(super) fn hints(
6364
mode_and_needs_parens_for_adjustment_hints(expr, config.adjustment_hints_mode);
6465

6566
if needs_outer_parens {
66-
acc.push(InlayHint::opening_paren(expr.syntax().text_range()));
67+
acc.push(InlayHint::opening_paren_before(
68+
InlayKind::Adjustment,
69+
expr.syntax().text_range(),
70+
));
6771
}
6872

6973
if postfix && needs_inner_parens {
70-
acc.push(InlayHint::opening_paren(expr.syntax().text_range()));
71-
acc.push(InlayHint::closing_paren(expr.syntax().text_range()));
74+
acc.push(InlayHint::opening_paren_before(
75+
InlayKind::Adjustment,
76+
expr.syntax().text_range(),
77+
));
78+
acc.push(InlayHint::closing_paren_after(InlayKind::Adjustment, expr.syntax().text_range()));
7279
}
7380

74-
let (mut tmp0, mut tmp1);
75-
let iter: &mut dyn Iterator<Item = _> = if postfix {
76-
tmp0 = adjustments.into_iter();
77-
&mut tmp0
81+
let mut iter = if postfix {
82+
Either::Left(adjustments.into_iter())
7883
} else {
79-
tmp1 = adjustments.into_iter().rev();
80-
&mut tmp1
84+
Either::Right(adjustments.into_iter().rev())
8185
};
86+
let iter: &mut dyn Iterator<Item = _> = iter.as_mut().either(|it| it as _, |it| it as _);
8287

8388
for Adjustment { source, target, kind } in iter {
8489
if source == target {
@@ -134,7 +139,10 @@ pub(super) fn hints(
134139
};
135140
acc.push(InlayHint {
136141
range: expr.syntax().text_range(),
137-
kind: if postfix { InlayKind::AdjustmentPostfix } else { InlayKind::Adjustment },
142+
pad_left: false,
143+
pad_right: false,
144+
position: if postfix { InlayHintPosition::After } else { InlayHintPosition::Before },
145+
kind: InlayKind::Adjustment,
138146
label: InlayHintLabel::simple(
139147
if postfix { format!(".{}", text.trim_end()) } else { text.to_owned() },
140148
Some(InlayTooltip::Markdown(format!(
@@ -148,11 +156,14 @@ pub(super) fn hints(
148156
});
149157
}
150158
if !postfix && needs_inner_parens {
151-
acc.push(InlayHint::opening_paren(expr.syntax().text_range()));
152-
acc.push(InlayHint::closing_paren(expr.syntax().text_range()));
159+
acc.push(InlayHint::opening_paren_before(
160+
InlayKind::Adjustment,
161+
expr.syntax().text_range(),
162+
));
163+
acc.push(InlayHint::closing_paren_after(InlayKind::Adjustment, expr.syntax().text_range()));
153164
}
154165
if needs_outer_parens {
155-
acc.push(InlayHint::closing_paren(expr.syntax().text_range()));
166+
acc.push(InlayHint::closing_paren_after(InlayKind::Adjustment, expr.syntax().text_range()));
156167
}
157168
Some(())
158169
}

0 commit comments

Comments
 (0)