From 28b71cbc03b1e86981789f0ece8ad4d8a59642c4 Mon Sep 17 00:00:00 2001 From: Nathan Sobo Date: Sat, 26 Feb 2022 14:12:31 -0700 Subject: [PATCH] Select query when focusing query editor Also: Clear the selection when we focus the results editor because we continue to render the selection even when the editor isn't focused and it looks awkward. Another approach we could take is to not render selections for non-focused editors, either always or with an option. But considering that we select all anyways next time we return focus to the query editor, I think this is ok for now. --- crates/find/src/project_find.rs | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/crates/find/src/project_find.rs b/crates/find/src/project_find.rs index af2d1856cc..2d0466a8b2 100644 --- a/crates/find/src/project_find.rs +++ b/crates/find/src/project_find.rs @@ -1,5 +1,5 @@ use crate::SearchOption; -use editor::{Anchor, Autoscroll, Editor, MultiBuffer}; +use editor::{Anchor, Autoscroll, Editor, MultiBuffer, SelectAll}; use gpui::{ action, elements::*, keymap::Binding, platform::CursorStyle, AppContext, ElementBox, Entity, ModelContext, ModelHandle, MutableAppContext, RenderContext, Task, View, ViewContext, @@ -230,7 +230,7 @@ impl View for ProjectFindView { if self.model.read(cx).highlighted_ranges.is_empty() { cx.focus(&self.query_editor); } else { - cx.focus(&self.results_editor); + self.focus_results_editor(cx); } } } @@ -471,13 +471,24 @@ impl ProjectFindView { fn toggle_focus(&mut self, _: &ToggleFocus, cx: &mut ViewContext) { if self.query_editor.is_focused(cx) { if !self.model.read(cx).highlighted_ranges.is_empty() { - cx.focus(&self.results_editor); + self.focus_results_editor(cx); } } else { + self.query_editor.update(cx, |query_editor, cx| { + query_editor.select_all(&SelectAll, cx); + }); cx.focus(&self.query_editor); } } + fn focus_results_editor(&self, cx: &mut ViewContext) { + self.query_editor.update(cx, |query_editor, cx| { + let head = query_editor.newest_anchor_selection().head(); + query_editor.select_ranges([head.clone()..head], None, cx); + }); + cx.focus(&self.results_editor); + } + fn model_changed(&mut self, reset_selections: bool, cx: &mut ViewContext) { let highlighted_ranges = self.model.read(cx).highlighted_ranges.clone(); if !highlighted_ranges.is_empty() { @@ -489,7 +500,7 @@ impl ProjectFindView { } }); if self.query_editor.is_focused(cx) { - cx.focus(&self.results_editor); + self.focus_results_editor(cx); } }