Skip to content

Commit 86a7269

Browse files
committed
Overlay all diagnostics with highest severity on top
Here we separate the diagnostics by severity and then overlay the Vec of spans for each severity on top of the highlights. The error diagnostics end up overlaid on the warning diagnostics, which are overlaid on the hints, overlaid on info, overlaid on any other severity (default), then overlaid on the syntax highlights. This fixes two things: * Error diagnostics are now always visible when overlapped with other diagnostics. * Ghost text is eliminated. * Ghost text was caused by duplicate diagnostics at the EOF: overlaps within the merged `Vec<(usize, Range<usize>)>` violate assumptions in `helix_core::syntax::Merge`. * When we push a new range, we check it against the last range and merge the two if they overlap. This is safe because they both have the same severity and therefore highlight. The actual merge is skipped for any of these when they are empty, so this is very fast in practice. For some data, I threw together an FPS counter which renders as fast as possible and logs the renders per second. With no diagnostics, I see an FPS gain from this change from 868 FPS to 878 (+1.1%) on a release build on a Rust file. On an Erlang file with 12 error diagnostics and 6 warnings in view (233 errors and 66 warnings total), I see a decrease in average FPS from 795 to 790 (-0.6%) on a release build.
1 parent c927d61 commit 86a7269

File tree

1 file changed

+46
-19
lines changed

1 file changed

+46
-19
lines changed

helix-term/src/ui/editor.rs

+46-19
Original file line numberDiff line numberDiff line change
@@ -119,8 +119,15 @@ impl EditorView {
119119
Self::highlight_cursorline(doc, view, surface, theme);
120120
}
121121

122-
let highlights = Self::doc_syntax_highlights(doc, view.offset, inner.height, theme);
123-
let highlights = syntax::merge(highlights, Self::doc_diagnostics_highlights(doc, theme));
122+
let mut highlights = Self::doc_syntax_highlights(doc, view.offset, inner.height, theme);
123+
for diagnostic in Self::doc_diagnostics_highlights(doc, theme) {
124+
// Most of the `diagnostic` Vecs are empty most of the time. Skipping
125+
// a merge for any empty Vec saves a significant amount of work.
126+
if diagnostic.is_empty() {
127+
continue;
128+
}
129+
highlights = Box::new(syntax::merge(highlights, diagnostic));
130+
}
124131
let highlights: Box<dyn Iterator<Item = HighlightEvent>> = if is_focused {
125132
Box::new(syntax::merge(
126133
highlights,
@@ -264,7 +271,7 @@ impl EditorView {
264271
pub fn doc_diagnostics_highlights(
265272
doc: &Document,
266273
theme: &Theme,
267-
) -> Vec<(usize, std::ops::Range<usize>)> {
274+
) -> [Vec<(usize, std::ops::Range<usize>)>; 5] {
268275
use helix_core::diagnostic::Severity;
269276
let get_scope_of = |scope| {
270277
theme
@@ -285,22 +292,42 @@ impl EditorView {
285292
let error = get_scope_of("diagnostic.error");
286293
let r#default = get_scope_of("diagnostic"); // this is a bit redundant but should be fine
287294

288-
doc.diagnostics()
289-
.iter()
290-
.map(|diagnostic| {
291-
let diagnostic_scope = match diagnostic.severity {
292-
Some(Severity::Info) => info,
293-
Some(Severity::Hint) => hint,
294-
Some(Severity::Warning) => warning,
295-
Some(Severity::Error) => error,
296-
_ => r#default,
297-
};
298-
(
299-
diagnostic_scope,
300-
diagnostic.range.start..diagnostic.range.end,
301-
)
302-
})
303-
.collect()
295+
let mut default_vec: Vec<(usize, std::ops::Range<usize>)> = Vec::new();
296+
let mut info_vec = Vec::new();
297+
let mut hint_vec = Vec::new();
298+
let mut warning_vec = Vec::new();
299+
let mut error_vec = Vec::new();
300+
301+
let diagnostics = doc.diagnostics();
302+
303+
// Diagnostics must be sorted by range. Otherwise, the merge strategy
304+
// below would not be accurate.
305+
debug_assert!(diagnostics
306+
.windows(2)
307+
.all(|window| window[0].range.start <= window[1].range.start
308+
&& window[0].range.end <= window[1].range.end));
309+
310+
for diagnostic in diagnostics {
311+
// Separate diagnostics into different Vecs by severity.
312+
let (vec, scope) = match diagnostic.severity {
313+
Some(Severity::Info) => (&mut info_vec, info),
314+
Some(Severity::Hint) => (&mut hint_vec, hint),
315+
Some(Severity::Warning) => (&mut warning_vec, warning),
316+
Some(Severity::Error) => (&mut error_vec, error),
317+
_ => (&mut default_vec, r#default),
318+
};
319+
320+
// If any diagnostic overlaps ranges with the prior diagnostic,
321+
// merge the two together. Otherwise push a new span.
322+
match vec.last_mut() {
323+
Some((_, range)) if diagnostic.range.start <= range.end => {
324+
range.end = diagnostic.range.end.max(range.end)
325+
}
326+
_ => vec.push((scope, diagnostic.range.start..diagnostic.range.end)),
327+
}
328+
}
329+
330+
[default_vec, info_vec, hint_vec, warning_vec, error_vec]
304331
}
305332

306333
/// Get highlight spans for selections in a document view.

0 commit comments

Comments
 (0)