From 4b5e08ad79640d7924dac447024e0d873ce478d3 Mon Sep 17 00:00:00 2001 From: Will Crichton Date: Tue, 2 Nov 2021 18:48:07 -0700 Subject: [PATCH 1/2] Fix URL for scrape-examples.js in rustdoc page template --- src/librustdoc/html/templates/page.html | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/librustdoc/html/templates/page.html b/src/librustdoc/html/templates/page.html index cead54412bbbd..9fafea6914524 100644 --- a/src/librustdoc/html/templates/page.html +++ b/src/librustdoc/html/templates/page.html @@ -108,12 +108,12 @@ data-search-js="{{static_root_path | safe}}search{{page.resource_suffix}}.js"> {#- -#} {#- -#} - {%- if layout.scrape_examples_extension -%} - {#- -#} - {%- endif -%} {%- for script in page.static_extra_scripts -%} {#- -#} {% endfor %} + {%- if layout.scrape_examples_extension -%} + {#- -#} + {%- endif -%} {%- for script in page.extra_scripts -%} {#- -#} {% endfor %} From ce943d26f8f1449a40c70a48ead48e351fd1b128 Mon Sep 17 00:00:00 2001 From: Will Crichton Date: Tue, 2 Nov 2021 19:38:55 -0700 Subject: [PATCH 2/2] Add line number to URLs in "additional examples" section of rustdoc --- src/librustdoc/html/render/mod.rs | 33 +++++++++++++++++++------------ 1 file changed, 20 insertions(+), 13 deletions(-) diff --git a/src/librustdoc/html/render/mod.rs b/src/librustdoc/html/render/mod.rs index f78129050d7ec..25fef114d95fd 100644 --- a/src/librustdoc/html/render/mod.rs +++ b/src/librustdoc/html/render/mod.rs @@ -76,7 +76,7 @@ use crate::html::format::{ use crate::html::highlight; use crate::html::markdown::{HeadingOffset, Markdown, MarkdownHtml, MarkdownSummaryLine}; use crate::html::sources; -use crate::scrape_examples::CallData; +use crate::scrape_examples::{CallData, CallLocation}; use crate::try_none; /// A pair of name and its optional document. @@ -2594,6 +2594,21 @@ fn render_call_locations(w: &mut Buffer, cx: &Context<'_>, item: &clean::Item) { id = id ); + // Create a URL to a particular location in a reverse-dependency's source file + let link_to_loc = |call_data: &CallData, loc: &CallLocation| -> (String, String) { + let (line_lo, line_hi) = loc.call_expr.line_span; + let (anchor, title) = if line_lo == line_hi { + ((line_lo + 1).to_string(), format!("line {}", line_lo + 1)) + } else { + ( + format!("{}-{}", line_lo + 1, line_hi + 1), + format!("lines {}-{}", line_lo + 1, line_hi + 1), + ) + }; + let url = format!("{}{}#{}", cx.root_path(), call_data.url, anchor); + (url, title) + }; + // Generate the HTML for a single example, being the title and code block let write_example = |w: &mut Buffer, (path, call_data): (&PathBuf, &CallData)| -> bool { let contents = match fs::read_to_string(&path) { @@ -2631,15 +2646,7 @@ fn render_call_locations(w: &mut Buffer, cx: &Context<'_>, item: &clean::Item) { let (line_lo, line_hi) = loc.call_expr.line_span; let byte_range = (byte_lo - byte_min, byte_hi - byte_min); let line_range = (line_lo - line_min, line_hi - line_min); - let (anchor, line_title) = if line_lo == line_hi { - (format!("{}", line_lo + 1), format!("line {}", line_lo + 1)) - } else { - ( - format!("{}-{}", line_lo + 1, line_hi + 1), - format!("lines {}-{}", line_lo + 1, line_hi + 1), - ) - }; - let line_url = format!("{}{}#{}", cx.root_path(), call_data.url, anchor); + let (line_url, line_title) = link_to_loc(call_data, loc); (byte_range, (line_range, line_url, line_title)) }) @@ -2768,11 +2775,11 @@ fn render_call_locations(w: &mut Buffer, cx: &Context<'_>, item: &clean::Item) { if it.peek().is_some() { write!(w, r#"