2022-08-15 06:51:45 -05:00
|
|
|
//! Display and pretty printing routines.
|
|
|
|
|
|
|
|
use std::fmt::{self, Write};
|
|
|
|
|
2023-09-06 08:21:41 -05:00
|
|
|
use hir_expand::mod_path::PathKind;
|
2023-01-09 12:29:28 -06:00
|
|
|
use intern::Interned;
|
2022-08-15 06:51:45 -05:00
|
|
|
use itertools::Itertools;
|
|
|
|
|
|
|
|
use crate::{
|
2023-09-06 08:21:41 -05:00
|
|
|
db::DefDatabase,
|
|
|
|
lang_item::LangItemTarget,
|
2022-08-15 06:51:45 -05:00
|
|
|
path::{GenericArg, GenericArgs, Path},
|
|
|
|
type_ref::{Mutability, TraitBoundModifier, TypeBound, TypeRef},
|
|
|
|
};
|
|
|
|
|
2023-09-06 08:21:41 -05:00
|
|
|
pub(crate) fn print_path(db: &dyn DefDatabase, path: &Path, buf: &mut dyn Write) -> fmt::Result {
|
|
|
|
if let Path::LangItem(it, s) = path {
|
|
|
|
write!(buf, "builtin#lang(")?;
|
|
|
|
match *it {
|
|
|
|
LangItemTarget::ImplDef(it) => write!(buf, "{it:?}")?,
|
|
|
|
LangItemTarget::EnumId(it) => {
|
|
|
|
write!(buf, "{}", db.enum_data(it).name.display(db.upcast()))?
|
|
|
|
}
|
|
|
|
LangItemTarget::Function(it) => {
|
|
|
|
write!(buf, "{}", db.function_data(it).name.display(db.upcast()))?
|
|
|
|
}
|
|
|
|
LangItemTarget::Static(it) => {
|
|
|
|
write!(buf, "{}", db.static_data(it).name.display(db.upcast()))?
|
|
|
|
}
|
|
|
|
LangItemTarget::Struct(it) => {
|
|
|
|
write!(buf, "{}", db.struct_data(it).name.display(db.upcast()))?
|
|
|
|
}
|
|
|
|
LangItemTarget::Union(it) => {
|
|
|
|
write!(buf, "{}", db.union_data(it).name.display(db.upcast()))?
|
|
|
|
}
|
|
|
|
LangItemTarget::TypeAlias(it) => {
|
|
|
|
write!(buf, "{}", db.type_alias_data(it).name.display(db.upcast()))?
|
|
|
|
}
|
|
|
|
LangItemTarget::Trait(it) => {
|
|
|
|
write!(buf, "{}", db.trait_data(it).name.display(db.upcast()))?
|
|
|
|
}
|
2024-01-15 03:24:14 -06:00
|
|
|
LangItemTarget::EnumVariant(it) => {
|
|
|
|
write!(buf, "{}", db.enum_variant_data(it).name.display(db.upcast()))?
|
|
|
|
}
|
2023-09-06 08:21:41 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
if let Some(s) = s {
|
|
|
|
write!(buf, "::{}", s.display(db.upcast()))?;
|
|
|
|
}
|
|
|
|
return write!(buf, ")");
|
2023-05-12 09:47:15 -05:00
|
|
|
}
|
2022-08-15 06:51:45 -05:00
|
|
|
match path.type_anchor() {
|
|
|
|
Some(anchor) => {
|
|
|
|
write!(buf, "<")?;
|
2023-05-24 11:04:29 -05:00
|
|
|
print_type_ref(db, anchor, buf)?;
|
2022-08-15 06:51:45 -05:00
|
|
|
write!(buf, ">::")?;
|
|
|
|
}
|
|
|
|
None => match path.kind() {
|
|
|
|
PathKind::Plain => {}
|
|
|
|
PathKind::Super(0) => write!(buf, "self")?,
|
|
|
|
PathKind::Super(n) => {
|
|
|
|
for i in 0..*n {
|
|
|
|
if i == 0 {
|
|
|
|
buf.write_str("super")?;
|
|
|
|
} else {
|
|
|
|
buf.write_str("::super")?;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
PathKind::Crate => write!(buf, "crate")?,
|
|
|
|
PathKind::Abs => {}
|
|
|
|
PathKind::DollarCrate(_) => write!(buf, "$crate")?,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i, segment) in path.segments().iter().enumerate() {
|
|
|
|
if i != 0 || !matches!(path.kind(), PathKind::Plain) {
|
|
|
|
write!(buf, "::")?;
|
|
|
|
}
|
|
|
|
|
2023-09-06 08:21:41 -05:00
|
|
|
write!(buf, "{}", segment.name.display(db.upcast()))?;
|
2022-08-15 06:51:45 -05:00
|
|
|
if let Some(generics) = segment.args_and_bindings {
|
|
|
|
write!(buf, "::<")?;
|
2023-05-24 11:04:29 -05:00
|
|
|
print_generic_args(db, generics, buf)?;
|
2022-08-15 06:51:45 -05:00
|
|
|
|
|
|
|
write!(buf, ">")?;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2023-05-24 11:04:29 -05:00
|
|
|
pub(crate) fn print_generic_args(
|
2023-09-06 08:21:41 -05:00
|
|
|
db: &dyn DefDatabase,
|
2023-05-24 11:04:29 -05:00
|
|
|
generics: &GenericArgs,
|
|
|
|
buf: &mut dyn Write,
|
|
|
|
) -> fmt::Result {
|
2022-08-15 06:51:45 -05:00
|
|
|
let mut first = true;
|
|
|
|
let args = if generics.has_self_type {
|
|
|
|
let (self_ty, args) = generics.args.split_first().unwrap();
|
|
|
|
write!(buf, "Self=")?;
|
2023-05-24 11:04:29 -05:00
|
|
|
print_generic_arg(db, self_ty, buf)?;
|
2022-08-15 06:51:45 -05:00
|
|
|
first = false;
|
|
|
|
args
|
|
|
|
} else {
|
|
|
|
&generics.args
|
|
|
|
};
|
|
|
|
for arg in args {
|
|
|
|
if !first {
|
|
|
|
write!(buf, ", ")?;
|
|
|
|
}
|
|
|
|
first = false;
|
2023-05-24 11:04:29 -05:00
|
|
|
print_generic_arg(db, arg, buf)?;
|
2022-08-15 06:51:45 -05:00
|
|
|
}
|
2023-02-14 10:24:40 -06:00
|
|
|
for binding in generics.bindings.iter() {
|
2022-08-15 06:51:45 -05:00
|
|
|
if !first {
|
|
|
|
write!(buf, ", ")?;
|
|
|
|
}
|
|
|
|
first = false;
|
2023-09-06 08:21:41 -05:00
|
|
|
write!(buf, "{}", binding.name.display(db.upcast()))?;
|
2022-08-15 06:51:45 -05:00
|
|
|
if !binding.bounds.is_empty() {
|
|
|
|
write!(buf, ": ")?;
|
2023-05-24 11:04:29 -05:00
|
|
|
print_type_bounds(db, &binding.bounds, buf)?;
|
2022-08-15 06:51:45 -05:00
|
|
|
}
|
|
|
|
if let Some(ty) = &binding.type_ref {
|
|
|
|
write!(buf, " = ")?;
|
2023-05-24 11:04:29 -05:00
|
|
|
print_type_ref(db, ty, buf)?;
|
2022-08-15 06:51:45 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2023-05-24 11:04:29 -05:00
|
|
|
pub(crate) fn print_generic_arg(
|
2023-09-06 08:21:41 -05:00
|
|
|
db: &dyn DefDatabase,
|
2023-05-24 11:04:29 -05:00
|
|
|
arg: &GenericArg,
|
|
|
|
buf: &mut dyn Write,
|
|
|
|
) -> fmt::Result {
|
2022-08-15 06:51:45 -05:00
|
|
|
match arg {
|
2023-05-24 11:04:29 -05:00
|
|
|
GenericArg::Type(ty) => print_type_ref(db, ty, buf),
|
2023-09-06 08:21:41 -05:00
|
|
|
GenericArg::Const(c) => write!(buf, "{}", c.display(db.upcast())),
|
|
|
|
GenericArg::Lifetime(lt) => write!(buf, "{}", lt.name.display(db.upcast())),
|
2022-08-15 06:51:45 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-24 11:04:29 -05:00
|
|
|
pub(crate) fn print_type_ref(
|
2023-09-06 08:21:41 -05:00
|
|
|
db: &dyn DefDatabase,
|
2023-05-24 11:04:29 -05:00
|
|
|
type_ref: &TypeRef,
|
|
|
|
buf: &mut dyn Write,
|
|
|
|
) -> fmt::Result {
|
2022-08-15 06:51:45 -05:00
|
|
|
// FIXME: deduplicate with `HirDisplay` impl
|
|
|
|
match type_ref {
|
|
|
|
TypeRef::Never => write!(buf, "!")?,
|
|
|
|
TypeRef::Placeholder => write!(buf, "_")?,
|
|
|
|
TypeRef::Tuple(fields) => {
|
|
|
|
write!(buf, "(")?;
|
|
|
|
for (i, field) in fields.iter().enumerate() {
|
|
|
|
if i != 0 {
|
|
|
|
write!(buf, ", ")?;
|
|
|
|
}
|
2023-05-24 11:04:29 -05:00
|
|
|
print_type_ref(db, field, buf)?;
|
2022-08-15 06:51:45 -05:00
|
|
|
}
|
|
|
|
write!(buf, ")")?;
|
|
|
|
}
|
2023-05-24 11:04:29 -05:00
|
|
|
TypeRef::Path(path) => print_path(db, path, buf)?,
|
2022-08-15 06:51:45 -05:00
|
|
|
TypeRef::RawPtr(pointee, mtbl) => {
|
|
|
|
let mtbl = match mtbl {
|
|
|
|
Mutability::Shared => "*const",
|
|
|
|
Mutability::Mut => "*mut",
|
|
|
|
};
|
2022-12-23 12:42:58 -06:00
|
|
|
write!(buf, "{mtbl} ")?;
|
2023-05-24 11:04:29 -05:00
|
|
|
print_type_ref(db, pointee, buf)?;
|
2022-08-15 06:51:45 -05:00
|
|
|
}
|
|
|
|
TypeRef::Reference(pointee, lt, mtbl) => {
|
|
|
|
let mtbl = match mtbl {
|
|
|
|
Mutability::Shared => "",
|
|
|
|
Mutability::Mut => "mut ",
|
|
|
|
};
|
|
|
|
write!(buf, "&")?;
|
|
|
|
if let Some(lt) = lt {
|
2023-09-06 08:21:41 -05:00
|
|
|
write!(buf, "{} ", lt.name.display(db.upcast()))?;
|
2022-08-15 06:51:45 -05:00
|
|
|
}
|
2022-12-23 12:42:58 -06:00
|
|
|
write!(buf, "{mtbl}")?;
|
2023-05-24 11:04:29 -05:00
|
|
|
print_type_ref(db, pointee, buf)?;
|
2022-08-15 06:51:45 -05:00
|
|
|
}
|
|
|
|
TypeRef::Array(elem, len) => {
|
|
|
|
write!(buf, "[")?;
|
2023-05-24 11:04:29 -05:00
|
|
|
print_type_ref(db, elem, buf)?;
|
2023-09-06 08:21:41 -05:00
|
|
|
write!(buf, "; {}]", len.display(db.upcast()))?;
|
2022-08-15 06:51:45 -05:00
|
|
|
}
|
|
|
|
TypeRef::Slice(elem) => {
|
|
|
|
write!(buf, "[")?;
|
2023-05-24 11:04:29 -05:00
|
|
|
print_type_ref(db, elem, buf)?;
|
2022-08-15 06:51:45 -05:00
|
|
|
write!(buf, "]")?;
|
|
|
|
}
|
2024-01-17 11:23:14 -06:00
|
|
|
TypeRef::Fn(args_and_ret, varargs, is_unsafe, abi) => {
|
2022-08-15 06:51:45 -05:00
|
|
|
let ((_, return_type), args) =
|
|
|
|
args_and_ret.split_last().expect("TypeRef::Fn is missing return type");
|
2022-11-04 15:07:15 -05:00
|
|
|
if *is_unsafe {
|
|
|
|
write!(buf, "unsafe ")?;
|
|
|
|
}
|
2024-01-17 11:23:14 -06:00
|
|
|
if let Some(abi) = abi {
|
|
|
|
buf.write_str("extern ")?;
|
|
|
|
buf.write_str(abi)?;
|
|
|
|
buf.write_char(' ')?;
|
|
|
|
}
|
2022-08-15 06:51:45 -05:00
|
|
|
write!(buf, "fn(")?;
|
|
|
|
for (i, (_, typeref)) in args.iter().enumerate() {
|
|
|
|
if i != 0 {
|
|
|
|
write!(buf, ", ")?;
|
|
|
|
}
|
2023-05-24 11:04:29 -05:00
|
|
|
print_type_ref(db, typeref, buf)?;
|
2022-08-15 06:51:45 -05:00
|
|
|
}
|
|
|
|
if *varargs {
|
|
|
|
if !args.is_empty() {
|
|
|
|
write!(buf, ", ")?;
|
|
|
|
}
|
|
|
|
write!(buf, "...")?;
|
|
|
|
}
|
|
|
|
write!(buf, ") -> ")?;
|
2023-05-24 11:04:29 -05:00
|
|
|
print_type_ref(db, return_type, buf)?;
|
2022-08-15 06:51:45 -05:00
|
|
|
}
|
|
|
|
TypeRef::Macro(_ast_id) => {
|
|
|
|
write!(buf, "<macro>")?;
|
|
|
|
}
|
|
|
|
TypeRef::Error => write!(buf, "{{unknown}}")?,
|
|
|
|
TypeRef::ImplTrait(bounds) => {
|
|
|
|
write!(buf, "impl ")?;
|
2023-05-24 11:04:29 -05:00
|
|
|
print_type_bounds(db, bounds, buf)?;
|
2022-08-15 06:51:45 -05:00
|
|
|
}
|
|
|
|
TypeRef::DynTrait(bounds) => {
|
|
|
|
write!(buf, "dyn ")?;
|
2023-05-24 11:04:29 -05:00
|
|
|
print_type_bounds(db, bounds, buf)?;
|
2022-08-15 06:51:45 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) fn print_type_bounds(
|
2023-09-06 08:21:41 -05:00
|
|
|
db: &dyn DefDatabase,
|
2022-08-15 06:51:45 -05:00
|
|
|
bounds: &[Interned<TypeBound>],
|
|
|
|
buf: &mut dyn Write,
|
|
|
|
) -> fmt::Result {
|
|
|
|
for (i, bound) in bounds.iter().enumerate() {
|
|
|
|
if i != 0 {
|
|
|
|
write!(buf, " + ")?;
|
|
|
|
}
|
|
|
|
|
|
|
|
match bound.as_ref() {
|
|
|
|
TypeBound::Path(path, modifier) => {
|
|
|
|
match modifier {
|
|
|
|
TraitBoundModifier::None => (),
|
|
|
|
TraitBoundModifier::Maybe => write!(buf, "?")?,
|
|
|
|
}
|
2023-05-24 11:04:29 -05:00
|
|
|
print_path(db, path, buf)?;
|
2022-08-15 06:51:45 -05:00
|
|
|
}
|
|
|
|
TypeBound::ForLifetime(lifetimes, path) => {
|
2023-09-06 08:21:41 -05:00
|
|
|
write!(
|
|
|
|
buf,
|
|
|
|
"for<{}> ",
|
|
|
|
lifetimes.iter().map(|it| it.display(db.upcast())).format(", ")
|
|
|
|
)?;
|
2023-05-24 11:04:29 -05:00
|
|
|
print_path(db, path, buf)?;
|
2022-08-15 06:51:45 -05:00
|
|
|
}
|
2023-09-06 08:21:41 -05:00
|
|
|
TypeBound::Lifetime(lt) => write!(buf, "{}", lt.name.display(db.upcast()))?,
|
2022-08-15 06:51:45 -05:00
|
|
|
TypeBound::Error => write!(buf, "{{unknown}}")?,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|