Display member type in viewer::text::TextViewer
Previously, we only displayed the member name when viewing the documentation of a member. With this patch, we use the ItemType enum to also print the type of the member. For consistency, we remove the title field from doc::Doc that was previously used for the titles of modules and items and use the same title as for members. While we lose the links to the parent items, these were not particularly helpful anyway. If we want to, we could also generate them from the item name.
This commit is contained in:
parent
627830dcbe
commit
f08490eeff
33
src/doc.rs
33
src/doc.rs
@ -69,7 +69,6 @@ pub struct Item {
|
||||
pub struct Doc {
|
||||
pub name: Fqn,
|
||||
pub ty: ItemType,
|
||||
pub title: Option<String>,
|
||||
pub description: Option<String>,
|
||||
pub definition: Option<String>,
|
||||
pub members: Vec<(String, Vec<Doc>)>,
|
||||
@ -209,6 +208,37 @@ impl ops::Deref for Fqn {
|
||||
}
|
||||
|
||||
impl ItemType {
|
||||
pub fn name(&self) -> &str {
|
||||
match self {
|
||||
ItemType::Module => "Module",
|
||||
ItemType::ExternCrate => "Extern Crate",
|
||||
ItemType::Import => "Import",
|
||||
ItemType::Struct => "Struct",
|
||||
ItemType::Enum => "Enum",
|
||||
ItemType::Function => "Function",
|
||||
ItemType::Typedef => "Typedef",
|
||||
ItemType::Static => "Static",
|
||||
ItemType::Trait => "Trait",
|
||||
ItemType::Impl => "Impl",
|
||||
ItemType::TyMethod => "Required Method",
|
||||
ItemType::Method => "Method",
|
||||
ItemType::StructField => "Field",
|
||||
ItemType::Variant => "Variant",
|
||||
ItemType::Macro => "Macro",
|
||||
ItemType::Primitive => "Primitive",
|
||||
ItemType::AssocType => "Associated Type",
|
||||
ItemType::Constant => "Constant",
|
||||
ItemType::AssocConst => "Associated Const",
|
||||
ItemType::Union => "Union",
|
||||
ItemType::ForeignType => "Foreign Type",
|
||||
ItemType::Keyword => "Keyword",
|
||||
ItemType::OpaqueTy => "Opaque Type",
|
||||
ItemType::ProcAttribute => "Proc Attribute",
|
||||
ItemType::ProcDerive => "Proc Derive",
|
||||
ItemType::TraitAlias => "Trait Alias",
|
||||
}
|
||||
}
|
||||
|
||||
pub fn group_name(&self) -> &str {
|
||||
match self {
|
||||
ItemType::Module => "Modules",
|
||||
@ -393,7 +423,6 @@ impl Doc {
|
||||
Self {
|
||||
name,
|
||||
ty,
|
||||
title: Default::default(),
|
||||
description: Default::default(),
|
||||
definition: Default::default(),
|
||||
members: Default::default(),
|
||||
|
@ -78,12 +78,10 @@ fn select_first(
|
||||
|
||||
pub fn parse_item_doc(item: &doc::Item) -> anyhow::Result<doc::Doc> {
|
||||
let document = parse_file(&item.path)?;
|
||||
let heading = select_first(&document, ".fqn .in-band")?.context("Could not find heading")?;
|
||||
let definition = select_first(&document, ".docblock.type-decl")?;
|
||||
let description = select_first(&document, "#main > .docblock:not(.type-decl)")?;
|
||||
|
||||
let mut doc = doc::Doc::new(item.name.clone(), item.ty);
|
||||
doc.title = Some(get_html(heading.as_node())?);
|
||||
doc.description = description.map(|n| get_html(n.as_node())).transpose()?;
|
||||
doc.definition = definition.map(|n| get_html(n.as_node())).transpose()?;
|
||||
Ok(doc)
|
||||
@ -107,11 +105,9 @@ const MODULE_MEMBER_TYPES: &[doc::ItemType] = &[
|
||||
|
||||
pub fn parse_module_doc(item: &doc::Item) -> anyhow::Result<doc::Doc> {
|
||||
let document = parse_file(&item.path)?;
|
||||
let heading = select_first(&document, ".fqn .in-band")?.context("Could not find heading")?;
|
||||
let description = select_first(&document, ".docblock")?;
|
||||
|
||||
let mut doc = doc::Doc::new(item.name.clone(), item.ty);
|
||||
doc.title = Some(get_html(heading.as_node())?);
|
||||
doc.description = description.map(|n| get_html(n.as_node())).transpose()?;
|
||||
for item_type in MODULE_MEMBER_TYPES {
|
||||
let members = get_members(&document, item, *item_type)?;
|
||||
@ -203,13 +199,6 @@ mod tests {
|
||||
|
||||
assert_eq!(name, doc.name);
|
||||
assert_eq!(doc::ItemType::Struct, doc.ty);
|
||||
assert_eq!(
|
||||
"<span class=\"in-band\">\
|
||||
Struct <a href=\"index.html\">kuchiki</a>::<wbr>\
|
||||
<a class=\"struct\" href=\"\">NodeRef</a>\
|
||||
</span>",
|
||||
&doc.title.unwrap()
|
||||
);
|
||||
assert!(doc.definition.is_some());
|
||||
assert!(doc.description.is_some());
|
||||
}
|
||||
@ -224,7 +213,6 @@ mod tests {
|
||||
|
||||
assert_eq!(name, doc.name);
|
||||
assert_eq!(doc::ItemType::Method, doc.ty);
|
||||
assert!(doc.title.is_none());
|
||||
assert_eq!(
|
||||
"<code id=\"as_node.v\">\
|
||||
pub fn <a class=\"fnname\" href=\"#method.as_node\">as_node</a>(&self) \
|
||||
|
@ -29,11 +29,8 @@ impl<P: Printer> TextViewer<P> {
|
||||
}
|
||||
|
||||
fn print_doc(&self, doc: &doc::Doc) -> io::Result<()> {
|
||||
if let Some(title) = &doc.title {
|
||||
self.printer.print_heading(1, title)?;
|
||||
} else {
|
||||
self.printer.print_heading(1, doc.name.as_ref())?;
|
||||
}
|
||||
self.printer
|
||||
.print_heading(1, &format!("{} {}", doc.ty.name(), doc.name.as_ref()))?;
|
||||
self.print_opt(doc.definition.as_deref())?;
|
||||
self.print_opt(doc.description.as_deref())?;
|
||||
for (heading, items) in &doc.members {
|
||||
|
Loading…
Reference in New Issue
Block a user