Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit ce6955c

Browse files
committed
Auto merge of rust-lang#13946 - Veykril:inlay-hints, r=Veykril
Remove hover inlay tooltips, replace them with location links Turns out we re-implemented what clients can already figure out through the use of location-links. We might want lazy resolves tooltips later on still, but for now this simplifies things again.
2 parents 32be158 + f2444b2 commit ce6955c

18 files changed

+282
-471
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/ide/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ pulldown-cmark-to-cmark = "10.0.4"
2020
pulldown-cmark = { version = "0.9.1", default-features = false }
2121
url = "2.3.1"
2222
dot = "0.1.4"
23+
smallvec = "1.10.0"
2324

2425
stdx = { path = "../stdx", version = "0.0.0" }
2526
syntax = { path = "../syntax", version = "0.0.0" }

crates/ide/src/inlay_hints.rs

Lines changed: 76 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,11 @@ use either::Either;
77
use hir::{known, HasVisibility, HirDisplay, HirWrite, ModuleDef, ModuleDefId, Semantics};
88
use ide_db::{base_db::FileRange, famous_defs::FamousDefs, RootDatabase};
99
use itertools::Itertools;
10+
use smallvec::{smallvec, SmallVec};
1011
use stdx::never;
1112
use syntax::{
1213
ast::{self, AstNode},
13-
match_ast, NodeOrToken, SyntaxNode, TextRange, TextSize,
14+
match_ast, NodeOrToken, SyntaxNode, TextRange,
1415
};
1516

1617
use crate::{navigation_target::TryToNav, FileId};
@@ -83,75 +84,108 @@ pub enum AdjustmentHintsMode {
8384
PreferPostfix,
8485
}
8586

86-
#[derive(Clone, Debug, PartialEq, Eq)]
87+
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
8788
pub enum InlayKind {
88-
BindingModeHint,
89-
ChainingHint,
90-
ClosingBraceHint,
91-
ClosureReturnTypeHint,
92-
GenericParamListHint,
93-
AdjustmentHint,
94-
AdjustmentHintPostfix,
95-
LifetimeHint,
96-
ParameterHint,
97-
TypeHint,
98-
DiscriminantHint,
89+
BindingMode,
90+
Chaining,
91+
ClosingBrace,
92+
ClosureReturnType,
93+
GenericParamList,
94+
Adjustment,
95+
AdjustmentPostfix,
96+
Lifetime,
97+
Parameter,
98+
Type,
99+
Discriminant,
99100
OpeningParenthesis,
100101
ClosingParenthesis,
101102
}
102103

103104
#[derive(Debug)]
104105
pub struct InlayHint {
106+
/// The text range this inlay hint applies to.
105107
pub range: TextRange,
108+
/// The kind of this inlay hint. This is used to determine side and padding of the hint for
109+
/// rendering purposes.
106110
pub kind: InlayKind,
111+
/// The actual label to show in the inlay hint.
107112
pub label: InlayHintLabel,
108-
pub tooltip: Option<InlayTooltip>,
113+
}
114+
115+
impl InlayHint {
116+
fn closing_paren(range: TextRange) -> InlayHint {
117+
InlayHint { range, kind: InlayKind::ClosingParenthesis, label: InlayHintLabel::from(")") }
118+
}
119+
fn opening_paren(range: TextRange) -> InlayHint {
120+
InlayHint { range, kind: InlayKind::OpeningParenthesis, label: InlayHintLabel::from("(") }
121+
}
109122
}
110123

111124
#[derive(Debug)]
112125
pub enum InlayTooltip {
113126
String(String),
114-
HoverRanged(FileId, TextRange),
115-
HoverOffset(FileId, TextSize),
127+
Markdown(String),
116128
}
117129

118130
#[derive(Default)]
119131
pub struct InlayHintLabel {
120-
pub parts: Vec<InlayHintLabelPart>,
132+
pub parts: SmallVec<[InlayHintLabelPart; 1]>,
121133
}
122134

