about summary refs log tree commit diff
path: root/src
diff options
context:
space:
mode:
authorGuillaume Gomez <guillaume1.gomez@gmail.com>2021-05-05 14:19:51 +0200
committerGuillaume Gomez <guillaume.gomez@huawei.com>2021-08-05 23:08:28 +0200
commitdffc9c0a79bcf06a3abe6746056746bb11125b6a (patch)
tree328932614d10f3089adee4376530a8b69af628ce /src
parente8869cb7a7e760f39088190a724991726cd05c50 (diff)
downloadrust-dffc9c0a79bcf06a3abe6746056746bb11125b6a.tar.gz
rust-dffc9c0a79bcf06a3abe6746056746bb11125b6a.zip
Move extra arguments for highlight URL generation into a new ContextInfo struct for better readability
Diffstat (limited to 'src')
-rw-r--r--src/librustdoc/html/highlight.rs70
-rw-r--r--src/librustdoc/html/highlight/tests.rs4
-rw-r--r--src/librustdoc/html/markdown.rs2
-rw-r--r--src/librustdoc/html/render/print_item.rs2
-rw-r--r--src/librustdoc/html/sources.rs4
5 files changed, 42 insertions, 40 deletions
diff --git a/src/librustdoc/html/highlight.rs b/src/librustdoc/html/highlight.rs
index 808e1ca236f..d1dbfeff876 100644
--- a/src/librustdoc/html/highlight.rs
+++ b/src/librustdoc/html/highlight.rs
@@ -19,6 +19,21 @@ use rustc_span::symbol::Symbol;
 use super::format::{self, Buffer};
 use super::render::{LightSpan, LinkFromSrc};
 
