]> git.lizzy.rs Git - rust.git/blobdiff - src/librustdoc/html/sources.rs
Rollup merge of #89876 - AlexApps99:const_ops, r=oli-obk
[rust.git] / src / librustdoc / html / sources.rs
index c3441036d503b9cd3ebcd7ed12d9800edc6afd7e..667bbc24ba5edb5deac643d0514f3543e6ac99d0 100644 (file)
@@ -67,7 +67,7 @@ fn add_local_source(&mut self, item: &clean::Item) {
         }
 
         let mut href = String::new();
-        clean_path(&self.src_root, &p, false, |component| {
+        clean_path(self.src_root, &p, false, |component| {
             href.push_str(&component.to_string_lossy());
             href.push('/');
         });
@@ -168,7 +168,7 @@ fn emit_source(
         };
 
         // Remove the utf-8 BOM if any
-        let contents = if contents.starts_with('\u{feff}') { &contents[3..] } else { &contents };
+        let contents = contents.strip_prefix('\u{feff}').unwrap_or(&contents);
 
         // Create the intermediate directories
         let mut cur = self.dst.clone();
@@ -209,10 +209,10 @@ fn emit_source(
                     contents,
                     self.cx.shared.edition(),
                     file_span,
-                    &self.cx,
+                    self.cx,
                     &root_path,
                     None,
-                    None,
+                    SourceContext::Standalone,
                 )
             },
             &self.cx.shared.style_files,
@@ -250,6 +250,11 @@ fn emit_source(
     }
 }
 
+crate enum SourceContext {
+    Standalone,
+    Embedded { offset: usize },
+}
+
 /// Wrapper struct to render the source code of a file. This will do things like
 /// adding line numbers to the left-hand side.
 crate fn print_src(
@@ -259,8 +264,8 @@ fn emit_source(
     file_span: rustc_span::Span,
     context: &Context<'_>,
     root_path: &str,
-    offset: Option<usize>,
     decoration_info: Option<highlight::DecorationInfo>,
+    source_context: SourceContext,
 ) {
     let lines = s.lines().count();
     let mut line_numbers = Buffer::empty_from(buf);
@@ -271,9 +276,15 @@ fn emit_source(
         tmp /= 10;
     }
     line_numbers.write_str("<pre class=\"line-numbers\">");
-    let offset = offset.unwrap_or(0);
     for i in 1..=lines {
-        writeln!(line_numbers, "<span id=\"{0}\">{0:1$}</span>", i + offset, cols);
+        match source_context {
+            SourceContext::Standalone => {
+                writeln!(line_numbers, "<span id=\"{0}\">{0:1$}</span>", i, cols)
+            }
+            SourceContext::Embedded { offset } => {
+                writeln!(line_numbers, "<span>{0:1$}</span>", i + offset, cols)
+            }
+        }
     }
     line_numbers.write_str("</pre>");
     highlight::render_with_highlighting(