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 span::ErasedFileAstId;
use crate::{
generics::{TypeOrConstParamData, WherePredicate, WherePredicateTypeTarget},
pretty::{print_path, print_type_bounds, print_type_ref},
@ -118,7 +120,11 @@ impl Printer<'_> {
w!(self, "{{");
self.indented(|this| {
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_visibility(*visibility);
w!(this, "{}: ", name.display(self.db.upcast()));
@ -132,7 +138,11 @@ impl Printer<'_> {
w!(self, "(");
self.indented(|this| {
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_visibility(*visibility);
w!(this, "{}: ", name.display(self.db.upcast()));
@ -200,14 +210,16 @@ impl Printer<'_> {
match item {
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);
w!(self, "use ");
self.print_use_tree(use_tree);
wln!(self, ";");
}
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);
w!(self, "extern crate {}", name.display(self.db.upcast()));
if let Some(alias) = alias {
@ -216,7 +228,8 @@ impl Printer<'_> {
wln!(self, ";");
}
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 ");
if let Some(abi) = abi {
w!(self, "\"{}\" ", abi);
@ -237,9 +250,10 @@ impl Printer<'_> {
abi,
params,
ret_type,
ast_id: _,
ast_id,
flags,
} = &self.tree[it];
self.print_ast_id(ast_id.erase());
self.print_visibility(*visibility);
if flags.contains(FnFlags::HAS_DEFAULT_KW) {
w!(self, "default ");
@ -263,7 +277,12 @@ impl Printer<'_> {
self.indented(|this| {
for param in params.clone() {
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) => {
if flags.contains(FnFlags::HAS_SELF_PARAM) {
w!(this, "self: ");
@ -288,7 +307,8 @@ impl Printer<'_> {
}
}
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);
w!(self, "struct {}", name.display(self.db.upcast()));
self.print_generic_params(generic_params);
@ -300,7 +320,8 @@ impl Printer<'_> {
}
}
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);
w!(self, "union {}", name.display(self.db.upcast()));
self.print_generic_params(generic_params);
@ -312,14 +333,16 @@ impl Printer<'_> {
}
}
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);
w!(self, "enum {}", name.display(self.db.upcast()));
self.print_generic_params(generic_params);
self.print_where_clause_and_opening_brace(generic_params);
self.indented(|this| {
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");
w!(this, "{}", name.display(self.db.upcast()));
this.print_fields(fields);
@ -329,7 +352,8 @@ impl Printer<'_> {
wln!(self, "}}");
}
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);
w!(self, "const ");
match name {
@ -341,7 +365,8 @@ impl Printer<'_> {
wln!(self, " = _;");
}
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);
w!(self, "static ");
if *mutable {
@ -353,15 +378,9 @@ impl Printer<'_> {
wln!(self);
}
ModItem::Trait(it) => {
let Trait {
name,
visibility,
is_auto,
is_unsafe,
items,
generic_params,
ast_id: _,
} = &self.tree[it];
let Trait { name, visibility, is_auto, is_unsafe, items, generic_params, ast_id } =
&self.tree[it];
self.print_ast_id(ast_id.erase());
self.print_visibility(*visibility);
if *is_unsafe {
w!(self, "unsafe ");
@ -380,7 +399,8 @@ impl Printer<'_> {
wln!(self, "}}");
}
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);
w!(self, "trait {}", name.display(self.db.upcast()));
self.print_generic_params(generic_params);
@ -397,8 +417,9 @@ impl Printer<'_> {
is_unsafe,
items,
generic_params,
ast_id: _,
ast_id,
} = &self.tree[it];
self.print_ast_id(ast_id.erase());
if *is_unsafe {
w!(self, "unsafe");
}
@ -422,8 +443,9 @@ impl Printer<'_> {
wln!(self, "}}");
}
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.print_ast_id(ast_id.erase());
self.print_visibility(*visibility);
w!(self, "type {}", name.display(self.db.upcast()));
self.print_generic_params(generic_params);
@ -440,7 +462,8 @@ impl Printer<'_> {
wln!(self);
}
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);
w!(self, "mod {}", name.display(self.db.upcast()));
match kind {
@ -459,15 +482,24 @@ impl Printer<'_> {
}
}
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()));
}
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()));
}
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);
wln!(self, "macro {} {{ ... }}", name.display(self.db.upcast()));
}
@ -583,6 +615,10 @@ impl Printer<'_> {
});
true
}
fn print_ast_id(&mut self, ast_id: ErasedFileAstId) {
wln!(self, "// AstId: {:?}", ast_id.into_raw());
}
}
impl Write for Printer<'_> {

View file

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