Skip to content

Commit 28cd9de

Browse files
Provide resolve data only when it can actually be resolved
1 parent caf0185 commit 28cd9de

13 files changed

+66
-35
lines changed

crates/ide/src/inlay_hints.rs

+7
Original file line numberDiff line numberDiff line change
@@ -153,11 +153,13 @@ pub struct InlayHint {
153153
pub label: InlayHintLabel,
154154
/// Text edit to apply when "accepting" this inlay hint.
155155
pub text_edit: Option<TextEdit>,
156+
pub needs_resolve: bool,
156157
}
157158

158159
impl InlayHint {
159160
fn closing_paren_after(kind: InlayKind, range: TextRange) -> InlayHint {
160161
InlayHint {
162+
needs_resolve: false,
161163
range,
162164
kind,
163165
label: InlayHintLabel::from(")"),
@@ -169,6 +171,7 @@ impl InlayHint {
169171
}
170172
fn opening_paren_before(kind: InlayKind, range: TextRange) -> InlayHint {
171173
InlayHint {
174+
needs_resolve: false,
172175
range,
173176
kind,
174177
label: InlayHintLabel::from("("),
@@ -226,6 +229,10 @@ impl InlayHintLabel {
226229
}),
227230
}
228231
}
232+
233+
pub fn needs_resolve(&self) -> bool {
234+
self.parts.iter().any(|part| part.linked_location.is_some() || part.tooltip.is_some())
235+
}
229236
}
230237

231238
impl From<String> for InlayHintLabel {

crates/ide/src/inlay_hints/adjustment.rs

+11-9
Original file line numberDiff line numberDiff line change
@@ -137,21 +137,23 @@ pub(super) fn hints(
137137
}
138138
_ => continue,
139139
};
140+
let label = InlayHintLabel::simple(
141+
if postfix { format!(".{}", text.trim_end()) } else { text.to_owned() },
142+
Some(InlayTooltip::Markdown(format!(
143+
"`{}` → `{}` ({coercion} coercion)",
144+
source.display(sema.db),
145+
target.display(sema.db),
146+
))),
147+
None,
148+
);
140149
acc.push(InlayHint {
150+
needs_resolve: label.needs_resolve(),
141151
range: expr.syntax().text_range(),
142152
pad_left: false,
143153
pad_right: false,
144154
position: if postfix { InlayHintPosition::After } else { InlayHintPosition::Before },
145155
kind: InlayKind::Adjustment,
146-
label: InlayHintLabel::simple(
147-
if postfix { format!(".{}", text.trim_end()) } else { text.to_owned() },
148-
Some(InlayTooltip::Markdown(format!(
149-
"`{}` → `{}` ({coercion} coercion)",
150-
source.display(sema.db),
151-
target.display(sema.db),
152-
))),
153-
None,
154-
),
156+
label,
155157
text_edit: None,
156158
});
157159
}

crates/ide/src/inlay_hints/bind_pat.rs