+/// This type is needed in case we want to render links on items to allow to go to their definition.
+crate struct ContextInfo<'a, 'b, 'c> {
+    crate context: &'a Context<'b>,
+    /// This represents the "lo" bytes of the current file we're rendering. To get a [`Span`] from
+    /// it, you just need to add add your current byte position in the string and its length (to get
+    /// the "hi" part).
+    ///
+    /// This is used to create a [`LightSpan`] which is then used as an index in the `span_map` in
+    /// order to retrieve the definition's [`Span`] (which is used to generate the URL).
+    crate file_span_lo: u32,
+    /// This field is used to know "how far" from the top of the directory we are to link to either
+    /// documentation pages or other source pages.
+    crate root_path: &'c str,
+}
+
 /// Highlights `src`, returning the HTML output.
 crate fn render_with_highlighting(
     src: &str,
@@ -28,9 +43,7 @@ crate fn render_with_highlighting(
     tooltip: Option<(Option<Edition>, &str)>,
     edition: Edition,
     extra_content: Option<Buffer>,
-    file_span_lo: u32,
-    context: Option<&Context<'_>>,
-    root_path: &str,
+    context_info: Option<ContextInfo<'_, '_, '_>>,
 ) {
     debug!("highlighting: ================\n{}\n==============", src);
     if let Some((edition_info, class)) = tooltip {
@@ -47,7 +60,7 @@ crate fn render_with_highlighting(
     }
 
     write_header(out, class, extra_content);
-    write_code(out, &src, edition, file_span_lo, context, root_path);
+    write_code(out, &src, edition, context_info);
     write_footer(out, playground_button);
 }
 
@@ -69,37 +82,28 @@ fn write_header(out: &mut Buffer, class: Option<&str>, extra_content: Option<Buf
 ///
 /// Some explanations on the last arguments:
 ///
-/// In case we are rendering a code block and not a source code file, `file_span_lo` value doesn't
-/// matter and `context` will be `None`. To put it more simply: if `context` is `None`, the code
-/// won't try to generate links to an ident definition.
+/// In case we are rendering a code block and not a source code file, `context_info` will be `None`.
+/// To put it more simply: if `context_info` is `None`, the code won't try to generate links to an
+/// item definition.
 ///
 /// More explanations about spans and how we use them here are provided in the
 /// [`LightSpan::new_in_file`] function documentation about how it works.
-///
-/// As for `root_path`, it's used to know "how far" from the top of the directory we are to link
-/// to either documentation pages or other source pages.
-///
-/// Same as `file_span_lo`: its value doesn't matter in case you are not rendering a source code
-/// file.
 fn write_code(
     out: &mut Buffer,
     src: &str,
     edition: Edition,
-    file_span_lo: u32,
-    context: Option<&Context<'_>>,
-    root_path: &str,
+    context_info: Option<ContextInfo<'_, '_, '_>>,
 ) {
     // This replace allows to fix how the code source with DOS backline characters is displayed.
     let src = src.replace("\r\n", "\n");
-    Classifier::new(&src, edition, file_span_lo).highlight(&mut |highlight| {
-        match highlight {
-            Highlight::Token { text, class } => {
-                string(out, Escape(text), class, context, root_path)
-            }
-            Highlight::EnterSpan { class } => enter_span(out, class),
-            Highlight::ExitSpan => exit_span(out),
-        };
-    });
+    Classifier::new(&src, edition, context_info.as_ref().map(|c| c.file_span_lo).unwrap_or(0))
+        .highlight(&mut |highlight| {
+            match highlight {
+                Highlight::Token { text, class } => string(out, Escape(text), class, &context_info),
+                Highlight::EnterSpan { class } => enter_span(out, class),
+                Highlight::ExitSpan => exit_span(out),
+            };
+        });
 }
 
 fn write_footer(out: &mut Buffer, playground_button: Option<&str>) {
@@ -540,8 +544,7 @@ fn string<T: Display>(
     out: &mut Buffer,
     text: T,
     klass: Option<Class>,
-    context: Option<&Context<'_>>,
-    root_path: &str,
+    context_info: &Option<ContextInfo<'_, '_, '_>>,
 ) {
     let klass = match klass {
         None => return write!(out, "{}", text),
@@ -570,14 +573,19 @@ fn string<T: Display>(
                 path
             });
         }
-        if let Some(context) = context {
-            if let Some(href) =
-                context.shared.span_correspondance_map.get(&def_span).and_then(|href| {
+        if let Some(context_info) = context_info {
+            if let Some(href) = context_info
+                .context
+                .shared
+                .span_correspondance_map
+                .get(&def_span)
+                .and_then(|href| {
+                    let context = context_info.context;
                     match href {
                         LinkFromSrc::Local(span) => {
                             context
                                 .href_from_span(clean::Span::wrap_raw(*span))
-                                .map(|s| format!("{}{}", root_path, s))
+                                .map(|s| format!("{}{}", context_info.root_path, s))
                         }
                         LinkFromSrc::External(def_id) => {
                             format::href(*def_id, context).map(|(url, _, _)| url)
diff --git a/src/librustdoc/html/highlight/tests.rs b/src/librustdoc/html/highlight/tests.rs
index a637d8085cb..1259a1f3a23 100644
--- a/src/librustdoc/html/highlight/tests.rs
+++ b/src/librustdoc/html/highlight/tests.rs
@@ -22,7 +22,7 @@ fn test_html_highlighting() {
         let src = include_str!("fixtures/sample.rs");
         let html = {
             let mut out = Buffer::new();
-            write_code(&mut out, src, Edition::Edition2018, 0, None, "");
+            write_code(&mut out, src, Edition::Edition2018, None);
             format!("{}<pre><code>{}</code></pre>\n", STYLE, out.into_inner())
         };
         expect_file!["fixtures/sample.html"].assert_eq(&html);
@@ -36,7 +36,7 @@ fn test_dos_backline() {
     println!(\"foo\");\r\n\
 }\r\n";
         let mut html = Buffer::new();
-        write_code(&mut html, src, Edition::Edition2018, 0, None, "");
+        write_code(&mut html, src, Edition::Edition2018, None);
         expect_file!["fixtures/dos_line.html"].assert_eq(&html.into_inner());
     });
 }
diff --git a/src/librustdoc/html/markdown.rs b/src/librustdoc/html/markdown.rs
index 5d79ccce8e1..472323daf30 100644
--- a/src/librustdoc/html/markdown.rs
+++ b/src/librustdoc/html/markdown.rs
@@ -330,9 +330,7 @@ impl<'a, I: Iterator<Item = Event<'a>>> Iterator for CodeBlocks<'_, 'a, I> {
             tooltip,
             edition,
             None,
-            0,
             None,
-            "",
         );
         Some(Event::Html(s.into_inner().into()))
     }
diff --git a/src/librustdoc/html/render/print_item.rs b/src/librustdoc/html/render/print_item.rs
index 7b6bd4dd597..f31305c76e6 100644
--- a/src/librustdoc/html/render/print_item.rs
+++ b/src/librustdoc/html/render/print_item.rs
@@ -1081,9 +1081,7 @@ fn item_macro(w: &mut Buffer, cx: &Context<'_>, it: &clean::Item, t: &clean::Mac
             None,
             it.span(cx.tcx()).inner().edition(),
             None,
-            0,
             None,
-            "",
         );
     });
     document(w, cx, it, None)
diff --git a/src/librustdoc/html/sources.rs b/src/librustdoc/html/sources.rs
index 24821950869..9204c94bd76 100644
--- a/src/librustdoc/html/sources.rs
+++ b/src/librustdoc/html/sources.rs
@@ -275,8 +275,6 @@ fn print_src(
         None,
         edition,
         Some(line_numbers),
-        file_span_lo,
-        Some(context),
-        root_path,
+        Some(highlight::ContextInfo { context, file_span_lo, root_path }),
     );
 }