1
Fork 0

Improve several aspects of the Rustdoc scrape-examples UI.

* Examples take up less screen height.
* Snippets from binary crates are prioritized.
* toggle-all-docs does not expand "More examples" sections.
This commit is contained in:
Will Crichton 2022-11-27 13:11:21 -06:00
parent 01fbc5ae78
commit 6ccd14a782
11 changed files with 106 additions and 33 deletions

View file

@ -2957,14 +2957,22 @@ fn render_call_locations(w: &mut Buffer, cx: &mut Context<'_>, item: &clean::Ite
// The call locations are output in sequence, so that sequence needs to be determined.
// Ideally the most "relevant" examples would be shown first, but there's no general algorithm
// for determining relevance. Instead, we prefer the smallest examples being likely the easiest to
// understand at a glance.
// for determining relevance. We instead make a proxy for relevance with the following heuristics:
// 1. Code written to be an example is better than code not written to be an example, e.g.
// a snippet from examples/foo.rs is better than src/lib.rs. We don't know the Cargo directory
// structure in Rustdoc, so we proxy this by prioriting code that comes from a --crate-type bin.
// 2. Smaller examples are better than large examples. So we prioritize snippets that have the
// smallest line span for their enclosing item.
// 3. Finally we sort by the displayed file name, which is arbitrary but prevents the ordering
// of examples from randomly changing between Rustdoc invocations.
let ordered_locations = {
let sort_criterion = |(_, call_data): &(_, &CallData)| {
fn sort_criterion<'a>(
(_, call_data): &(&PathBuf, &'a CallData),
) -> (bool, u32, &'a String) {
// Use the first location because that's what the user will see initially
let (lo, hi) = call_data.locations[0].enclosing_item.byte_span;
hi - lo
};
(!call_data.is_bin, hi - lo, &call_data.display_name)
}
let mut locs = call_locations.iter().collect::<Vec<_>>();
locs.sort_by_key(sort_criterion);