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

View File

@ -9,6 +9,8 @@ use rustc_hir::{ExprKind, GenericParam, GenericParamKind, HirId, Mod, Node};
use rustc_middle::ty::TyCtxt; use rustc_middle::ty::TyCtxt;
use rustc_span::Span; use rustc_span::Span;
use std::path::{Path, PathBuf};
/// This enum allows us to store two different kinds of information: /// 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 /// 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( crate fn collect_spans_and_sources(
tcx: TyCtxt<'_>, tcx: TyCtxt<'_>,
krate: clean::Crate, krate: clean::Crate,
src_root: &std::path::Path, src_root: &Path,
include_sources: bool, include_sources: bool,
generate_link_to_definition: 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() }; let mut visitor = SpanMapVisitor { tcx, matches: FxHashMap::default() };
if include_sources { if include_sources {