Select query when focusing query editor

Nathan Sobo created

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.

Change summary

crates/find/src/project_find.rs | 19 +++++++++++++++----
1 file changed, 15 insertions(+), 4 deletions(-)

Detailed changes

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<Self>) {
         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>) {

+        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<Self>) {
         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);

             }
         }