on master help sign in
Display highest severity diagnostic in gutter (#2835)

* Display highest severity diagnostic in gutter * Improve gutter diagnostic performance Very slight improvement (doesn't really make a difference), iterates over the diagnostics of the line once instead of twice. * Add comment justifying unwrap

commit43027d91046f79d6dc495b351cdcbfd3819cd9e1 authorA-Walrus <58790821+A-Walrus@users.noreply.github.com> dateTue 21 Jun 2022, 05:39 (4 years ago) parent009f8c4
1 file changed+12−1
helix-view/src/gutter.rs+12−1
helix-view/src/gutter.rs+12−1
@@ -26,7 +26,18 @@ pub fn diagnostic<'doc>(
2626 Box::new(move |line: usize, _selected: bool, out: &mut String| {
2727 use helix_core::diagnostic::Severity;
2828 if let Ok(index) = diagnostics.binary_search_by_key(&line, |d| d.line) {
29− let diagnostic = &diagnostics[index];
29+ let after = diagnostics[index..].iter().take_while(|d| d.line == line);
30+
31+ let before = diagnostics[..index]
32+ .iter()
33+ .rev()
34+ .take_while(|d| d.line == line);
35+
36+ let diagnostics_on_line = after.chain(before);
37+
38+ // This unwrap is safe because the iterator cannot be empty as it contains at least the item found by the binary search.
39+ let diagnostic = diagnostics_on_line.max_by_key(|d| d.severity).unwrap();
40+
3041 write!(out, "●").unwrap();
3142 return Some(match diagnostic.severity {
3243 Some(Severity::Error) => error,