mirror of
https://github.com/rust-lang/rust-analyzer
synced 2025-01-26 03:45:04 +00:00
Make impl HirDisplay for TraitRef
provide just the trait type
This commit is contained in:
parent
34dc94bb2d
commit
03cb63cc75
4 changed files with 24 additions and 68 deletions
|
@ -474,7 +474,9 @@ impl HirDisplay for ProjectionTy {
|
||||||
|
|
||||||
let trait_ref = self.trait_ref(f.db);
|
let trait_ref = self.trait_ref(f.db);
|
||||||
write!(f, "<")?;
|
write!(f, "<")?;
|
||||||
fmt_trait_ref(f, &trait_ref, TraitRefFormat::SelfAsTrait)?;
|
trait_ref.self_type_parameter(Interner).hir_fmt(f)?;
|
||||||
|
write!(f, " as ")?;
|
||||||
|
trait_ref.hir_fmt(f)?;
|
||||||
write!(
|
write!(
|
||||||
f,
|
f,
|
||||||
">::{}",
|
">::{}",
|
||||||
|
@ -1775,50 +1777,14 @@ fn write_bounds_like_dyn_trait(
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Clone, Copy)]
|
impl HirDisplay for TraitRef {
|
||||||
pub enum TraitRefFormat {
|
|
||||||
SelfAsTrait,
|
|
||||||
SelfImplementsTrait,
|
|
||||||
OnlyTrait,
|
|
||||||
}
|
|
||||||
|
|
||||||
fn fmt_trait_ref(
|
|
||||||
f: &mut HirFormatter<'_>,
|
|
||||||
tr: &TraitRef,
|
|
||||||
format: TraitRefFormat,
|
|
||||||
) -> Result<(), HirDisplayError> {
|
|
||||||
if f.should_truncate() {
|
|
||||||
return write!(f, "{TYPE_HINT_TRUNCATION}");
|
|
||||||
}
|
|
||||||
|
|
||||||
match format {
|
|
||||||
TraitRefFormat::SelfAsTrait => {
|
|
||||||
tr.self_type_parameter(Interner).hir_fmt(f)?;
|
|
||||||
write!(f, " as ")?;
|
|
||||||
}
|
|
||||||
TraitRefFormat::SelfImplementsTrait => {
|
|
||||||
tr.self_type_parameter(Interner).hir_fmt(f)?;
|
|
||||||
write!(f, ": ")?;
|
|
||||||
}
|
|
||||||
TraitRefFormat::OnlyTrait => {}
|
|
||||||
}
|
|
||||||
|
|
||||||
let trait_ = tr.hir_trait_id();
|
|
||||||
f.start_location_link(trait_.into());
|
|
||||||
write!(f, "{}", f.db.trait_data(trait_).name.display(f.db.upcast(), f.edition()))?;
|
|
||||||
f.end_location_link();
|
|
||||||
let substs = tr.substitution.as_slice(Interner);
|
|
||||||
hir_fmt_generics(f, &substs[1..], None, substs[0].ty(Interner))
|
|
||||||
}
|
|
||||||
|
|
||||||
pub struct TraitRefDisplayWrapper {
|
|
||||||
pub trait_ref: TraitRef,
|
|
||||||
pub format: TraitRefFormat,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl HirDisplay for TraitRefDisplayWrapper {
|
|
||||||
fn hir_fmt(&self, f: &mut HirFormatter<'_>) -> Result<(), HirDisplayError> {
|
fn hir_fmt(&self, f: &mut HirFormatter<'_>) -> Result<(), HirDisplayError> {
|
||||||
fmt_trait_ref(f, &self.trait_ref, self.format)
|
let trait_ = self.hir_trait_id();
|
||||||
|
f.start_location_link(trait_.into());
|
||||||
|
write!(f, "{}", f.db.trait_data(trait_).name.display(f.db.upcast(), f.edition()))?;
|
||||||
|
f.end_location_link();
|
||||||
|
let substs = self.substitution.as_slice(Interner);
|
||||||
|
hir_fmt_generics(f, &substs[1..], None, substs[0].ty(Interner))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1830,11 +1796,16 @@ impl HirDisplay for WhereClause {
|
||||||
|
|
||||||
match self {
|
match self {
|
||||||
WhereClause::Implemented(trait_ref) => {
|
WhereClause::Implemented(trait_ref) => {
|
||||||
fmt_trait_ref(f, trait_ref, TraitRefFormat::SelfImplementsTrait)?;
|
trait_ref.self_type_parameter(Interner).hir_fmt(f)?;
|
||||||
|
write!(f, ": ")?;
|
||||||
|
trait_ref.hir_fmt(f)?;
|
||||||
}
|
}
|
||||||
WhereClause::AliasEq(AliasEq { alias: AliasTy::Projection(projection_ty), ty }) => {
|
WhereClause::AliasEq(AliasEq { alias: AliasTy::Projection(projection_ty), ty }) => {
|
||||||
write!(f, "<")?;
|
write!(f, "<")?;
|
||||||
fmt_trait_ref(f, &projection_ty.trait_ref(f.db), TraitRefFormat::SelfAsTrait)?;
|
let trait_ref = &projection_ty.trait_ref(f.db);
|
||||||
|
trait_ref.self_type_parameter(Interner).hir_fmt(f)?;
|
||||||
|
write!(f, " as ")?;
|
||||||
|
trait_ref.hir_fmt(f)?;
|
||||||
write!(f, ">::",)?;
|
write!(f, ">::",)?;
|
||||||
let type_alias = from_assoc_type_id(projection_ty.associated_ty_id);
|
let type_alias = from_assoc_type_id(projection_ty.associated_ty_id);
|
||||||
f.start_location_link(type_alias.into());
|
f.start_location_link(type_alias.into());
|
||||||
|
|
|
@ -743,18 +743,9 @@ impl HirDisplay for Static {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct TraitRefDisplayWrapper {
|
impl HirDisplay for TraitRef {
|
||||||
pub trait_ref: TraitRef,
|
|
||||||
pub format: hir_ty::display::TraitRefFormat,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl HirDisplay for TraitRefDisplayWrapper {
|
|
||||||
fn hir_fmt(&self, f: &mut HirFormatter<'_>) -> Result<(), HirDisplayError> {
|
fn hir_fmt(&self, f: &mut HirFormatter<'_>) -> Result<(), HirDisplayError> {
|
||||||
hir_ty::display::TraitRefDisplayWrapper {
|
self.trait_ref.hir_fmt(f)
|
||||||
format: self.format,
|
|
||||||
trait_ref: self.trait_ref.trait_ref.clone(),
|
|
||||||
}
|
|
||||||
.hir_fmt(f)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -96,7 +96,6 @@ use crate::db::{DefDatabase, HirDatabase};
|
||||||
pub use crate::{
|
pub use crate::{
|
||||||
attrs::{resolve_doc_path_on, HasAttrs},
|
attrs::{resolve_doc_path_on, HasAttrs},
|
||||||
diagnostics::*,
|
diagnostics::*,
|
||||||
display::TraitRefDisplayWrapper,
|
|
||||||
has_source::HasSource,
|
has_source::HasSource,
|
||||||
semantics::{
|
semantics::{
|
||||||
PathResolution, Semantics, SemanticsImpl, SemanticsScope, TypeInfo, VisibleTraits,
|
PathResolution, Semantics, SemanticsImpl, SemanticsScope, TypeInfo, VisibleTraits,
|
||||||
|
@ -149,7 +148,7 @@ pub use {
|
||||||
hir_ty::{
|
hir_ty::{
|
||||||
consteval::ConstEvalError,
|
consteval::ConstEvalError,
|
||||||
diagnostics::UnsafetyReason,
|
diagnostics::UnsafetyReason,
|
||||||
display::{ClosureStyle, HirDisplay, HirDisplayError, HirWrite, TraitRefFormat},
|
display::{ClosureStyle, HirDisplay, HirDisplayError, HirWrite},
|
||||||
dyn_compatibility::{DynCompatibilityViolation, MethodViolationCode},
|
dyn_compatibility::{DynCompatibilityViolation, MethodViolationCode},
|
||||||
layout::LayoutError,
|
layout::LayoutError,
|
||||||
mir::{MirEvalError, MirLowerError},
|
mir::{MirEvalError, MirLowerError},
|
||||||
|
|
|
@ -3,10 +3,7 @@
|
||||||
|
|
||||||
use core::fmt;
|
use core::fmt;
|
||||||
|
|
||||||
use hir::{
|
use hir::{Adt, AsAssocItem, Crate, HirDisplay, MacroKind, Semantics};
|
||||||
Adt, AsAssocItem, Crate, HirDisplay, MacroKind, Semantics, TraitRefDisplayWrapper,
|
|
||||||
TraitRefFormat,
|
|
||||||
};
|
|
||||||
use ide_db::{
|
use ide_db::{
|
||||||
base_db::{CrateOrigin, LangCrateOrigin},
|
base_db::{CrateOrigin, LangCrateOrigin},
|
||||||
defs::{Definition, IdentClass},
|
defs::{Definition, IdentClass},
|
||||||
|
@ -312,15 +309,13 @@ fn def_to_non_local_moniker(
|
||||||
match def {
|
match def {
|
||||||
Definition::SelfType(impl_) => {
|
Definition::SelfType(impl_) => {
|
||||||
if let Some(trait_ref) = impl_.trait_ref(db) {
|
if let Some(trait_ref) = impl_.trait_ref(db) {
|
||||||
// Trait impls use `trait_type` constraint syntax for the 2nd parameter.
|
// Trait impls use the trait type for the 2nd parameter.
|
||||||
let trait_ref_for_display =
|
|
||||||
TraitRefDisplayWrapper { trait_ref, format: TraitRefFormat::OnlyTrait };
|
|
||||||
reverse_description.push(MonikerDescriptor {
|
reverse_description.push(MonikerDescriptor {
|
||||||
name: display(db, edition, module, trait_ref_for_display),
|
name: display(db, edition, module, trait_ref),
|
||||||
desc: MonikerDescriptorKind::TypeParameter,
|
desc: MonikerDescriptorKind::TypeParameter,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
// Both inherent and trait impls use `self_type` as the first parameter.
|
// Both inherent and trait impls use the self type for the first parameter.
|
||||||
reverse_description.push(MonikerDescriptor {
|
reverse_description.push(MonikerDescriptor {
|
||||||
name: display(db, edition, module, impl_.self_ty(db)),
|
name: display(db, edition, module, impl_.self_ty(db)),
|
||||||
desc: MonikerDescriptorKind::TypeParameter,
|
desc: MonikerDescriptorKind::TypeParameter,
|
||||||
|
|
Loading…
Reference in a new issue