Improve ItemTree pretty print output

This commit is contained in:
Jonas Schievink 2022-04-14 14:24:27 +02:00
parent 4cee507d28
commit fbded178fa
2 changed files with 50 additions and 38 deletions

View file

@ -90,11 +90,10 @@ impl<'a> Printer<'a> {
for attr in &**attrs { for attr in &**attrs {
wln!( wln!(
self, self,
"#{}[{}{}] // {:?}", "#{}[{}{}]",
inner, inner,
attr.path, attr.path,
attr.input.as_ref().map(|it| it.to_string()).unwrap_or_default(), attr.input.as_ref().map(|it| it.to_string()).unwrap_or_default(),
attr.id,
); );
} }
} }
@ -242,10 +241,19 @@ impl<'a> Printer<'a> {
ast_id: _, ast_id: _,
flags, flags,
} = &self.tree[it]; } = &self.tree[it];
if flags.bits != 0 {
wln!(self, "// flags = 0x{:X}", flags.bits);
}
self.print_visibility(*visibility); self.print_visibility(*visibility);
if flags.contains(FnFlags::HAS_DEFAULT_KW) {
w!(self, "default ");
}
if flags.contains(FnFlags::HAS_CONST_KW) {
w!(self, "const ");
}
if flags.contains(FnFlags::HAS_ASYNC_KW) {
w!(self, "async ");
}
if flags.contains(FnFlags::HAS_UNSAFE_KW) {
w!(self, "unsafe ");
}
if let Some(abi) = abi { if let Some(abi) = abi {
w!(self, "extern \"{}\" ", abi); w!(self, "extern \"{}\" ", abi);
} }
@ -254,7 +262,7 @@ impl<'a> Printer<'a> {
w!(self, "("); w!(self, "(");
if !params.is_empty() { if !params.is_empty() {
self.indented(|this| { self.indented(|this| {
for param in params.clone() { for (i, param) in params.clone().enumerate() {
this.print_attrs_of(param); this.print_attrs_of(param);
match &this.tree[param] { match &this.tree[param] {
Param::Normal(name, ty) => { Param::Normal(name, ty) => {
@ -263,7 +271,12 @@ impl<'a> Printer<'a> {
None => w!(this, "_: "), None => w!(this, "_: "),
} }
this.print_type_ref(ty); this.print_type_ref(ty);
wln!(this, ","); w!(this, ",");
if flags.contains(FnFlags::HAS_SELF_PARAM) && i == 0 {
wln!(this, " // self");
} else {
wln!(this);
}
} }
Param::Varargs => { Param::Varargs => {
wln!(this, "..."); wln!(this, "...");
@ -275,7 +288,11 @@ impl<'a> Printer<'a> {
w!(self, ") -> "); w!(self, ") -> ");
self.print_type_ref(ret_type); self.print_type_ref(ret_type);
self.print_where_clause(explicit_generic_params); self.print_where_clause(explicit_generic_params);
wln!(self, ";"); if flags.contains(FnFlags::HAS_BODY) {
wln!(self, " {{ ... }}");
} else {
wln!(self, ";");
}
} }
ModItem::Struct(it) => { ModItem::Struct(it) => {
let Struct { visibility, name, fields, generic_params, ast_id: _ } = &self.tree[it]; let Struct { visibility, name, fields, generic_params, ast_id: _ } = &self.tree[it];

View file

@ -30,9 +30,9 @@ use crate::{A, B};
use a::{c, d::{e}}; use a::{c, d::{e}};
"#, "#,
expect![[r##" expect![[r##"
#![doc = " file comment"] // AttrId { ast_index: 0 } #![doc = " file comment"]
#![no_std] // AttrId { ast_index: 1 } #![no_std]
#![doc = " another file comment"] // AttrId { ast_index: 2 } #![doc = " another file comment"]
pub(self) extern crate self as renamed; pub(self) extern crate self as renamed;
@ -42,7 +42,7 @@ use a::{c, d::{e}};
pub(self) use globs::*; pub(self) use globs::*;
#[doc = " docs on import"] // AttrId { ast_index: 0 } #[doc = " docs on import"]
pub(self) use crate::{A, B}; pub(self) use crate::{A, B};
pub(self) use a::{c, d::{e}}; pub(self) use a::{c, d::{e}};
@ -67,15 +67,15 @@ extern "C" {
} }
"#, "#,
expect![[r##" expect![[r##"
#[on_extern_block] // AttrId { ast_index: 0 } #[on_extern_block]
extern "C" { extern "C" {
#[on_extern_type] // AttrId { ast_index: 0 } #[on_extern_type]
pub(self) type ExType; pub(self) type ExType;
#[on_extern_static] // AttrId { ast_index: 0 } #[on_extern_static]
pub(self) static EX_STATIC: u8 = _; pub(self) static EX_STATIC: u8 = _;
#[on_extern_fn] // AttrId { ast_index: 0 } #[on_extern_fn]
pub(self) fn ex_fn() -> (); pub(self) fn ex_fn() -> ();
} }
"##]], "##]],
@ -115,14 +115,14 @@ enum E {
expect![[r##" expect![[r##"
pub(self) struct Unit; pub(self) struct Unit;
#[derive(Debug)] // AttrId { ast_index: 0 } #[derive(Debug)]
pub(self) struct Struct { pub(self) struct Struct {
#[doc = " fld docs"] // AttrId { ast_index: 0 } #[doc = " fld docs"]
pub(self) fld: (), pub(self) fld: (),
} }
pub(self) struct Tuple( pub(self) struct Tuple(
#[attr] // AttrId { ast_index: 0 } #[attr]
pub(self) 0: u8, pub(self) 0: u8,
); );
@ -132,14 +132,14 @@ enum E {
} }
pub(self) enum E { pub(self) enum E {
#[doc = " comment on Unit"] // AttrId { ast_index: 0 } #[doc = " comment on Unit"]
Unit, Unit,
#[doc = " comment on Tuple"] // AttrId { ast_index: 0 } #[doc = " comment on Tuple"]
Tuple( Tuple(
pub(self) 0: u8, pub(self) 0: u8,
), ),
Struct { Struct {
#[doc = " comment on a: u8"] // AttrId { ast_index: 0 } #[doc = " comment on a: u8"]
pub(self) a: u8, pub(self) a: u8,
}, },
} }
@ -170,14 +170,13 @@ trait Tr: SuperTrait + 'lifetime {
pub(self) const _: Anon = _; pub(self) const _: Anon = _;
#[attr] // AttrId { ast_index: 0 } #[attr]
#[inner_attr_in_fn] // AttrId { ast_index: 1 } #[inner_attr_in_fn]
// flags = 0x2
pub(self) fn f( pub(self) fn f(
#[attr] // AttrId { ast_index: 0 } #[attr]
arg: u8, arg: u8,
_: (), _: (),
) -> (); ) -> () { ... }
pub(self) trait Tr<Self> pub(self) trait Tr<Self>
where where
@ -186,9 +185,8 @@ trait Tr: SuperTrait + 'lifetime {
{ {
pub(self) type Assoc: AssocBound = Default; pub(self) type Assoc: AssocBound = Default;
// flags = 0x1
pub(self) fn method( pub(self) fn method(
_: &Self, _: &Self, // self
) -> (); ) -> ();
} }
"##]], "##]],
@ -211,13 +209,12 @@ mod inline {
mod outline; mod outline;
"#, "#,
expect![[r##" expect![[r##"
#[doc = " outer"] // AttrId { ast_index: 0 } #[doc = " outer"]
#[doc = " inner"] // AttrId { ast_index: 1 } #[doc = " inner"]
pub(self) mod inline { pub(self) mod inline {
pub(self) use super::*; pub(self) use super::*;
// flags = 0x2 pub(self) fn fn_in_module() -> () { ... }
pub(self) fn fn_in_module() -> ();
} }
pub(self) mod outline; pub(self) mod outline;
@ -338,12 +335,11 @@ trait Tr<'a, T: 'a>: Super where Self: for<'a> Tr<'a, T> {}
T: 'a, T: 'a,
T: 'b T: 'b
{ {
// flags = 0x2
pub(self) fn f<G>( pub(self) fn f<G>(
arg: impl Copy, arg: impl Copy,
) -> impl Copy ) -> impl Copy
where where
G: 'a; G: 'a { ... }
} }
pub(self) enum Enum<'a, T, const U: u8> { pub(self) enum Enum<'a, T, const U: u8> {
@ -392,10 +388,9 @@ pub(crate) trait Tr {
pub(crate) trait Tr<Self> { pub(crate) trait Tr<Self> {
pub(crate) fn f() -> (); pub(crate) fn f() -> ();
// flags = 0x3
pub(crate) fn method( pub(crate) fn method(
_: &Self, _: &Self, // self
) -> (); ) -> () { ... }
} }
"#]], "#]],
) )