diff --git a/crates/assistant/src/assistant_panel.rs b/crates/assistant/src/assistant_panel.rs index ab060ef513..423427231e 100644 --- a/crates/assistant/src/assistant_panel.rs +++ b/crates/assistant/src/assistant_panel.rs @@ -64,6 +64,7 @@ use serde::{Deserialize, Serialize}; use settings::{update_settings_file, Settings}; use smol::stream::StreamExt; use std::{ + any::TypeId, borrow::Cow, cmp, ops::{ControlFlow, Range}, @@ -4186,6 +4187,21 @@ impl Item for ContextEditor { fn deactivated(&mut self, cx: &mut ViewContext) { self.editor.update(cx, Item::deactivated) } + + fn act_as_type<'a>( + &'a self, + type_id: TypeId, + self_handle: &'a View, + _: &'a AppContext, + ) -> Option { + if type_id == TypeId::of::() { + Some(self_handle.to_any()) + } else if type_id == TypeId::of::() { + Some(self.editor.to_any()) + } else { + None + } + } } impl SearchableItem for ContextEditor { diff --git a/crates/vim/src/normal/search.rs b/crates/vim/src/normal/search.rs index 507d21429b..6a9651e788 100644 --- a/crates/vim/src/normal/search.rs +++ b/crates/vim/src/normal/search.rs @@ -112,7 +112,9 @@ impl Vim { } fn search(&mut self, action: &Search, cx: &mut ViewContext) { - let Some(pane) = self.pane(cx) else { return }; + let Some(pane) = self.pane(cx) else { + return; + }; let direction = if action.backwards { Direction::Prev } else { diff --git a/crates/vim/src/vim.rs b/crates/vim/src/vim.rs index e265b0201e..a5c2249734 100644 --- a/crates/vim/src/vim.rs +++ b/crates/vim/src/vim.rs @@ -114,12 +114,13 @@ pub fn init(cx: &mut AppContext) { }); workspace.register_action(|workspace, _: &SearchSubmit, cx| { - let Some(vim) = workspace - .active_item_as::(cx) - .and_then(|editor| editor.read(cx).addon::().cloned()) - else { - return; - }; + let vim = workspace + .focused_pane(cx) + .read(cx) + .active_item() + .and_then(|item| item.act_as::(cx)) + .and_then(|editor| editor.read(cx).addon::().cloned()); + let Some(vim) = vim else { return }; vim.view .update(cx, |_, cx| cx.defer(|vim, cx| vim.search_submit(cx))) }); @@ -334,13 +335,15 @@ impl Vim { self.editor.upgrade() } - pub fn workspace(&self, cx: &ViewContext) -> Option> { - self.editor().and_then(|editor| editor.read(cx).workspace()) + pub fn workspace(&self, cx: &mut ViewContext) -> Option> { + cx.window_handle() + .downcast::() + .and_then(|handle| handle.root(cx).ok()) } - pub fn pane(&self, cx: &ViewContext) -> Option> { + pub fn pane(&self, cx: &mut ViewContext) -> Option> { self.workspace(cx) - .and_then(|workspace| workspace.read(cx).pane_for(&self.editor()?)) + .map(|workspace| workspace.read(cx).focused_pane(cx)) } pub fn enabled(cx: &mut AppContext) -> bool { diff --git a/crates/vim/src/visual.rs b/crates/vim/src/visual.rs index 71c8947f59..96f76c168a 100644 --- a/crates/vim/src/visual.rs +++ b/crates/vim/src/visual.rs @@ -576,14 +576,9 @@ impl Vim { pub fn select_match(&mut self, direction: Direction, cx: &mut ViewContext) { let count = self.take_count(cx).unwrap_or(1); - let Some(workspace) = self - .editor - .upgrade() - .and_then(|editor| editor.read(cx).workspace()) - else { + let Some(pane) = self.pane(cx) else { return; }; - let pane = workspace.read(cx).active_pane().clone(); let vim_is_normal = self.mode == Mode::Normal; let mut start_selection = 0usize; let mut end_selection = 0usize; diff --git a/crates/workspace/src/workspace.rs b/crates/workspace/src/workspace.rs index eda6fc2acb..1ab3e9afde 100644 --- a/crates/workspace/src/workspace.rs +++ b/crates/workspace/src/workspace.rs @@ -3219,6 +3219,21 @@ impl Workspace { &self.active_pane } + pub fn focused_pane(&self, cx: &WindowContext) -> View { + for dock in [&self.left_dock, &self.right_dock, &self.bottom_dock] { + if dock.focus_handle(cx).contains_focused(cx) { + if let Some(pane) = dock + .read(cx) + .active_panel() + .and_then(|panel| panel.pane(cx)) + { + return pane; + } + } + } + self.active_pane().clone() + } + pub fn adjacent_pane(&mut self, cx: &mut ViewContext) -> View { self.find_pane_in_direction(SplitDirection::Right, cx) .or_else(|| self.find_pane_in_direction(SplitDirection::Left, cx))