Skip to content

Commit f10dcee

Browse files
committed
Change handling of spans in scrape examples, add test for highlight decorations
1 parent e22e858 commit f10dcee

File tree

3 files changed

+23
-4
lines changed

3 files changed

+23
-4
lines changed
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
<span class="example"><span class="kw">let</span> <span class="ident">x</span> <span class="op">=</span> <span class="number">1</span>;</span>
2+
<span class="kw">let</span> <span class="ident">y</span> <span class="op">=</span> <span class="number">2</span>;

src/librustdoc/html/highlight/tests.rs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
use super::write_code;
1+
use super::{write_code, DecorationInfo};
22
use crate::html::format::Buffer;
33
use expect_test::expect_file;
4+
use rustc_data_structures::fx::FxHashMap;
45
use rustc_span::create_default_session_globals_then;
56
use rustc_span::edition::Edition;
67

@@ -64,3 +65,17 @@ fn test_union_highlighting() {
6465
expect_file!["fixtures/union.html"].assert_eq(&html.into_inner());
6566
});
6667
}
68+
69+
#[test]
70+
fn test_decorations() {
71+
create_default_session_globals_then(|| {
72+
let src = "let x = 1;
73+
let y = 2;";
74+
let mut decorations = FxHashMap::default();
75+
decorations.insert("example", vec![(0, 10)]);
76+
77+
let mut html = Buffer::new();
78+
write_code(&mut html, src, Edition::Edition2018, None, Some(DecorationInfo(decorations)));
79+
expect_file!["fixtures/decorations.html"].assert_eq(&html.into_inner());
80+
});
81+
}

src/librustdoc/scrape_examples.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -149,9 +149,11 @@ where
149149
}
150150
};
151151

152-
// We need to get the file the example originates in. If the call is contained
153-
// in a macro, then trace the span back to the macro source (rather than macro definition).
154-
let span = span.source_callsite();
152+
// If this span comes from a macro expansion, then the source code may not actually show
153+
// a use of the given item, so it would be a poor example. Hence, we skip all uses in macros.
154+
if span.from_expansion() {
155+
return;
156+
}
155157

156158
// Save call site if the function resolves to a concrete definition
157159
if let ty::FnDef(def_id, _) = ty.kind() {

0 commit comments

Comments
 (0)