From e1431ede363e9bc0de361fb8f856e5fd370cd2a5 Mon Sep 17 00:00:00 2001 From: Max Brunsfeld Date: Wed, 3 Aug 2022 09:10:00 -0700 Subject: [PATCH] Avoid crash when clicking the popover of a diagnostic that's been removed --- crates/editor/src/editor.rs | 41 ++++++++++++++++++++----------------- 1 file changed, 22 insertions(+), 19 deletions(-) diff --git a/crates/editor/src/editor.rs b/crates/editor/src/editor.rs index 2cb5b83808..c806acaa5d 100644 --- a/crates/editor/src/editor.rs +++ b/crates/editor/src/editor.rs @@ -4661,12 +4661,13 @@ impl Editor { if direction == Direction::Next { if let Some(popover) = self.hover_state.diagnostic_popover.as_ref() { let (group_id, jump_to) = popover.activation_info(); - self.activate_diagnostics(group_id, cx); - self.change_selections(Some(Autoscroll::Center), cx, |s| { - let mut new_selection = s.newest_anchor().clone(); - new_selection.collapse_to(jump_to, SelectionGoal::None); - s.select_anchors(vec![new_selection.clone()]); - }); + if self.activate_diagnostics(group_id, cx) { + self.change_selections(Some(Autoscroll::Center), cx, |s| { + let mut new_selection = s.newest_anchor().clone(); + new_selection.collapse_to(jump_to, SelectionGoal::None); + s.select_anchors(vec![new_selection.clone()]); + }); + } return; } } @@ -4706,16 +4707,17 @@ impl Editor { }); if let Some((primary_range, group_id)) = group { - self.activate_diagnostics(group_id, cx); - self.change_selections(Some(Autoscroll::Center), cx, |s| { - s.select(vec![Selection { - id: selection.id, - start: primary_range.start, - end: primary_range.start, - reversed: false, - goal: SelectionGoal::None, - }]); - }); + if self.activate_diagnostics(group_id, cx) { + self.change_selections(Some(Autoscroll::Center), cx, |s| { + s.select(vec![Selection { + id: selection.id, + start: primary_range.start, + end: primary_range.start, + reversed: false, + goal: SelectionGoal::None, + }]); + }); + } break; } else { // Cycle around to the start of the buffer, potentially moving back to the start of @@ -5162,7 +5164,7 @@ impl Editor { } } - fn activate_diagnostics(&mut self, group_id: usize, cx: &mut ViewContext) { + fn activate_diagnostics(&mut self, group_id: usize, cx: &mut ViewContext) -> bool { self.dismiss_diagnostics(cx); self.active_diagnostics = self.display_map.update(cx, |display_map, cx| { let buffer = self.buffer.read(cx).snapshot(cx); @@ -5183,8 +5185,8 @@ impl Editor { entry }) .collect::>(); - let primary_range = primary_range.unwrap(); - let primary_message = primary_message.unwrap(); + let primary_range = primary_range?; + let primary_message = primary_message?; let primary_range = buffer.anchor_after(primary_range.start)..buffer.anchor_before(primary_range.end); @@ -5214,6 +5216,7 @@ impl Editor { is_valid: true, }) }); + self.active_diagnostics.is_some() } fn dismiss_diagnostics(&mut self, cx: &mut ViewContext) {