Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rollup of 5 pull requests #90203

Merged
merged 35 commits into from
Oct 23, 2021
Merged
Changes from 1 commit
Commits
Show all changes
35 commits
Select commit Hold shift + click to select a range
4b3f82a
Add updated support for example-analyzer
willcrichton May 9, 2021
7831fee
Fix check issue
willcrichton May 30, 2021
2855bf0
Factor scraping and rendering into separate calls to rustdoc
willcrichton Jun 1, 2021
b6338e7
Generate example source files with corresponding links
willcrichton Jun 3, 2021
eea8f0a
Sort examples by size
willcrichton Aug 26, 2021
55bb517
Move highlighting logic from JS to Rust
willcrichton Aug 26, 2021
18edcf8
Reduce blur size, fix example width bug, add better error handling fo…
willcrichton Sep 14, 2021
a1cb194
Add styles for non-white themes
willcrichton Sep 14, 2021
829b1a9
Incorporate jyn's feedback
willcrichton Sep 17, 2021
5c05b3c
Add target crates as inputs to reduce size of intermediates
willcrichton Sep 20, 2021
df5e3a6
Change serialized format to use DefPathHash instead of custom String
willcrichton Sep 21, 2021
25323ec
Move JS into a standalone file
willcrichton Oct 1, 2021
55731bb
Fix lint error, change scrape-examples.js minify call
willcrichton Oct 1, 2021
ed8e12f
Unversioned -> InvocationSpecific
willcrichton Oct 1, 2021
5584c79
Update to latest rustc and rustdoc styles
willcrichton Oct 7, 2021
bb383ed
Move some expansion logic into generation-time, fix section header li…
willcrichton Oct 7, 2021
8b141a2
Add variance constraints for const params
voidc Oct 7, 2021
e22e858
Move more scrape-examples logic from JS to rust
willcrichton Oct 7, 2021
f10dcee
Change handling of spans in scrape examples, add test for highlight d…
willcrichton Oct 8, 2021
9e4958a
Add test for prev/back arrows + examples across multiple files
willcrichton Oct 9, 2021
b1616f3
Add test for ordering of examples, simplify with single scrape.mk file
willcrichton Oct 9, 2021
59b36bc
Add UI test for the variance of types appearing in consts
voidc Oct 12, 2021
a400f10
Bless tests
voidc Oct 12, 2021
24a71cb
Fix local crate not being scraped
willcrichton Oct 13, 2021
02e4d0b
Make all proc-macro back-compat lints deny-by-default
Aaron1011 Aug 15, 2021
8f80d86
Small scrape-example fixes
willcrichton Oct 20, 2021
d1c29c6
Revert def_id addition from clean::Function, add test for
willcrichton Oct 22, 2021
fd5d614
Move def_id logic into render_call_locations
willcrichton Oct 22, 2021
e4aeeca
Reset qualifs when a storage of a local ends
tmiasko Oct 22, 2021
fd25491
Add caveat about changing parallelism and function call overhead
the8472 Oct 23, 2021
dcf9242
Rollup merge of #85833 - willcrichton:example-analyzer, r=jyn514
matthiaskrgr Oct 23, 2021
17c602d
Rollup merge of #88041 - Aaron1011:deny-proc-macro-hack, r=wesleywiser
matthiaskrgr Oct 23, 2021
2b874f0
Rollup merge of #89829 - voidc:assoc-const-variance, r=lcnr
matthiaskrgr Oct 23, 2021
74a0c49
Rollup merge of #90168 - tmiasko:const-qualif-storage, r=matthewjasper
matthiaskrgr Oct 23, 2021
1680359
Rollup merge of #90198 - the8472:available-parallelism-runtime, r=jos…
matthiaskrgr Oct 23, 2021
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
Prev Previous commit
Next Next commit
Move more scrape-examples logic from JS to rust
Fix failing test

Add missing backslash

