From 2b4a5374ee12e8c7094cc8cd261366756cf6d81c Mon Sep 17 00:00:00 2001 From: Lukas Wirth Date: Tue, 7 Feb 2023 22:42:03 +0100 Subject: [PATCH] Properly use location links for type hints of impl Future and its assoc type --- crates/hir-ty/src/display.rs | 28 ++++++++++++++-- crates/ide/src/inlay_hints.rs | 15 ++++++--- crates/ide/src/inlay_hints/bind_pat.rs | 30 +++++++++++++++-- crates/ide/src/inlay_hints/chaining.rs | 45 ++++++++++++++++++++++++-- 4 files changed, 106 insertions(+), 12 deletions(-) diff --git a/crates/hir-ty/src/display.rs b/crates/hir-ty/src/display.rs index ae2162dd7c..1d4bf9ecb7 100644 --- a/crates/hir-ty/src/display.rs +++ b/crates/hir-ty/src/display.rs @@ -12,7 +12,7 @@ use hir_def::{ find_path, generics::{TypeOrConstParamData, TypeParamProvenance}, item_scope::ItemInNs, - lang_item::LangItem, + lang_item::{LangItem, LangItemTarget}, path::{Path, PathKind}, type_ref::{ConstScalar, TraitBoundModifier, TypeBound, TypeRef}, visibility::Visibility, @@ -731,8 +731,30 @@ impl HirDisplay for Ty { )?; // FIXME: it would maybe be good to distinguish this from the alias type (when debug printing), and to show the substitution } - ImplTraitId::AsyncBlockTypeImplTrait(..) => { - write!(f, "impl Future { + let future_trait = db + .lang_item(body.module(db.upcast()).krate(), LangItem::Future) + .and_then(LangItemTarget::as_trait); + let output = future_trait.and_then(|t| { + db.trait_data(t).associated_type_by_name(&hir_expand::name!(Output)) + }); + write!(f, "impl ")?; + if let Some(t) = future_trait { + f.start_location_link(t.into()); + } + write!(f, "Future")?; + if let Some(_) = future_trait { + f.end_location_link(); + } + write!(f, "<")?; + if let Some(t) = output { + f.start_location_link(t.into()); + } + write!(f, "Output")?; + if let Some(_) = output { + f.end_location_link(); + } + write!(f, " = ")?; parameters.at(Interner, 0).hir_fmt(f)?; write!(f, ">")?; } diff --git a/crates/ide/src/inlay_hints.rs b/crates/ide/src/inlay_hints.rs index a9581db9ad..ac477339ec 100644 --- a/crates/ide/src/inlay_hints.rs +++ b/crates/ide/src/inlay_hints.rs @@ -294,10 +294,12 @@ fn label_of_ty( ) -> Result<(), HirDisplayError> { let iter_item_type = hint_iterator(sema, famous_defs, &ty); match iter_item_type { - Some((iter_trait, ty)) => { + Some((iter_trait, item, ty)) => { const LABEL_START: &str = "impl "; const LABEL_ITERATOR: &str = "Iterator"; - const LABEL_MIDDLE: &str = ", famous_defs: &FamousDefs<'_, '_>, ty: &hir::Type, -) -> Option<(hir::Trait, hir::Type)> { +) -> Option<(hir::Trait, hir::TypeAlias, hir::Type)> { let db = sema.db; let strukt = ty.strip_references().as_adt()?; let krate = strukt.module(db).krate(); @@ -460,7 +467,7 @@ fn hint_iterator( _ => None, })?; if let Some(ty) = ty.normalize_trait_assoc_type(db, &[], assoc_type_item) { - return Some((iter_trait, ty)); + return Some((iter_trait, assoc_type_item, ty)); } } diff --git a/crates/ide/src/inlay_hints/bind_pat.rs b/crates/ide/src/inlay_hints/bind_pat.rs index da44d95297..f5b5c44737 100644 --- a/crates/ide/src/inlay_hints/bind_pat.rs +++ b/crates/ide/src/inlay_hints/bind_pat.rs @@ -330,7 +330,20 @@ fn main(a: SliceIter<'_, Container>) { ), tooltip: "", }, - ") { ), tooltip: "", }, - ">", + "<", + InlayHintLabelPart { + text: "Item", + linked_location: Some( + FileRange { + file_id: FileId( + 1, + ), + range: 2643..2647, + }, + ), + tooltip: "", + }, + " = &&str>>", ], }, InlayHint { diff --git a/crates/ide/src/inlay_hints/chaining.rs b/crates/ide/src/inlay_hints/chaining.rs index 222ee59be8..0c54f084c1 100644 --- a/crates/ide/src/inlay_hints/chaining.rs +++ b/crates/ide/src/inlay_hints/chaining.rs @@ -440,7 +440,20 @@ fn main() { ), tooltip: "", }, - "", + "<", + InlayHintLabelPart { + text: "Item", + linked_location: Some( + FileRange { + file_id: FileId( + 1, + ), + range: 2643..2647, + }, + ), + tooltip: "", + }, + " = ()>", ], }, InlayHint { @@ -460,7 +473,20 @@ fn main() { ), tooltip: "", }, - "", + "<", + InlayHintLabelPart { + text: "Item", + linked_location: Some( + FileRange { + file_id: FileId( + 1, + ), + range: 2643..2647, + }, + ), + tooltip: "", + }, + " = ()>", ], }, InlayHint { @@ -480,7 +506,20 @@ fn main() { ), tooltip: "", }, - "", + "<", + InlayHintLabelPart { + text: "Item", + linked_location: Some( + FileRange { + file_id: FileId( + 1, + ), + range: 2643..2647, + }, + ), + tooltip: "", + }, + " = ()>", ], }, InlayHint {