Sort scraped call locations before serializing

This commit is contained in:
Will Crichton 2021-11-04 13:57:09 -07:00
parent ce943d26f8
commit 3ad6d12827
5 changed files with 26 additions and 4 deletions

View file

@ -270,12 +270,18 @@ struct Decorations {
impl Decorations {
fn new(info: DecorationInfo) -> Self {
let (starts, ends) = info
// Extract tuples (start, end, kind) into separate sequences of (start, kind) and (end).
let (mut starts, mut ends): (Vec<_>, Vec<_>) = info
.0
.into_iter()
.map(|(kind, ranges)| ranges.into_iter().map(move |(lo, hi)| ((lo, kind), hi)))
.flatten()
.unzip();
// Sort the sequences in document order.
starts.sort_by_key(|(lo, _)| *lo);
ends.sort();
Decorations { starts, ends }
}
}

View file

@ -240,6 +240,13 @@ crate fn run(
let mut finder = FindCalls { calls: &mut calls, tcx, map: tcx.hir(), cx, target_crates };
tcx.hir().visit_all_item_likes(&mut finder.as_deep_visitor());
// Sort call locations within a given file in document order
for fn_calls in calls.values_mut() {
for file_calls in fn_calls.values_mut() {
file_calls.locations.sort_by_key(|loc| loc.call_expr.byte_span.0);
}
}
// Save output to provided path
let mut encoder = FileEncoder::new(options.output_path).map_err(|e| e.to_string())?;
calls.encode(&mut encoder).map_err(|e| e.to_string())?;

View file

@ -1,8 +1,10 @@
fn main() {
foobar::ok();
foobar::ok(0);
// this is a
// ..
// BIG
// item

View file

@ -1,4 +1,8 @@
fn main() {
foobar::ok();
foobar::ok(1);
// small item
}
fn f() {
foobar::ok(2);
}

View file

@ -1,4 +1,7 @@
// @has foobar/fn.ok.html '//*[@class="docblock scraped-example-list"]' 'ex2'
// @has foobar/fn.ok.html '//*[@class="more-scraped-examples"]' 'ex1'
// @has foobar/fn.ok.html '//*[@class="highlight focus"]' '1'
// @has foobar/fn.ok.html '//*[@class="highlight"]' '2'
// @has foobar/fn.ok.html '//*[@class="highlight focus"]' '0'
pub fn ok() {}
pub fn ok(_x: i32) {}