Skip to content

Commit

Permalink
Improve code readability
Browse files Browse the repository at this point in the history
  • Loading branch information
GuillaumeGomez committed Aug 5, 2021
1 parent 38444f6 commit c5c927d
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 57 deletions.
97 changes: 42 additions & 55 deletions src/librustdoc/html/highlight.rs
Expand Up @@ -560,64 +560,51 @@ fn string<T: Display>(
context: Option<&Context<'_>>,
root_path: &str,
) {
match klass {
None => write!(out, "{}", text),
Some(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 { "" };
match t {
"self" | "Self" => write!(
&mut path,
"{}<span class=\"{}\">{}</span>",
pre,
Class::Self_((0, 0)).as_html(),
t
),
"crate" | "super" => write!(
&mut path,
"{}<span class=\"{}\">{}</span>",
pre,
Class::KeyWord.as_html(),
t
),
t => write!(&mut path, "{}{}", pre, t),
}
.expect("Failed to build source HTML path");
path
});
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("::").intersperse("::").fold(String::new(), |mut path, t| {
match t {
"self" | "Self" => write!(
&mut path,
"<span class=\"{}\">{}</span>",
Class::Self_((0, 0)).as_html(),
t
),
"crate" | "super" => write!(
&mut path,
"<span class=\"{}\">{}</span>",
Class::KeyWord.as_html(),
t
),
t => write!(&mut path, "{}", t),
}
if let Some(context) = context {
if let Some(href) =
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))
}
LinkFromSrc::External(def_id) => {
format::href(*def_id, context).map(|(url, _, _)| url)
}
}
})
{
write!(
out,
"<a class=\"{}\" href=\"{}\">{}</a>",
klass.as_html(),
href,
text
);
return;
.expect("Failed to build source HTML path");
path
});
}
if let Some(context) = context {
if let Some(href) =
context.shared.span_correspondance_map.get(&def_span).and_then(|href| {
match href {
LinkFromSrc::Local(span) => {
context
.href_from_span(clean::Span::wrap_raw(*span))
.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, "<span class=\"{}\">{}</span>", klass.as_html(), text);
}
}
write!(out, "<span class=\"{}\">{}</span>", klass.as_html(), text);
Expand Down
6 changes: 4 additions & 2 deletions src/librustdoc/html/render/span_map.rs
Expand Up @@ -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
Expand All @@ -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 {
Expand Down

0 comments on commit c5c927d

Please sign in to comment.