Improve code readability

This commit is contained in:
Guillaume Gomez 2021-05-04 13:52:53 +02:00 committed by Guillaume Gomez
parent 38444f61bb
commit c5c927dfda
2 changed files with 46 additions and 57 deletions

View file

@ -560,31 +560,28 @@ fn string<T: Display>(
context: Option<&Context<'_>>,
root_path: &str,
) {
match klass {
None => write!(out, "{}", text),
Some(klass) => {
let klass = match klass {
None => return write!(out, "{}", text),
Some(klass) => klass,
};
if let Some(def_span) = klass.get_span() {
let mut text = text.to_string();
if text.contains("::") {
text =
text.split("::").enumerate().fold(String::new(), |mut path, (pos, t)| {
let pre = if pos != 0 { "::" } else { "" };
text = text.split("::").intersperse("::").fold(String::new(), |mut path, t| {
match t {
"self" | "Self" => write!(
&mut path,
"{}<span class=\"{}\">{}</span>",
pre,
"<span class=\"{}\">{}</span>",
Class::Self_((0, 0)).as_html(),
t
),
"crate" | "super" => write!(
&mut path,
"{}<span class=\"{}\">{}</span>",
pre,
"<span class=\"{}\">{}</span>",
Class::KeyWord.as_html(),
t
),
t => write!(&mut path, "{}{}", pre, t),
t => write!(&mut path, "{}", t),
}
.expect("Failed to build source HTML path");
path
@ -595,7 +592,6 @@ fn string<T: Display>(
context.shared.span_correspondance_map.get(&def_span).and_then(|href| {
match href {
LinkFromSrc::Local(span) => {
eprintln!("==> {:?}:{:?}", span.lo(), span.hi());
context
.href_from_span(clean::Span::wrap_raw(*span))
.map(|s| format!("{}{}", root_path, s))
@ -606,21 +602,12 @@ fn string<T: Display>(
}
})
{
write!(
out,
"<a class=\"{}\" href=\"{}\">{}</a>",
klass.as_html(),
href,
text
);
write!(out, "<a class=\"{}\" href=\"{}\">{}</a>", klass.as_html(), href, text);
return;
}
}
}
write!(out, "<span class=\"{}\">{}</span>", klass.as_html(), text);
}
}
write!(out, "<span class=\"{}\">{}</span>", klass.as_html(), text);
}
#[cfg(test)]

View file

@ -9,6 +9,8 @@ use rustc_hir::{ExprKind, GenericParam, GenericParamKind, HirId, Mod, Node};
use rustc_middle::ty::TyCtxt;
use rustc_span::Span;
use std::path::{Path, PathBuf};
/// This enum allows us to store two different kinds of information:
///
/// In case the `span` definition comes from the same crate, we can simply get the `span` and use
@ -35,10 +37,10 @@ crate enum LinkFromSrc {
crate fn collect_spans_and_sources(
tcx: TyCtxt<'_>,
krate: clean::Crate,
src_root: &std::path::Path,
src_root: &Path,
include_sources: bool,
generate_link_to_definition: bool,
) -> (clean::Crate, FxHashMap<std::path::PathBuf, String>, FxHashMap<(u32, u32), LinkFromSrc>) {
) -> (clean::Crate, FxHashMap<PathBuf, String>, FxHashMap<(u32, u32), LinkFromSrc>) {
let mut visitor = SpanMapVisitor { tcx, matches: FxHashMap::default() };
if include_sources {