From 0ada4ce900a13af97c732cba7d3eae240749e4b8 Mon Sep 17 00:00:00 2001 From: Smit Barmase Date: Wed, 16 Jul 2025 01:47:40 +0530 Subject: [PATCH] editor: Add ToggleFocus action (#34495) This PR adds action `editor: toggle focus` which focuses to last active editor pane item in workspace. Release Notes: - Added `editor: toggle focus` action, which focuses to last active editor pane item. --------- Co-authored-by: Danilo Leal --- crates/editor/src/actions.rs | 2 ++ crates/editor/src/editor.rs | 13 +++++++++++++ crates/workspace/src/workspace.rs | 21 +++++++++++++++++++++ 3 files changed, 36 insertions(+) diff --git a/crates/editor/src/actions.rs b/crates/editor/src/actions.rs index c4866179c1d98bc1a36001b469cabe875ea42806..87463d246d2aba96485247467b15025c58f9d5d5 100644 --- a/crates/editor/src/actions.rs +++ b/crates/editor/src/actions.rs @@ -425,6 +425,8 @@ actions!( FoldRecursive, /// Folds the selected ranges. FoldSelectedRanges, + /// Toggles focus back to the last active buffer. + ToggleFocus, /// Toggles folding at the current position. ToggleFold, /// Toggles recursive folding at the current position. diff --git a/crates/editor/src/editor.rs b/crates/editor/src/editor.rs index acd9c23c97682f648fe3389e8c0466be4d7b9667..e5ff75561594746a17a228ee1c466f003097e37a 100644 --- a/crates/editor/src/editor.rs +++ b/crates/editor/src/editor.rs @@ -356,6 +356,7 @@ pub fn init(cx: &mut App) { workspace.register_action(Editor::new_file_vertical); workspace.register_action(Editor::new_file_horizontal); workspace.register_action(Editor::cancel_language_server_work); + workspace.register_action(Editor::toggle_focus); }, ) .detach(); @@ -16954,6 +16955,18 @@ impl Editor { cx.notify(); } + pub fn toggle_focus( + workspace: &mut Workspace, + _: &actions::ToggleFocus, + window: &mut Window, + cx: &mut Context, + ) { + let Some(item) = workspace.recent_active_item_by_type::(cx) else { + return; + }; + workspace.activate_item(&item, true, true, window, cx); + } + pub fn toggle_fold( &mut self, _: &actions::ToggleFold, diff --git a/crates/workspace/src/workspace.rs b/crates/workspace/src/workspace.rs index dc2c6516dd6892feb7749daf5bf654db94bfb11e..be5d693d356e3b328d1f3d0575a76df5c429f7dc 100644 --- a/crates/workspace/src/workspace.rs +++ b/crates/workspace/src/workspace.rs @@ -1711,6 +1711,27 @@ impl Workspace { history } + pub fn recent_active_item_by_type(&self, cx: &App) -> Option> { + let mut recent_item: Option> = None; + let mut recent_timestamp = 0; + for pane_handle in &self.panes { + let pane = pane_handle.read(cx); + let item_map: HashMap> = + pane.items().map(|item| (item.item_id(), item)).collect(); + for entry in pane.activation_history() { + if entry.timestamp > recent_timestamp { + if let Some(&item) = item_map.get(&entry.entity_id) { + if let Some(typed_item) = item.act_as::(cx) { + recent_timestamp = entry.timestamp; + recent_item = Some(typed_item); + } + } + } + } + } + recent_item + } + pub fn recent_navigation_history_iter( &self, cx: &App,