Unify macro source display

This commit is contained in:
Guillaume Gomez 2021-07-05 11:00:27 +02:00
parent c70250dfbd
commit 84f259e44c
3 changed files with 48 additions and 58 deletions

View File

@ -543,39 +543,15 @@ fn build_static(cx: &mut DocContext<'_>, did: DefId, mutable: bool) -> clean::St
}
}
fn build_macro(cx: &mut DocContext<'_>, did: DefId, name: Symbol) -> clean::ItemKind {
let imported_from = cx.tcx.crate_name(did.krate);
match cx.enter_resolver(|r| r.cstore().load_macro_untracked(did, cx.sess())) {
fn build_macro(cx: &mut DocContext<'_>, def_id: DefId, name: Symbol) -> clean::ItemKind {
let imported_from = cx.tcx.crate_name(def_id.krate);
match cx.enter_resolver(|r| r.cstore().load_macro_untracked(def_id, cx.sess())) {
LoadedMacro::MacroDef(item_def, _) => {
if let ast::ItemKind::MacroDef(ref def) = item_def.kind {
let tts: Vec<_> = def.body.inner_tokens().into_trees().collect();
let matchers = tts.chunks(4).map(|arm| &arm[0]);
let source = if def.macro_rules {
format!(
"macro_rules! {} {{\n{}}}",
name,
utils::render_macro_arms(matchers, ";")
)
} else {
let vis = item_def.vis.clean(cx);
if matchers.len() <= 1 {
format!(
"{}macro {}{} {{\n ...\n}}",
vis.to_src_with_space(cx.tcx, did),
name,
matchers.map(utils::render_macro_matcher).collect::<String>(),
)
} else {
format!(
"{}macro {} {{\n{}}}",
vis.to_src_with_space(cx.tcx, did),
name,
utils::render_macro_arms(matchers, ";"),
)
}
};
clean::MacroItem(clean::Macro { source, imported_from: Some(imported_from) })
clean::MacroItem(clean::Macro {
source: utils::display_macro_source(cx, name, def, def_id, item_def.vis),
imported_from: Some(imported_from),
})
} else {
unreachable!()
}

View File

@ -2175,37 +2175,15 @@ impl Clean<Item> for (&hir::MacroDef<'_>, Option<Symbol>) {
fn clean(&self, cx: &mut DocContext<'_>) -> Item {
let (item, renamed) = self;
let name = renamed.unwrap_or(item.ident.name);
let tts = item.ast.body.inner_tokens().trees().collect::<Vec<_>>();
// Extract the macro's matchers. They represent the "interface" of the macro.
let matchers = tts.chunks(4).map(|arm| &arm[0]);
let source = if item.ast.macro_rules {
format!("macro_rules! {} {{\n{}}}", name, render_macro_arms(matchers, ";"))
} else {
let vis = item.vis.clean(cx);
let def_id = item.def_id.to_def_id();
if matchers.len() <= 1 {
format!(
"{}macro {}{} {{\n ...\n}}",
vis.to_src_with_space(cx.tcx, def_id),
name,
matchers.map(render_macro_matcher).collect::<String>(),
)
} else {
format!(
"{}macro {} {{\n{}}}",
vis.to_src_with_space(cx.tcx, def_id),
name,
render_macro_arms(matchers, ","),
)
}
};
let def_id = item.def_id.to_def_id();
Item::from_hir_id_and_parts(
item.hir_id(),
Some(name),
MacroItem(Macro { source, imported_from: None }),
MacroItem(Macro {
source: display_macro_source(cx, name, &item.ast, def_id, &item.vis),
imported_from: None,
}),
cx,
)
}

View File

@ -3,10 +3,12 @@ use crate::clean::blanket_impl::BlanketImplFinder;
use crate::clean::{
inline, Clean, Crate, Generic, GenericArg, GenericArgs, ImportSource, Item, ItemKind, Lifetime,
Path, PathSegment, PolyTrait, Primitive, PrimitiveType, ResolvedPath, Type, TypeBinding,
Visibility,
};
use crate::core::DocContext;
use crate::formats::item_type::ItemType;
use rustc_ast as ast;
use rustc_ast::tokenstream::TokenTree;
use rustc_hir as hir;
use rustc_hir::def::{DefKind, Res};
@ -577,3 +579,37 @@ pub(super) fn render_macro_arms<'a>(
pub(super) fn render_macro_matcher(matcher: &TokenTree) -> String {
rustc_ast_pretty::pprust::tt_to_string(matcher)
}
pub(super) fn display_macro_source(
cx: &mut DocContext<'_>,
name: Symbol,
def: &ast::MacroDef,
def_id: DefId,
vis: impl Clean<Visibility>,
) -> String {
let tts: Vec<_> = def.body.inner_tokens().into_trees().collect();
// Extract the spans of all matchers. They represent the "interface" of the macro.
let matchers = tts.chunks(4).map(|arm| &arm[0]);
if def.macro_rules {
format!("macro_rules! {} {{\n{}}}", name, render_macro_arms(matchers, ";"))
} else {
let vis = vis.clean(cx);
if matchers.len() <= 1 {
format!(
"{}macro {}{} {{\n ...\n}}",
vis.to_src_with_space(cx.tcx, def_id),
name,
matchers.map(render_macro_matcher).collect::<String>(),
)
} else {
format!(
"{}macro {} {{\n{}}}",
vis.to_src_with_space(cx.tcx, def_id),
name,
render_macro_arms(matchers, ","),
)
}
}
}