2022-02-28 09:34:11 +00:00
|
|
|
use crate::{
|
2022-06-16 12:28:37 +00:00
|
|
|
SearchOption, SelectNextMatch, SelectPrevMatch, ToggleCaseSensitive, ToggleRegex,
|
|
|
|
ToggleWholeWord,
|
2022-02-28 09:34:11 +00:00
|
|
|
};
|
2022-02-28 01:07:46 +00:00
|
|
|
use collections::HashMap;
|
2022-08-30 22:37:54 +00:00
|
|
|
use editor::{
|
2022-09-01 20:45:46 +00:00
|
|
|
items::active_match_index, Anchor, Autoscroll, Editor, MultiBuffer, SelectAll,
|
|
|
|
MAX_TAB_TITLE_LEN,
|
2022-08-30 22:37:54 +00:00
|
|
|
};
|
2022-02-27 14:47:46 +00:00
|
|
|
use gpui::{
|
2022-08-08 02:23:22 +00:00
|
|
|
actions, elements::*, platform::CursorStyle, Action, AnyViewHandle, AppContext, ElementBox,
|
|
|
|
Entity, ModelContext, ModelHandle, MouseButton, MutableAppContext, RenderContext, Subscription,
|
|
|
|
Task, View, ViewContext, ViewHandle, WeakModelHandle, WeakViewHandle,
|
2022-02-27 14:47:46 +00:00
|
|
|
};
|
2022-05-26 14:36:30 +00:00
|
|
|
use menu::Confirm;
|
2022-02-27 14:47:46 +00:00
|
|
|
use project::{search::SearchQuery, Project};
|
2022-04-06 00:10:17 +00:00
|
|
|
use settings::Settings;
|
2022-05-22 23:48:33 +00:00
|
|
|
use smallvec::SmallVec;
|
2022-02-27 14:47:46 +00:00
|
|
|
use std::{
|
|
|
|
any::{Any, TypeId},
|
|
|
|
ops::Range,
|
|
|
|
path::PathBuf,
|
|
|
|
};
|
|
|
|
use util::ResultExt as _;
|
2022-04-21 22:24:05 +00:00
|
|
|
use workspace::{
|
2022-09-01 20:45:46 +00:00
|
|
|
searchable::{Direction, SearchableItem, SearchableItemHandle},
|
2022-09-06 21:39:58 +00:00
|
|
|
Item, ItemEvent, ItemHandle, ItemNavHistory, Pane, ToolbarItemLocation, ToolbarItemView,
|
|
|
|
Workspace,
|
2022-04-21 22:24:05 +00:00
|
|
|
};
|
2022-02-27 14:47:46 +00:00
|
|
|
|
2022-08-04 15:42:42 +00:00
|
|
|
actions!(project_search, [SearchInNew, ToggleFocus]);
|
2022-02-27 14:47:46 +00:00
|
|
|
|
2022-02-28 01:07:46 +00:00
|
|
|
#[derive(Default)]
|
2022-03-16 20:34:04 +00:00
|
|
|
struct ActiveSearches(HashMap<WeakModelHandle<Project>, WeakViewHandle<ProjectSearchView>>);
|
2022-02-28 01:07:46 +00:00
|
|
|
|
2022-02-27 14:47:46 +00:00
|
|
|
pub fn init(cx: &mut MutableAppContext) {
|
2022-03-17 13:33:01 +00:00
|
|
|
cx.set_global(ActiveSearches::default());
|
2022-02-27 15:15:38 +00:00
|
|
|
cx.add_action(ProjectSearchView::deploy);
|
2022-03-29 15:04:39 +00:00
|
|
|
cx.add_action(ProjectSearchBar::search);
|
|
|
|
cx.add_action(ProjectSearchBar::search_in_new);
|
2022-04-08 22:32:56 +00:00
|
|
|
cx.add_action(ProjectSearchBar::select_next_match);
|
|
|
|
cx.add_action(ProjectSearchBar::select_prev_match);
|
2022-03-29 15:04:39 +00:00
|
|
|
cx.add_action(ProjectSearchBar::toggle_focus);
|
|
|
|
cx.capture_action(ProjectSearchBar::tab);
|
2022-06-16 12:28:37 +00:00
|
|
|
add_toggle_option_action::<ToggleCaseSensitive>(SearchOption::CaseSensitive, cx);
|
|
|
|
add_toggle_option_action::<ToggleWholeWord>(SearchOption::WholeWord, cx);
|
|
|
|
add_toggle_option_action::<ToggleRegex>(SearchOption::Regex, cx);
|
|
|
|
}
|
|
|
|
|
|
|
|
fn add_toggle_option_action<A: Action>(option: SearchOption, cx: &mut MutableAppContext) {
|
|
|
|
cx.add_action(move |pane: &mut Pane, _: &A, cx: &mut ViewContext<Pane>| {
|
|
|
|
if let Some(search_bar) = pane.toolbar().read(cx).item_of_type::<ProjectSearchBar>() {
|
|
|
|
if search_bar.update(cx, |search_bar, cx| {
|
|
|
|
search_bar.toggle_search_option(option, cx)
|
|
|
|
}) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
cx.propagate_action();
|
|
|
|
});
|
2022-02-27 14:47:46 +00:00
|
|
|
}
|
|
|
|
|
2022-02-27 15:15:38 +00:00
|
|
|
struct ProjectSearch {
|
2022-02-27 14:47:46 +00:00
|
|
|
project: ModelHandle<Project>,
|
|
|
|
excerpts: ModelHandle<MultiBuffer>,
|
|
|
|
pending_search: Option<Task<Option<()>>>,
|
2022-02-27 21:18:04 +00:00
|
|
|
match_ranges: Vec<Range<Anchor>>,
|
2022-02-27 14:47:46 +00:00
|
|
|
active_query: Option<SearchQuery>,
|
|
|
|
}
|
|
|
|
|
2022-03-31 16:36:39 +00:00
|
|
|
pub struct ProjectSearchView {
|
2022-02-27 15:15:38 +00:00
|
|
|
model: ModelHandle<ProjectSearch>,
|
2022-02-27 14:47:46 +00:00
|
|
|
query_editor: ViewHandle<Editor>,
|
|
|
|
results_editor: ViewHandle<Editor>,
|
|
|
|
case_sensitive: bool,
|
|
|
|
whole_word: bool,
|
|
|
|
regex: bool,
|
|
|
|
query_contains_error: bool,
|
2022-02-27 21:18:04 +00:00
|
|
|
active_match_index: Option<usize>,
|
2022-02-27 14:47:46 +00:00
|
|
|
}
|
|
|
|
|
2022-03-29 15:04:39 +00:00
|
|
|
pub struct ProjectSearchBar {
|
|
|
|
active_project_search: Option<ViewHandle<ProjectSearchView>>,
|
|
|
|
subscription: Option<Subscription>,
|
|
|
|
}
|
|
|
|
|
2022-02-27 15:15:38 +00:00
|
|
|
impl Entity for ProjectSearch {
|
2022-02-27 14:47:46 +00:00
|
|
|
type Event = ();
|
|
|
|
}
|
|
|
|
|
2022-02-27 15:15:38 +00:00
|
|
|
impl ProjectSearch {
|
2022-02-27 14:47:46 +00:00
|
|
|
fn new(project: ModelHandle<Project>, cx: &mut ModelContext<Self>) -> Self {
|
|
|
|
let replica_id = project.read(cx).replica_id();
|
|
|
|
Self {
|
|
|
|
project,
|
|
|
|
excerpts: cx.add_model(|_| MultiBuffer::new(replica_id)),
|
|
|
|
pending_search: Default::default(),
|
2022-02-27 21:18:04 +00:00
|
|
|
match_ranges: Default::default(),
|
2022-02-27 14:47:46 +00:00
|
|
|
active_query: None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-27 16:49:16 +00:00
|
|
|
fn clone(&self, cx: &mut ModelContext<Self>) -> ModelHandle<Self> {
|
|
|
|
cx.add_model(|cx| Self {
|
2022-02-27 14:47:46 +00:00
|
|
|
project: self.project.clone(),
|
|
|
|
excerpts: self
|
|
|
|
.excerpts
|
2022-02-27 16:49:16 +00:00
|
|
|
.update(cx, |excerpts, cx| cx.add_model(|cx| excerpts.clone(cx))),
|
2022-02-27 14:47:46 +00:00
|
|
|
pending_search: Default::default(),
|
2022-02-27 21:18:04 +00:00
|
|
|
match_ranges: self.match_ranges.clone(),
|
2022-02-27 14:47:46 +00:00
|
|
|
active_query: self.active_query.clone(),
|
2022-02-27 16:49:16 +00:00
|
|
|
})
|
2022-02-27 14:47:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn search(&mut self, query: SearchQuery, cx: &mut ModelContext<Self>) {
|
|
|
|
let search = self
|
|
|
|
.project
|
|
|
|
.update(cx, |project, cx| project.search(query.clone(), cx));
|
|
|
|
self.active_query = Some(query);
|
2022-02-27 21:18:04 +00:00
|
|
|
self.match_ranges.clear();
|
2022-02-27 14:47:46 +00:00
|
|
|
self.pending_search = Some(cx.spawn_weak(|this, mut cx| async move {
|
|
|
|
let matches = search.await.log_err()?;
|
|
|
|
if let Some(this) = this.upgrade(&cx) {
|
|
|
|
this.update(&mut cx, |this, cx| {
|
2022-02-27 21:18:04 +00:00
|
|
|
this.match_ranges.clear();
|
2022-02-27 14:47:46 +00:00
|
|
|
let mut matches = matches.into_iter().collect::<Vec<_>>();
|
|
|
|
matches
|
|
|
|
.sort_by_key(|(buffer, _)| buffer.read(cx).file().map(|file| file.path()));
|
|
|
|
this.excerpts.update(cx, |excerpts, cx| {
|
|
|
|
excerpts.clear(cx);
|
|
|
|
for (buffer, buffer_matches) in matches {
|
|
|
|
let ranges_to_highlight = excerpts.push_excerpts_with_context_lines(
|
|
|
|
buffer,
|
|
|
|
buffer_matches.clone(),
|
|
|
|
1,
|
|
|
|
cx,
|
|
|
|
);
|
2022-02-27 21:18:04 +00:00
|
|
|
this.match_ranges.extend(ranges_to_highlight);
|
2022-02-27 14:47:46 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
this.pending_search.take();
|
|
|
|
cx.notify();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
None
|
|
|
|
}));
|
|
|
|
cx.notify();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-31 16:36:39 +00:00
|
|
|
pub enum ViewEvent {
|
2022-02-27 14:47:46 +00:00
|
|
|
UpdateTab,
|
2022-07-21 01:52:32 +00:00
|
|
|
Activate,
|
2022-04-11 17:13:07 +00:00
|
|
|
EditorEvent(editor::Event),
|
2022-02-27 14:47:46 +00:00
|
|
|
}
|
|
|
|
|
2022-02-27 15:15:38 +00:00
|
|
|
impl Entity for ProjectSearchView {
|
2022-02-27 14:47:46 +00:00
|
|
|
type Event = ViewEvent;
|
|
|
|
}
|
|
|
|
|
2022-02-27 15:15:38 +00:00
|
|
|
impl View for ProjectSearchView {
|
2022-02-27 14:47:46 +00:00
|
|
|
fn ui_name() -> &'static str {
|
2022-02-27 15:15:38 +00:00
|
|
|
"ProjectSearchView"
|
2022-02-27 14:47:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn render(&mut self, cx: &mut RenderContext<Self>) -> ElementBox {
|
|
|
|
let model = &self.model.read(cx);
|
2022-03-29 15:04:39 +00:00
|
|
|
if model.match_ranges.is_empty() {
|
2022-07-21 01:52:32 +00:00
|
|
|
enum Status {}
|
|
|
|
|
|
|
|
let theme = cx.global::<Settings>().theme.clone();
|
2022-02-27 14:47:46 +00:00
|
|
|
let text = if self.query_editor.read(cx).text(cx).is_empty() {
|
|
|
|
""
|
|
|
|
} else if model.pending_search.is_some() {
|
|
|
|
"Searching..."
|
|
|
|
} else {
|
|
|
|
"No results"
|
|
|
|
};
|
2022-09-10 00:29:52 +00:00
|
|
|
MouseEventHandler::<Status>::new(0, cx, |_, _| {
|
2022-07-21 01:52:32 +00:00
|
|
|
Label::new(text.to_string(), theme.search.results_status.clone())
|
|
|
|
.aligned()
|
|
|
|
.contained()
|
|
|
|
.with_background_color(theme.editor.background)
|
|
|
|
.flex(1., true)
|
|
|
|
.boxed()
|
|
|
|
})
|
|
|
|
.on_down(MouseButton::Left, |_, cx| {
|
|
|
|
cx.focus_parent_view();
|
|
|
|
})
|
|
|
|
.boxed()
|
2022-02-27 14:47:46 +00:00
|
|
|
} else {
|
2022-10-13 13:40:21 +00:00
|
|
|
ChildView::new(&self.results_editor, cx)
|
|
|
|
.flex(1., true)
|
|
|
|
.boxed()
|
2022-03-29 15:04:39 +00:00
|
|
|
}
|
2022-02-27 14:47:46 +00:00
|
|
|
}
|
|
|
|
|
2022-08-08 02:23:22 +00:00
|
|
|
fn on_focus_in(&mut self, _: AnyViewHandle, cx: &mut ViewContext<Self>) {
|
2022-03-16 20:34:04 +00:00
|
|
|
let handle = cx.weak_handle();
|
2022-03-17 13:33:01 +00:00
|
|
|
cx.update_global(|state: &mut ActiveSearches, cx| {
|
2022-03-16 20:34:04 +00:00
|
|
|
state
|
|
|
|
.0
|
|
|
|
.insert(self.model.read(cx).project.downgrade(), handle)
|
2022-02-28 01:07:46 +00:00
|
|
|
});
|
2022-10-12 22:10:00 +00:00
|
|
|
|
|
|
|
if cx.is_self_focused() {
|
|
|
|
self.focus_query_editor(cx);
|
|
|
|
}
|
2022-02-27 14:47:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-17 10:32:46 +00:00
|
|
|
impl Item for ProjectSearchView {
|
2022-02-27 14:47:46 +00:00
|
|
|
fn act_as_type(
|
|
|
|
&self,
|
|
|
|
type_id: TypeId,
|
|
|
|
self_handle: &ViewHandle<Self>,
|
|
|
|
_: &gpui::AppContext,
|
|
|
|
) -> Option<gpui::AnyViewHandle> {
|
|
|
|
if type_id == TypeId::of::<Self>() {
|
|
|
|
Some(self_handle.into())
|
|
|
|
} else if type_id == TypeId::of::<Editor>() {
|
|
|
|
Some((&self.results_editor).into())
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn deactivated(&mut self, cx: &mut ViewContext<Self>) {
|
|
|
|
self.results_editor
|
|
|
|
.update(cx, |editor, cx| editor.deactivated(cx));
|
|
|
|
}
|
|
|
|
|
2022-07-14 09:49:10 +00:00
|
|
|
fn tab_content(
|
|
|
|
&self,
|
|
|
|
_detail: Option<usize>,
|
|
|
|
tab_theme: &theme::Tab,
|
|
|
|
cx: &gpui::AppContext,
|
|
|
|
) -> ElementBox {
|
2022-03-17 13:33:01 +00:00
|
|
|
let settings = cx.global::<Settings>();
|
2022-02-27 15:15:38 +00:00
|
|
|
let search_theme = &settings.theme.search;
|
2022-02-27 14:47:46 +00:00
|
|
|
Flex::row()
|
|
|
|
.with_child(
|
2022-07-19 21:11:15 +00:00
|
|
|
Svg::new("icons/magnifying_glass_12.svg")
|
2022-02-27 15:15:38 +00:00
|
|
|
.with_color(tab_theme.label.text.color)
|
2022-02-27 14:47:46 +00:00
|
|
|
.constrained()
|
2022-02-27 15:15:38 +00:00
|
|
|
.with_width(search_theme.tab_icon_width)
|
2022-02-27 14:47:46 +00:00
|
|
|
.aligned()
|
|
|
|
.boxed(),
|
|
|
|
)
|
|
|
|
.with_children(self.model.read(cx).active_query.as_ref().map(|query| {
|
2022-02-27 15:27:02 +00:00
|
|
|
let query_text = if query.as_str().len() > MAX_TAB_TITLE_LEN {
|
|
|
|
query.as_str()[..MAX_TAB_TITLE_LEN].to_string() + "…"
|
|
|
|
} else {
|
|
|
|
query.as_str().to_string()
|
|
|
|
};
|
|
|
|
|
|
|
|
Label::new(query_text, tab_theme.label.clone())
|
2022-02-27 14:47:46 +00:00
|
|
|
.aligned()
|
|
|
|
.contained()
|
2022-02-27 15:15:38 +00:00
|
|
|
.with_margin_left(search_theme.tab_icon_spacing)
|
2022-02-27 14:47:46 +00:00
|
|
|
.boxed()
|
|
|
|
}))
|
|
|
|
.boxed()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn project_path(&self, _: &gpui::AppContext) -> Option<project::ProjectPath> {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
|
2022-05-22 23:48:33 +00:00
|
|
|
fn project_entry_ids(&self, cx: &AppContext) -> SmallVec<[project::ProjectEntryId; 3]> {
|
|
|
|
self.results_editor.project_entry_ids(cx)
|
2022-03-17 17:58:20 +00:00
|
|
|
}
|
|
|
|
|
2022-05-23 23:03:00 +00:00
|
|
|
fn is_singleton(&self, _: &AppContext) -> bool {
|
|
|
|
false
|
|
|
|
}
|
|
|
|
|
2022-02-27 14:47:46 +00:00
|
|
|
fn can_save(&self, _: &gpui::AppContext) -> bool {
|
|
|
|
true
|
|
|
|
}
|
|
|
|
|
|
|
|
fn is_dirty(&self, cx: &AppContext) -> bool {
|
|
|
|
self.results_editor.read(cx).is_dirty(cx)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn has_conflict(&self, cx: &AppContext) -> bool {
|
|
|
|
self.results_editor.read(cx).has_conflict(cx)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn save(
|
|
|
|
&mut self,
|
|
|
|
project: ModelHandle<Project>,
|
|
|
|
cx: &mut ViewContext<Self>,
|
|
|
|
) -> Task<anyhow::Result<()>> {
|
|
|
|
self.results_editor
|
|
|
|
.update(cx, |editor, cx| editor.save(project, cx))
|
|
|
|
}
|
|
|
|
|
|
|
|
fn save_as(
|
|
|
|
&mut self,
|
|
|
|
_: ModelHandle<Project>,
|
|
|
|
_: PathBuf,
|
|
|
|
_: &mut ViewContext<Self>,
|
|
|
|
) -> Task<anyhow::Result<()>> {
|
|
|
|
unreachable!("save_as should not have been called")
|
|
|
|
}
|
|
|
|
|
2022-04-01 12:02:49 +00:00
|
|
|
fn reload(
|
|
|
|
&mut self,
|
|
|
|
project: ModelHandle<Project>,
|
|
|
|
cx: &mut ViewContext<Self>,
|
|
|
|
) -> Task<anyhow::Result<()>> {
|
|
|
|
self.results_editor
|
|
|
|
.update(cx, |editor, cx| editor.reload(project, cx))
|
|
|
|
}
|
|
|
|
|
2022-03-16 20:34:04 +00:00
|
|
|
fn clone_on_split(&self, cx: &mut ViewContext<Self>) -> Option<Self>
|
2022-02-27 14:47:46 +00:00
|
|
|
where
|
|
|
|
Self: Sized,
|
|
|
|
{
|
2022-02-27 16:49:16 +00:00
|
|
|
let model = self.model.update(cx, |model, cx| model.clone(cx));
|
2022-03-16 20:34:04 +00:00
|
|
|
Some(Self::new(model, cx))
|
|
|
|
}
|
|
|
|
|
|
|
|
fn set_nav_history(&mut self, nav_history: ItemNavHistory, cx: &mut ViewContext<Self>) {
|
|
|
|
self.results_editor.update(cx, |editor, _| {
|
|
|
|
editor.set_nav_history(Some(nav_history));
|
|
|
|
});
|
2022-02-27 16:49:16 +00:00
|
|
|
}
|
|
|
|
|
2022-03-23 18:05:46 +00:00
|
|
|
fn navigate(&mut self, data: Box<dyn Any>, cx: &mut ViewContext<Self>) -> bool {
|
2022-02-27 16:49:16 +00:00
|
|
|
self.results_editor
|
2022-03-23 18:05:46 +00:00
|
|
|
.update(cx, |editor, cx| editor.navigate(data, cx))
|
2022-02-27 16:49:16 +00:00
|
|
|
}
|
|
|
|
|
2022-09-06 21:39:58 +00:00
|
|
|
fn to_item_events(event: &Self::Event) -> Vec<ItemEvent> {
|
|
|
|
match event {
|
2022-09-06 23:05:36 +00:00
|
|
|
ViewEvent::UpdateTab => vec![ItemEvent::UpdateBreadcrumbs, ItemEvent::UpdateTab],
|
2022-09-06 21:39:58 +00:00
|
|
|
ViewEvent::EditorEvent(editor_event) => Editor::to_item_events(editor_event),
|
|
|
|
_ => Vec::new(),
|
2022-07-06 14:11:06 +00:00
|
|
|
}
|
|
|
|
}
|
2022-09-06 23:05:36 +00:00
|
|
|
|
|
|
|
fn breadcrumb_location(&self) -> ToolbarItemLocation {
|
|
|
|
if self.has_matches() {
|
|
|
|
ToolbarItemLocation::Secondary
|
|
|
|
} else {
|
|
|
|
ToolbarItemLocation::Hidden
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn breadcrumbs(&self, theme: &theme::Theme, cx: &AppContext) -> Option<Vec<ElementBox>> {
|
|
|
|
self.results_editor.breadcrumbs(theme, cx)
|
|
|
|
}
|
2022-02-27 16:49:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl ProjectSearchView {
|
2022-03-16 20:34:04 +00:00
|
|
|
fn new(model: ModelHandle<ProjectSearch>, cx: &mut ViewContext<Self>) -> Self {
|
2022-02-27 16:49:16 +00:00
|
|
|
let project;
|
|
|
|
let excerpts;
|
|
|
|
let mut query_text = String::new();
|
|
|
|
let mut regex = false;
|
|
|
|
let mut case_sensitive = false;
|
|
|
|
let mut whole_word = false;
|
|
|
|
|
|
|
|
{
|
|
|
|
let model = model.read(cx);
|
|
|
|
project = model.project.clone();
|
|
|
|
excerpts = model.excerpts.clone();
|
|
|
|
if let Some(active_query) = model.active_query.as_ref() {
|
|
|
|
query_text = active_query.as_str().to_string();
|
|
|
|
regex = active_query.is_regex();
|
|
|
|
case_sensitive = active_query.case_sensitive();
|
|
|
|
whole_word = active_query.whole_word();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
cx.observe(&model, |this, _, cx| this.model_changed(true, cx))
|
|
|
|
.detach();
|
|
|
|
|
2022-02-27 14:47:46 +00:00
|
|
|
let query_editor = cx.add_view(|cx| {
|
2022-03-30 11:35:17 +00:00
|
|
|
let mut editor =
|
|
|
|
Editor::single_line(Some(|theme| theme.search.editor.input.clone()), cx);
|
2022-02-27 16:49:16 +00:00
|
|
|
editor.set_text(query_text, cx);
|
2022-02-27 14:47:46 +00:00
|
|
|
editor
|
|
|
|
});
|
2022-04-11 17:13:07 +00:00
|
|
|
// Subcribe to query_editor in order to reraise editor events for workspace item activation purposes
|
|
|
|
cx.subscribe(&query_editor, |_, _, event, cx| {
|
2022-08-10 21:39:24 +00:00
|
|
|
cx.emit(ViewEvent::EditorEvent(*event))
|
2022-04-11 17:13:07 +00:00
|
|
|
})
|
|
|
|
.detach();
|
2022-02-27 14:47:46 +00:00
|
|
|
|
|
|
|
let results_editor = cx.add_view(|cx| {
|
2022-03-17 13:39:02 +00:00
|
|
|
let mut editor = Editor::for_multibuffer(excerpts, Some(project), cx);
|
2022-02-27 14:47:46 +00:00
|
|
|
editor.set_searchable(false);
|
|
|
|
editor
|
|
|
|
});
|
2022-02-27 16:49:16 +00:00
|
|
|
cx.observe(&results_editor, |_, _, cx| cx.emit(ViewEvent::UpdateTab))
|
|
|
|
.detach();
|
2022-08-09 22:09:38 +00:00
|
|
|
|
2022-02-27 21:18:04 +00:00
|
|
|
cx.subscribe(&results_editor, |this, _, event, cx| {
|
2022-03-22 08:16:25 +00:00
|
|
|
if matches!(event, editor::Event::SelectionsChanged { .. }) {
|
2022-02-27 21:18:04 +00:00
|
|
|
this.update_match_index(cx);
|
|
|
|
}
|
2022-04-11 17:13:07 +00:00
|
|
|
// Reraise editor events for workspace item activation purposes
|
2022-08-10 21:39:24 +00:00
|
|
|
cx.emit(ViewEvent::EditorEvent(*event));
|
2022-02-27 21:18:04 +00:00
|
|
|
})
|
|
|
|
.detach();
|
2022-02-27 16:49:16 +00:00
|
|
|
|
|
|
|
let mut this = ProjectSearchView {
|
2022-02-27 14:47:46 +00:00
|
|
|
model,
|
|
|
|
query_editor,
|
|
|
|
results_editor,
|
2022-02-27 16:49:16 +00:00
|
|
|
case_sensitive,
|
|
|
|
whole_word,
|
|
|
|
regex,
|
|
|
|
query_contains_error: false,
|
2022-02-27 21:18:04 +00:00
|
|
|
active_match_index: None,
|
2022-02-27 14:47:46 +00:00
|
|
|
};
|
2022-02-27 16:49:16 +00:00
|
|
|
this.model_changed(false, cx);
|
|
|
|
this
|
2022-02-27 14:47:46 +00:00
|
|
|
}
|
|
|
|
|
2022-02-28 01:07:46 +00:00
|
|
|
// Re-activate the most recently activated search or the most recent if it has been closed.
|
|
|
|
// If no search exists in the workspace, create a new one.
|
2022-08-04 15:42:42 +00:00
|
|
|
fn deploy(
|
|
|
|
workspace: &mut Workspace,
|
|
|
|
_: &workspace::NewSearch,
|
|
|
|
cx: &mut ViewContext<Workspace>,
|
|
|
|
) {
|
2022-02-28 01:07:46 +00:00
|
|
|
// Clean up entries for dropped projects
|
2022-03-17 13:33:01 +00:00
|
|
|
cx.update_global(|state: &mut ActiveSearches, cx| {
|
2022-02-28 01:07:46 +00:00
|
|
|
state.0.retain(|project, _| project.is_upgradable(cx))
|
|
|
|
});
|
|
|
|
|
|
|
|
let active_search = cx
|
2022-03-17 13:33:01 +00:00
|
|
|
.global::<ActiveSearches>()
|
2022-02-28 01:07:46 +00:00
|
|
|
.0
|
|
|
|
.get(&workspace.project().downgrade());
|
|
|
|
|
|
|
|
let existing = active_search
|
|
|
|
.and_then(|active_search| {
|
|
|
|
workspace
|
2022-03-16 20:34:04 +00:00
|
|
|
.items_of_type::<ProjectSearchView>(cx)
|
2022-02-28 01:07:46 +00:00
|
|
|
.find(|search| search == active_search)
|
|
|
|
})
|
2022-03-16 20:34:04 +00:00
|
|
|
.or_else(|| workspace.item_of_type::<ProjectSearchView>(cx));
|
2022-02-28 01:07:46 +00:00
|
|
|
|
2022-06-07 08:02:04 +00:00
|
|
|
let query = workspace.active_item(cx).and_then(|item| {
|
|
|
|
let editor = item.act_as::<Editor>(cx)?;
|
2022-08-30 22:37:54 +00:00
|
|
|
let query = editor.query_suggestion(cx);
|
2022-06-07 08:02:04 +00:00
|
|
|
if query.is_empty() {
|
|
|
|
None
|
|
|
|
} else {
|
|
|
|
Some(query)
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
let search = if let Some(existing) = existing {
|
2022-02-27 14:47:46 +00:00
|
|
|
workspace.activate_item(&existing, cx);
|
2022-06-07 08:02:04 +00:00
|
|
|
existing
|
2022-02-27 14:47:46 +00:00
|
|
|
} else {
|
2022-02-27 15:15:38 +00:00
|
|
|
let model = cx.add_model(|cx| ProjectSearch::new(workspace.project().clone(), cx));
|
2022-06-07 08:02:04 +00:00
|
|
|
let view = cx.add_view(|cx| ProjectSearchView::new(model, cx));
|
|
|
|
workspace.add_item(Box::new(view.clone()), cx);
|
|
|
|
view
|
|
|
|
};
|
|
|
|
|
|
|
|
search.update(cx, |search, cx| {
|
|
|
|
if let Some(query) = query {
|
|
|
|
search.set_query(&query, cx);
|
|
|
|
}
|
|
|
|
search.focus_query_editor(cx)
|
|
|
|
});
|
2022-02-27 14:47:46 +00:00
|
|
|
}
|
|
|
|
|
2022-03-29 15:04:39 +00:00
|
|
|
fn search(&mut self, cx: &mut ViewContext<Self>) {
|
2022-02-27 14:47:46 +00:00
|
|
|
if let Some(query) = self.build_search_query(cx) {
|
|
|
|
self.model.update(cx, |model, cx| model.search(query, cx));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn build_search_query(&mut self, cx: &mut ViewContext<Self>) -> Option<SearchQuery> {
|
|
|
|
let text = self.query_editor.read(cx).text(cx);
|
|
|
|
if self.regex {
|
|
|
|
match SearchQuery::regex(text, self.whole_word, self.case_sensitive) {
|
|
|
|
Ok(query) => Some(query),
|
|
|
|
Err(_) => {
|
|
|
|
self.query_contains_error = true;
|
|
|
|
cx.notify();
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
Some(SearchQuery::text(
|
|
|
|
text,
|
|
|
|
self.whole_word,
|
|
|
|
self.case_sensitive,
|
|
|
|
))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-29 15:04:39 +00:00
|
|
|
fn select_match(&mut self, direction: Direction, cx: &mut ViewContext<Self>) {
|
2022-02-28 09:34:11 +00:00
|
|
|
if let Some(index) = self.active_match_index {
|
2022-09-01 20:45:46 +00:00
|
|
|
let match_ranges = self.model.read(cx).match_ranges.clone();
|
|
|
|
let new_index = self.results_editor.update(cx, |editor, cx| {
|
|
|
|
editor.match_index_for_direction(&match_ranges, index, direction, cx)
|
|
|
|
});
|
|
|
|
|
|
|
|
let range_to_select = match_ranges[new_index].clone();
|
2022-02-27 21:18:04 +00:00
|
|
|
self.results_editor.update(cx, |editor, cx| {
|
2022-03-24 16:16:21 +00:00
|
|
|
editor.unfold_ranges([range_to_select.clone()], false, cx);
|
2022-05-12 21:18:46 +00:00
|
|
|
editor.change_selections(Some(Autoscroll::Fit), cx, |s| {
|
|
|
|
s.select_ranges([range_to_select])
|
2022-05-06 04:09:26 +00:00
|
|
|
});
|
2022-02-27 21:18:04 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-28 01:07:46 +00:00
|
|
|
fn focus_query_editor(&self, cx: &mut ViewContext<Self>) {
|
|
|
|
self.query_editor.update(cx, |query_editor, cx| {
|
|
|
|
query_editor.select_all(&SelectAll, cx);
|
|
|
|
});
|
|
|
|
cx.focus(&self.query_editor);
|
|
|
|
}
|
|
|
|
|
2022-06-07 08:02:04 +00:00
|
|
|
fn set_query(&mut self, query: &str, cx: &mut ViewContext<Self>) {
|
|
|
|
self.query_editor
|
|
|
|
.update(cx, |query_editor, cx| query_editor.set_text(query, cx));
|
|
|
|
}
|
|
|
|
|
2022-02-27 14:47:46 +00:00
|
|
|
fn focus_results_editor(&self, cx: &mut ViewContext<Self>) {
|
|
|
|
self.query_editor.update(cx, |query_editor, cx| {
|
2022-05-06 04:09:26 +00:00
|
|
|
let cursor = query_editor.selections.newest_anchor().head();
|
2022-05-12 21:18:46 +00:00
|
|
|
query_editor.change_selections(None, cx, |s| s.select_ranges([cursor.clone()..cursor]));
|
2022-02-27 14:47:46 +00:00
|
|
|
});
|
|
|
|
cx.focus(&self.results_editor);
|
|
|
|
}
|
|
|
|
|
|
|
|
fn model_changed(&mut self, reset_selections: bool, cx: &mut ViewContext<Self>) {
|
2022-02-27 21:18:04 +00:00
|
|
|
let match_ranges = self.model.read(cx).match_ranges.clone();
|
|
|
|
if match_ranges.is_empty() {
|
|
|
|
self.active_match_index = None;
|
|
|
|
} else {
|
2022-02-27 14:47:46 +00:00
|
|
|
self.results_editor.update(cx, |editor, cx| {
|
|
|
|
if reset_selections {
|
2022-05-12 21:18:46 +00:00
|
|
|
editor.change_selections(Some(Autoscroll::Fit), cx, |s| {
|
|
|
|
s.select_ranges(match_ranges.first().cloned())
|
2022-05-06 04:09:26 +00:00
|
|
|
});
|
2022-02-27 14:47:46 +00:00
|
|
|
}
|
2022-04-13 22:30:57 +00:00
|
|
|
editor.highlight_background::<Self>(
|
|
|
|
match_ranges,
|
|
|
|
|theme| theme.search.match_background,
|
|
|
|
cx,
|
|
|
|
);
|
2022-02-27 14:47:46 +00:00
|
|
|
});
|
|
|
|
if self.query_editor.is_focused(cx) {
|
|
|
|
self.focus_results_editor(cx);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
cx.emit(ViewEvent::UpdateTab);
|
|
|
|
cx.notify();
|
|
|
|
}
|
|
|
|
|
2022-02-27 21:18:04 +00:00
|
|
|
fn update_match_index(&mut self, cx: &mut ViewContext<Self>) {
|
2022-02-28 09:34:11 +00:00
|
|
|
let results_editor = self.results_editor.read(cx);
|
|
|
|
let new_index = active_match_index(
|
|
|
|
&self.model.read(cx).match_ranges,
|
2022-05-06 04:09:26 +00:00
|
|
|
&results_editor.selections.newest_anchor().head(),
|
2022-05-13 23:58:30 +00:00
|
|
|
&results_editor.buffer().read(cx).snapshot(cx),
|
2022-02-28 09:34:11 +00:00
|
|
|
);
|
|
|
|
if self.active_match_index != new_index {
|
|
|
|
self.active_match_index = new_index;
|
|
|
|
cx.notify();
|
2022-02-27 21:18:04 +00:00
|
|
|
}
|
|
|
|
}
|
2022-04-01 08:00:21 +00:00
|
|
|
|
|
|
|
pub fn has_matches(&self) -> bool {
|
|
|
|
self.active_match_index.is_some()
|
|
|
|
}
|
2022-03-29 15:04:39 +00:00
|
|
|
}
|
2022-02-27 21:18:04 +00:00
|
|
|
|
2022-08-10 21:39:24 +00:00
|
|
|
impl Default for ProjectSearchBar {
|
|
|
|
fn default() -> Self {
|
|
|
|
Self::new()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-29 15:04:39 +00:00
|
|
|
impl ProjectSearchBar {
|
|
|
|
pub fn new() -> Self {
|
|
|
|
Self {
|
|
|
|
active_project_search: Default::default(),
|
|
|
|
subscription: Default::default(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-21 22:24:05 +00:00
|
|
|
fn search(&mut self, _: &Confirm, cx: &mut ViewContext<Self>) {
|
2022-03-29 15:04:39 +00:00
|
|
|
if let Some(search_view) = self.active_project_search.as_ref() {
|
|
|
|
search_view.update(cx, |search_view, cx| search_view.search(cx));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn search_in_new(workspace: &mut Workspace, _: &SearchInNew, cx: &mut ViewContext<Workspace>) {
|
|
|
|
if let Some(search_view) = workspace
|
|
|
|
.active_item(cx)
|
|
|
|
.and_then(|item| item.downcast::<ProjectSearchView>())
|
|
|
|
{
|
|
|
|
let new_query = search_view.update(cx, |search_view, cx| {
|
|
|
|
let new_query = search_view.build_search_query(cx);
|
|
|
|
if new_query.is_some() {
|
|
|
|
if let Some(old_query) = search_view.model.read(cx).active_query.clone() {
|
|
|
|
search_view.query_editor.update(cx, |editor, cx| {
|
|
|
|
editor.set_text(old_query.as_str(), cx);
|
|
|
|
});
|
|
|
|
search_view.regex = old_query.is_regex();
|
|
|
|
search_view.whole_word = old_query.whole_word();
|
|
|
|
search_view.case_sensitive = old_query.case_sensitive();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
new_query
|
|
|
|
});
|
|
|
|
if let Some(new_query) = new_query {
|
|
|
|
let model = cx.add_model(|cx| {
|
|
|
|
let mut model = ProjectSearch::new(workspace.project().clone(), cx);
|
|
|
|
model.search(new_query, cx);
|
|
|
|
model
|
|
|
|
});
|
|
|
|
workspace.add_item(
|
|
|
|
Box::new(cx.add_view(|cx| ProjectSearchView::new(model, cx))),
|
|
|
|
cx,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-08 22:32:56 +00:00
|
|
|
fn select_next_match(pane: &mut Pane, _: &SelectNextMatch, cx: &mut ViewContext<Pane>) {
|
2022-03-29 15:04:39 +00:00
|
|
|
if let Some(search_view) = pane
|
|
|
|
.active_item()
|
|
|
|
.and_then(|item| item.downcast::<ProjectSearchView>())
|
|
|
|
{
|
2022-04-08 22:32:56 +00:00
|
|
|
search_view.update(cx, |view, cx| view.select_match(Direction::Next, cx));
|
|
|
|
} else {
|
|
|
|
cx.propagate_action();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn select_prev_match(pane: &mut Pane, _: &SelectPrevMatch, 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.select_match(Direction::Prev, cx));
|
2022-02-27 14:47:46 +00:00
|
|
|
} else {
|
2022-03-29 15:04:39 +00:00
|
|
|
cx.propagate_action();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn toggle_focus(pane: &mut Pane, _: &ToggleFocus, cx: &mut ViewContext<Pane>) {
|
|
|
|
if let Some(search_view) = pane
|
|
|
|
.active_item()
|
|
|
|
.and_then(|item| item.downcast::<ProjectSearchView>())
|
|
|
|
{
|
|
|
|
search_view.update(cx, |search_view, cx| {
|
|
|
|
if search_view.query_editor.is_focused(cx) {
|
|
|
|
if !search_view.model.read(cx).match_ranges.is_empty() {
|
|
|
|
search_view.focus_results_editor(cx);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
search_view.focus_query_editor(cx);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
cx.propagate_action();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn tab(&mut self, _: &editor::Tab, cx: &mut ViewContext<Self>) {
|
|
|
|
if let Some(search_view) = self.active_project_search.as_ref() {
|
|
|
|
search_view.update(cx, |search_view, cx| {
|
|
|
|
if search_view.query_editor.is_focused(cx) {
|
|
|
|
if !search_view.model.read(cx).match_ranges.is_empty() {
|
|
|
|
search_view.focus_results_editor(cx);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
cx.propagate_action();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
cx.propagate_action();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-16 12:18:45 +00:00
|
|
|
fn toggle_search_option(&mut self, option: SearchOption, cx: &mut ViewContext<Self>) -> bool {
|
2022-03-29 15:04:39 +00:00
|
|
|
if let Some(search_view) = self.active_project_search.as_ref() {
|
|
|
|
search_view.update(cx, |search_view, cx| {
|
|
|
|
let value = match option {
|
|
|
|
SearchOption::WholeWord => &mut search_view.whole_word,
|
|
|
|
SearchOption::CaseSensitive => &mut search_view.case_sensitive,
|
|
|
|
SearchOption::Regex => &mut search_view.regex,
|
|
|
|
};
|
|
|
|
*value = !*value;
|
|
|
|
search_view.search(cx);
|
|
|
|
});
|
|
|
|
cx.notify();
|
2022-06-16 12:18:45 +00:00
|
|
|
true
|
|
|
|
} else {
|
|
|
|
false
|
2022-03-29 15:04:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn render_nav_button(
|
|
|
|
&self,
|
|
|
|
icon: &str,
|
|
|
|
direction: Direction,
|
|
|
|
cx: &mut RenderContext<Self>,
|
|
|
|
) -> ElementBox {
|
2022-06-16 12:37:33 +00:00
|
|
|
let action: Box<dyn Action>;
|
|
|
|
let tooltip;
|
|
|
|
match direction {
|
|
|
|
Direction::Prev => {
|
|
|
|
action = Box::new(SelectPrevMatch);
|
|
|
|
tooltip = "Select Previous Match";
|
|
|
|
}
|
|
|
|
Direction::Next => {
|
|
|
|
action = Box::new(SelectNextMatch);
|
|
|
|
tooltip = "Select Next Match";
|
|
|
|
}
|
|
|
|
};
|
|
|
|
let tooltip_style = cx.global::<Settings>().theme.tooltip.clone();
|
|
|
|
|
2022-03-29 15:04:39 +00:00
|
|
|
enum NavButton {}
|
2022-09-10 00:29:52 +00:00
|
|
|
MouseEventHandler::<NavButton>::new(direction as usize, cx, |state, cx| {
|
2022-04-28 22:29:03 +00:00
|
|
|
let style = &cx
|
|
|
|
.global::<Settings>()
|
|
|
|
.theme
|
|
|
|
.search
|
|
|
|
.option_button
|
|
|
|
.style_for(state, false);
|
2022-03-29 15:04:39 +00:00
|
|
|
Label::new(icon.to_string(), style.text.clone())
|
|
|
|
.contained()
|
|
|
|
.with_style(style.container)
|
|
|
|
.boxed()
|
|
|
|
})
|
2022-07-18 06:19:32 +00:00
|
|
|
.on_click(MouseButton::Left, {
|
2022-06-16 12:37:33 +00:00
|
|
|
let action = action.boxed_clone();
|
2022-07-18 06:19:32 +00:00
|
|
|
move |_, cx| cx.dispatch_any_action(action.boxed_clone())
|
2022-04-08 22:32:56 +00:00
|
|
|
})
|
2022-03-29 15:04:39 +00:00
|
|
|
.with_cursor_style(CursorStyle::PointingHand)
|
2022-06-16 12:37:33 +00:00
|
|
|
.with_tooltip::<NavButton, _>(
|
|
|
|
direction as usize,
|
|
|
|
tooltip.to_string(),
|
|
|
|
Some(action),
|
|
|
|
tooltip_style,
|
|
|
|
cx,
|
|
|
|
)
|
2022-03-29 15:04:39 +00:00
|
|
|
.boxed()
|
2022-02-27 14:47:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn render_option_button(
|
|
|
|
&self,
|
|
|
|
icon: &str,
|
|
|
|
option: SearchOption,
|
|
|
|
cx: &mut RenderContext<Self>,
|
|
|
|
) -> ElementBox {
|
2022-06-16 11:44:00 +00:00
|
|
|
let tooltip_style = cx.global::<Settings>().theme.tooltip.clone();
|
2022-03-29 15:04:39 +00:00
|
|
|
let is_active = self.is_option_enabled(option, cx);
|
2022-09-10 00:29:52 +00:00
|
|
|
MouseEventHandler::<Self>::new(option as usize, cx, |state, cx| {
|
2022-04-28 22:29:03 +00:00
|
|
|
let style = &cx
|
|
|
|
.global::<Settings>()
|
|
|
|
.theme
|
|
|
|
.search
|
|
|
|
.option_button
|
|
|
|
.style_for(state, is_active);
|
2022-02-27 14:47:46 +00:00
|
|
|
Label::new(icon.to_string(), style.text.clone())
|
|
|
|
.contained()
|
|
|
|
.with_style(style.container)
|
|
|
|
.boxed()
|
|
|
|
})
|
2022-07-18 06:19:32 +00:00
|
|
|
.on_click(MouseButton::Left, move |_, cx| {
|
|
|
|
cx.dispatch_any_action(option.to_toggle_action())
|
|
|
|
})
|
2022-02-27 14:47:46 +00:00
|
|
|
.with_cursor_style(CursorStyle::PointingHand)
|
2022-06-16 11:44:00 +00:00
|
|
|
.with_tooltip::<Self, _>(
|
|
|
|
option as usize,
|
|
|
|
format!("Toggle {}", option.label()),
|
2022-06-16 12:28:37 +00:00
|
|
|
Some(option.to_toggle_action()),
|
2022-06-16 11:44:00 +00:00
|
|
|
tooltip_style,
|
|
|
|
cx,
|
|
|
|
)
|
2022-02-27 14:47:46 +00:00
|
|
|
.boxed()
|
|
|
|
}
|
|
|
|
|
2022-03-29 15:04:39 +00:00
|
|
|
fn is_option_enabled(&self, option: SearchOption, cx: &AppContext) -> bool {
|
|
|
|
if let Some(search) = self.active_project_search.as_ref() {
|
|
|
|
let search = search.read(cx);
|
|
|
|
match option {
|
|
|
|
SearchOption::WholeWord => search.whole_word,
|
|
|
|
SearchOption::CaseSensitive => search.case_sensitive,
|
|
|
|
SearchOption::Regex => search.regex,
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
false
|
2022-02-27 14:47:46 +00:00
|
|
|
}
|
|
|
|
}
|
2022-03-29 15:04:39 +00:00
|
|
|
}
|
2022-02-27 21:18:04 +00:00
|
|
|
|
2022-03-29 15:04:39 +00:00
|
|
|
impl Entity for ProjectSearchBar {
|
|
|
|
type Event = ();
|
|
|
|
}
|
|
|
|
|
|
|
|
impl View for ProjectSearchBar {
|
|
|
|
fn ui_name() -> &'static str {
|
|
|
|
"ProjectSearchBar"
|
|
|
|
}
|
|
|
|
|
|
|
|
fn render(&mut self, cx: &mut RenderContext<Self>) -> ElementBox {
|
|
|
|
if let Some(search) = self.active_project_search.as_ref() {
|
|
|
|
let search = search.read(cx);
|
|
|
|
let theme = cx.global::<Settings>().theme.clone();
|
|
|
|
let editor_container = if search.query_contains_error {
|
|
|
|
theme.search.invalid_editor
|
2022-02-27 21:18:04 +00:00
|
|
|
} else {
|
2022-03-30 11:35:17 +00:00
|
|
|
theme.search.editor.input.container
|
2022-02-27 21:18:04 +00:00
|
|
|
};
|
2022-03-29 15:04:39 +00:00
|
|
|
Flex::row()
|
|
|
|
.with_child(
|
|
|
|
Flex::row()
|
2022-04-01 07:40:49 +00:00
|
|
|
.with_child(
|
2022-10-13 13:40:21 +00:00
|
|
|
ChildView::new(&search.query_editor, cx)
|
2022-04-01 07:40:49 +00:00
|
|
|
.aligned()
|
|
|
|
.left()
|
|
|
|
.flex(1., true)
|
|
|
|
.boxed(),
|
|
|
|
)
|
2022-03-29 15:04:39 +00:00
|
|
|
.with_children(search.active_match_index.map(|match_ix| {
|
|
|
|
Label::new(
|
|
|
|
format!(
|
|
|
|
"{}/{}",
|
|
|
|
match_ix + 1,
|
|
|
|
search.model.read(cx).match_ranges.len()
|
|
|
|
),
|
|
|
|
theme.search.match_index.text.clone(),
|
|
|
|
)
|
|
|
|
.contained()
|
|
|
|
.with_style(theme.search.match_index.container)
|
|
|
|
.aligned()
|
|
|
|
.boxed()
|
|
|
|
}))
|
|
|
|
.contained()
|
|
|
|
.with_style(editor_container)
|
|
|
|
.aligned()
|
|
|
|
.constrained()
|
2022-04-01 07:40:49 +00:00
|
|
|
.with_min_width(theme.search.editor.min_width)
|
2022-03-30 11:35:17 +00:00
|
|
|
.with_max_width(theme.search.editor.max_width)
|
2022-04-01 07:40:49 +00:00
|
|
|
.flex(1., false)
|
2022-03-29 15:04:39 +00:00
|
|
|
.boxed(),
|
|
|
|
)
|
|
|
|
.with_child(
|
|
|
|
Flex::row()
|
|
|
|
.with_child(self.render_nav_button("<", Direction::Prev, cx))
|
|
|
|
.with_child(self.render_nav_button(">", Direction::Next, cx))
|
|
|
|
.aligned()
|
|
|
|
.boxed(),
|
|
|
|
)
|
|
|
|
.with_child(
|
|
|
|
Flex::row()
|
|
|
|
.with_child(self.render_option_button(
|
|
|
|
"Case",
|
|
|
|
SearchOption::CaseSensitive,
|
|
|
|
cx,
|
|
|
|
))
|
|
|
|
.with_child(self.render_option_button("Word", SearchOption::WholeWord, cx))
|
|
|
|
.with_child(self.render_option_button("Regex", SearchOption::Regex, cx))
|
|
|
|
.contained()
|
|
|
|
.with_style(theme.search.option_button_group)
|
|
|
|
.aligned()
|
|
|
|
.boxed(),
|
|
|
|
)
|
2022-02-27 21:18:04 +00:00
|
|
|
.contained()
|
2022-04-01 07:40:49 +00:00
|
|
|
.with_style(theme.search.container)
|
|
|
|
.aligned()
|
|
|
|
.left()
|
2022-03-29 15:04:39 +00:00
|
|
|
.named("project search")
|
|
|
|
} else {
|
|
|
|
Empty::new().boxed()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ToolbarItemView for ProjectSearchBar {
|
|
|
|
fn set_active_pane_item(
|
|
|
|
&mut self,
|
|
|
|
active_pane_item: Option<&dyn workspace::ItemHandle>,
|
|
|
|
cx: &mut ViewContext<Self>,
|
2022-03-31 16:36:39 +00:00
|
|
|
) -> ToolbarItemLocation {
|
|
|
|
cx.notify();
|
2022-03-29 15:04:39 +00:00
|
|
|
self.subscription = None;
|
|
|
|
self.active_project_search = None;
|
|
|
|
if let Some(search) = active_pane_item.and_then(|i| i.downcast::<ProjectSearchView>()) {
|
2022-08-09 22:09:38 +00:00
|
|
|
let query_editor = search.read(cx).query_editor.clone();
|
|
|
|
cx.reparent(query_editor);
|
2022-03-29 15:04:39 +00:00
|
|
|
self.subscription = Some(cx.observe(&search, |_, _, cx| cx.notify()));
|
|
|
|
self.active_project_search = Some(search);
|
2022-04-01 08:55:38 +00:00
|
|
|
ToolbarItemLocation::PrimaryLeft {
|
|
|
|
flex: Some((1., false)),
|
|
|
|
}
|
2022-03-31 16:36:39 +00:00
|
|
|
} else {
|
|
|
|
ToolbarItemLocation::Hidden
|
2022-03-29 15:04:39 +00:00
|
|
|
}
|
2022-02-27 21:18:04 +00:00
|
|
|
}
|
2022-02-27 14:47:46 +00:00
|
|
|
}
|
2022-02-28 10:10:22 +00:00
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
|
|
|
use editor::DisplayPoint;
|
|
|
|
use gpui::{color::Color, TestAppContext};
|
|
|
|
use project::FakeFs;
|
|
|
|
use serde_json::json;
|
|
|
|
use std::sync::Arc;
|
|
|
|
|
|
|
|
#[gpui::test]
|
2022-03-01 11:01:02 +00:00
|
|
|
async fn test_project_search(cx: &mut TestAppContext) {
|
2022-02-28 10:10:22 +00:00
|
|
|
let fonts = cx.font_cache();
|
2022-08-10 21:39:24 +00:00
|
|
|
let mut theme = gpui::fonts::with_font_cache(fonts.clone(), theme::Theme::default);
|
2022-02-28 10:10:22 +00:00
|
|
|
theme.search.match_background = Color::red();
|
2022-07-12 18:00:02 +00:00
|
|
|
cx.update(|cx| {
|
|
|
|
let mut settings = Settings::test(cx);
|
|
|
|
settings.theme = Arc::new(theme);
|
2022-08-05 03:55:10 +00:00
|
|
|
cx.set_global(settings);
|
|
|
|
cx.set_global(ActiveSearches::default());
|
2022-07-12 18:00:02 +00:00
|
|
|
});
|
2022-02-28 10:10:22 +00:00
|
|
|
|
|
|
|
let fs = FakeFs::new(cx.background());
|
|
|
|
fs.insert_tree(
|
|
|
|
"/dir",
|
|
|
|
json!({
|
|
|
|
"one.rs": "const ONE: usize = 1;",
|
|
|
|
"two.rs": "const TWO: usize = one::ONE + one::ONE;",
|
|
|
|
"three.rs": "const THREE: usize = one::ONE + two::TWO;",
|
|
|
|
"four.rs": "const FOUR: usize = one::ONE + three::THREE;",
|
|
|
|
}),
|
|
|
|
)
|
|
|
|
.await;
|
2022-05-19 21:37:26 +00:00
|
|
|
let project = Project::test(fs.clone(), ["/dir".as_ref()], cx).await;
|
2022-02-28 10:10:22 +00:00
|
|
|
let search = cx.add_model(|cx| ProjectSearch::new(project, cx));
|
2022-08-05 03:55:10 +00:00
|
|
|
let (_, search_view) = cx.add_window(|cx| ProjectSearchView::new(search.clone(), cx));
|
2022-02-28 10:10:22 +00:00
|
|
|
|
2022-03-01 11:01:02 +00:00
|
|
|
search_view.update(cx, |search_view, cx| {
|
2022-02-28 10:10:22 +00:00
|
|
|
search_view
|
|
|
|
.query_editor
|
|
|
|
.update(cx, |query_editor, cx| query_editor.set_text("TWO", cx));
|
2022-03-29 15:04:39 +00:00
|
|
|
search_view.search(cx);
|
2022-02-28 10:10:22 +00:00
|
|
|
});
|
2022-08-10 21:39:24 +00:00
|
|
|
search_view.next_notification(cx).await;
|
2022-03-01 11:01:02 +00:00
|
|
|
search_view.update(cx, |search_view, cx| {
|
2022-02-28 10:10:22 +00:00
|
|
|
assert_eq!(
|
|
|
|
search_view
|
|
|
|
.results_editor
|
|
|
|
.update(cx, |editor, cx| editor.display_text(cx)),
|
|
|
|
"\n\nconst THREE: usize = one::ONE + two::TWO;\n\n\nconst TWO: usize = one::ONE + one::ONE;"
|
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
search_view
|
|
|
|
.results_editor
|
2022-03-09 21:53:31 +00:00
|
|
|
.update(cx, |editor, cx| editor.all_background_highlights(cx)),
|
2022-02-28 10:10:22 +00:00
|
|
|
&[
|
|
|
|
(
|
|
|
|
DisplayPoint::new(2, 32)..DisplayPoint::new(2, 35),
|
|
|
|
Color::red()
|
|
|
|
),
|
|
|
|
(
|
|
|
|
DisplayPoint::new(2, 37)..DisplayPoint::new(2, 40),
|
|
|
|
Color::red()
|
|
|
|
),
|
|
|
|
(
|
|
|
|
DisplayPoint::new(5, 6)..DisplayPoint::new(5, 9),
|
|
|
|
Color::red()
|
|
|
|
)
|
|
|
|
]
|
|
|
|
);
|
|
|
|
assert_eq!(search_view.active_match_index, Some(0));
|
|
|
|
assert_eq!(
|
|
|
|
search_view
|
|
|
|
.results_editor
|
2022-05-12 23:04:27 +00:00
|
|
|
.update(cx, |editor, cx| editor.selections.display_ranges(cx)),
|
2022-02-28 10:10:22 +00:00
|
|
|
[DisplayPoint::new(2, 32)..DisplayPoint::new(2, 35)]
|
|
|
|
);
|
|
|
|
|
2022-03-29 15:04:39 +00:00
|
|
|
search_view.select_match(Direction::Next, cx);
|
2022-02-28 10:10:22 +00:00
|
|
|
});
|
|
|
|
|
2022-03-01 11:01:02 +00:00
|
|
|
search_view.update(cx, |search_view, cx| {
|
2022-02-28 10:10:22 +00:00
|
|
|
assert_eq!(search_view.active_match_index, Some(1));
|
|
|
|
assert_eq!(
|
2022-05-12 23:04:27 +00:00
|
|
|
search_view
|
|
|
|
.results_editor
|
|
|
|
.update(cx, |editor, cx| editor.selections.display_ranges(cx)),
|
2022-02-28 10:10:22 +00:00
|
|
|
[DisplayPoint::new(2, 37)..DisplayPoint::new(2, 40)]
|
|
|
|
);
|
2022-03-29 15:04:39 +00:00
|
|
|
search_view.select_match(Direction::Next, cx);
|
2022-02-28 10:10:22 +00:00
|
|
|
});
|
|
|
|
|
2022-03-01 11:01:02 +00:00
|
|
|
search_view.update(cx, |search_view, cx| {
|
2022-02-28 10:10:22 +00:00
|
|
|
assert_eq!(search_view.active_match_index, Some(2));
|
|
|
|
assert_eq!(
|
2022-05-12 23:04:27 +00:00
|
|
|
search_view
|
|
|
|
.results_editor
|
|
|
|
.update(cx, |editor, cx| editor.selections.display_ranges(cx)),
|
2022-02-28 10:10:22 +00:00
|
|
|
[DisplayPoint::new(5, 6)..DisplayPoint::new(5, 9)]
|
|
|
|
);
|
2022-03-29 15:04:39 +00:00
|
|
|
search_view.select_match(Direction::Next, cx);
|
2022-02-28 10:10:22 +00:00
|
|
|
});
|
|
|
|
|
2022-03-01 11:01:02 +00:00
|
|
|
search_view.update(cx, |search_view, cx| {
|
2022-02-28 10:10:22 +00:00
|
|
|
assert_eq!(search_view.active_match_index, Some(0));
|
|
|
|
assert_eq!(
|
2022-05-12 23:04:27 +00:00
|
|
|
search_view
|
|
|
|
.results_editor
|
|
|
|
.update(cx, |editor, cx| editor.selections.display_ranges(cx)),
|
2022-02-28 10:10:22 +00:00
|
|
|
[DisplayPoint::new(2, 32)..DisplayPoint::new(2, 35)]
|
|
|
|
);
|
2022-03-29 15:04:39 +00:00
|
|
|
search_view.select_match(Direction::Prev, cx);
|
2022-02-28 10:10:22 +00:00
|
|
|
});
|
|
|
|
|
2022-03-01 11:01:02 +00:00
|
|
|
search_view.update(cx, |search_view, cx| {
|
2022-02-28 10:10:22 +00:00
|
|
|
assert_eq!(search_view.active_match_index, Some(2));
|
|
|
|
assert_eq!(
|
2022-05-12 23:04:27 +00:00
|
|
|
search_view
|
|
|
|
.results_editor
|
|
|
|
.update(cx, |editor, cx| editor.selections.display_ranges(cx)),
|
2022-02-28 10:10:22 +00:00
|
|
|
[DisplayPoint::new(5, 6)..DisplayPoint::new(5, 9)]
|
|
|
|
);
|
2022-03-29 15:04:39 +00:00
|
|
|
search_view.select_match(Direction::Prev, cx);
|
2022-02-28 10:10:22 +00:00
|
|
|
});
|
|
|
|
|
2022-03-01 11:01:02 +00:00
|
|
|
search_view.update(cx, |search_view, cx| {
|
2022-02-28 10:10:22 +00:00
|
|
|
assert_eq!(search_view.active_match_index, Some(1));
|
|
|
|
assert_eq!(
|
2022-05-12 23:04:27 +00:00
|
|
|
search_view
|
|
|
|
.results_editor
|
|
|
|
.update(cx, |editor, cx| editor.selections.display_ranges(cx)),
|
2022-02-28 10:10:22 +00:00
|
|
|
[DisplayPoint::new(2, 37)..DisplayPoint::new(2, 40)]
|
|
|
|
);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|