diff options
| author | Will Crichton <wcrichto@cs.stanford.edu> | 2021-10-08 15:32:22 -0700 |
|---|---|---|
| committer | Will Crichton <wcrichto@cs.stanford.edu> | 2021-10-08 15:32:22 -0700 |
| commit | f10dceeae2532b61c6e7b893406658515b24ee82 (patch) | |
| tree | 7e630d216b872eca82f5a5ce8ac1daed3e2594ef /src/librustdoc | |
| parent | e22e8586877925c731e712f3116e53bdcccfffa7 (diff) | |
| download | rust-f10dceeae2532b61c6e7b893406658515b24ee82.tar.gz rust-f10dceeae2532b61c6e7b893406658515b24ee82.zip | |
Change handling of spans in scrape examples, add test for highlight decorations
Diffstat (limited to 'src/librustdoc')
| -rw-r--r-- | src/librustdoc/html/highlight/fixtures/decorations.html | 2 | ||||
| -rw-r--r-- | src/librustdoc/html/highlight/tests.rs | 17 | ||||
| -rw-r--r-- | src/librustdoc/scrape_examples.rs | 8 |
3 files changed, 23 insertions, 4 deletions
diff --git a/src/librustdoc/html/highlight/fixtures/decorations.html b/src/librustdoc/html/highlight/fixtures/decorations.html new file mode 100644 index 00000000000..45f567880c9 --- /dev/null +++ b/src/librustdoc/html/highlight/fixtures/decorations.html @@ -0,0 +1,2 @@ +<span class="example"><span class="kw">let</span> <span class="ident">x</span> <span class="op">=</span> <span class="number">1</span>;</span> +<span class="kw">let</span> <span class="ident">y</span> <span class="op">=</span> <span class="number">2</span>; \ No newline at end of file diff --git a/src/librustdoc/html/highlight/tests.rs b/src/librustdoc/html/highlight/tests.rs index 3fa386dded9..1fea7e983b4 100644 --- a/src/librustdoc/html/highlight/tests.rs +++ b/src/librustdoc/html/highlight/tests.rs @@ -1,6 +1,7 @@ -use super::write_code; +use super::{write_code, DecorationInfo}; use crate::html::format::Buffer; use expect_test::expect_file; +use rustc_data_structures::fx::FxHashMap; use rustc_span::create_default_session_globals_then; use rustc_span::edition::Edition; @@ -64,3 +65,17 @@ fn test_union_highlighting() { expect_file!["fixtures/union.html"].assert_eq(&html.into_inner()); }); } + +#[test] +fn test_decorations() { + create_default_session_globals_then(|| { + let src = "let x = 1; +let y = 2;"; + let mut decorations = FxHashMap::default(); + decorations.insert("example", vec![(0, 10)]); + + let mut html = Buffer::new(); + write_code(&mut html, src, Edition::Edition2018, None, Some(DecorationInfo(decorations))); + expect_file!["fixtures/decorations.html"].assert_eq(&html.into_inner()); + }); +} diff --git a/src/librustdoc/scrape_examples.rs b/src/librustdoc/scrape_examples.rs index c75a4244620..776073db7f5 100644 --- a/src/librustdoc/scrape_examples.rs +++ b/src/librustdoc/scrape_examples.rs @@ -149,9 +149,11 @@ where } }; - // We need to get the file the example originates in. If the call is contained - // in a macro, then trace the span back to the macro source (rather than macro definition). - let span = span.source_callsite(); + // If this span comes from a macro expansion, then the source code may not actually show + // a use of the given item, so it would be a poor example. Hence, we skip all uses in macros. + if span.from_expansion() { + return; + } // Save call site if the function resolves to a concrete definition if let ty::FnDef(def_id, _) = ty.kind() { |