123135
impl InlayHintLabel {
124-
pub fn as_simple_str(&self) -> Option<&str> {
125-
match &*self.parts {
126-
[part] => part.as_simple_str(),
127-
_ => None,
136+
pub fn simple(
137+
s: impl Into<String>,
138+
tooltip: Option<InlayTooltip>,
139+
linked_location: Option<FileRange>,
140+
) -> InlayHintLabel {
141+
InlayHintLabel {
142+
parts: smallvec![InlayHintLabelPart { text: s.into(), linked_location, tooltip }],
128143
}
129144
}
130145

131146
pub fn prepend_str(&mut self, s: &str) {
132147
match &mut *self.parts {
133-
[part, ..] if part.as_simple_str().is_some() => part.text = format!("{s}{}", part.text),
134-
_ => self.parts.insert(0, InlayHintLabelPart { text: s.into(), linked_location: None }),
148+
[InlayHintLabelPart { text, linked_location: None, tooltip: None }, ..] => {
149+
text.insert_str(0, s)
150+
}
151+
_ => self.parts.insert(
152+
0,
153+
InlayHintLabelPart { text: s.into(), linked_location: None, tooltip: None },
154+
),
135155
}
136156
}
137157

138158
pub fn append_str(&mut self, s: &str) {
139159
match &mut *self.parts {
140-
[.., part] if part.as_simple_str().is_some() => part.text.push_str(s),
141-
_ => self.parts.push(InlayHintLabelPart { text: s.into(), linked_location: None }),
160+
[.., InlayHintLabelPart { text, linked_location: None, tooltip: None }] => {
161+
text.push_str(s)
162+
}
163+
_ => self.parts.push(InlayHintLabelPart {
164+
text: s.into(),
165+
linked_location: None,
166+
tooltip: None,
167+
}),
142168
}
143169
}
144170
}
145171

146172
impl From<String> for InlayHintLabel {
147173
fn from(s: String) -> Self {
148-
Self { parts: vec![InlayHintLabelPart { text: s, linked_location: None }] }
174+
Self {
175+
parts: smallvec![InlayHintLabelPart { text: s, linked_location: None, tooltip: None }],
176+
}
149177
}
150178
}
151179

152180
impl From<&str> for InlayHintLabel {
153181
fn from(s: &str) -> Self {
154-
Self { parts: vec![InlayHintLabelPart { text: s.into(), linked_location: None }] }
182+
Self {
183+
parts: smallvec![InlayHintLabelPart {
184+
text: s.into(),
185+
linked_location: None,
186+
tooltip: None
187+
}],
188+
}
155189
}
156190
}
157191

@@ -175,25 +209,25 @@ pub struct InlayHintLabelPart {
175209
/// When setting this, no tooltip must be set on the containing hint, or VS Code will display
176210
/// them both.
177211
pub linked_location: Option<FileRange>,
178-
}
179-
180-
impl InlayHintLabelPart {
181-
pub fn as_simple_str(&self) -> Option<&str> {
182-
match self {
183-
Self { text, linked_location: None } => Some(text),
184-
_ => None,
185-
}
186-
}
212+
/// The tooltip to show when hovering over the inlay hint, this may invoke other actions like
213+
/// hover requests to show.
214+
pub tooltip: Option<InlayTooltip>,
187215
}
188216

189217
impl fmt::Debug for InlayHintLabelPart {
190218
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
191-
match self.as_simple_str() {
192-
Some(string) => string.fmt(f),
193-
None => f
219+
match self {
220+
Self { text, linked_location: None, tooltip: None } => text.fmt(f),
221+
Self { text, linked_location, tooltip } => f
194222
.debug_struct("InlayHintLabelPart")
195-
.field("text", &self.text)
196-
.field("linked_location", &self.linked_location)
223+
.field("text", text)
224+
.field("linked_location", linked_location)
225+
.field(
226+
"tooltip",
227+
&tooltip.as_ref().map_or("", |it| match it {
228+
InlayTooltip::String(it) | InlayTooltip::Markdown(it) => it,
229+
}),
230+
)
197231
.finish(),
198232
}
199233
}
@@ -242,6 +276,7 @@ impl InlayHintLabelBuilder<'_> {
242276
self.result.parts.push(InlayHintLabelPart {
243277
text: take(&mut self.last_part),
244278
linked_location: self.location.take(),
279+
tooltip: None,
245280
});
246281
}
247282

crates/ide/src/inlay_hints/adjustment.rs

Lines changed: 7 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -44,27 +44,12 @@ pub(super) fn hints(
4444
mode_and_needs_parens_for_adjustment_hints(expr, config.adjustment_hints_mode);
4545

4646
if needs_outer_parens {
47-
acc.push(InlayHint {
48-
range: expr.syntax().text_range(),
49-
kind: InlayKind::OpeningParenthesis,
50-
label: "(".into(),
51-
tooltip: None,
52-
});
47+
acc.push(InlayHint::opening_paren(expr.syntax().text_range()));
5348
}
5449

5550
if postfix && needs_inner_parens {
56-
acc.push(InlayHint {
57-
range: expr.syntax().text_range(),
58-
kind: InlayKind::OpeningParenthesis,
59-
label: "(".into(),
60-
tooltip: None,
61-
});
62-
acc.push(InlayHint {
63-
range: expr.syntax().text_range(),
64-
kind: InlayKind::ClosingParenthesis,
65-
label: ")".into(),
66-
tooltip: None,
67-
});
51+
acc.push(InlayHint::opening_paren(expr.syntax().text_range()));
52+
acc.push(InlayHint::closing_paren(expr.syntax().text_range()));
6853
}
6954

7055
let (mut tmp0, mut tmp1);
@@ -112,36 +97,16 @@ pub(super) fn hints(
11297
};
11398
acc.push(InlayHint {
11499
range: expr.syntax().text_range(),
115-
kind: if postfix {
116-
InlayKind::AdjustmentHintPostfix
117-
} else {
118-
InlayKind::AdjustmentHint
119-
},
100+
kind: if postfix { InlayKind::AdjustmentPostfix } else { InlayKind::Adjustment },
120101
label: if postfix { format!(".{}", text.trim_end()).into() } else { text.into() },
121-
tooltip: None,
122102
});
123103
}
124104
if !postfix && needs_inner_parens {
125-
acc.push(InlayHint {
126-
range: expr.syntax().text_range(),
127-
kind: InlayKind::OpeningParenthesis,
128-
label: "(".into(),
129-
tooltip: None,
130-
});
131-
acc.push(InlayHint {
132-
range: expr.syntax().text_range(),
133-
kind: InlayKind::ClosingParenthesis,
134-
label: ")".into(),
135-
tooltip: None,
136-
});
105+
acc.push(InlayHint::opening_paren(expr.syntax().text_range()));
106+
acc.push(InlayHint::closing_paren(expr.syntax().text_range()));
137107
}
138108
if needs_outer_parens {
139-
acc.push(InlayHint {
140-
range: expr.syntax().text_range(),
141-
kind: InlayKind::ClosingParenthesis,
142-
label: ")".into(),
143-
tooltip: None,
144-
});
109+
acc.push(InlayHint::closing_paren(expr.syntax().text_range()));
145110
}
146111
Some(())
147112
}

crates/ide/src/inlay_hints/bind_pat.rs

Lines changed: 7 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,15 @@ use syntax::{
1212
match_ast,
1313
};
1414

15-
use crate::{
16-
inlay_hints::closure_has_block_body, InlayHint, InlayHintsConfig, InlayKind, InlayTooltip,
17-
};
15+
use crate::{inlay_hints::closure_has_block_body, InlayHint, InlayHintsConfig, InlayKind};
1816

1917
use super::label_of_ty;
2018

2119
pub(super) fn hints(
2220
acc: &mut Vec<InlayHint>,
2321
famous_defs @ FamousDefs(sema, _): &FamousDefs<'_, '_>,
2422
config: &InlayHintsConfig,
25-
file_id: FileId,
23+
_file_id: FileId,
2624
pat: &ast::IdentPat,
2725
) -> Option<()> {
2826
if !config.type_hints {
@@ -50,12 +48,8 @@ pub(super) fn hints(
5048
Some(name) => name.syntax().text_range(),
5149
None => pat.syntax().text_range(),
5250
},
53-
kind: InlayKind::TypeHint,
51+
kind: InlayKind::Type,
5452
label,
55-
tooltip: pat
56-
.name()
57-
.map(|it| it.syntax().text_range())
58-
.map(|it| InlayTooltip::HoverRanged(file_id, it)),
5953
});
6054

6155
Some(())
@@ -322,22 +316,14 @@ fn main(a: SliceIter<'_, Container>) {
322316
[
323317
InlayHint {
324318
range: 484..554,
325-
kind: ChainingHint,
319+
kind: Chaining,
326320
label: [
327321
"impl Iterator<Item = impl Iterator<Item = &&str>>",
328322
],
329-
tooltip: Some(
330-
HoverRanged(
331-
FileId(
332-
0,
333-
),
334-
484..554,
335-
),
336-
),
337323
},
338324
InlayHint {
339325
range: 484..485,
340-
kind: ChainingHint,
326+
kind: Chaining,
341327
label: [
342328
"",
343329
InlayHintLabelPart {
@@ -350,6 +336,7 @@ fn main(a: SliceIter<'_, Container>) {
350336
range: 289..298,
351337
},
352338
),
339+
tooltip: "",
353340
},
354341
"<",
355342
InlayHintLabelPart {
@@ -362,17 +349,10 @@ fn main(a: SliceIter<'_, Container>) {
362349
range: 238..247,
363350
},
364351
),
352+
tooltip: "",
365353
},
366354
">",
367355
],
368-
tooltip: Some(
369-
HoverRanged(
370-
FileId(
371-
0,
372-
),
373-
484..485,
374-
),
375-
),
376356
},
377357
]
378358
"#]],

0 commit comments

Comments
 (0)