Keyboard navigation and setting persistence for project search (#2996)

Enable keyboard shortcuts for Project Search modes, and ensure project
search settings are persisted search to search.

Release Notes:

- Added alt-cmd-s to Toggle Semantic Search Mode
- Added alt-cmd-g to Toggle Regex Search Mode
- Added alt-cmd-x to Toggle Text Search Mode
- Defaulted new project searches to using last used search mode and
settings.
This commit is contained in:
Kyle Caverly 2023-09-20 12:44:30 -04:00 committed by GitHub
commit 5fe8aa064f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 143 additions and 22 deletions

View File

@ -248,7 +248,10 @@
"context": "ProjectSearchBar", "context": "ProjectSearchBar",
"bindings": { "bindings": {
"escape": "project_search::ToggleFocus", "escape": "project_search::ToggleFocus",
"alt-tab": "search::CycleMode" "alt-tab": "search::CycleMode",
"alt-cmd-g": "search::ActivateRegexMode",
"alt-cmd-s": "search::ActivateSemanticMode",
"alt-cmd-x": "search::ActivateTextMode"
} }
}, },
{ {
@ -262,7 +265,10 @@
"context": "ProjectSearchView", "context": "ProjectSearchView",
"bindings": { "bindings": {
"escape": "project_search::ToggleFocus", "escape": "project_search::ToggleFocus",
"alt-tab": "search::CycleMode" "alt-tab": "search::CycleMode",
"alt-cmd-g": "search::ActivateRegexMode",
"alt-cmd-s": "search::ActivateSemanticMode",
"alt-cmd-x": "search::ActivateTextMode"
} }
}, },
{ {
@ -275,7 +281,10 @@
"alt-cmd-c": "search::ToggleCaseSensitive", "alt-cmd-c": "search::ToggleCaseSensitive",
"alt-cmd-w": "search::ToggleWholeWord", "alt-cmd-w": "search::ToggleWholeWord",
"alt-tab": "search::CycleMode", "alt-tab": "search::CycleMode",
"alt-cmd-f": "project_search::ToggleFilters" "alt-cmd-f": "project_search::ToggleFilters",
"alt-cmd-g": "search::ActivateRegexMode",
"alt-cmd-s": "search::ActivateSemanticMode",
"alt-cmd-x": "search::ActivateTextMode"
} }
}, },
// Bindings from VS Code // Bindings from VS Code

View File

