4022: Fix incorrect order of syntax highlight ranges r=ltentrup a=ltentrup

A fix for the bug #4013 which is caused by a difference between tree traversal order and text representation order. In the case of #4013, the attributes of a macro were visited after the macro definition which caused the syntax highlight ranges to be in wrong order. The fix is to sort the ranges before returning. 

Co-authored-by: Leander Tentrup <leander.tentrup@gmail.com>
This commit is contained in:
bors[bot] 2020-04-17 20:52:46 +00:00 committed by GitHub
commit 746b2e003e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 14 additions and 1 deletions

View File

@ -174,7 +174,8 @@ pub(crate) fn highlight(
}
assert_eq!(res.len(), 1, "after DFS traversal, the stack should only contain a single element");
let res = res.pop().unwrap();
let mut res = res.pop().unwrap();
res.sort_by_key(|range| range.range.start());
// Check that ranges are sorted and disjoint
assert!(res
.iter()

View File

@ -156,3 +156,15 @@ fn main() {
fs::write(dst_file, &actual_html).unwrap();
assert_eq_text!(expected_html, actual_html);
}
#[test]
fn ranges_sorted() {
let (analysis, file_id) = single_file(
r#"
#[foo(bar = "bar")]
macro_rules! test {}
}"#
.trim(),
);
let _ = analysis.highlight(file_id).unwrap();
}