Skip to content

Commit ce943d2

Browse files
committed
Add line number to URLs in "additional examples" section of rustdoc
1 parent 4b5e08a commit ce943d2

File tree

1 file changed

+20
-13
lines changed
  • src/librustdoc/html/render

1 file changed

+20
-13
lines changed

Diff for: src/librustdoc/html/render/mod.rs

+20-13
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ use crate::html::format::{
7676
use crate::html::highlight;
7777
use crate::html::markdown::{HeadingOffset, Markdown, MarkdownHtml, MarkdownSummaryLine};
7878
use crate::html::sources;
79-
use crate::scrape_examples::CallData;
79+
use crate::scrape_examples::{CallData, CallLocation};
8080
use crate::try_none;
8181

8282
/// A pair of name and its optional document.
@@ -2594,6 +2594,21 @@ fn render_call_locations(w: &mut Buffer, cx: &Context<'_>, item: &clean::Item) {
25942594
id = id
25952595
);
25962596

2597+
// Create a URL to a particular location in a reverse-dependency's source file
2598+
let link_to_loc = |call_data: &CallData, loc: &CallLocation| -> (String, String) {
2599+
let (line_lo, line_hi) = loc.call_expr.line_span;
2600+
let (anchor, title) = if line_lo == line_hi {
2601+
((line_lo + 1).to_string(), format!("line {}", line_lo + 1))
2602+
} else {
2603+
(
2604+
format!("{}-{}", line_lo + 1, line_hi + 1),
2605+
format!("lines {}-{}", line_lo + 1, line_hi + 1),
2606+
)
2607+
};
2608+
let url = format!("{}{}#{}", cx.root_path(), call_data.url, anchor);
2609+
(url, title)
2610+
};
2611+
25972612
// Generate the HTML for a single example, being the title and code block
25982613
let write_example = |w: &mut Buffer, (path, call_data): (&PathBuf, &CallData)| -> bool {
25992614
let contents = match fs::read_to_string(&path) {
@@ -2631,15 +2646,7 @@ fn render_call_locations(w: &mut Buffer, cx: &Context<'_>, item: &clean::Item) {
26312646
let (line_lo, line_hi) = loc.call_expr.line_span;
26322647
let byte_range = (byte_lo - byte_min, byte_hi - byte_min);
26332648
let line_range = (line_lo - line_min, line_hi - line_min);
2634-
let (anchor, line_title) = if line_lo == line_hi {
2635-
(format!("{}", line_lo + 1), format!("line {}", line_lo + 1))
2636-
} else {
2637-
(
2638-
format!("{}-{}", line_lo + 1, line_hi + 1),
2639-
format!("lines {}-{}", line_lo + 1, line_hi + 1),
2640-
)
2641-
};
2642-
let line_url = format!("{}{}#{}", cx.root_path(), call_data.url, anchor);
2649+
let (line_url, line_title) = link_to_loc(call_data, loc);
26432650

26442651
(byte_range, (line_range, line_url, line_title))
26452652
})
@@ -2768,11 +2775,11 @@ fn render_call_locations(w: &mut Buffer, cx: &Context<'_>, item: &clean::Item) {
27682775
if it.peek().is_some() {
27692776
write!(w, r#"<div class="example-links">Additional examples can be found in:<br><ul>"#);
27702777
it.for_each(|(_, call_data)| {
2778+
let (url, _) = link_to_loc(&call_data, &call_data.locations[0]);
27712779
write!(
27722780
w,
2773-
r#"<li><a href="{root}{url}">{name}</a></li>"#,
2774-
root = cx.root_path(),
2775-
url = call_data.url,
2781+
r#"<li><a href="{url}">{name}</a></li>"#,
2782+
url = url,
27762783
name = call_data.display_name
27772784
);
27782785
});

0 commit comments

Comments
 (0)