@ -370,8 +370,7 @@
}, },
// Difference settings for semantic_index // Difference settings for semantic_index
"semantic_index": { "semantic_index": {
"enabled": false, "enabled": false
"reindexing_delay_seconds": 600
}, },
// Different settings for specific languages. // Different settings for specific languages.
"languages": { "languages": {

View File

@ -2,8 +2,9 @@ use crate::{
history::SearchHistory, history::SearchHistory,
mode::{SearchMode, Side}, mode::{SearchMode, Side},
search_bar::{render_nav_button, render_option_button_icon, render_search_mode_button}, search_bar::{render_nav_button, render_option_button_icon, render_search_mode_button},
ActivateRegexMode, CycleMode, NextHistoryQuery, PreviousHistoryQuery, SearchOptions, ActivateRegexMode, ActivateSemanticMode, ActivateTextMode, CycleMode, NextHistoryQuery,
SelectNextMatch, SelectPrevMatch, ToggleCaseSensitive, ToggleWholeWord, PreviousHistoryQuery, SearchOptions, SelectNextMatch, SelectPrevMatch, ToggleCaseSensitive,
ToggleWholeWord,
}; };
use anyhow::{Context, Result}; use anyhow::{Context, Result};
use collections::HashMap; use collections::HashMap;
@ -51,8 +52,12 @@ actions!(
#[derive(Default)] #[derive(Default)]
struct ActiveSearches(HashMap<WeakModelHandle<Project>, WeakViewHandle<ProjectSearchView>>); struct ActiveSearches(HashMap<WeakModelHandle<Project>, WeakViewHandle<ProjectSearchView>>);
#[derive(Default)]
struct ActiveSettings(HashMap<WeakModelHandle<Project>, ProjectSearchSettings>);
pub fn init(cx: &mut AppContext) { pub fn init(cx: &mut AppContext) {
cx.set_global(ActiveSearches::default()); cx.set_global(ActiveSearches::default());
cx.set_global(ActiveSettings::default());
cx.add_action(ProjectSearchView::deploy); cx.add_action(ProjectSearchView::deploy);
cx.add_action(ProjectSearchView::move_focus_to_results); cx.add_action(ProjectSearchView::move_focus_to_results);
cx.add_action(ProjectSearchBar::search); cx.add_action(ProjectSearchBar::search);
@ -63,6 +68,13 @@ pub fn init(cx: &mut AppContext) {
cx.add_action(ProjectSearchBar::next_history_query); cx.add_action(ProjectSearchBar::next_history_query);
cx.add_action(ProjectSearchBar::previous_history_query); cx.add_action(ProjectSearchBar::previous_history_query);
cx.add_action(ProjectSearchBar::activate_regex_mode); cx.add_action(ProjectSearchBar::activate_regex_mode);
cx.add_action(ProjectSearchBar::activate_text_mode);
// This action should only be registered if the semantic index is enabled
// We are registering it all the time, as I dont want to introduce a dependency
// for Semantic Index Settings globally whenever search is tested.
cx.add_action(ProjectSearchBar::activate_semantic_mode);
cx.capture_action(ProjectSearchBar::tab); cx.capture_action(ProjectSearchBar::tab);
cx.capture_action(ProjectSearchBar::tab_previous); cx.capture_action(ProjectSearchBar::tab_previous);
add_toggle_option_action::<ToggleCaseSensitive>(SearchOptions::CASE_SENSITIVE, cx); add_toggle_option_action::<ToggleCaseSensitive>(SearchOptions::CASE_SENSITIVE, cx);
@ -135,6 +147,13 @@ struct SemanticState {
_subscription: Subscription, _subscription: Subscription,
} }
#[derive(Debug, Clone)]
struct ProjectSearchSettings {
search_options: SearchOptions,
filters_enabled: bool,
current_mode: SearchMode,
}
pub struct ProjectSearchBar { pub struct ProjectSearchBar {
active_project_search: Option<ViewHandle<ProjectSearchView>>, active_project_search: Option<ViewHandle<ProjectSearchView>>,
subscription: Option<Subscription>, subscription: Option<Subscription>,
@ -460,6 +479,13 @@ impl View for ProjectSearchView {
.insert(self.model.read(cx).project.downgrade(), handle) .insert(self.model.read(cx).project.downgrade(), handle)
}); });
cx.update_global(|state: &mut ActiveSettings, cx| {
state.0.insert(
self.model.read(cx).project.downgrade(),
self.current_settings(),
);
});
if cx.is_self_focused() { if cx.is_self_focused() {
if self.query_editor_was_focused { if self.query_editor_was_focused {
cx.focus(&self.query_editor); cx.focus(&self.query_editor);
@ -483,6 +509,7 @@ impl Item for ProjectSearchView {
fn should_close_item_on_event(event: &Self::Event) -> bool { fn should_close_item_on_event(event: &Self::Event) -> bool {
event == &Self::Event::Dismiss event == &Self::Event::Dismiss
} }
fn act_as_type<'a>( fn act_as_type<'a>(
&'a self, &'a self,
type_id: TypeId, type_id: TypeId,
@ -593,7 +620,7 @@ impl Item for ProjectSearchView {
Self: Sized, Self: Sized,
{ {
let model = self.model.update(cx, |model, cx| model.clone(cx)); let model = self.model.update(cx, |model, cx| model.clone(cx));
Some(Self::new(model, cx)) Some(Self::new(model, cx, None))
} }
fn added_to_workspace(&mut self, workspace: &mut Workspace, cx: &mut ViewContext<Self>) { fn added_to_workspace(&mut self, workspace: &mut Workspace, cx: &mut ViewContext<Self>) {
@ -651,8 +678,31 @@ impl Item for ProjectSearchView {
} }
impl ProjectSearchView { impl ProjectSearchView {
fn toggle_search_option(&mut self, option: SearchOptions) { fn toggle_filters(&mut self, cx: &mut ViewContext<Self>) {
self.filters_enabled = !self.filters_enabled;
cx.update_global(|state: &mut ActiveSettings, cx| {
state.0.insert(
self.model.read(cx).project.downgrade(),
self.current_settings(),
);
});
}
fn current_settings(&self) -> ProjectSearchSettings {
ProjectSearchSettings {
search_options: self.search_options,
filters_enabled: self.filters_enabled,
current_mode: self.current_mode,
}
}
fn toggle_search_option(&mut self, option: SearchOptions, cx: &mut ViewContext<Self>) {
self.search_options.toggle(option); self.search_options.toggle(option);
cx.update_global(|state: &mut ActiveSettings, cx| {
state.0.insert(
self.model.read(cx).project.downgrade(),
self.current_settings(),
);
});
} }
fn index_project(&mut self, cx: &mut ViewContext<Self>) { fn index_project(&mut self, cx: &mut ViewContext<Self>) {
@ -785,14 +835,35 @@ impl ProjectSearchView {
} }
} }
cx.update_global(|state: &mut ActiveSettings, cx| {
state.0.insert(
self.model.read(cx).project.downgrade(),
self.current_settings(),
);
});
cx.notify(); cx.notify();
} }
fn new(model: ModelHandle<ProjectSearch>, cx: &mut ViewContext<Self>) -> Self { fn new(
model: ModelHandle<ProjectSearch>,
cx: &mut ViewContext<Self>,
settings: Option<ProjectSearchSettings>,
) -> Self {
let project; let project;
let excerpts; let excerpts;
let mut query_text = String::new(); let mut query_text = String::new();
let mut options = SearchOptions::NONE;
// Read in settings if available
let (mut options, current_mode, filters_enabled) = if let Some(settings) = settings {
(
settings.search_options,
settings.current_mode,
settings.filters_enabled,
)
} else {
(SearchOptions::NONE, Default::default(), false)
};
{ {
let model = model.read(cx); let model = model.read(cx);
@ -871,7 +942,6 @@ impl ProjectSearchView {
cx.emit(ViewEvent::EditorEvent(event.clone())) cx.emit(ViewEvent::EditorEvent(event.clone()))
}) })
.detach(); .detach();
let filters_enabled = false;
// Check if Worktrees have all been previously indexed // Check if Worktrees have all been previously indexed
let mut this = ProjectSearchView { let mut this = ProjectSearchView {
@ -888,7 +958,7 @@ impl ProjectSearchView {
included_files_editor, included_files_editor,
excluded_files_editor, excluded_files_editor,
filters_enabled, filters_enabled,
current_mode: Default::default(), current_mode,
}; };
this.model_changed(cx); this.model_changed(cx);
this this
@ -919,7 +989,7 @@ impl ProjectSearchView {
}; };
let model = cx.add_model(|cx| ProjectSearch::new(workspace.project().clone(), cx)); let model = cx.add_model(|cx| ProjectSearch::new(workspace.project().clone(), cx));
let search = cx.add_view(|cx| ProjectSearchView::new(model, cx)); let search = cx.add_view(|cx| ProjectSearchView::new(model, cx, None));
workspace.add_item(Box::new(search.clone()), cx); workspace.add_item(Box::new(search.clone()), cx);
search.update(cx, |search, cx| { search.update(cx, |search, cx| {
search search
@ -969,8 +1039,20 @@ impl ProjectSearchView {
workspace.activate_item(&existing, cx); workspace.activate_item(&existing, cx);
existing existing
} else { } else {
let settings = cx
.global::<ActiveSettings>()
.0
.get(&workspace.project().downgrade());
let settings = if let Some(settings) = settings {
Some(settings.clone())
} else {
None
};
let model = cx.add_model(|cx| ProjectSearch::new(workspace.project().clone(), cx)); let model = cx.add_model(|cx| ProjectSearch::new(workspace.project().clone(), cx));
let view = cx.add_view(|cx| ProjectSearchView::new(model, cx)); let view = cx.add_view(|cx| ProjectSearchView::new(model, cx, settings));
workspace.add_item(Box::new(view.clone()), cx); workspace.add_item(Box::new(view.clone()), cx);
view view
}; };
@ -1246,7 +1328,7 @@ impl ProjectSearchBar {
model model
}); });
workspace.add_item( workspace.add_item(
Box::new(cx.add_view(|cx| ProjectSearchView::new(model, cx))), Box::new(cx.add_view(|cx| ProjectSearchView::new(model, cx, None))),
cx, cx,
); );
} }
@ -1325,9 +1407,10 @@ impl ProjectSearchBar {
fn toggle_search_option(&mut self, option: SearchOptions, cx: &mut ViewContext<Self>) -> bool { fn toggle_search_option(&mut self, option: SearchOptions, cx: &mut ViewContext<Self>) -> bool {
if let Some(search_view) = self.active_project_search.as_ref() { if let Some(search_view) = self.active_project_search.as_ref() {
search_view.update(cx, |search_view, cx| { search_view.update(cx, |search_view, cx| {
search_view.toggle_search_option(option); search_view.toggle_search_option(option, cx);
search_view.search(cx); search_view.search(cx);
}); });
cx.notify(); cx.notify();
true true
} else { } else {
@ -1335,6 +1418,19 @@ impl ProjectSearchBar {
} }
} }
fn activate_text_mode(pane: &mut Pane, _: &ActivateTextMode, cx: &mut ViewContext<Pane>) {
if let Some(search_view) = pane
.active_item()
.and_then(|item| item.downcast::<ProjectSearchView>())
{
search_view.update(cx, |view, cx| {
view.activate_search_mode(SearchMode::Text, cx)
});
} else {
cx.propagate_action();
}
}
fn activate_regex_mode(pane: &mut Pane, _: &ActivateRegexMode, cx: &mut ViewContext<Pane>) { fn activate_regex_mode(pane: &mut Pane, _: &ActivateRegexMode, cx: &mut ViewContext<Pane>) {
if let Some(search_view) = pane if let Some(search_view) = pane
.active_item() .active_item()
@ -1348,10 +1444,29 @@ impl ProjectSearchBar {
} }
} }
fn activate_semantic_mode(
pane: &mut Pane,
_: &ActivateSemanticMode,
cx: &mut ViewContext<Pane>,
) {
if SemanticIndex::enabled(cx) {
if let Some(search_view) = pane
.active_item()
.and_then(|item| item.downcast::<ProjectSearchView>())
{
search_view.update(cx, |view, cx| {
view.activate_search_mode(SearchMode::Semantic, cx)
});
} else {
cx.propagate_action();
}
}
}
fn toggle_filters(&mut self, cx: &mut ViewContext<Self>) -> bool { fn toggle_filters(&mut self, cx: &mut ViewContext<Self>) -> bool {
if let Some(search_view) = self.active_project_search.as_ref() { if let Some(search_view) = self.active_project_search.as_ref() {
search_view.update(cx, |search_view, cx| { search_view.update(cx, |search_view, cx| {
search_view.filters_enabled = !search_view.filters_enabled; search_view.toggle_filters(cx);
search_view search_view
.included_files_editor .included_files_editor
.update(cx, |_, cx| cx.notify()); .update(cx, |_, cx| cx.notify());
@ -1715,7 +1830,7 @@ pub mod tests {
let project = Project::test(fs.clone(), ["/dir".as_ref()], cx).await; let project = Project::test(fs.clone(), ["/dir".as_ref()], cx).await;
let search = cx.add_model(|cx| ProjectSearch::new(project, cx)); let search = cx.add_model(|cx| ProjectSearch::new(project, cx));
let search_view = cx let search_view = cx
.add_window(|cx| ProjectSearchView::new(search.clone(), cx)) .add_window(|cx| ProjectSearchView::new(search.clone(), cx, None))
.root(cx); .root(cx);
search_view.update(cx, |search_view, cx| { search_view.update(cx, |search_view, cx| {

View File

@ -39,7 +39,7 @@ actions!(
ActivateSemanticMode, ActivateSemanticMode,
ActivateRegexMode, ActivateRegexMode,
ReplaceAll, ReplaceAll,
ReplaceNext ReplaceNext,
] ]
); );

View File

@ -6,13 +6,11 @@ use settings::Setting;
#[derive(Deserialize, Debug)] #[derive(Deserialize, Debug)]
pub struct SemanticIndexSettings { pub struct SemanticIndexSettings {
pub enabled: bool, pub enabled: bool,
pub reindexing_delay_seconds: usize,
} }
#[derive(Clone, Default, Serialize, Deserialize, JsonSchema, Debug)] #[derive(Clone, Default, Serialize, Deserialize, JsonSchema, Debug)]
pub struct SemanticIndexSettingsContent { pub struct SemanticIndexSettingsContent {
pub enabled: Option<bool>, pub enabled: Option<bool>,
pub reindexing_delay_seconds: Option<usize>,
} }
impl Setting for SemanticIndexSettings { impl Setting for SemanticIndexSettings {