Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 37 additions & 15 deletions src/librustdoc/html/render/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2812,24 +2812,46 @@ fn render_call_locations<W: fmt::Write>(
let needs_expansion = line_max - line_min > NUM_VISIBLE_LINES;
let locations_encoded = serde_json::to_string(&line_ranges).unwrap();

// Look for the example file in the source map if it exists, otherwise return a dummy span
let file_span = (|| {
let source_map = tcx.sess.source_map();
let crate_src = tcx.sess.local_crate_source_file()?.into_local_path()?;
let source_map = tcx.sess.source_map();
let files = source_map.files();
let local = tcx.sess.local_crate_source_file().unwrap();

let get_file_start_pos = || {
let crate_src = local.clone().into_local_path()?;
let abs_crate_src = crate_src.canonicalize().ok()?;
let crate_root = abs_crate_src.parent()?.parent()?;
let rel_path = path.strip_prefix(crate_root).ok()?;
let files = source_map.files();
let file = files.iter().find(|file| match &file.name {
FileName::Real(RealFileName::LocalPath(other_path)) => rel_path == other_path,
_ => false,
})?;
Some(rustc_span::Span::with_root_ctxt(
file.start_pos + BytePos(byte_min),
file.start_pos + BytePos(byte_max),
))
})()
.unwrap_or(DUMMY_SP);
files
.iter()
.find(|file| match &file.name {
FileName::Real(RealFileName::LocalPath(other_path)) => rel_path == other_path,
_ => false,
})
.map(|file| file.start_pos)
};

// Look for the example file in the source map if it exists, otherwise
// return a span to the local crate's source file
let Some(file_span) = get_file_start_pos()
.or_else(|| {
files
.iter()
.find(|file| match &file.name {
FileName::Real(file_name) => file_name == &local,
_ => false,
})
.map(|file| file.start_pos)
})
.map(|start_pos| {
rustc_span::Span::with_root_ctxt(
start_pos + BytePos(byte_min),
start_pos + BytePos(byte_max),
)
})
else {
// if the fallback span can't be built, don't render the code for this example
return false;
};

let mut decoration_info = FxIndexMap::default();
decoration_info.insert("highlight focus", vec![byte_ranges.remove(0)]);
Expand Down
6 changes: 6 additions & 0 deletions tests/rustdoc-gui/scrape-examples-ice-links.goml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// Check that the line number column has the correct layout.
go-to: "file://" + |DOC_PATH| + "/scrape_ice/struct.ObscurelyNamedType1.html"
wait-for: ".scraped-example-title"
assert-attribute: (".scraped-example-title a", {"href": "../src/bar/bar.rs.html#2"})
click: ".scraped-example-title a"
wait-for-property: ("h1", {"innerText": "bar/\nbar.rs"})
2 changes: 1 addition & 1 deletion tests/rustdoc-gui/sidebar-source-code.goml
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ assert: "//*[@class='dir-entry' and @open]/*[normalize-space()='sub_mod']"
// Only "another_folder" should be "open" in "lib2".
assert: "//*[@class='dir-entry' and not(@open)]/*[normalize-space()='another_mod']"
// All other trees should be collapsed.
assert-count: ("//*[@id='src-sidebar']/details[not(normalize-space()='lib2') and not(@open)]", 12)
assert-count: ("//*[@id='src-sidebar']/details[not(normalize-space()='lib2') and not(@open)]", 13)

// We now switch to mobile mode.
set-window-size: (600, 600)
Expand Down
7 changes: 7 additions & 0 deletions tests/rustdoc-gui/src/scrape_examples_ice/Cargo.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 4

[[package]]
name = "scrape_ice"
version = "0.1.0"
9 changes: 9 additions & 0 deletions tests/rustdoc-gui/src/scrape_examples_ice/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[package]
name = "scrape_ice"
version = "0.1.0"
edition = "2024"

[[example]]
name = "bar"
path = "examples/bar.rs"
doc-scrape-examples = true
1 change: 1 addition & 0 deletions tests/rustdoc-gui/src/scrape_examples_ice/empty.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

3 changes: 3 additions & 0 deletions tests/rustdoc-gui/src/scrape_examples_ice/examples/bar.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
fn main() {
let mut bar = scrape_ice::ObscurelyNamedType1::new();
}
9 changes: 9 additions & 0 deletions tests/rustdoc-gui/src/scrape_examples_ice/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
//@ run-flags:-Zrustdoc-scrape-examples
//@ compile-flags: --html-after-content empty.html
pub struct ObscurelyNamedType1;

impl ObscurelyNamedType1 {
pub fn new() -> Self {
ObscurelyNamedType1
}
}
Loading