+1
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ pub(super) fn hints(
9999
None => pat.syntax().text_range(),
100100
};
101101
acc.push(InlayHint {
102+
needs_resolve: label.needs_resolve() || text_edit.is_some(),
102103
range: match type_ascriptable {
103104
Some(Some(t)) => text_range.cover(t.text_range()),
104105
_ => text_range,

crates/ide/src/inlay_hints/binding_mode.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,10 @@ pub(super) fn hints(
5050
_ => return,
5151
};
5252
acc.push(InlayHint {
53+
needs_resolve: false,
5354
range,
5455
kind: InlayKind::BindingMode,
55-
label: r.to_string().into(),
56+
label: r.into(),
5657
text_edit: None,
5758
position: InlayHintPosition::Before,
5859
pad_left: false,
@@ -68,9 +69,10 @@ pub(super) fn hints(
6869
hir::BindingMode::Ref(Mutability::Shared) => "ref",
6970
};
7071
acc.push(InlayHint {
72+
needs_resolve: false,
7173
range: pat.syntax().text_range(),
7274
kind: InlayKind::BindingMode,
73-
label: bm.to_string().into(),
75+
label: bm.into(),
7476
text_edit: None,
7577
position: InlayHintPosition::Before,
7678
pad_left: false,

crates/ide/src/inlay_hints/chaining.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -57,10 +57,12 @@ pub(super) fn hints(
5757
}
5858
}
5959
}
60+
let label = label_of_ty(famous_defs, config, &ty)?;
6061
acc.push(InlayHint {
62+
needs_resolve: label.needs_resolve(),
6163
range: expr.syntax().text_range(),
6264
kind: InlayKind::Chaining,
63-
label: label_of_ty(famous_defs, config, &ty)?,
65+
label,
6466
text_edit: None,
6567
position: InlayHintPosition::After,
6668
pad_left: true,

crates/ide/src/inlay_hints/closing_brace.rs

+1
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@ pub(super) fn hints(
109109

110110
let linked_location = name_range.map(|range| FileRange { file_id, range });
111111
acc.push(InlayHint {
112+
needs_resolve: linked_location.is_some(),
112113
range: closing_token.text_range(),
113114
kind: InlayKind::ClosingBrace,
114115
label: InlayHintLabel::simple(label, None, linked_location),

crates/ide/src/inlay_hints/closure_captures.rs

+22-16
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,10 @@ pub(super) fn hints(
3131
let range = closure.syntax().first_token()?.prev_token()?.text_range();
3232
let range = TextRange::new(range.end() - TextSize::from(1), range.end());
3333
acc.push(InlayHint {
34+
needs_resolve: false,
3435
range,
3536
kind: InlayKind::ClosureCapture,
36-
label: InlayHintLabel::simple("move", None, None),
37+
label: InlayHintLabel::from("move"),
3738
text_edit: None,
3839
position: InlayHintPosition::After,
3940
pad_left: false,
@@ -43,6 +44,7 @@ pub(super) fn hints(
4344
}
4445
};
4546
acc.push(InlayHint {
47+
needs_resolve: false,
4648
range: move_kw_range,
4749
kind: InlayKind::ClosureCapture,
4850
label: InlayHintLabel::from("("),
@@ -59,23 +61,25 @@ pub(super) fn hints(
5961
// force cache the source file, otherwise sema lookup will potentially panic
6062
_ = sema.parse_or_expand(source.file());
6163

64+
let label = InlayHintLabel::simple(
65+
format!(
66+
"{}{}",
67+
match capture.kind() {
68+
hir::CaptureKind::SharedRef => "&",
69+
hir::CaptureKind::UniqueSharedRef => "&unique ",
70+
hir::CaptureKind::MutableRef => "&mut ",
71+
hir::CaptureKind::Move => "",
72+
},
73+
capture.display_place(sema.db)
74+
),
75+
None,
76+
source.name().and_then(|name| name.syntax().original_file_range_opt(sema.db)),
77+
);
6278
acc.push(InlayHint {
79+
needs_resolve: label.needs_resolve(),
6380
range: move_kw_range,
6481
kind: InlayKind::ClosureCapture,
65-
label: InlayHintLabel::simple(
66-
format!(
67-
"{}{}",
68-
match capture.kind() {
69-
hir::CaptureKind::SharedRef => "&",
70-
hir::CaptureKind::UniqueSharedRef => "&unique ",
71-
hir::CaptureKind::MutableRef => "&mut ",
72-
hir::CaptureKind::Move => "",
73-
},
74-
capture.display_place(sema.db)
75-
),
76-
None,
77-
source.name().and_then(|name| name.syntax().original_file_range_opt(sema.db)),
78-
),
82+
label,
7983
text_edit: None,
8084
position: InlayHintPosition::After,
8185
pad_left: false,
@@ -84,9 +88,10 @@ pub(super) fn hints(
8488

8589
if idx != last {
8690
acc.push(InlayHint {
91+
needs_resolve: false,
8792
range: move_kw_range,
8893
kind: InlayKind::ClosureCapture,
89-
label: InlayHintLabel::simple(", ", None, None),
94+
label: InlayHintLabel::from(", "),
9095
text_edit: None,
9196
position: InlayHintPosition::After,
9297
pad_left: false,
@@ -95,6 +100,7 @@ pub(super) fn hints(
95100
}
96101
}
97102
acc.push(InlayHint {
103+
needs_resolve: false,
98104
range: move_kw_range,
99105
kind: InlayKind::ClosureCapture,
100106
label: InlayHintLabel::from(")"),

crates/ide/src/inlay_hints/closure_ret.rs

+1
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ pub(super) fn hints(
6464
};
6565

6666
acc.push(InlayHint {
67+
needs_resolve: label.needs_resolve() || text_edit.is_some(),
6768
range: param_list.syntax().text_range(),
6869
kind: InlayKind::Type,
6970
label,

crates/ide/src/inlay_hints/discriminant.rs

+1
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ fn variant_hints(
7979
None,
8080
);
8181
acc.push(InlayHint {
82+
needs_resolve: label.needs_resolve(),
8283
range: match eq_token {
8384
Some(t) => range.cover(t.text_range()),
8485
_ => range,

crates/ide/src/inlay_hints/fn_lifetime_fn.rs

+3
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ pub(super) fn hints(
2222
}
2323

2424
let mk_lt_hint = |t: SyntaxToken, label: String| InlayHint {
25+
needs_resolve: false,
2526
range: t.text_range(),
2627
kind: InlayKind::Lifetime,
2728
label: label.into(),
@@ -185,6 +186,7 @@ pub(super) fn hints(
185186
let angle_tok = gpl.l_angle_token()?;
186187
let is_empty = gpl.generic_params().next().is_none();
187188
acc.push(InlayHint {
189+
needs_resolve: false,
188190
range: angle_tok.text_range(),
189191
kind: InlayKind::Lifetime,
190192
label: format!(
@@ -200,6 +202,7 @@ pub(super) fn hints(
200202
});
201203
}
202204
(None, allocated_lifetimes) => acc.push(InlayHint {
205+
needs_resolve: false,
203206
range: func.name()?.syntax().text_range(),
204207
kind: InlayKind::GenericParamList,
205208
label: format!("<{}>", allocated_lifetimes.iter().format(", "),).into(),

crates/ide/src/inlay_hints/implicit_static.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,10 @@ pub(super) fn hints(
3131
if ty.lifetime().is_none() {
3232
let t = ty.amp_token()?;
3333
acc.push(InlayHint {
34+
needs_resolve: false,
3435
range: t.text_range(),
3536
kind: InlayKind::Lifetime,
36-
label: "'static".to_owned().into(),
37+
label: "'static".into(),
3738
text_edit: None,
3839
position: InlayHintPosition::After,
3940
pad_left: false,

crates/ide/src/inlay_hints/param_name.rs

+1
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ pub(super) fn hints(
5757
let label =
5858
InlayHintLabel::simple(format!("{param_name}{colon}"), None, linked_location);
5959
InlayHint {
60+
needs_resolve: label.needs_resolve(),
6061
range,
6162
kind: InlayKind::Parameter,
6263
label,

crates/rust-analyzer/src/lsp/to_proto.rs

+9-6
Original file line numberDiff line numberDiff line change
@@ -442,13 +442,15 @@ pub(crate) fn inlay_hint(
442442
file_id: FileId,
443443
inlay_hint: InlayHint,
444444
) -> Cancellable<lsp_types::InlayHint> {
445-
let (label, tooltip) = inlay_hint_label(snap, fields_to_resolve, inlay_hint.label)?;
446-
let data = if fields_to_resolve.can_resolve() {
445+
let needs_resolve = inlay_hint.needs_resolve;
446+
let (label, tooltip) =
447+
inlay_hint_label(snap, fields_to_resolve, needs_resolve, inlay_hint.label)?;
448+
let data = if needs_resolve && fields_to_resolve.can_resolve() {
447449
Some(to_value(lsp_ext::InlayHintResolveData { file_id: file_id.0 }).unwrap())
448450
} else {
449451
None
450452
};
451-
let text_edits = if fields_to_resolve.resolve_text_edits {
453+
let text_edits = if needs_resolve && fields_to_resolve.resolve_text_edits {
452454
None
453455
} else {
454456
inlay_hint.text_edit.map(|it| text_edit_vec(line_index, it))
@@ -476,12 +478,13 @@ pub(crate) fn inlay_hint(
476478
fn inlay_hint_label(
477479
snap: &GlobalStateSnapshot,
478480
fields_to_resolve: &InlayFieldsToResolve,
481+
needs_resolve: bool,
479482
mut label: InlayHintLabel,
480483
) -> Cancellable<(lsp_types::InlayHintLabel, Option<lsp_types::InlayHintTooltip>)> {
481484
let res = match &*label.parts {
482485
[InlayHintLabelPart { linked_location: None, .. }] => {
483486
let InlayHintLabelPart { text, tooltip, .. } = label.parts.pop().unwrap();
484-
let hint_tooltip = if fields_to_resolve.resolve_hint_tooltip {
487+
let hint_tooltip = if needs_resolve && fields_to_resolve.resolve_hint_tooltip {
485488
None
486489
} else {
487490
match tooltip {
@@ -504,7 +507,7 @@ fn inlay_hint_label(
504507
.parts
505508
.into_iter()
506509
.map(|part| {
507-
let tooltip = if fields_to_resolve.resolve_label_tooltip {
510+
let tooltip = if needs_resolve && fields_to_resolve.resolve_label_tooltip {
508511
None
509512
} else {
510513
match part.tooltip {
@@ -522,7 +525,7 @@ fn inlay_hint_label(
522525
None => None,
523526
}
524527
};
525-
let location = if fields_to_resolve.resolve_label_location {
528+
let location = if needs_resolve && fields_to_resolve.resolve_label_location {
526529
None
527530
} else {
528531
part.linked_location.map(|range| location(snap, range)).transpose()?

0 commit comments

Comments
 (0)