Fix padding issue with horizontal scrollbar
willcrichton committed Oct 8, 2021
commit e22e8586877925c731e712f3116e53bdcccfffa7
2 changes: 1 addition & 1 deletion src/librustdoc/html/highlight.rs
Original file line number Diff line number Diff line change
@@ -691,7 +691,7 @@ fn string<T: Display>(
// https://github.com/rust-lang/rust/blob/60f1a2fc4b535ead9c85ce085fdce49b1b097531/src/librustdoc/html/render/context.rs#L315-L338
match href {
LinkFromSrc::Local(span) => context
.href_from_span(*span)
.href_from_span(*span, true)
.map(|s| format!("{}{}", context_info.root_path, s)),
LinkFromSrc::External(def_id) => {
format::href_with_root_path(*def_id, context, Some(context_info.root_path))
26 changes: 18 additions & 8 deletions src/librustdoc/html/render/context.rs
Original file line number Diff line number Diff line change
@@ -295,10 +295,10 @@ impl<'tcx> Context<'tcx> {
/// may happen, for example, with externally inlined items where the source
/// of their crate documentation isn't known.
pub(super) fn src_href(&self, item: &clean::Item) -> Option<String> {
self.href_from_span(item.span(self.tcx()))
self.href_from_span(item.span(self.tcx()), true)
}

crate fn href_from_span(&self, span: clean::Span) -> Option<String> {
crate fn href_from_span(&self, span: clean::Span, with_lines: bool) -> Option<String> {
if span.is_dummy() {
return None;
}
@@ -345,16 +345,26 @@ impl<'tcx> Context<'tcx> {
(&*symbol, &path)
};

let loline = span.lo(self.sess()).line;
let hiline = span.hi(self.sess()).line;
let lines =
if loline == hiline { loline.to_string() } else { format!("{}-{}", loline, hiline) };
let anchor = if with_lines {
let loline = span.lo(self.sess()).line;
let hiline = span.hi(self.sess()).line;
format!(
"#{}",
if loline == hiline {
loline.to_string()
} else {
format!("{}-{}", loline, hiline)
}
)
} else {
"".to_string()
};
Some(format!(
"{root}src/{krate}/{path}#{lines}",
"{root}src/{krate}/{path}{anchor}",
root = Escape(&root),
krate = krate,
path = path,
lines = lines
anchor = anchor
))
}
}
45 changes: 27 additions & 18 deletions src/librustdoc/html/render/mod.rs
Original file line number Diff line number Diff line change
@@ -2475,12 +2475,11 @@ fn render_call_locations(w: &mut Buffer, cx: &Context<'_>, def_id: DefId, item:
};

// Generate a unique ID so users can link to this section for a given method
// FIXME: this should use init_id_map instead of derive
let id = cx.id_map.borrow_mut().derive("scraped-examples");
write!(
w,
"<div class=\"docblock scraped-example-list\">\
<span></span>
<span></span>\
<h5 id=\"{id}\" class=\"section-header\">\
<a href=\"#{id}\">Examples found in repository</a>\
</h5>",
@@ -2516,42 +2515,51 @@ fn render_call_locations(w: &mut Buffer, cx: &Context<'_>, def_id: DefId, item:

// The call locations need to be updated to reflect that the size of the program has changed.
// Specifically, the ranges are all subtracted by `byte_min` since that's the new zero point.
let (byte_ranges, line_ranges): (Vec<_>, Vec<_>) = call_data
let (mut byte_ranges, line_ranges): (Vec<_>, Vec<_>) = call_data
.locations
.iter()
.map(|loc| {
let (byte_lo, byte_hi) = loc.call_expr.byte_span;
let (line_lo, line_hi) = loc.call_expr.line_span;
((byte_lo - byte_min, byte_hi - byte_min), (line_lo - line_min, line_hi - line_min))
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);

(byte_range, (line_range, line_url, line_title))
})
.unzip();

let (init_min, init_max) = line_ranges[0];
let line_range = if init_min == init_max {
format!("line {}", init_min + line_min + 1)
} else {
format!("lines {}-{}", init_min + line_min + 1, init_max + line_min + 1)
};

let (_, init_url, init_title) = &line_ranges[0];
let needs_expansion = line_max - line_min > NUM_VISIBLE_LINES;
let locations_encoded = serde_json::to_string(&line_ranges).unwrap();

write!(
w,
"<div class=\"scraped-example {expanded_cls}\" data-locs=\"{locations}\">\
<div class=\"scraped-example-title\">\
{name} (<a href=\"{root}{url}\">{line_range}</a>)\
{name} (<a href=\"{url}\">{title}</a>)\
</div>\
<div class=\"code-wrapper\">",
root = cx.root_path(),
url = call_data.url,
name = call_data.display_name,
line_range = line_range,
expanded_cls = if needs_expansion { "" } else { "expanded" },
name = call_data.display_name,
url = init_url,
title = init_title,
// The locations are encoded as a data attribute, so they can be read
// later by the JS for interactions.
locations = serde_json::to_string(&line_ranges).unwrap(),
locations = Escape(&locations_encoded)
);
write!(w, r#"<span class="prev">&pr;</span> <span class="next">&sc;</span>"#);

if line_ranges.len() > 1 {
write!(w, r#"<span class="prev">&pr;</span> <span class="next">&sc;</span>"#);
}

if needs_expansion {
write!(w, r#"<span class="expand">&varr;</span>"#);
@@ -2580,6 +2588,7 @@ fn render_call_locations(w: &mut Buffer, cx: &Context<'_>, def_id: DefId, item:
let root_path = vec!["../"; cx.current.len() - 1].join("");

let mut decoration_info = FxHashMap::default();
decoration_info.insert("highlight focus", vec![byte_ranges.remove(0)]);
decoration_info.insert("highlight", byte_ranges);

sources::print_src(
1 change: 1 addition & 0 deletions src/librustdoc/html/static/css/rustdoc.css
Original file line number Diff line number Diff line change
@@ -1986,6 +1986,7 @@ details.undocumented[open] > summary::before {
.scraped-example:not(.expanded) .code-wrapper .example-wrap pre.rust {
overflow-y: hidden;
max-height: 240px;
padding-bottom: 0;
}

.scraped-example .code-wrapper .prev {
46 changes: 13 additions & 33 deletions src/librustdoc/html/static/js/scrape-examples.js
Original file line number Diff line number Diff line change
@@ -15,41 +15,23 @@

function updateScrapedExample(example) {
var locs = JSON.parse(example.attributes.getNamedItem("data-locs").textContent);
var first_line_no = example.querySelector('.line-numbers > span:first-child');
var offset = parseInt(first_line_no.innerHTML) - 1;

var locIndex = 0;
var highlights = example.querySelectorAll('.highlight');
var link = example.querySelector('.scraped-example-title a');
addClass(highlights[0], 'focus');

if (locs.length > 1) {
// Toggle through list of examples in a given file
var onChangeLoc = function(f) {
var onChangeLoc = function(changeIndex) {
removeClass(highlights[locIndex], 'focus');
f();
scrollToLoc(example, locs[locIndex]);
changeIndex();
scrollToLoc(example, locs[locIndex][0]);
addClass(highlights[locIndex], 'focus');

var curLoc = locs[locIndex];
var minLine = curLoc[0] + offset + 1;
var maxLine = curLoc[1] + offset + 1;

var text;
var anchor;
if (minLine == maxLine) {
text = 'line ' + minLine.toString();
anchor = minLine.toString();
} else {
var range = minLine.toString() + '-' + maxLine.toString();
text = 'lines ' + range;
anchor = range;
}

var url = new URL(link.href);
url.hash = anchor;
var url = locs[locIndex][1];
var title = locs[locIndex][2];

link.href = url.toString();
link.innerHTML = text;
link.href = url;
link.innerHTML = title;
};

example.querySelector('.prev')
@@ -61,28 +43,26 @@

example.querySelector('.next')
.addEventListener('click', function() {
onChangeLoc(function() { locIndex = (locIndex + 1) % locs.length; });
onChangeLoc(function() {
locIndex = (locIndex + 1) % locs.length;
});
});
} else {
// Remove buttons if there's only one example in the file
example.querySelector('.prev').remove();
example.querySelector('.next').remove();
}

var expandButton = example.querySelector('.expand');
if (expandButton) {
expandButton.addEventListener('click', function () {
if (hasClass(example, "expanded")) {
removeClass(example, "expanded");
scrollToLoc(example, locs[0]);
scrollToLoc(example, locs[0][0]);
} else {
addClass(example, "expanded");
}
});
}

// Start with the first example in view
scrollToLoc(example, locs[0]);
scrollToLoc(example, locs[0][0]);
}

var firstExamples = document.querySelectorAll('.scraped-example-list > .scraped-example');
3 changes: 2 additions & 1 deletion src/librustdoc/scrape_examples.rs
Original file line number Diff line number Diff line change
@@ -171,7 +171,7 @@ where
let cx = &self.cx;
let mk_call_data = || {
let clean_span = crate::clean::types::Span::new(span);
let url = cx.href_from_span(clean_span).unwrap();
let url = cx.href_from_span(clean_span, false).unwrap();
let display_name = file_path.display().to_string();
let edition = span.edition();
CallData { locations: Vec::new(), url, display_name, edition }
@@ -233,6 +233,7 @@ crate fn run(
Ok(())
}

// Note: the Handler must be passed in explicitly because sess isn't available while parsing options
crate fn load_call_locations(
with_examples: Vec<String>,
diag: &rustc_errors::Handler,
4 changes: 2 additions & 2 deletions src/test/run-make/rustdoc-scrape-examples-remap/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// @has foobar/b/fn.foo.html '//*[@class="scraped-example"]' 'ex.rs'
// @has foobar/c/fn.foo.html '//*[@class="scraped-example"]' 'ex.rs'
// @has foobar/b/fn.foo.html '//*[@class="scraped-example expanded"]' 'ex.rs'
// @has foobar/c/fn.foo.html '//*[@class="scraped-example expanded"]' 'ex.rs'

#[path = "a.rs"]
pub mod b;