Skip to content

Commit

Permalink
Auto merge of #90517 - willcrichton:example-analyzer, r=jyn514
Browse files Browse the repository at this point in the history
Fix URL for scrape-examples.js in rustdoc page template

Also adds line numbers to URLs in the "additional examples" section of rustdoc.

r? `@jyn514`
  • Loading branch information
bors committed Nov 3, 2021
2 parents 3831aaa + ce943d2 commit 473eaa4
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 16 deletions.
33 changes: 20 additions & 13 deletions src/librustdoc/html/render/mod.rs
Expand Up @@ -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.
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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))
})
Expand Down Expand Up @@ -2768,11 +2775,11 @@ fn render_call_locations(w: &mut Buffer, cx: &Context<'_>, item: &clean::Item) {
if it.peek().is_some() {
write!(w, r#"<div class="example-links">Additional examples can be found in:<br><ul>"#);
it.for_each(|(_, call_data)| {
let (url, _) = link_to_loc(&call_data, &call_data.locations[0]);
write!(
w,
r#"<li><a href="{root}{url}">{name}</a></li>"#,
root = cx.root_path(),
url = call_data.url,
r#"<li><a href="{url}">{name}</a></li>"#,
url = url,
name = call_data.display_name
);
});
Expand Down
6 changes: 3 additions & 3 deletions src/librustdoc/html/templates/page.html
Expand Up @@ -108,12 +108,12 @@
data-search-js="{{static_root_path | safe}}search{{page.resource_suffix}}.js"> {#- -#}
</div>
<script src="{{static_root_path | safe}}main{{page.resource_suffix}}.js"></script> {#- -#}
{%- if layout.scrape_examples_extension -%}
<script src="{{static_root_path | safe}}scrape-examples{{page.resource_suffix}}.js"></script> {#- -#}
{%- endif -%}
{%- for script in page.static_extra_scripts -%}
<script src="{{static_root_path | safe}}{{script}}.js"></script> {#- -#}
{% endfor %}
{%- if layout.scrape_examples_extension -%}
<script src="{{page.root_path | safe}}scrape-examples{{page.resource_suffix}}.js"></script> {#- -#}
{%- endif -%}
{%- for script in page.extra_scripts -%}
<script src="{{page.root_path | safe}}{{script}}.js"></script> {#- -#}
{% endfor %}
Expand Down

0 comments on commit 473eaa4

Please sign in to comment.