Render AstIds in item-tree view

This commit is contained in:
Lukas Wirth 2024-01-16 10:47:28 +01:00
parent cf905cff76
commit 90a1b484f7
2 changed files with 132 additions and 31 deletions

View file

@ -2,6 +2,8 @@
use std::fmt::{self, Write}; use std::fmt::{self, Write};
use span::ErasedFileAstId;
use crate::{ use crate::{
generics::{TypeOrConstParamData, WherePredicate, WherePredicateTypeTarget}, generics::{TypeOrConstParamData, WherePredicate, WherePredicateTypeTarget},
pretty::{print_path, print_type_bounds, print_type_ref}, pretty::{print_path, print_type_bounds, print_type_ref},
@ -118,7 +120,11 @@ impl Printer<'_> {
w!(self, "{{"); w!(self, "{{");
self.indented(|this| { self.indented(|this| {
for field in fields.clone() { for field in fields.clone() {
let Field { visibility, name, type_ref, ast_id: _ } = &this.tree[field]; let Field { visibility, name, type_ref, ast_id } = &this.tree[field];
this.print_ast_id(match ast_id {
FieldAstId::Record(it) => it.erase(),
FieldAstId::Tuple(it) => it.erase(),
});
this.print_attrs_of(field, "\n"); this.print_attrs_of(field, "\n");
this.print_visibility(*visibility); this.print_visibility(*visibility);
w!(this, "{}: ", name.display(self.db.upcast())); w!(this, "{}: ", name.display(self.db.upcast()));
@ -132,7 +138,11 @@ impl Printer<'_> {
w!(self, "("); w!(self, "(");
self.indented(|this| { self.indented(|this| {
for field in fields.clone() { for field in fields.clone() {
let Field { visibility, name, type_ref, ast_id: _ } = &this.tree[field]; let Field { visibility, name, type_ref, ast_id } = &this.tree[field];
this.print_ast_id(match ast_id {
FieldAstId::Record(it) => it.erase(),
FieldAstId::Tuple(it) => it.erase(),
});
this.print_attrs_of(field, "\n"); this.print_attrs_of(field, "\n");
this.print_visibility(*visibility); this.print_visibility(*visibility);
w!(this, "{}: ", name.display(self.db.upcast())); w!(this, "{}: ", name.display(self.db.upcast()));
@ -200,14 +210,16 @@ impl Printer<'_> {
match item { match item {
ModItem::Use(it) => { ModItem::Use(it) => {
let Use { visibility, use_tree, ast_id: _ } = &self.tree[it]; let Use { visibility, use_tree, ast_id } = &self.tree[it];
self.print_ast_id(ast_id.erase());
self.print_visibility(*visibility); self.print_visibility(*visibility);
w!(self, "use "); w!(self, "use ");
self.print_use_tree(use_tree); self.print_use_tree(use_tree);
wln!(self, ";"); wln!(self, ";");
} }
ModItem::ExternCrate(it) => { ModItem::ExternCrate(it) => {
let ExternCrate { name, alias, visibility, ast_id: _ } = &self.tree[it]; let ExternCrate { name, alias, visibility, ast_id } = &self.tree[it];
self.print_ast_id(ast_id.erase());
self.print_visibility(*visibility); self.print_visibility(*visibility);
w!(self, "extern crate {}", name.display(self.db.upcast())); w!(self, "extern crate {}", name.display(self.db.upcast()));
if let Some(alias) = alias { if let Some(alias) = alias {
@ -216,7 +228,8 @@ impl Printer<'_> {
wln!(self, ";"); wln!(self, ";");
} }
ModItem::ExternBlock(it) => { ModItem::ExternBlock(it) => {
let ExternBlock { abi, ast_id: _, children } = &self.tree[it]; let ExternBlock { abi, ast_id, children } = &self.tree[it];
self.print_ast_id(ast_id.erase());
w!(self, "extern "); w!(self, "extern ");
if let Some(abi) = abi { if let Some(abi) = abi {
w!(self, "\"{}\" ", abi); w!(self, "\"{}\" ", abi);
@ -237,9 +250,10 @@ impl Printer<'_> {
abi, abi,
params, params,
ret_type, ret_type,
ast_id: _, ast_id,
flags, flags,
} = &self.tree[it]; } = &self.tree[it];
self.print_ast_id(ast_id.erase());
self.print_visibility(*visibility); self.print_visibility(*visibility);
if flags.contains(FnFlags::HAS_DEFAULT_KW) { if flags.contains(FnFlags::HAS_DEFAULT_KW) {
w!(self, "default "); w!(self, "default ");
@ -263,7 +277,12 @@ impl Printer<'_> {
self.indented(|this| { self.indented(|this| {
for param in params.clone() { for param in params.clone() {
this.print_attrs_of(param, "\n"); this.print_attrs_of(param, "\n");
match &this.tree[param].type_ref { let Param { type_ref, ast_id } = &this.tree[param];
this.print_ast_id(match ast_id {
ParamAstId::Param(it) => it.erase(),
ParamAstId::SelfParam(it) => it.erase(),
});
match type_ref {
Some(ty) => { Some(ty) => {
if flags.contains(FnFlags::HAS_SELF_PARAM) { if flags.contains(FnFlags::HAS_SELF_PARAM) {
w!(this, "self: "); w!(this, "self: ");
@ -288,7 +307,8 @@ impl Printer<'_> {
} }
} }
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];
self.print_ast_id(ast_id.erase());
self.print_visibility(*visibility); self.print_visibility(*visibility);
w!(self, "struct {}", name.display(self.db.upcast())); w!(self, "struct {}", name.display(self.db.upcast()));
self.print_generic_params(generic_params); self.print_generic_params(generic_params);
@ -300,7 +320,8 @@ impl Printer<'_> {
} }
} }
ModItem::Union(it) => { ModItem::Union(it) => {
let Union { name, visibility, fields, generic_params, ast_id: _ } = &self.tree[it]; let Union { name, visibility, fields, generic_params, ast_id } = &self.tree[it];
self.print_ast_id(ast_id.erase());
self.print_visibility(*visibility); self.print_visibility(*visibility);
w!(self, "union {}", name.display(self.db.upcast())); w!(self, "union {}", name.display(self.db.upcast()));
self.print_generic_params(generic_params); self.print_generic_params(generic_params);
@ -312,14 +333,16 @@ impl Printer<'_> {
} }
} }
ModItem::Enum(it) => { ModItem::Enum(it) => {
let Enum { name, visibility, variants, generic_params, ast_id: _ } = &self.tree[it]; let Enum { name, visibility, variants, generic_params, ast_id } = &self.tree[it];
self.print_ast_id(ast_id.erase());
self.print_visibility(*visibility); self.print_visibility(*visibility);
w!(self, "enum {}", name.display(self.db.upcast())); w!(self, "enum {}", name.display(self.db.upcast()));
self.print_generic_params(generic_params); self.print_generic_params(generic_params);
self.print_where_clause_and_opening_brace(generic_params); self.print_where_clause_and_opening_brace(generic_params);
self.indented(|this| { self.indented(|this| {
for variant in FileItemTreeId::range_iter(variants.clone()) { for variant in FileItemTreeId::range_iter(variants.clone()) {
let Variant { name, fields, ast_id: _ } = &this.tree[variant]; let Variant { name, fields, ast_id } = &this.tree[variant];
this.print_ast_id(ast_id.erase());
this.print_attrs_of(variant, "\n"); this.print_attrs_of(variant, "\n");
w!(this, "{}", name.display(self.db.upcast())); w!(this, "{}", name.display(self.db.upcast()));
this.print_fields(fields); this.print_fields(fields);
@ -329,7 +352,8 @@ impl Printer<'_> {
wln!(self, "}}"); wln!(self, "}}");
} }
ModItem::Const(it) => { ModItem::Const(it) => {
let Const { name, visibility, type_ref, ast_id: _ } = &self.tree[it]; let Const { name, visibility, type_ref, ast_id } = &self.tree[it];
self.print_ast_id(ast_id.erase());
self.print_visibility(*visibility); self.print_visibility(*visibility);
w!(self, "const "); w!(self, "const ");
match name { match name {
@ -341,7 +365,8 @@ impl Printer<'_> {
wln!(self, " = _;"); wln!(self, " = _;");
} }
ModItem::Static(it) => { ModItem::Static(it) => {
let Static { name, visibility, mutable, type_ref, ast_id: _ } = &self.tree[it]; let Static { name, visibility, mutable, type_ref, ast_id } = &self.tree[it];
self.print_ast_id(ast_id.erase());
self.print_visibility(*visibility); self.print_visibility(*visibility);
w!(self, "static "); w!(self, "static ");
if *mutable { if *mutable {
@ -353,15 +378,9 @@ impl Printer<'_> {
wln!(self); wln!(self);
} }
ModItem::Trait(it) => { ModItem::Trait(it) => {
let Trait { let Trait { name, visibility, is_auto, is_unsafe, items, generic_params, ast_id } =
name, &self.tree[it];
visibility, self.print_ast_id(ast_id.erase());
is_auto,
is_unsafe,
items,
generic_params,
ast_id: _,
} = &self.tree[it];
self.print_visibility(*visibility); self.print_visibility(*visibility);
if *is_unsafe { if *is_unsafe {
w!(self, "unsafe "); w!(self, "unsafe ");
@ -380,7 +399,8 @@ impl Printer<'_> {
wln!(self, "}}"); wln!(self, "}}");
} }
ModItem::TraitAlias(it) => { ModItem::TraitAlias(it) => {
let TraitAlias { name, visibility, generic_params, ast_id: _ } = &self.tree[it]; let TraitAlias { name, visibility, generic_params, ast_id } = &self.tree[it];
self.print_ast_id(ast_id.erase());
self.print_visibility(*visibility); self.print_visibility(*visibility);
w!(self, "trait {}", name.display(self.db.upcast())); w!(self, "trait {}", name.display(self.db.upcast()));
self.print_generic_params(generic_params); self.print_generic_params(generic_params);
@ -397,8 +417,9 @@ impl Printer<'_> {
is_unsafe, is_unsafe,
items, items,
generic_params, generic_params,
ast_id: _, ast_id,
} = &self.tree[it]; } = &self.tree[it];
self.print_ast_id(ast_id.erase());
if *is_unsafe { if *is_unsafe {
w!(self, "unsafe"); w!(self, "unsafe");
} }
@ -422,8 +443,9 @@ impl Printer<'_> {
wln!(self, "}}"); wln!(self, "}}");
} }
ModItem::TypeAlias(it) => { ModItem::TypeAlias(it) => {
let TypeAlias { name, visibility, bounds, type_ref, generic_params, ast_id: _ } = let TypeAlias { name, visibility, bounds, type_ref, generic_params, ast_id } =
&self.tree[it]; &self.tree[it];
self.print_ast_id(ast_id.erase());
self.print_visibility(*visibility); self.print_visibility(*visibility);
w!(self, "type {}", name.display(self.db.upcast())); w!(self, "type {}", name.display(self.db.upcast()));
self.print_generic_params(generic_params); self.print_generic_params(generic_params);
@ -440,7 +462,8 @@ impl Printer<'_> {
wln!(self); wln!(self);
} }
ModItem::Mod(it) => { ModItem::Mod(it) => {
let Mod { name, visibility, kind, ast_id: _ } = &self.tree[it]; let Mod { name, visibility, kind, ast_id } = &self.tree[it];
self.print_ast_id(ast_id.erase());
self.print_visibility(*visibility); self.print_visibility(*visibility);
w!(self, "mod {}", name.display(self.db.upcast())); w!(self, "mod {}", name.display(self.db.upcast()));
match kind { match kind {
@ -459,15 +482,24 @@ impl Printer<'_> {
} }
} }
ModItem::MacroCall(it) => { ModItem::MacroCall(it) => {
let MacroCall { path, ast_id: _, expand_to: _, call_site: _ } = &self.tree[it]; let MacroCall { path, ast_id, expand_to, call_site } = &self.tree[it];
let _ = writeln!(
self,
"// AstId: {:?}, Span: {}, ExpandTo: {:?}",
ast_id.erase().into_raw(),
call_site,
expand_to
);
wln!(self, "{}!(...);", path.display(self.db.upcast())); wln!(self, "{}!(...);", path.display(self.db.upcast()));
} }
ModItem::MacroRules(it) => { ModItem::MacroRules(it) => {
let MacroRules { name, ast_id: _ } = &self.tree[it]; let MacroRules { name, ast_id } = &self.tree[it];
self.print_ast_id(ast_id.erase());
wln!(self, "macro_rules! {} {{ ... }}", name.display(self.db.upcast())); wln!(self, "macro_rules! {} {{ ... }}", name.display(self.db.upcast()));
} }
ModItem::Macro2(it) => { ModItem::Macro2(it) => {
let Macro2 { name, visibility, ast_id: _ } = &self.tree[it]; let Macro2 { name, visibility, ast_id } = &self.tree[it];
self.print_ast_id(ast_id.erase());
self.print_visibility(*visibility); self.print_visibility(*visibility);
wln!(self, "macro {} {{ ... }}", name.display(self.db.upcast())); wln!(self, "macro {} {{ ... }}", name.display(self.db.upcast()));
} }
@ -583,6 +615,10 @@ impl Printer<'_> {
}); });
true true
} }
fn print_ast_id(&mut self, ast_id: ErasedFileAstId) {
wln!(self, "// AstId: {:?}", ast_id.into_raw());
}
} }
impl Write for Printer<'_> { impl Write for Printer<'_> {

View file

@ -34,17 +34,23 @@ use a::{c, d::{e}};
#![no_std] #![no_std]
#![doc = " another file comment"] #![doc = " another file comment"]
// AstId: 1
pub(self) extern crate self as renamed; pub(self) extern crate self as renamed;
// AstId: 2
pub(super) extern crate bli; pub(super) extern crate bli;
// AstId: 3
pub use crate::path::{nested, items as renamed, Trait as _}; pub use crate::path::{nested, items as renamed, Trait as _};
// AstId: 4
pub(self) use globs::*; pub(self) use globs::*;
#[doc = " docs on import"] #[doc = " docs on import"]
// AstId: 5
pub(self) use crate::{A, B}; pub(self) use crate::{A, B};
// AstId: 6
pub(self) use a::{c, d::{e}}; pub(self) use a::{c, d::{e}};
"##]], "##]],
); );
@ -68,14 +74,18 @@ extern "C" {
"#, "#,
expect![[r##" expect![[r##"
#[on_extern_block] #[on_extern_block]
// AstId: 1
extern "C" { extern "C" {
#[on_extern_type] #[on_extern_type]
// AstId: 2
pub(self) type ExType; pub(self) type ExType;
#[on_extern_static] #[on_extern_static]
// AstId: 3
pub(self) static EX_STATIC: u8 = _; pub(self) static EX_STATIC: u8 = _;
#[on_extern_fn] #[on_extern_fn]
// AstId: 4
pub(self) fn ex_fn() -> (); pub(self) fn ex_fn() -> ();
} }
"##]], "##]],
@ -112,38 +122,52 @@ enum E {
} }
} }
"#, "#,
expect![[r##" expect![[r#"
// AstId: 1
pub(self) struct Unit; pub(self) struct Unit;
#[derive(Debug)] #[derive(Debug)]
// AstId: 2
pub(self) struct Struct { pub(self) struct Struct {
// AstId: 6
#[doc = " fld docs"] #[doc = " fld docs"]
pub(self) fld: (), pub(self) fld: (),
} }
// AstId: 3
pub(self) struct Tuple( pub(self) struct Tuple(
// AstId: 7
#[attr] #[attr]
pub(self) 0: u8, pub(self) 0: u8,
); );
// AstId: 4
pub(self) union Ize { pub(self) union Ize {
// AstId: 8
pub(self) a: (), pub(self) a: (),
// AstId: 9
pub(self) b: (), pub(self) b: (),
} }
// AstId: 5
pub(self) enum E { pub(self) enum E {
// AstId: 10
#[doc = " comment on Unit"] #[doc = " comment on Unit"]
Unit, Unit,
// AstId: 11
#[doc = " comment on Tuple"] #[doc = " comment on Tuple"]
Tuple( Tuple(
// AstId: 13
pub(self) 0: u8, pub(self) 0: u8,
), ),
// AstId: 12
Struct { Struct {
// AstId: 14
#[doc = " comment on a: u8"] #[doc = " comment on a: u8"]
pub(self) a: u8, pub(self) a: u8,
}, },
} }
"##]], "#]],
); );
} }
@ -166,26 +190,35 @@ trait Tr: SuperTrait + 'lifetime {
} }
"#, "#,
expect![[r#" expect![[r#"
// AstId: 1
pub static mut ST: () = _; pub static mut ST: () = _;
// AstId: 2
pub(self) const _: Anon = _; pub(self) const _: Anon = _;
#[attr] #[attr]
#[inner_attr_in_fn] #[inner_attr_in_fn]
// AstId: 3
pub(self) fn f( pub(self) fn f(
#[attr] #[attr]
// AstId: 5
u8, u8,
// AstId: 6
(), (),
) -> () { ... } ) -> () { ... }
// AstId: 4
pub(self) trait Tr<Self> pub(self) trait Tr<Self>
where where
Self: SuperTrait, Self: SuperTrait,
Self: 'lifetime Self: 'lifetime
{ {
// AstId: 8
pub(self) type Assoc: AssocBound = Default; pub(self) type Assoc: AssocBound = Default;
// AstId: 9
pub(self) fn method( pub(self) fn method(
// AstId: 10
self: &Self, self: &Self,
) -> (); ) -> ();
} }
@ -211,12 +244,16 @@ mod outline;
expect![[r##" expect![[r##"
#[doc = " outer"] #[doc = " outer"]
#[doc = " inner"] #[doc = " inner"]
// AstId: 1
pub(self) mod inline { pub(self) mod inline {
// AstId: 3
pub(self) use super::*; pub(self) use super::*;
// AstId: 4
pub(self) fn fn_in_module() -> () { ... } pub(self) fn fn_in_module() -> () { ... }
} }
// AstId: 2
pub(self) mod outline; pub(self) mod outline;
"##]], "##]],
); );
@ -235,10 +272,13 @@ pub macro m2() {}
m!(); m!();
"#, "#,
expect![[r#" expect![[r#"
// AstId: 1
macro_rules! m { ... } macro_rules! m { ... }
// AstId: 2
pub macro m2 { ... } pub macro m2 { ... }
// AstId: 3, Span: 0:3@0..5#0, ExpandTo: Items
m!(...); m!(...);
"#]], "#]],
); );
@ -258,12 +298,19 @@ struct S {
} }
"#, "#,
expect![[r#" expect![[r#"
// AstId: 1
pub(self) struct S { pub(self) struct S {
// AstId: 2
pub(self) a: self::Ty, pub(self) a: self::Ty,
// AstId: 3
pub(self) b: super::SuperTy, pub(self) b: super::SuperTy,
// AstId: 4
pub(self) c: super::super::SuperSuperTy, pub(self) c: super::super::SuperSuperTy,
// AstId: 5
pub(self) d: ::abs::Path, pub(self) d: ::abs::Path,
// AstId: 6
pub(self) e: crate::Crate, pub(self) e: crate::Crate,
// AstId: 7
pub(self) f: plain::path::Ty, pub(self) f: plain::path::Ty,
} }
"#]], "#]],
@ -282,10 +329,15 @@ struct S {
} }
"#, "#,
expect![[r#" expect![[r#"
// AstId: 1
pub(self) struct S { pub(self) struct S {
// AstId: 2
pub(self) a: Mixed::<'a, T, Item = (), OtherItem = u8>, pub(self) a: Mixed::<'a, T, Item = (), OtherItem = u8>,
// AstId: 3
pub(self) b: Qualified::<Self=Fully>::Syntax, pub(self) b: Qualified::<Self=Fully>::Syntax,
// AstId: 4
pub(self) c: <TypeAnchored>::Path::<'a>, pub(self) c: <TypeAnchored>::Path::<'a>,
// AstId: 5
pub(self) d: dyn for<'a> Trait::<'a>, pub(self) d: dyn for<'a> Trait::<'a>,
} }
"#]], "#]],
@ -312,42 +364,53 @@ union Union<'a, T, const U: u8> {}
trait Tr<'a, T: 'a>: Super where Self: for<'a> Tr<'a, T> {} trait Tr<'a, T: 'a>: Super where Self: for<'a> Tr<'a, T> {}
"#, "#,
expect![[r#" expect![[r#"
// AstId: 1
pub(self) struct S<'a, 'b, T, const K: u8> pub(self) struct S<'a, 'b, T, const K: u8>
where where
T: Copy, T: Copy,
T: 'a, T: 'a,
T: 'b T: 'b
{ {
// AstId: 8
pub(self) field: &'a &'b T, pub(self) field: &'a &'b T,
} }
// AstId: 2
pub(self) struct Tuple<T, U>( pub(self) struct Tuple<T, U>(
// AstId: 9
pub(self) 0: T, pub(self) 0: T,
// AstId: 10
pub(self) 1: U, pub(self) 1: U,
) )
where where
T: Copy, T: Copy,
U: ?Sized; U: ?Sized;
// AstId: 3
impl<'a, 'b, T, const K: u8> S::<'a, 'b, T, K> impl<'a, 'b, T, const K: u8> S::<'a, 'b, T, K>
where where
T: Copy, T: Copy,
T: 'a, T: 'a,
T: 'b T: 'b
{ {
// AstId: 12
pub(self) fn f<G>( pub(self) fn f<G>(
// AstId: 13
impl Copy, impl Copy,
) -> impl Copy ) -> impl Copy
where where
G: 'a { ... } G: 'a { ... }
} }
// AstId: 4
pub(self) enum Enum<'a, T, const U: u8> { pub(self) enum Enum<'a, T, const U: u8> {
} }
// AstId: 5
pub(self) union Union<'a, T, const U: u8> { pub(self) union Union<'a, T, const U: u8> {
} }
// AstId: 6
pub(self) trait Tr<'a, Self, T> pub(self) trait Tr<'a, Self, T>
where where
Self: Super, Self: Super,
@ -366,6 +429,7 @@ fn generics_with_attributes() {
struct S<#[cfg(never)] T>; struct S<#[cfg(never)] T>;
"#, "#,
expect![[r#" expect![[r#"
// AstId: 1
pub(self) struct S<#[cfg(never)] T>; pub(self) struct S<#[cfg(never)] T>;
"#]], "#]],
) )
@ -378,6 +442,7 @@ fn pub_self() {
pub(self) struct S; pub(self) struct S;
"#, "#,
expect![[r#" expect![[r#"
// AstId: 1
pub(self) struct S; pub(self) struct S;
"#]], "#]],
) )