Fix the panic when trying to interact with editor-like not-editors (#49608) (cherry-pick to stable) (#49611)

zed-zippy[bot] and Kirill Bulatov created

Cherry-pick of #49608 to stable

----
For example, LSP log view is not a real editor but can pretend to be
one.

Release Notes:

- Fixed dev highlights view panicking when interacting with editor-like
not-editors

Co-authored-by: Kirill Bulatov <kirill@zed.dev>

Change summary

crates/language_tools/src/highlights_tree_view.rs | 26 ++++++++++------
1 file changed, 16 insertions(+), 10 deletions(-)

Detailed changes

crates/language_tools/src/highlights_tree_view.rs 🔗

@@ -189,8 +189,9 @@ impl HighlightsTreeView {
     ) {
         let Some(editor) = active_item
             .filter(|item| item.item_id() != cx.entity_id())
-            .and_then(|item| item.act_as::<Editor>(cx))
+            .and_then(|item| item.downcast::<Editor>())
         else {
+            self.clear(cx);
             return;
         };
 
@@ -214,20 +215,27 @@ impl HighlightsTreeView {
             .as_ref()
             .is_some_and(|state| state.editor.entity_id() == *item_id)
         {
-            self.editor = None;
-            self.cached_entries.clear();
-            self.display_items.clear();
-            cx.notify();
+            self.clear(cx);
         }
     }
 
+    fn clear(&mut self, cx: &mut Context<Self>) {
+        self.cached_entries.clear();
+        self.display_items.clear();
+        self.selected_item_ix = None;
+        self.hovered_item_ix = None;
+        if let Some(state) = self.editor.take() {
+            Self::clear_editor_highlights(&state.editor, cx);
+        }
+        cx.notify();
+    }
+
     fn set_editor(&mut self, editor: Entity<Editor>, window: &mut Window, cx: &mut Context<Self>) {
         if let Some(state) = &self.editor {
             if state.editor == editor {
                 return;
             }
-            let key = HighlightKey::HighlightsTreeView(editor.entity_id().as_u64() as usize);
-            editor.update(cx, |editor, cx| editor.clear_background_highlights(key, cx));
+            Self::clear_editor_highlights(&state.editor, cx);
         }
 
         let subscription =
@@ -248,9 +256,7 @@ impl HighlightsTreeView {
 
     fn refresh_highlights(&mut self, _window: &mut Window, cx: &mut Context<Self>) {
         let Some(editor_state) = self.editor.as_ref() else {
-            self.cached_entries.clear();
-            self.display_items.clear();
-            cx.notify();
+            self.clear(cx);
             return;
         };