From c5c927dfda986e7370985b5a7c8c3ce452639d44 Mon Sep 17 00:00:00 2001 From: Guillaume Gomez Date: Tue, 4 May 2021 13:52:53 +0200 Subject: [PATCH] Improve code readability --- src/librustdoc/html/highlight.rs | 97 +++++++++++--------------- src/librustdoc/html/render/span_map.rs | 6 +- 2 files changed, 46 insertions(+), 57 deletions(-) diff --git a/src/librustdoc/html/highlight.rs b/src/librustdoc/html/highlight.rs index 253343b560d..edbb5dba1f3 100644 --- a/src/librustdoc/html/highlight.rs +++ b/src/librustdoc/html/highlight.rs @@ -560,64 +560,51 @@ fn string( 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, - "{}{}", - pre, - Class::Self_((0, 0)).as_html(), - t - ), - "crate" | "super" => write!( - &mut path, - "{}{}", - 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, + "{}", + Class::Self_((0, 0)).as_html(), + t + ), + "crate" | "super" => write!( + &mut path, + "{}", + 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, - "{}", - 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, "{}", klass.as_html(), href, text); + return; } - write!(out, "{}", klass.as_html(), text); } } write!(out, "{}", klass.as_html(), text); diff --git a/src/librustdoc/html/render/span_map.rs b/src/librustdoc/html/render/span_map.rs index 9e16c451626..9990d6f0ce6 100644 --- a/src/librustdoc/html/render/span_map.rs +++ b/src/librustdoc/html/render/span_map.rs @@ -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, FxHashMap<(u32, u32), LinkFromSrc>) { +) -> (clean::Crate, FxHashMap, FxHashMap<(u32, u32), LinkFromSrc>) { let mut visitor = SpanMapVisitor { tcx, matches: FxHashMap::default() }; if include_sources {