Skip to content

Commit c5c927d

Browse files
Improve code readability
1 parent 38444f6 commit c5c927d

File tree

2 files changed

+46
-57
lines changed

2 files changed

+46
-57
lines changed

src/librustdoc/html/highlight.rs

Lines changed: 42 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -560,64 +560,51 @@ fn string<T: Display>(
560560
context: Option<&Context<'_>>,
561561
root_path: &str,
562562
) {
563-
match klass {
564-
None => write!(out, "{}", text),
565-
Some(klass) => {
566-
if let Some(def_span) = klass.get_span() {
567-
let mut text = text.to_string();
568-
if text.contains("::") {
569-
text =
570-
text.split("::").enumerate().fold(String::new(), |mut path, (pos, t)| {
571-
let pre = if pos != 0 { "::" } else { "" };
572-
match t {
573-
"self" | "Self" => write!(
574-
&mut path,
575-
"{}<span class=\"{}\">{}</span>",
576-
pre,
577-
Class::Self_((0, 0)).as_html(),
578-
t
579-
),
580-
"crate" | "super" => write!(
581-
&mut path,
582-
"{}<span class=\"{}\">{}</span>",
583-
pre,
584-
Class::KeyWord.as_html(),
585-
t
586-
),
587-
t => write!(&mut path, "{}{}", pre, t),
588-
}
589-
.expect("Failed to build source HTML path");
590-
path
591-
});
563+
let klass = match klass {
564+
None => return write!(out, "{}", text),
565+
Some(klass) => klass,
566+
};
567+
if let Some(def_span) = klass.get_span() {
568+
let mut text = text.to_string();
569+
if text.contains("::") {
570+
text = text.split("::").intersperse("::").fold(String::new(), |mut path, t| {
571+
match t {
572+
"self" | "Self" => write!(
573+
&mut path,
574+
"<span class=\"{}\">{}</span>",
575+
Class::Self_((0, 0)).as_html(),
576+
t
577+
),
578+
"crate" | "super" => write!(
579+
&mut path,
580+
"<span class=\"{}\">{}</span>",
581+
Class::KeyWord.as_html(),
582+
t
583+
),
584+
t => write!(&mut path, "{}", t),
592585
}
593-
if let Some(context) = context {
594-
if let Some(href) =
595-
context.shared.span_correspondance_map.get(&def_span).and_then(|href| {
596-
match href {
597-
LinkFromSrc::Local(span) => {
598-
eprintln!("==> {:?}:{:?}", span.lo(), span.hi());
599-
context
600-
.href_from_span(clean::Span::wrap_raw(*span))
601-
.map(|s| format!("{}{}", root_path, s))
602-
}
603-
LinkFromSrc::External(def_id) => {
604-
format::href(*def_id, context).map(|(url, _, _)| url)
605-
}
606-
}
607-
})
608-
{
609-
write!(
610-
out,
611-
"<a class=\"{}\" href=\"{}\">{}</a>",
612-
klass.as_html(),
613-
href,
614-
text
615-
);
616-
return;
586+
.expect("Failed to build source HTML path");
587+
path
588+
});
589+
}
590+
if let Some(context) = context {
591+
if let Some(href) =
592+
context.shared.span_correspondance_map.get(&def_span).and_then(|href| {
593+
match href {
594+
LinkFromSrc::Local(span) => {
595+
context
596+
.href_from_span(clean::Span::wrap_raw(*span))
597+
.map(|s| format!("{}{}", root_path, s))
598+
}
599+
LinkFromSrc::External(def_id) => {
600+
format::href(*def_id, context).map(|(url, _, _)| url)
601+
}
617602
}
618-
}
603+
})
604+
{
605+
write!(out, "<a class=\"{}\" href=\"{}\">{}</a>", klass.as_html(), href, text);
606+
return;
619607
}
620-
write!(out, "<span class=\"{}\">{}</span>", klass.as_html(), text);
621608
}
622609
}
623610
write!(out, "<span class=\"{}\">{}</span>", klass.as_html(), text);

src/librustdoc/html/render/span_map.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ use rustc_hir::{ExprKind, GenericParam, GenericParamKind, HirId, Mod, Node};
99
use rustc_middle::ty::TyCtxt;
1010
use rustc_span::Span;
1111

12+
use std::path::{Path, PathBuf};
13+
1214
/// This enum allows us to store two different kinds of information:
1315
///
1416
/// 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 {
3537
crate fn collect_spans_and_sources(
3638
tcx: TyCtxt<'_>,
3739
krate: clean::Crate,
38-
src_root: &std::path::Path,
40+
src_root: &Path,
3941
include_sources: bool,
4042
generate_link_to_definition: bool,
41-
) -> (clean::Crate, FxHashMap<std::path::PathBuf, String>, FxHashMap<(u32, u32), LinkFromSrc>) {
43+
) -> (clean::Crate, FxHashMap<PathBuf, String>, FxHashMap<(u32, u32), LinkFromSrc>) {
4244
let mut visitor = SpanMapVisitor { tcx, matches: FxHashMap::default() };
4345

4446
if include_sources {

0 commit comments

Comments
 (0)