Unify macro source display
This commit is contained in:
parent
c70250dfbd
commit
84f259e44c
3 changed files with 48 additions and 58 deletions
|
@ -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 {
|
fn build_macro(cx: &mut DocContext<'_>, def_id: DefId, name: Symbol) -> clean::ItemKind {
|
||||||
let imported_from = cx.tcx.crate_name(did.krate);
|
let imported_from = cx.tcx.crate_name(def_id.krate);
|
||||||
match cx.enter_resolver(|r| r.cstore().load_macro_untracked(did, cx.sess())) {
|
match cx.enter_resolver(|r| r.cstore().load_macro_untracked(def_id, cx.sess())) {
|
||||||
LoadedMacro::MacroDef(item_def, _) => {
|
LoadedMacro::MacroDef(item_def, _) => {
|
||||||
if let ast::ItemKind::MacroDef(ref def) = item_def.kind {
|
if let ast::ItemKind::MacroDef(ref def) = item_def.kind {
|
||||||
let tts: Vec<_> = def.body.inner_tokens().into_trees().collect();
|
clean::MacroItem(clean::Macro {
|
||||||
let matchers = tts.chunks(4).map(|arm| &arm[0]);
|
source: utils::display_macro_source(cx, name, def, def_id, item_def.vis),
|
||||||
let source = if def.macro_rules {
|
imported_from: Some(imported_from),
|
||||||
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) })
|
|
||||||
} else {
|
} else {
|
||||||
unreachable!()
|
unreachable!()
|
||||||
}
|
}
|
||||||
|
|
|
@ -2175,37 +2175,15 @@ impl Clean<Item> for (&hir::MacroDef<'_>, Option<Symbol>) {
|
||||||
fn clean(&self, cx: &mut DocContext<'_>) -> Item {
|
fn clean(&self, cx: &mut DocContext<'_>) -> Item {
|
||||||
let (item, renamed) = self;
|
let (item, renamed) = self;
|
||||||
let name = renamed.unwrap_or(item.ident.name);
|
let name = renamed.unwrap_or(item.ident.name);
|
||||||
let tts = item.ast.body.inner_tokens().trees().collect::<Vec<_>>();
|
let def_id = item.def_id.to_def_id();
|
||||||
// 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, ","),
|
|
||||||
)
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
Item::from_hir_id_and_parts(
|
Item::from_hir_id_and_parts(
|
||||||
item.hir_id(),
|
item.hir_id(),
|
||||||
Some(name),
|
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,
|
cx,
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
|
@ -3,10 +3,12 @@ use crate::clean::blanket_impl::BlanketImplFinder;
|
||||||
use crate::clean::{
|
use crate::clean::{
|
||||||
inline, Clean, Crate, Generic, GenericArg, GenericArgs, ImportSource, Item, ItemKind, Lifetime,
|
inline, Clean, Crate, Generic, GenericArg, GenericArgs, ImportSource, Item, ItemKind, Lifetime,
|
||||||
Path, PathSegment, PolyTrait, Primitive, PrimitiveType, ResolvedPath, Type, TypeBinding,
|
Path, PathSegment, PolyTrait, Primitive, PrimitiveType, ResolvedPath, Type, TypeBinding,
|
||||||
|
Visibility,
|
||||||
};
|
};
|
||||||
use crate::core::DocContext;
|
use crate::core::DocContext;
|
||||||
use crate::formats::item_type::ItemType;
|
use crate::formats::item_type::ItemType;
|
||||||
|
|
||||||
|
use rustc_ast as ast;
|
||||||
use rustc_ast::tokenstream::TokenTree;
|
use rustc_ast::tokenstream::TokenTree;
|
||||||
use rustc_hir as hir;
|
use rustc_hir as hir;
|
||||||
use rustc_hir::def::{DefKind, Res};
|
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 {
|
pub(super) fn render_macro_matcher(matcher: &TokenTree) -> String {
|
||||||
rustc_ast_pretty::pprust::tt_to_string(matcher)
|
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, ","),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue