mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-10-01 11:31:15 +00:00
Impl HirDisplay
for SelfParam
This commit is contained in:
parent
6918eb6ebd
commit
6a2f83a8a2
@ -8,7 +8,6 @@ use hir_def::{
|
|||||||
type_ref::{TypeBound, TypeRef},
|
type_ref::{TypeBound, TypeRef},
|
||||||
AdtId, GenericDefId,
|
AdtId, GenericDefId,
|
||||||
};
|
};
|
||||||
use hir_expand::name;
|
|
||||||
use hir_ty::{
|
use hir_ty::{
|
||||||
display::{
|
display::{
|
||||||
write_bounds_like_dyn_trait_with_prefix, write_visibility, HirDisplay, HirDisplayError,
|
write_bounds_like_dyn_trait_with_prefix, write_visibility, HirDisplay, HirDisplayError,
|
||||||
@ -19,8 +18,9 @@ use hir_ty::{
|
|||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
Adt, AsAssocItem, AssocItemContainer, Const, ConstParam, Enum, ExternCrateDecl, Field,
|
Adt, AsAssocItem, AssocItemContainer, Const, ConstParam, Enum, ExternCrateDecl, Field,
|
||||||
Function, GenericParam, HasCrate, HasVisibility, LifetimeParam, Macro, Module, Static, Struct,
|
Function, GenericParam, HasCrate, HasVisibility, LifetimeParam, Macro, Module, SelfParam,
|
||||||
Trait, TraitAlias, TyBuilder, Type, TypeAlias, TypeOrConstParam, TypeParam, Union, Variant,
|
Static, Struct, Trait, TraitAlias, TyBuilder, Type, TypeAlias, TypeOrConstParam, TypeParam,
|
||||||
|
Union, Variant,
|
||||||
};
|
};
|
||||||
|
|
||||||
impl HirDisplay for Function {
|
impl HirDisplay for Function {
|
||||||
@ -57,37 +57,21 @@ impl HirDisplay for Function {
|
|||||||
|
|
||||||
f.write_char('(')?;
|
f.write_char('(')?;
|
||||||
|
|
||||||
let write_self_param = |ty: &TypeRef, f: &mut HirFormatter<'_>| match ty {
|
|
||||||
TypeRef::Path(p) if p.is_self_type() => f.write_str("self"),
|
|
||||||
TypeRef::Reference(inner, lifetime, mut_) if matches!(&**inner, TypeRef::Path(p) if p.is_self_type()) =>
|
|
||||||
{
|
|
||||||
f.write_char('&')?;
|
|
||||||
if let Some(lifetime) = lifetime {
|
|
||||||
write!(f, "{} ", lifetime.name.display(f.db.upcast()))?;
|
|
||||||
}
|
|
||||||
if let hir_def::type_ref::Mutability::Mut = mut_ {
|
|
||||||
f.write_str("mut ")?;
|
|
||||||
}
|
|
||||||
f.write_str("self")
|
|
||||||
}
|
|
||||||
_ => {
|
|
||||||
f.write_str("self: ")?;
|
|
||||||
ty.hir_fmt(f)
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
let mut first = true;
|
let mut first = true;
|
||||||
|
let mut skip_self = 0;
|
||||||
|
if let Some(self_param) = self.self_param(db) {
|
||||||
|
self_param.hir_fmt(f)?;
|
||||||
|
first = false;
|
||||||
|
skip_self = 1;
|
||||||
|
}
|
||||||
|
|
||||||
// FIXME: Use resolved `param.ty` once we no longer discard lifetimes
|
// FIXME: Use resolved `param.ty` once we no longer discard lifetimes
|
||||||
for (type_ref, param) in data.params.iter().zip(self.assoc_fn_params(db)) {
|
for (type_ref, param) in data.params.iter().zip(self.assoc_fn_params(db)).skip(skip_self) {
|
||||||
let local = param.as_local(db).map(|it| it.name(db));
|
let local = param.as_local(db).map(|it| it.name(db));
|
||||||
if !first {
|
if !first {
|
||||||
f.write_str(", ")?;
|
f.write_str(", ")?;
|
||||||
} else {
|
} else {
|
||||||
first = false;
|
first = false;
|
||||||
if local == Some(name!(self)) {
|
|
||||||
write_self_param(type_ref, f)?;
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
match local {
|
match local {
|
||||||
Some(name) => write!(f, "{}: ", name.display(f.db.upcast()))?,
|
Some(name) => write!(f, "{}: ", name.display(f.db.upcast()))?,
|
||||||
@ -137,6 +121,31 @@ impl HirDisplay for Function {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl HirDisplay for SelfParam {
|
||||||
|
fn hir_fmt(&self, f: &mut HirFormatter<'_>) -> Result<(), HirDisplayError> {
|
||||||
|
let data = f.db.function_data(self.func);
|
||||||
|
let param = data.params.first().unwrap();
|
||||||
|
match &**param {
|
||||||
|
TypeRef::Path(p) if p.is_self_type() => f.write_str("self"),
|
||||||
|
TypeRef::Reference(inner, lifetime, mut_) if matches!(&**inner, TypeRef::Path(p) if p.is_self_type()) =>
|
||||||
|
{
|
||||||
|
f.write_char('&')?;
|
||||||
|
if let Some(lifetime) = lifetime {
|
||||||
|
write!(f, "{} ", lifetime.name.display(f.db.upcast()))?;
|
||||||
|
}
|
||||||
|
if let hir_def::type_ref::Mutability::Mut = mut_ {
|
||||||
|
f.write_str("mut ")?;
|
||||||
|
}
|
||||||
|
f.write_str("self")
|
||||||
|
}
|
||||||
|
ty => {
|
||||||
|
f.write_str("self: ")?;
|
||||||
|
ty.hir_fmt(f)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl HirDisplay for Adt {
|
impl HirDisplay for Adt {
|
||||||
fn hir_fmt(&self, f: &mut HirFormatter<'_>) -> Result<(), HirDisplayError> {
|
fn hir_fmt(&self, f: &mut HirFormatter<'_>) -> Result<(), HirDisplayError> {
|
||||||
match self {
|
match self {
|
||||||
|
@ -2096,14 +2096,6 @@ impl SelfParam {
|
|||||||
.unwrap_or(Access::Owned)
|
.unwrap_or(Access::Owned)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn display(self, db: &dyn HirDatabase) -> &'static str {
|
|
||||||
match self.access(db) {
|
|
||||||
Access::Shared => "&self",
|
|
||||||
Access::Exclusive => "&mut self",
|
|
||||||
Access::Owned => "self",
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn source(&self, db: &dyn HirDatabase) -> Option<InFile<ast::SelfParam>> {
|
pub fn source(&self, db: &dyn HirDatabase) -> Option<InFile<ast::SelfParam>> {
|
||||||
let InFile { file_id, value } = Function::from(self.func).source(db)?;
|
let InFile { file_id, value } = Function::from(self.func).source(db)?;
|
||||||
value
|
value
|
||||||
|
Loading…
x
Reference in New Issue
Block a user