2022-02-28 09:34:11 +00:00
|
|
|
use crate::{
|
|
|
|
active_match_index, match_index_for_direction, Direction, SearchOption, SelectMatch,
|
|
|
|
ToggleSearchOption,
|
|
|
|
};
|
2022-02-28 01:07:46 +00:00
|
|
|
use collections::HashMap;
|
2022-02-27 21:21:28 +00:00
|
|
|
use editor::{Anchor, Autoscroll, Editor, MultiBuffer, SelectAll};
|
2022-02-27 14:47:46 +00:00
|
|
|
use gpui::{
|
|
|
|
action, elements::*, keymap::Binding, platform::CursorStyle, AppContext, ElementBox, Entity,
|
|
|
|
ModelContext, ModelHandle, MutableAppContext, RenderContext, Task, View, ViewContext,
|
2022-02-28 01:07:46 +00:00
|
|
|
ViewHandle, WeakModelHandle,
|
2022-02-27 14:47:46 +00:00
|
|
|
};
|
|
|
|
use postage::watch;
|
|
|
|
use project::{search::SearchQuery, Project};
|
|
|
|
use std::{
|
|
|
|
any::{Any, TypeId},
|
|
|
|
ops::Range,
|
|
|
|
path::PathBuf,
|
|
|
|
};
|
|
|
|
use util::ResultExt as _;
|
2022-02-28 01:17:15 +00:00
|
|
|
use workspace::{Item, ItemHandle, ItemNavHistory, ItemView, Settings, Workspace};
|
2022-02-27 14:47:46 +00:00
|
|
|
|
|
|
|
action!(Deploy);
|
|
|
|
action!(Search);
|
|
|
|
action!(SearchInNew);
|
|
|
|
action!(ToggleFocus);
|
|
|
|
|
2022-02-27 15:27:02 +00:00
|
|
|
const MAX_TAB_TITLE_LEN: usize = 24;
|
|
|
|
|
2022-02-28 01:07:46 +00:00
|
|
|
#[derive(Default)]
|
|
|
|
struct ActiveSearches(HashMap<WeakModelHandle<Project>, WeakModelHandle<ProjectSearch>>);
|
|
|
|
|
2022-02-27 14:47:46 +00:00
|
|
|
pub fn init(cx: &mut MutableAppContext) {
|
2022-02-28 01:07:46 +00:00
|
|
|
cx.add_app_state(ActiveSearches::default());
|
2022-02-27 14:47:46 +00:00
|
|
|
cx.add_bindings([
|
2022-02-27 15:15:38 +00:00
|
|
|
Binding::new("cmd-shift-F", ToggleFocus, Some("ProjectSearchView")),
|
|
|
|
Binding::new("cmd-f", ToggleFocus, Some("ProjectSearchView")),
|
2022-02-27 14:47:46 +00:00
|
|
|
Binding::new("cmd-shift-F", Deploy, Some("Workspace")),
|
2022-02-27 15:15:38 +00:00
|
|
|
Binding::new("enter", Search, Some("ProjectSearchView")),
|
|
|
|
Binding::new("cmd-enter", SearchInNew, Some("ProjectSearchView")),
|
2022-02-27 21:18:04 +00:00
|
|
|
Binding::new(
|
|
|
|
"cmd-g",
|
|
|
|
SelectMatch(Direction::Next),
|
|
|
|
Some("ProjectSearchView"),
|
|
|
|
),
|
|
|
|
Binding::new(
|
|
|
|
"cmd-shift-G",
|
|
|
|
SelectMatch(Direction::Prev),
|
|
|
|
Some("ProjectSearchView"),
|
|
|
|
),
|
2022-02-27 14:47:46 +00:00
|
|
|
]);
|
2022-02-27 15:15:38 +00:00
|
|
|
cx.add_action(ProjectSearchView::deploy);
|
|
|
|
cx.add_action(ProjectSearchView::search);
|
|
|
|
cx.add_action(ProjectSearchView::search_in_new);
|
|
|
|
cx.add_action(ProjectSearchView::toggle_search_option);
|
2022-02-27 21:18:04 +00:00
|
|
|
cx.add_action(ProjectSearchView::select_match);
|
2022-02-27 23:15:06 +00:00
|
|
|
cx.add_action(ProjectSearchView::toggle_focus);
|
|
|
|
cx.capture_action(ProjectSearchView::tab);
|
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-02-27 15:15:38 +00:00
|
|
|
struct ProjectSearchView {
|
|
|
|
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
|
|
|
settings: watch::Receiver<Settings>,
|
|
|
|
}
|
|
|
|
|
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-02-27 15:15:38 +00:00
|
|
|
impl Item for ProjectSearch {
|
|
|
|
type View = ProjectSearchView;
|
2022-02-27 14:47:46 +00:00
|
|
|
|
|
|
|
fn build_view(
|
|
|
|
model: ModelHandle<Self>,
|
|
|
|
workspace: &Workspace,
|
|
|
|
nav_history: ItemNavHistory,
|
|
|
|
cx: &mut gpui::ViewContext<Self::View>,
|
|
|
|
) -> Self::View {
|
2022-02-28 10:10:22 +00:00
|
|
|
ProjectSearchView::new(model, Some(nav_history), workspace.settings(), cx)
|
2022-02-27 14:47:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn project_path(&self) -> Option<project::ProjectPath> {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
enum ViewEvent {
|
|
|
|
UpdateTab,
|
|
|
|
}
|
|
|
|
|
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-02-27 21:18:04 +00:00
|
|
|
let results = if model.match_ranges.is_empty() {
|
2022-02-27 14:47:46 +00:00
|
|
|
let theme = &self.settings.borrow().theme;
|
|
|
|
let text = if self.query_editor.read(cx).text(cx).is_empty() {
|
|
|
|
""
|
|
|
|
} else if model.pending_search.is_some() {
|
|
|
|
"Searching..."
|
|
|
|
} else {
|
|
|
|
"No results"
|
|
|
|
};
|
2022-02-27 15:15:38 +00:00
|
|
|
Label::new(text.to_string(), theme.search.results_status.clone())
|
2022-02-27 14:47:46 +00:00
|
|
|
.aligned()
|
|
|
|
.contained()
|
|
|
|
.with_background_color(theme.editor.background)
|
|
|
|
.flexible(1., true)
|
|
|
|
.boxed()
|
|
|
|
} else {
|
|
|
|
ChildView::new(&self.results_editor)
|
|
|
|
.flexible(1., true)
|
|
|
|
.boxed()
|
|
|
|
};
|
|
|
|
|
|
|
|
Flex::column()
|
|
|
|
.with_child(self.render_query_editor(cx))
|
|
|
|
.with_child(results)
|
|
|
|
.boxed()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn on_focus(&mut self, cx: &mut ViewContext<Self>) {
|
2022-02-28 01:07:46 +00:00
|
|
|
cx.update_app_state(|state: &mut ActiveSearches, cx| {
|
|
|
|
state.0.insert(
|
|
|
|
self.model.read(cx).project.downgrade(),
|
|
|
|
self.model.downgrade(),
|
|
|
|
)
|
|
|
|
});
|
|
|
|
|
2022-02-27 21:18:04 +00:00
|
|
|
if self.model.read(cx).match_ranges.is_empty() {
|
2022-02-27 14:47:46 +00:00
|
|
|
cx.focus(&self.query_editor);
|
|
|
|
} else {
|
|
|
|
self.focus_results_editor(cx);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-27 15:15:38 +00:00
|
|
|
impl ItemView 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));
|
|
|
|
}
|
|
|
|
|
|
|
|
fn item(&self, _: &gpui::AppContext) -> Box<dyn ItemHandle> {
|
|
|
|
Box::new(self.model.clone())
|
|
|
|
}
|
|
|
|
|
2022-02-27 15:15:38 +00:00
|
|
|
fn tab_content(&self, tab_theme: &theme::Tab, cx: &gpui::AppContext) -> ElementBox {
|
2022-02-27 14:47:46 +00:00
|
|
|
let settings = self.settings.borrow();
|
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(
|
|
|
|
Svg::new("icons/magnifier.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
|
|
|
|
}
|
|
|
|
|
|
|
|
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 can_save_as(&self, _: &gpui::AppContext) -> bool {
|
|
|
|
false
|
|
|
|
}
|
|
|
|
|
|
|
|
fn save_as(
|
|
|
|
&mut self,
|
|
|
|
_: ModelHandle<Project>,
|
|
|
|
_: PathBuf,
|
|
|
|
_: &mut ViewContext<Self>,
|
|
|
|
) -> Task<anyhow::Result<()>> {
|
|
|
|
unreachable!("save_as should not have been called")
|
|
|
|
}
|
|
|
|
|
|
|
|
fn clone_on_split(
|
|
|
|
&self,
|
|
|
|
nav_history: ItemNavHistory,
|
|
|
|
cx: &mut ViewContext<Self>,
|
|
|
|
) -> Option<Self>
|
|
|
|
where
|
|
|
|
Self: Sized,
|
|
|
|
{
|
2022-02-27 16:49:16 +00:00
|
|
|
let model = self.model.update(cx, |model, cx| model.clone(cx));
|
2022-02-28 10:10:22 +00:00
|
|
|
Some(Self::new(
|
|
|
|
model,
|
|
|
|
Some(nav_history),
|
|
|
|
self.settings.clone(),
|
|
|
|
cx,
|
|
|
|
))
|
2022-02-27 16:49:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn navigate(&mut self, data: Box<dyn Any>, cx: &mut ViewContext<Self>) {
|
|
|
|
self.results_editor
|
|
|
|
.update(cx, |editor, cx| editor.navigate(data, cx));
|
|
|
|
}
|
|
|
|
|
|
|
|
fn should_update_tab_on_event(event: &ViewEvent) -> bool {
|
|
|
|
matches!(event, ViewEvent::UpdateTab)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ProjectSearchView {
|
|
|
|
fn new(
|
|
|
|
model: ModelHandle<ProjectSearch>,
|
2022-02-28 10:10:22 +00:00
|
|
|
nav_history: Option<ItemNavHistory>,
|
2022-02-27 16:49:16 +00:00
|
|
|
settings: watch::Receiver<Settings>,
|
|
|
|
cx: &mut ViewContext<Self>,
|
|
|
|
) -> Self {
|
|
|
|
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-02-27 16:49:16 +00:00
|
|
|
let mut editor = Editor::single_line(
|
|
|
|
settings.clone(),
|
2022-02-27 15:15:38 +00:00
|
|
|
Some(|theme| theme.search.editor.input.clone()),
|
2022-02-27 14:47:46 +00:00
|
|
|
cx,
|
|
|
|
);
|
2022-02-27 16:49:16 +00:00
|
|
|
editor.set_text(query_text, cx);
|
2022-02-27 14:47:46 +00:00
|
|
|
editor
|
|
|
|
});
|
|
|
|
|
|
|
|
let results_editor = cx.add_view(|cx| {
|
2022-02-27 16:49:16 +00:00
|
|
|
let mut editor = Editor::for_buffer(excerpts, Some(project), settings.clone(), cx);
|
2022-02-27 14:47:46 +00:00
|
|
|
editor.set_searchable(false);
|
2022-02-28 10:10:22 +00:00
|
|
|
editor.set_nav_history(nav_history);
|
2022-02-27 14:47:46 +00:00
|
|
|
editor
|
|
|
|
});
|
2022-02-27 16:49:16 +00:00
|
|
|
cx.observe(&results_editor, |_, _, cx| cx.emit(ViewEvent::UpdateTab))
|
|
|
|
.detach();
|
2022-02-27 21:18:04 +00:00
|
|
|
cx.subscribe(&results_editor, |this, _, event, cx| {
|
|
|
|
if matches!(event, editor::Event::SelectionsChanged) {
|
|
|
|
this.update_match_index(cx);
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.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 16:49:16 +00:00
|
|
|
settings,
|
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-02-27 14:47:46 +00:00
|
|
|
fn deploy(workspace: &mut Workspace, _: &Deploy, cx: &mut ViewContext<Workspace>) {
|
2022-02-28 01:07:46 +00:00
|
|
|
// Clean up entries for dropped projects
|
|
|
|
cx.update_app_state(|state: &mut ActiveSearches, cx| {
|
|
|
|
state.0.retain(|project, _| project.is_upgradable(cx))
|
|
|
|
});
|
|
|
|
|
|
|
|
let active_search = cx
|
|
|
|
.app_state::<ActiveSearches>()
|
|
|
|
.0
|
|
|
|
.get(&workspace.project().downgrade());
|
|
|
|
|
|
|
|
let existing = active_search
|
|
|
|
.and_then(|active_search| {
|
|
|
|
workspace
|
|
|
|
.items_of_type::<ProjectSearch>(cx)
|
|
|
|
.find(|search| search == active_search)
|
|
|
|
})
|
|
|
|
.or_else(|| workspace.item_of_type::<ProjectSearch>(cx));
|
|
|
|
|
|
|
|
if let Some(existing) = existing {
|
2022-02-27 14:47:46 +00:00
|
|
|
workspace.activate_item(&existing, cx);
|
|
|
|
} else {
|
2022-02-27 15:15:38 +00:00
|
|
|
let model = cx.add_model(|cx| ProjectSearch::new(workspace.project().clone(), cx));
|
2022-02-27 14:47:46 +00:00
|
|
|
workspace.open_item(model, cx);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn search(&mut self, _: &Search, cx: &mut ViewContext<Self>) {
|
|
|
|
if let Some(query) = self.build_search_query(cx) {
|
|
|
|
self.model.update(cx, |model, cx| model.search(query, cx));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn search_in_new(workspace: &mut Workspace, _: &SearchInNew, cx: &mut ViewContext<Workspace>) {
|
2022-02-27 15:15:38 +00:00
|
|
|
if let Some(search_view) = workspace
|
2022-02-27 14:47:46 +00:00
|
|
|
.active_item(cx)
|
2022-02-27 15:15:38 +00:00
|
|
|
.and_then(|item| item.downcast::<ProjectSearchView>())
|
2022-02-27 14:47:46 +00:00
|
|
|
{
|
2022-02-27 15:15:38 +00:00
|
|
|
let new_query = search_view.update(cx, |search_view, cx| {
|
|
|
|
let new_query = search_view.build_search_query(cx);
|
2022-02-27 14:47:46 +00:00
|
|
|
if new_query.is_some() {
|
2022-02-27 15:15:38 +00:00
|
|
|
if let Some(old_query) = search_view.model.read(cx).active_query.clone() {
|
|
|
|
search_view.query_editor.update(cx, |editor, cx| {
|
2022-02-27 14:47:46 +00:00
|
|
|
editor.set_text(old_query.as_str(), cx);
|
|
|
|
});
|
2022-02-27 15:15:38 +00:00
|
|
|
search_view.regex = old_query.is_regex();
|
|
|
|
search_view.whole_word = old_query.whole_word();
|
|
|
|
search_view.case_sensitive = old_query.case_sensitive();
|
2022-02-27 14:47:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
new_query
|
|
|
|
});
|
|
|
|
if let Some(new_query) = new_query {
|
|
|
|
let model = cx.add_model(|cx| {
|
2022-02-27 15:15:38 +00:00
|
|
|
let mut model = ProjectSearch::new(workspace.project().clone(), cx);
|
2022-02-27 14:47:46 +00:00
|
|
|
model.search(new_query, cx);
|
|
|
|
model
|
|
|
|
});
|
|
|
|
workspace.open_item(model, 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,
|
|
|
|
))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn toggle_search_option(
|
|
|
|
&mut self,
|
|
|
|
ToggleSearchOption(option): &ToggleSearchOption,
|
|
|
|
cx: &mut ViewContext<Self>,
|
|
|
|
) {
|
|
|
|
let value = match option {
|
|
|
|
SearchOption::WholeWord => &mut self.whole_word,
|
|
|
|
SearchOption::CaseSensitive => &mut self.case_sensitive,
|
|
|
|
SearchOption::Regex => &mut self.regex,
|
|
|
|
};
|
|
|
|
*value = !*value;
|
|
|
|
self.search(&Search, cx);
|
|
|
|
cx.notify();
|
|
|
|
}
|
|
|
|
|
2022-02-27 21:18:04 +00:00
|
|
|
fn select_match(&mut self, &SelectMatch(direction): &SelectMatch, cx: &mut ViewContext<Self>) {
|
2022-02-28 09:34:11 +00:00
|
|
|
if let Some(index) = self.active_match_index {
|
|
|
|
let model = self.model.read(cx);
|
|
|
|
let results_editor = self.results_editor.read(cx);
|
|
|
|
let new_index = match_index_for_direction(
|
|
|
|
&model.match_ranges,
|
|
|
|
&results_editor.newest_anchor_selection().head(),
|
|
|
|
index,
|
|
|
|
direction,
|
|
|
|
&results_editor.buffer().read(cx).read(cx),
|
|
|
|
);
|
|
|
|
let range_to_select = model.match_ranges[new_index].clone();
|
2022-02-27 21:18:04 +00:00
|
|
|
self.results_editor.update(cx, |editor, cx| {
|
|
|
|
editor.select_ranges([range_to_select], Some(Autoscroll::Fit), cx);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-27 14:47:46 +00:00
|
|
|
fn toggle_focus(&mut self, _: &ToggleFocus, cx: &mut ViewContext<Self>) {
|
|
|
|
if self.query_editor.is_focused(cx) {
|
2022-02-27 21:18:04 +00:00
|
|
|
if !self.model.read(cx).match_ranges.is_empty() {
|
2022-02-27 14:47:46 +00:00
|
|
|
self.focus_results_editor(cx);
|
|
|
|
}
|
|
|
|
} else {
|
2022-02-28 01:07:46 +00:00
|
|
|
self.focus_query_editor(cx);
|
2022-02-27 14:47:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-27 23:15:06 +00:00
|
|
|
fn tab(&mut self, _: &editor::Tab, cx: &mut ViewContext<Self>) {
|
|
|
|
if self.query_editor.is_focused(cx) {
|
|
|
|
if !self.model.read(cx).match_ranges.is_empty() {
|
|
|
|
self.focus_results_editor(cx);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
cx.propagate_action()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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-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-02-27 21:18:04 +00:00
|
|
|
let cursor = query_editor.newest_anchor_selection().head();
|
|
|
|
query_editor.select_ranges([cursor.clone()..cursor], None, cx);
|
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 15:15:38 +00:00
|
|
|
let theme = &self.settings.borrow().theme.search;
|
2022-02-27 14:47:46 +00:00
|
|
|
self.results_editor.update(cx, |editor, cx| {
|
|
|
|
if reset_selections {
|
2022-02-27 21:24:06 +00:00
|
|
|
editor.select_ranges(match_ranges.first().cloned(), Some(Autoscroll::Fit), cx);
|
2022-02-27 14:47:46 +00:00
|
|
|
}
|
2022-02-27 21:24:06 +00:00
|
|
|
editor.highlight_ranges::<Self>(match_ranges, theme.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,
|
|
|
|
&results_editor.newest_anchor_selection().head(),
|
|
|
|
&results_editor.buffer().read(cx).read(cx),
|
|
|
|
);
|
|
|
|
if self.active_match_index != new_index {
|
|
|
|
self.active_match_index = new_index;
|
|
|
|
cx.notify();
|
2022-02-27 21:18:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-27 14:47:46 +00:00
|
|
|
fn render_query_editor(&self, cx: &mut RenderContext<Self>) -> ElementBox {
|
|
|
|
let theme = &self.settings.borrow().theme;
|
|
|
|
let editor_container = if self.query_contains_error {
|
2022-02-27 15:15:38 +00:00
|
|
|
theme.search.invalid_editor
|
2022-02-27 14:47:46 +00:00
|
|
|
} else {
|
2022-02-27 15:15:38 +00:00
|
|
|
theme.search.editor.input.container
|
2022-02-27 14:47:46 +00:00
|
|
|
};
|
|
|
|
Flex::row()
|
|
|
|
.with_child(
|
|
|
|
ChildView::new(&self.query_editor)
|
|
|
|
.contained()
|
|
|
|
.with_style(editor_container)
|
|
|
|
.aligned()
|
|
|
|
.constrained()
|
2022-02-27 15:15:38 +00:00
|
|
|
.with_max_width(theme.search.editor.max_width)
|
2022-02-27 14:47:46 +00:00
|
|
|
.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()
|
2022-02-27 15:15:38 +00:00
|
|
|
.with_style(theme.search.option_button_group)
|
2022-02-27 14:47:46 +00:00
|
|
|
.aligned()
|
|
|
|
.boxed(),
|
|
|
|
)
|
2022-02-27 21:18:04 +00:00
|
|
|
.with_children({
|
|
|
|
self.active_match_index.into_iter().flat_map(|match_ix| {
|
|
|
|
[
|
|
|
|
Flex::row()
|
|
|
|
.with_child(self.render_nav_button("<", Direction::Prev, cx))
|
|
|
|
.with_child(self.render_nav_button(">", Direction::Next, cx))
|
|
|
|
.aligned()
|
|
|
|
.boxed(),
|
|
|
|
Label::new(
|
|
|
|
format!(
|
|
|
|
"{}/{}",
|
|
|
|
match_ix + 1,
|
|
|
|
self.model.read(cx).match_ranges.len()
|
|
|
|
),
|
|
|
|
theme.search.match_index.text.clone(),
|
|
|
|
)
|
|
|
|
.contained()
|
|
|
|
.with_style(theme.search.match_index.container)
|
|
|
|
.aligned()
|
|
|
|
.boxed(),
|
|
|
|
]
|
|
|
|
})
|
|
|
|
})
|
2022-02-27 14:47:46 +00:00
|
|
|
.contained()
|
2022-02-27 15:15:38 +00:00
|
|
|
.with_style(theme.search.container)
|
2022-02-27 14:47:46 +00:00
|
|
|
.constrained()
|
|
|
|
.with_height(theme.workspace.toolbar.height)
|
2022-02-27 15:15:38 +00:00
|
|
|
.named("project search")
|
2022-02-27 14:47:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn render_option_button(
|
|
|
|
&self,
|
|
|
|
icon: &str,
|
|
|
|
option: SearchOption,
|
|
|
|
cx: &mut RenderContext<Self>,
|
|
|
|
) -> ElementBox {
|
2022-02-27 15:15:38 +00:00
|
|
|
let theme = &self.settings.borrow().theme.search;
|
2022-02-27 14:47:46 +00:00
|
|
|
let is_active = self.is_option_enabled(option);
|
|
|
|
MouseEventHandler::new::<Self, _, _>(option as usize, cx, |state, _| {
|
|
|
|
let style = match (is_active, state.hovered) {
|
|
|
|
(false, false) => &theme.option_button,
|
|
|
|
(false, true) => &theme.hovered_option_button,
|
|
|
|
(true, false) => &theme.active_option_button,
|
|
|
|
(true, true) => &theme.active_hovered_option_button,
|
|
|
|
};
|
|
|
|
Label::new(icon.to_string(), style.text.clone())
|
|
|
|
.contained()
|
|
|
|
.with_style(style.container)
|
|
|
|
.boxed()
|
|
|
|
})
|
|
|
|
.on_click(move |cx| cx.dispatch_action(ToggleSearchOption(option)))
|
|
|
|
.with_cursor_style(CursorStyle::PointingHand)
|
|
|
|
.boxed()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn is_option_enabled(&self, option: SearchOption) -> bool {
|
|
|
|
match option {
|
|
|
|
SearchOption::WholeWord => self.whole_word,
|
|
|
|
SearchOption::CaseSensitive => self.case_sensitive,
|
|
|
|
SearchOption::Regex => self.regex,
|
|
|
|
}
|
|
|
|
}
|
2022-02-27 21:18:04 +00:00
|
|
|
|
|
|
|
fn render_nav_button(
|
|
|
|
&self,
|
|
|
|
icon: &str,
|
|
|
|
direction: Direction,
|
|
|
|
cx: &mut RenderContext<Self>,
|
|
|
|
) -> ElementBox {
|
|
|
|
let theme = &self.settings.borrow().theme.search;
|
|
|
|
enum NavButton {}
|
|
|
|
MouseEventHandler::new::<NavButton, _, _>(direction as usize, cx, |state, _| {
|
|
|
|
let style = if state.hovered {
|
|
|
|
&theme.hovered_option_button
|
|
|
|
} else {
|
|
|
|
&theme.option_button
|
|
|
|
};
|
|
|
|
Label::new(icon.to_string(), style.text.clone())
|
|
|
|
.contained()
|
|
|
|
.with_style(style.container)
|
|
|
|
.boxed()
|
|
|
|
})
|
|
|
|
.on_click(move |cx| cx.dispatch_action(SelectMatch(direction)))
|
|
|
|
.with_cursor_style(CursorStyle::PointingHand)
|
|
|
|
.boxed()
|
|
|
|
}
|
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();
|
|
|
|
let mut theme = gpui::fonts::with_font_cache(fonts.clone(), || theme::Theme::default());
|
|
|
|
theme.search.match_background = Color::red();
|
|
|
|
let settings = Settings::new("Courier", &fonts, Arc::new(theme)).unwrap();
|
|
|
|
let settings = watch::channel_with(settings).1;
|
|
|
|
|
|
|
|
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-03-01 11:01:02 +00:00
|
|
|
let project = Project::test(fs.clone(), cx);
|
2022-02-28 10:10:22 +00:00
|
|
|
let (tree, _) = project
|
2022-03-01 11:01:02 +00:00
|
|
|
.update(cx, |project, cx| {
|
2022-03-02 19:04:48 +00:00
|
|
|
project.find_or_create_local_worktree("/dir", true, cx)
|
2022-02-28 10:10:22 +00:00
|
|
|
})
|
|
|
|
.await
|
|
|
|
.unwrap();
|
|
|
|
cx.read(|cx| tree.read(cx).as_local().unwrap().scan_complete())
|
|
|
|
.await;
|
|
|
|
|
|
|
|
let search = cx.add_model(|cx| ProjectSearch::new(project, cx));
|
|
|
|
let search_view = cx.add_view(Default::default(), |cx| {
|
|
|
|
ProjectSearchView::new(search.clone(), None, settings, cx)
|
|
|
|
});
|
|
|
|
|
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));
|
|
|
|
search_view.search(&Search, cx);
|
|
|
|
});
|
|
|
|
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
|
|
|
|
.update(cx, |editor, cx| editor.all_highlighted_ranges(cx)),
|
|
|
|
&[
|
|
|
|
(
|
|
|
|
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
|
|
|
|
.update(cx, |editor, cx| editor.selected_display_ranges(cx)),
|
|
|
|
[DisplayPoint::new(2, 32)..DisplayPoint::new(2, 35)]
|
|
|
|
);
|
|
|
|
|
|
|
|
search_view.select_match(&SelectMatch(Direction::Next), cx);
|
|
|
|
});
|
|
|
|
|
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!(
|
|
|
|
search_view
|
|
|
|
.results_editor
|
|
|
|
.update(cx, |editor, cx| editor.selected_display_ranges(cx)),
|
|
|
|
[DisplayPoint::new(2, 37)..DisplayPoint::new(2, 40)]
|
|
|
|
);
|
|
|
|
search_view.select_match(&SelectMatch(Direction::Next), cx);
|
|
|
|
});
|
|
|
|
|
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!(
|
|
|
|
search_view
|
|
|
|
.results_editor
|
|
|
|
.update(cx, |editor, cx| editor.selected_display_ranges(cx)),
|
|
|
|
[DisplayPoint::new(5, 6)..DisplayPoint::new(5, 9)]
|
|
|
|
);
|
|
|
|
search_view.select_match(&SelectMatch(Direction::Next), cx);
|
|
|
|
});
|
|
|
|
|
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!(
|
|
|
|
search_view
|
|
|
|
.results_editor
|
|
|
|
.update(cx, |editor, cx| editor.selected_display_ranges(cx)),
|
|
|
|
[DisplayPoint::new(2, 32)..DisplayPoint::new(2, 35)]
|
|
|
|
);
|
|
|
|
search_view.select_match(&SelectMatch(Direction::Prev), cx);
|
|
|
|
});
|
|
|
|
|
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!(
|
|
|
|
search_view
|
|
|
|
.results_editor
|
|
|
|
.update(cx, |editor, cx| editor.selected_display_ranges(cx)),
|
|
|
|
[DisplayPoint::new(5, 6)..DisplayPoint::new(5, 9)]
|
|
|
|
);
|
|
|
|
search_view.select_match(&SelectMatch(Direction::Prev), cx);
|
|
|
|
});
|
|
|
|
|
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!(
|
|
|
|
search_view
|
|
|
|
.results_editor
|
|
|
|
.update(cx, |editor, cx| editor.selected_display_ranges(cx)),
|
|
|
|
[DisplayPoint::new(2, 37)..DisplayPoint::new(2, 40)]
|
|
|
|
);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|