Skip to content

Commit 2b4a537

Browse files
committed
Properly use location links for type hints of impl Future and its assoc type
1 parent 0b32b65 commit 2b4a537

File tree

4 files changed

+106
-12
lines changed

4 files changed

+106
-12
lines changed

crates/hir-ty/src/display.rs

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use hir_def::{
1212
find_path,
1313
generics::{TypeOrConstParamData, TypeParamProvenance},
1414
item_scope::ItemInNs,
15-
lang_item::LangItem,
15+
lang_item::{LangItem, LangItemTarget},
1616
path::{Path, PathKind},
1717
type_ref::{ConstScalar, TraitBoundModifier, TypeBound, TypeRef},
1818
visibility::Visibility,
@@ -731,8 +731,30 @@ impl HirDisplay for Ty {
731731
)?;
732732
// FIXME: it would maybe be good to distinguish this from the alias type (when debug printing), and to show the substitution
733733
}
734-
ImplTraitId::AsyncBlockTypeImplTrait(..) => {
735-
write!(f, "impl Future<Output = ")?;
734+
ImplTraitId::AsyncBlockTypeImplTrait(body, ..) => {
735+
let future_trait = db
736+
.lang_item(body.module(db.upcast()).krate(), LangItem::Future)
737+
.and_then(LangItemTarget::as_trait);
738+
let output = future_trait.and_then(|t| {
739+
db.trait_data(t).associated_type_by_name(&hir_expand::name!(Output))
740+
});
741+
write!(f, "impl ")?;
742+
if let Some(t) = future_trait {
743+
f.start_location_link(t.into());
744+
}
745+
write!(f, "Future")?;
746+
if let Some(_) = future_trait {
747+
f.end_location_link();
748+
}
749+
write!(f, "<")?;
750+
if let Some(t) = output {
751+
f.start_location_link(t.into());
752+
}
753+
write!(f, "Output")?;
754+
if let Some(_) = output {
755+
f.end_location_link();
756+
}
757+
write!(f, " = ")?;
736758
parameters.at(Interner, 0).hir_fmt(f)?;
737759
write!(f, ">")?;
738760
}

crates/ide/src/inlay_hints.rs

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -294,17 +294,20 @@ fn label_of_ty(
294294
) -> Result<(), HirDisplayError> {
295295
let iter_item_type = hint_iterator(sema, famous_defs, &ty);
296296
match iter_item_type {
297-
Some((iter_trait, ty)) => {
297+
Some((iter_trait, item, ty)) => {
298298
const LABEL_START: &str = "impl ";
299299
const LABEL_ITERATOR: &str = "Iterator";
300-
const LABEL_MIDDLE: &str = "<Item = ";
300+
const LABEL_MIDDLE: &str = "<";
301+
const LABEL_ITEM: &str = "Item";
302+
const LABEL_MIDDLE2: &str = " = ";
301303
const LABEL_END: &str = ">";
302304

303305
max_length = max_length.map(|len| {
304306
len.saturating_sub(
305307
LABEL_START.len()
306308
+ LABEL_ITERATOR.len()
307309
+ LABEL_MIDDLE.len()
310+
+ LABEL_MIDDLE2.len()
308311
+ LABEL_END.len(),
309312
)
310313
});
@@ -314,6 +317,10 @@ fn label_of_ty(
314317
label_builder.write_str(LABEL_ITERATOR)?;
315318
label_builder.end_location_link();
316319
label_builder.write_str(LABEL_MIDDLE)?;
320+
label_builder.start_location_link(ModuleDef::from(item).into());
321+
label_builder.write_str(LABEL_ITEM)?;
322+
label_builder.end_location_link();
323+
label_builder.write_str(LABEL_MIDDLE2)?;
317324
rec(sema, famous_defs, max_length, ty, label_builder)?;
318325
label_builder.write_str(LABEL_END)?;
319326
Ok(())
@@ -437,7 +444,7 @@ fn hint_iterator(
437444
sema: &Semantics<'_, RootDatabase>,
438445
famous_defs: &FamousDefs<'_, '_>,
439446
ty: &hir::Type,
440-
) -> Option<(hir::Trait, hir::Type)> {
447+
) -> Option<(hir::Trait, hir::TypeAlias, hir::Type)> {
441448
let db = sema.db;
442449
let strukt = ty.strip_references().as_adt()?;
443450
let krate = strukt.module(db).krate();
@@ -460,7 +467,7 @@ fn hint_iterator(
460467
_ => None,
461468
})?;
462469
if let Some(ty) = ty.normalize_trait_assoc_type(db, &[], assoc_type_item) {
463-
return Some((iter_trait, ty));
470+
return Some((iter_trait, assoc_type_item, ty));
464471
}
465472
}
466473

crates/ide/src/inlay_hints/bind_pat.rs

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -330,7 +330,20 @@ fn main(a: SliceIter<'_, Container>) {
330330
),
331331
tooltip: "",
332332
},
333-
"<Item = impl ",
333+
"<",
334+
InlayHintLabelPart {
335+
text: "Item",
336+
linked_location: Some(
337+
FileRange {
338+
file_id: FileId(
339+
1,
340+
),
341+
range: 2643..2647,
342+
},
343+
),
344+
tooltip: "",
345+
},
346+
" = impl ",
334347
InlayHintLabelPart {
335348
text: "Iterator",
336349
linked_location: Some(
@@ -343,7 +356,20 @@ fn main(a: SliceIter<'_, Container>) {
343356
),
344357
tooltip: "",
345358
},
346-
"<Item = &&str>>",
359+
"<",
360+
InlayHintLabelPart {
361+
text: "Item",
362+
linked_location: Some(
363+
FileRange {
364+
file_id: FileId(
365+
1,
366+
),
367+
range: 2643..2647,
368+
},
369+
),
370+
tooltip: "",
371+
},
372+
" = &&str>>",
347373
],
348374
},
349375
InlayHint {

crates/ide/src/inlay_hints/chaining.rs

Lines changed: 42 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -440,7 +440,20 @@ fn main() {
440440
),
441441
tooltip: "",
442442
},
443-
"<Item = ()>",
443+
"<",
444+
InlayHintLabelPart {
445+
text: "Item",
446+
linked_location: Some(
447+
FileRange {
448+
file_id: FileId(
449+
1,
450+
),
451+
range: 2643..2647,
452+
},
453+
),
454+
tooltip: "",
455+
},
456+
" = ()>",
444457
],
445458
},
446459
InlayHint {
@@ -460,7 +473,20 @@ fn main() {
460473
),
461474
tooltip: "",
462475
},
463-
"<Item = ()>",
476+
"<",
477+
InlayHintLabelPart {
478+
text: "Item",
479+
linked_location: Some(
480+
FileRange {
481+
file_id: FileId(
482+
1,
483+
),
484+
range: 2643..2647,
485+
},
486+
),
487+
tooltip: "",
488+
},
489+
" = ()>",
464490
],
465491
},
466492
InlayHint {
@@ -480,7 +506,20 @@ fn main() {
480506
),
481507
tooltip: "",
482508
},
483-
"<Item = ()>",
509+
"<",
510+
InlayHintLabelPart {
511+
text: "Item",
512+
linked_location: Some(
513+
FileRange {
514+
file_id: FileId(
515+
1,
516+
),
517+
range: 2643..2647,
518+
},
519+
),
520+
tooltip: "",
521+
},
522+
" = ()>",
484523
],
485524
},
486525
InlayHint {

0 commit comments

Comments
 (0)