Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

editor: Support walking through overlapping diagnostics #11139

Merged
merged 1 commit into from
May 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
44 changes: 30 additions & 14 deletions crates/editor/src/editor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1287,6 +1287,7 @@ impl CodeActionsMenu {
struct ActiveDiagnosticGroup {
primary_range: Range<Anchor>,
primary_message: String,
group_id: usize,
blocks: HashMap<BlockId, Diagnostic>,
is_valid: bool,
}
Expand Down Expand Up @@ -7520,7 +7521,7 @@ impl Editor {
});
let mut search_start = if let Some(active_primary_range) = active_primary_range.as_ref() {
if active_primary_range.contains(&selection.head()) {
*active_primary_range.end()
*active_primary_range.start()
} else {
selection.head()
}
Expand All @@ -7529,24 +7530,38 @@ impl Editor {
};
let snapshot = self.snapshot(cx);
loop {
let mut diagnostics = if direction == Direction::Prev {
let diagnostics = if direction == Direction::Prev {
buffer.diagnostics_in_range::<_, usize>(0..search_start, true)
} else {
buffer.diagnostics_in_range::<_, usize>(search_start..buffer.len(), false)
}
.filter(|diagnostic| !snapshot.intersects_fold(diagnostic.range.start));
let group = diagnostics.find_map(|entry| {
if entry.diagnostic.is_primary
&& entry.diagnostic.severity <= DiagnosticSeverity::WARNING
&& !entry.range.is_empty()
&& Some(entry.range.end) != active_primary_range.as_ref().map(|r| *r.end())
&& !entry.range.contains(&search_start)
{
Some((entry.range, entry.diagnostic.group_id))
} else {
None
}
});
let group = diagnostics
// relies on diagnostics_in_range to return diagnostics with the same starting range to
// be sorted in a stable way
// skip until we are at current active diagnostic, if it exists
.skip_while(|entry| {
(match direction {
Direction::Prev => entry.range.start >= search_start,
Direction::Next => entry.range.start <= search_start,
}) && self
.active_diagnostics
.as_ref()
.is_some_and(|a| a.group_id != entry.diagnostic.group_id)
})
.find_map(|entry| {
if entry.diagnostic.is_primary
&& entry.diagnostic.severity <= DiagnosticSeverity::WARNING
&& !entry.range.is_empty()
// if we match with the active diagnostic, skip it
&& Some(entry.diagnostic.group_id)
!= self.active_diagnostics.as_ref().map(|d| d.group_id)
{
Some((entry.range, entry.diagnostic.group_id))
} else {
None
}
});

if let Some((primary_range, group_id)) = group {
if self.activate_diagnostics(group_id, cx) {
Expand Down Expand Up @@ -8547,6 +8562,7 @@ impl Editor {
Some(ActiveDiagnosticGroup {
primary_range,
primary_message,
group_id,
blocks,
is_valid: true,
})
Expand Down
16 changes: 15 additions & 1 deletion crates/language/src/buffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3049,7 +3049,21 @@ impl BufferSnapshot {
.iter_mut()
.enumerate()
.flat_map(|(ix, iter)| Some((ix, iter.peek()?)))
.min_by(|(_, a), (_, b)| a.range.start.cmp(&b.range.start))?;
.min_by(|(_, a), (_, b)| {
let cmp = a
.range
.start
.cmp(&b.range.start)
// when range is equal, sort by diagnostic severity
.then(a.diagnostic.severity.cmp(&b.diagnostic.severity))
// and stabilize order with group_id
.then(a.diagnostic.group_id.cmp(&b.diagnostic.group_id));
if reversed {
cmp.reverse()
} else {
cmp
}
})?;
iterators[next_ix].next()
})
}
Expand Down