Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor: Extract render_summary from render_impl. #86128

Merged
merged 1 commit into from
Jun 9, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
164 changes: 86 additions & 78 deletions src/librustdoc/html/render/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1286,7 +1286,6 @@ fn render_impl(
// in documentation pages for trait with automatic implementations like "Send" and "Sync".
aliases: &[String],
) {
let tcx = cx.tcx();
let cache = cx.cache();
let traits = &cache.traits;
let trait_ = i.trait_did_full(cache).map(|did| &traits[&did]);
Expand Down Expand Up @@ -1558,94 +1557,34 @@ fn render_impl(
);
}
}
let toggled = !impl_items.is_empty() || !default_impl_items.is_empty();
let open_details = |close_tags: &mut String, is_collapsed: bool| {
if render_mode == RenderMode::Normal {
let is_implementing_trait = i.inner_impl().trait_.is_some();
let toggled = !impl_items.is_empty() || !default_impl_items.is_empty();
if toggled {
close_tags.insert_str(0, "</details>");
if is_collapsed {
"<details class=\"rustdoc-toggle implementors-toggle\"><summary>"
if is_implementing_trait {
write!(w, "<details class=\"rustdoc-toggle implementors-toggle\">");
} else {
"<details class=\"rustdoc-toggle implementors-toggle\" open><summary>"
write!(w, "<details class=\"rustdoc-toggle implementors-toggle\" open>");
}
} else {
""
}
};
if render_mode == RenderMode::Normal {
let is_implementing_trait;
let id = cx.derive_id(match i.inner_impl().trait_ {
Some(ref t) => {
is_implementing_trait = true;
if is_on_foreign_type {
get_id_for_impl_on_foreign_type(&i.inner_impl().for_, t, cx)
} else {
format!("impl-{}", small_url_encode(format!("{:#}", t.print(cx))))
}
}
None => {
is_implementing_trait = false;
"impl".to_string()
}
});
let aliases = if aliases.is_empty() {
String::new()
} else {
format!(" data-aliases=\"{}\"", aliases.join(","))
};
if let Some(use_absolute) = use_absolute {
write!(
w,
"{}<div id=\"{}\" class=\"impl has-srclink\"{}>\
<code class=\"in-band\">",
open_details(&mut close_tags, is_implementing_trait),
id,
aliases
);
write!(w, "{}", i.inner_impl().print(use_absolute, cx));
if show_def_docs {
for it in &i.inner_impl().items {
if let clean::TypedefItem(ref tydef, _) = *it.kind {
w.write_str("<span class=\"where fmt-newline\"> ");
assoc_type(
w,
it,
&[],
Some(&tydef.type_),
AssocItemLink::Anchor(None),
"",
cx,
);
w.write_str(";</span>");
}
}
}
w.write_str("</code>");
} else {
write!(
w,
"{}<div id=\"{}\" class=\"impl has-srclink\"{}>\
<code class=\"in-band\">{}</code>",
open_details(&mut close_tags, is_implementing_trait),
id,
aliases,
i.inner_impl().print(false, cx)
);
if toggled {
write!(w, "<summary>")
}
write!(w, "<a href=\"#{}\" class=\"anchor\"></a>", id);
render_stability_since_raw(
render_impl_summary(
w,
i.impl_item.stable_since(tcx).as_deref(),
i.impl_item.const_stable_since(tcx).as_deref(),
cx,
i,
outer_version,
outer_const_version,
show_def_docs,
use_absolute,
is_on_foreign_type,
aliases,
);
write_srclink(cx, &i.impl_item, w);
if !toggled {
w.write_str("</div>");
} else {
w.write_str("</div></summary>");
if toggled {
write!(w, "</summary>")
}

if trait_.is_some() {
if let Some(portability) = portability(&i.impl_item, Some(parent)) {
write!(w, "<div class=\"item-info\">{}</div>", portability);
Expand Down Expand Up @@ -1678,6 +1617,75 @@ fn render_impl(
w.write_str(&close_tags);
}

fn render_impl_summary(
w: &mut Buffer,
cx: &Context<'_>,
i: &Impl,
outer_version: Option<&str>,
outer_const_version: Option<&str>,
show_def_docs: bool,
use_absolute: Option<bool>,
is_on_foreign_type: bool,
// This argument is used to reference same type with different paths to avoid duplication
// in documentation pages for trait with automatic implementations like "Send" and "Sync".
aliases: &[String],
) {
let tcx = cx.tcx();
let id = cx.derive_id(match i.inner_impl().trait_ {
Some(ref t) => {
if is_on_foreign_type {
get_id_for_impl_on_foreign_type(&i.inner_impl().for_, t, cx)
} else {
format!("impl-{}", small_url_encode(format!("{:#}", t.print(cx))))
}
}
None => "impl".to_string(),
});
let aliases = if aliases.is_empty() {
String::new()
} else {
format!(" data-aliases=\"{}\"", aliases.join(","))
};
if let Some(use_absolute) = use_absolute {
write!(
w,
"<div id=\"{}\" class=\"impl has-srclink\"{}>\
<code class=\"in-band\">",
id, aliases
);
write!(w, "{}", i.inner_impl().print(use_absolute, cx));
if show_def_docs {
for it in &i.inner_impl().items {
if let clean::TypedefItem(ref tydef, _) = *it.kind {
w.write_str("<span class=\"where fmt-newline\"> ");
assoc_type(w, it, &[], Some(&tydef.type_), AssocItemLink::Anchor(None), "", cx);
w.write_str(";</span>");
}
}
}
w.write_str("</code>");
} else {
write!(
w,
"<div id=\"{}\" class=\"impl has-srclink\"{}>\
<code class=\"in-band\">{}</code>",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Raw strings could make this more readable.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good suggestion, thanks! I think we use non-raw strings in rustdoc to minimize whitespace (by putting \ at the end of the line). Also, I hope to introduce the use of a templating engine soonish, which would make things even more readable.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I confirm that we don't use raw strings to generate smaller content.

id,
aliases,
i.inner_impl().print(false, cx)
);
}
write!(w, "<a href=\"#{}\" class=\"anchor\"></a>", id);
render_stability_since_raw(
w,
i.impl_item.stable_since(tcx).as_deref(),
i.impl_item.const_stable_since(tcx).as_deref(),
outer_version,
outer_const_version,
);
write_srclink(cx, &i.impl_item, w);
w.write_str("</div>");
}

fn print_sidebar(cx: &Context<'_>, it: &clean::Item, buffer: &mut Buffer) {
let parentlen = cx.current.len() - if it.is_mod() { 1 } else { 0 };

Expand Down