From dfd68d4cb8f24818ee850409806a18dc2fe2e57a Mon Sep 17 00:00:00 2001 From: Piotr Osiewicz <24362066+osiewicz@users.noreply.github.com> Date: Mon, 13 Nov 2023 20:38:37 +0100 Subject: [PATCH 01/69] WIP: start search2 --- Cargo.lock | 30 + Cargo.toml | 1 + crates/editor2/src/items.rs | 39 +- crates/search2/Cargo.toml | 40 + crates/search2/src/buffer_search.rs | 1797 +++++++++++++++++ crates/search2/src/history.rs | 184 ++ crates/search2/src/mode.rs | 65 + crates/search2/src/project_search.rs | 2661 ++++++++++++++++++++++++++ crates/search2/src/search.rs | 115 ++ crates/search2/src/search_bar.rs | 177 ++ crates/ui2/src/components/icon.rs | 4 + crates/workspace2/src/pane.rs | 2 +- crates/workspace2/src/searchable.rs | 23 +- crates/workspace2/src/toolbar.rs | 6 +- crates/workspace2/src/workspace2.rs | 2 +- crates/zed2/Cargo.toml | 2 +- crates/zed2/src/main.rs | 2 +- crates/zed2/src/zed2.rs | 8 +- 18 files changed, 5115 insertions(+), 43 deletions(-) create mode 100644 crates/search2/Cargo.toml create mode 100644 crates/search2/src/buffer_search.rs create mode 100644 crates/search2/src/history.rs create mode 100644 crates/search2/src/mode.rs create mode 100644 crates/search2/src/project_search.rs create mode 100644 crates/search2/src/search.rs create mode 100644 crates/search2/src/search_bar.rs diff --git a/Cargo.lock b/Cargo.lock index 49f37fb0429b0d2ddd44b32099e1a1544adc6fbc..2cae47a029f2e73f7b4108e50e5472df64eeef2b 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -7855,6 +7855,35 @@ dependencies = [ "workspace", ] +[[package]] +name = "search2" +version = "0.1.0" +dependencies = [ + "anyhow", + "bitflags 1.3.2", + "client2", + "collections", + "editor2", + "futures 0.3.28", + "gpui2", + "language2", + "log", + "menu2", + "postage", + "project2", + "serde", + "serde_derive", + "serde_json", + "settings2", + "smallvec", + "smol", + "theme2", + "ui2", + "unindent", + "util", + "workspace2", +] + [[package]] name = "security-framework" version = "2.9.2" @@ -11425,6 +11454,7 @@ dependencies = [ "rsa 0.4.0", "rust-embed", "schemars", + "search2", "serde", "serde_derive", "serde_json", diff --git a/Cargo.toml b/Cargo.toml index 905750f8352b02422fa0815b7a51e17d74b0daff..958db9b7bd1a12cf9d46e66fa888bddc63b71187 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -86,6 +86,7 @@ members = [ "crates/rpc", "crates/rpc2", "crates/search", + "crates/search2", "crates/settings", "crates/settings2", "crates/snippet", diff --git a/crates/editor2/src/items.rs b/crates/editor2/src/items.rs index 25e9f91608fc857ea30191f25bbfc75aac6117ef..6b396278b693807ea39e94e25584e8215f4c1541 100644 --- a/crates/editor2/src/items.rs +++ b/crates/editor2/src/items.rs @@ -906,17 +906,16 @@ impl SearchableItem for Editor { type Match = Range; fn clear_matches(&mut self, cx: &mut ViewContext) { - todo!() - // self.clear_background_highlights::(cx); + self.clear_background_highlights::(cx); } fn update_matches(&mut self, matches: Vec>, cx: &mut ViewContext) { - todo!() - // self.highlight_background::( - // matches, - // |theme| theme.search.match_background, - // cx, - // ); + dbg!(&matches); + self.highlight_background::( + matches, + |theme| theme.title_bar_background, // todo: update theme + cx, + ); } fn query_suggestion(&mut self, cx: &mut ViewContext) -> String { @@ -951,22 +950,20 @@ impl SearchableItem for Editor { matches: Vec>, cx: &mut ViewContext, ) { - todo!() - // self.unfold_ranges([matches[index].clone()], false, true, cx); - // let range = self.range_for_match(&matches[index]); - // self.change_selections(Some(Autoscroll::fit()), cx, |s| { - // s.select_ranges([range]); - // }) + self.unfold_ranges([matches[index].clone()], false, true, cx); + let range = self.range_for_match(&matches[index]); + self.change_selections(Some(Autoscroll::fit()), cx, |s| { + s.select_ranges([range]); + }) } fn select_matches(&mut self, matches: Vec, cx: &mut ViewContext) { - todo!() - // self.unfold_ranges(matches.clone(), false, false, cx); - // let mut ranges = Vec::new(); - // for m in &matches { - // ranges.push(self.range_for_match(&m)) - // } - // self.change_selections(None, cx, |s| s.select_ranges(ranges)); + self.unfold_ranges(matches.clone(), false, false, cx); + let mut ranges = Vec::new(); + for m in &matches { + ranges.push(self.range_for_match(&m)) + } + self.change_selections(None, cx, |s| s.select_ranges(ranges)); } fn replace( &mut self, diff --git a/crates/search2/Cargo.toml b/crates/search2/Cargo.toml new file mode 100644 index 0000000000000000000000000000000000000000..97cfdd6494099eb802a2fd629df1e74edada4232 --- /dev/null +++ b/crates/search2/Cargo.toml @@ -0,0 +1,40 @@ +[package] +name = "search2" +version = "0.1.0" +edition = "2021" +publish = false + +[lib] +path = "src/search.rs" +doctest = false + +[dependencies] +bitflags = "1" +collections = { path = "../collections" } +editor = { package = "editor2", path = "../editor2" } +gpui = { package = "gpui2", path = "../gpui2" } +language = { package = "language2", path = "../language2" } +menu = { package = "menu2", path = "../menu2" } +project = { package = "project2", path = "../project2" } +settings = { package = "settings2", path = "../settings2" } +theme = { package = "theme2", path = "../theme2" } +util = { path = "../util" } +ui = {package = "ui2", path = "../ui2"} +workspace = { package = "workspace2", path = "../workspace2" } +#semantic_index = { path = "../semantic_index" } +anyhow.workspace = true +futures.workspace = true +log.workspace = true +postage.workspace = true +serde.workspace = true +serde_derive.workspace = true +smallvec.workspace = true +smol.workspace = true +serde_json.workspace = true +[dev-dependencies] +client = { package = "client2", path = "../client2", features = ["test-support"] } +editor = { package = "editor2", path = "../editor2", features = ["test-support"] } +gpui = { package = "gpui2", path = "../gpui2", features = ["test-support"] } + +workspace = { package = "workspace2", path = "../workspace2", features = ["test-support"] } +unindent.workspace = true diff --git a/crates/search2/src/buffer_search.rs b/crates/search2/src/buffer_search.rs new file mode 100644 index 0000000000000000000000000000000000000000..fdd03f71c3beb42378592b0e99dfb6a4fb179924 --- /dev/null +++ b/crates/search2/src/buffer_search.rs @@ -0,0 +1,1797 @@ +use crate::{ + history::SearchHistory, + mode::{next_mode, SearchMode, Side}, + search_bar::{render_nav_button, render_search_mode_button}, + CycleMode, NextHistoryQuery, PreviousHistoryQuery, ReplaceAll, ReplaceNext, SearchOptions, + SelectAllMatches, SelectNextMatch, SelectPrevMatch, ToggleCaseSensitive, ToggleReplace, + ToggleWholeWord, +}; +use collections::HashMap; +use editor::Editor; +use futures::channel::oneshot; +use gpui::{ + action, actions, div, Action, AnyElement, AnyView, AppContext, Component, Div, Entity, + EventEmitter, ParentElement as _, Render, Subscription, Svg, Task, View, ViewContext, + VisualContext as _, WindowContext, +}; +use project::search::SearchQuery; +use serde::Deserialize; +use std::{any::Any, sync::Arc}; +use theme::ActiveTheme; + +use ui::{IconButton, Label}; +use util::ResultExt; +use workspace::{ + item::ItemHandle, + searchable::{Direction, SearchEvent, SearchableItemHandle, WeakSearchableItemHandle}, + Pane, ToolbarItemLocation, ToolbarItemView, Workspace, +}; + +#[action] +pub struct Deploy { + pub focus: bool, +} + +actions!(Dismiss, FocusEditor); + +pub enum Event { + UpdateLocation, +} + +pub fn init(cx: &mut AppContext) { + dbg!("Registered"); + cx.observe_new_views(|workspace: &mut Workspace, _| BufferSearchBar::register(workspace)) + .detach(); +} + +pub struct BufferSearchBar { + query_editor: View, + replacement_editor: View, + active_searchable_item: Option>, + active_match_index: Option, + active_searchable_item_subscription: Option, + active_search: Option>, + searchable_items_with_matches: + HashMap, Vec>>, + pending_search: Option>, + search_options: SearchOptions, + default_options: SearchOptions, + query_contains_error: bool, + dismissed: bool, + search_history: SearchHistory, + current_mode: SearchMode, + replace_enabled: bool, +} + +impl EventEmitter for BufferSearchBar {} +impl EventEmitter for BufferSearchBar {} +impl Render for BufferSearchBar { + // fn ui_name() -> &'static str { + // "BufferSearchBar" + // } + + // fn update_keymap_context( + // &self, + // keymap: &mut gpui::keymap_matcher::KeymapContext, + // cx: &AppContext, + // ) { + // Self::reset_to_default_keymap_context(keymap); + // let in_replace = self + // .replacement_editor + // .read_with(cx, |_, cx| cx.is_self_focused()) + // .unwrap_or(false); + // if in_replace { + // keymap.add_identifier("in_replace"); + // } + // } + + // fn focus_in(&mut self, _: View, cx: &mut ViewContext) { + // cx.focus(&self.query_editor); + // } + type Element = Div; + fn render(&mut self, cx: &mut ViewContext) -> Self::Element { + let theme = cx.theme().clone(); + // let query_container_style = if self.query_contains_error { + // theme.search.invalid_editor + // } else { + // theme.search.editor.input.container + // }; + let supported_options = self + .active_searchable_item + .as_ref() + .map(|active_searchable_item| active_searchable_item.supported_options()) + .unwrap_or_default(); + + // let previous_query_keystrokes = + // cx.binding_for_action(&PreviousHistoryQuery {}) + // .map(|binding| { + // binding + // .keystrokes() + // .iter() + // .map(|k| k.to_string()) + // .collect::>() + // }); + // let next_query_keystrokes = cx.binding_for_action(&NextHistoryQuery {}).map(|binding| { + // binding + // .keystrokes() + // .iter() + // .map(|k| k.to_string()) + // .collect::>() + // }); + // let new_placeholder_text = match (previous_query_keystrokes, next_query_keystrokes) { + // (Some(previous_query_keystrokes), Some(next_query_keystrokes)) => { + // format!( + // "Search ({}/{} for previous/next query)", + // previous_query_keystrokes.join(" "), + // next_query_keystrokes.join(" ") + // ) + // } + // (None, Some(next_query_keystrokes)) => { + // format!( + // "Search ({} for next query)", + // next_query_keystrokes.join(" ") + // ) + // } + // (Some(previous_query_keystrokes), None) => { + // format!( + // "Search ({} for previous query)", + // previous_query_keystrokes.join(" ") + // ) + // } + // (None, None) => String::new(), + // }; + let new_placeholder_text = Arc::from("Fix this up!"); + self.query_editor.update(cx, |editor, cx| { + editor.set_placeholder_text(new_placeholder_text, cx); + }); + self.replacement_editor.update(cx, |editor, cx| { + editor.set_placeholder_text("Replace with...", cx); + }); + div() + .child(self.query_editor.clone()) + .child(self.replacement_editor.clone()) + // let search_button_for_mode = |mode, side, cx: &mut ViewContext| { + // let is_active = self.current_mode == mode; + + // render_search_mode_button( + // mode, + // side, + // is_active, + // move |_, this, cx| { + // this.activate_search_mode(mode, cx); + // }, + // cx, + // ) + // }; + // let search_option_button = |option| { + // let is_active = self.search_options.contains(option); + // option.as_button(is_active) + // }; + // let match_count = self + // .active_searchable_item + // .as_ref() + // .and_then(|searchable_item| { + // if self.query(cx).is_empty() { + // return None; + // } + // let matches = self + // .searchable_items_with_matches + // .get(&searchable_item.downgrade())?; + // let message = if let Some(match_ix) = self.active_match_index { + // format!("{}/{}", match_ix + 1, matches.len()) + // } else { + // "No matches".to_string() + // }; + + // Some( + // Label::new(message) + // .contained() + // .with_style(theme.search.match_index.container) + // .aligned(), + // ) + // }); + // let nav_button_for_direction = |label, direction, cx: &mut ViewContext| { + // render_nav_button( + // label, + // direction, + // self.active_match_index.is_some(), + // move |_, this, cx| match direction { + // Direction::Prev => this.select_prev_match(&Default::default(), cx), + // Direction::Next => this.select_next_match(&Default::default(), cx), + // }, + // cx, + // ) + // }; + // let query_column = Flex::row() + // .with_child( + // Svg::for_style(theme.search.editor_icon.clone().icon) + // .contained() + // .with_style(theme.search.editor_icon.clone().container), + // ) + // .with_child(ChildView::new(&self.query_editor, cx).flex(1., true)) + // .with_child( + // Flex::row() + // .with_children( + // supported_options + // .case + // .then(|| search_option_button(SearchOptions::CASE_SENSITIVE)), + // ) + // .with_children( + // supported_options + // .word + // .then(|| search_option_button(SearchOptions::WHOLE_WORD)), + // ) + // .flex_float() + // .contained(), + // ) + // .align_children_center() + // .contained() + // .with_style(query_container_style) + // .constrained() + // .with_min_width(theme.search.editor.min_width) + // .with_max_width(theme.search.editor.max_width) + // .with_height(theme.search.search_bar_row_height) + // .flex(1., false); + // let should_show_replace_input = self.replace_enabled && supported_options.replacement; + + // let replacement = should_show_replace_input.then(|| { + // div() + // .child( + // Svg::for_style(theme.search.replace_icon.clone().icon) + // .contained() + // .with_style(theme.search.replace_icon.clone().container), + // ) + // .child(self.replacement_editor) + // .align_children_center() + // .flex(1., true) + // .contained() + // .with_style(query_container_style) + // .constrained() + // .with_min_width(theme.search.editor.min_width) + // .with_max_width(theme.search.editor.max_width) + // .with_height(theme.search.search_bar_row_height) + // .flex(1., false) + // }); + // let replace_all = + // should_show_replace_input.then(|| super::replace_action(ReplaceAll, "Replace all")); + // let replace_next = + // should_show_replace_input.then(|| super::replace_action(ReplaceNext, "Replace next")); + // let switches_column = supported_options.replacement.then(|| { + // Flex::row() + // .align_children_center() + // .with_child(super::toggle_replace_button(self.replace_enabled)) + // .constrained() + // .with_height(theme.search.search_bar_row_height) + // .contained() + // .with_style(theme.search.option_button_group) + // }); + // let mode_column = div() + // .child(search_button_for_mode( + // SearchMode::Text, + // Some(Side::Left), + // cx, + // )) + // .child(search_button_for_mode( + // SearchMode::Regex, + // Some(Side::Right), + // cx, + // )) + // .contained() + // .with_style(theme.search.modes_container) + // .constrained() + // .with_height(theme.search.search_bar_row_height); + + // let nav_column = div() + // .align_children_center() + // .with_children(replace_next) + // .with_children(replace_all) + // .with_child(self.render_action_button("icons/select-all.svg", cx)) + // .with_child(div().children(match_count)) + // .with_child(nav_button_for_direction("<", Direction::Prev, cx)) + // .with_child(nav_button_for_direction(">", Direction::Next, cx)) + // .constrained() + // .with_height(theme.search.search_bar_row_height) + // .flex_float(); + + // div() + // .child(query_column) + // .child(mode_column) + // .children(switches_column) + // .children(replacement) + // .child(nav_column) + // .contained() + // .with_style(theme.search.container) + // .into_any_named("search bar") + } +} + +impl ToolbarItemView for BufferSearchBar { + fn set_active_pane_item( + &mut self, + item: Option<&dyn ItemHandle>, + cx: &mut ViewContext, + ) -> ToolbarItemLocation { + cx.notify(); + self.active_searchable_item_subscription.take(); + self.active_searchable_item.take(); + dbg!("Take?"); + self.pending_search.take(); + + if let Some(searchable_item_handle) = + item.and_then(|item| item.to_searchable_item_handle(cx)) + { + let this = cx.view().downgrade(); + self.active_searchable_item_subscription = + Some(searchable_item_handle.subscribe_to_search_events( + cx, + Box::new(move |search_event, cx| { + if let Some(this) = this.upgrade() { + this.update(cx, |this, cx| { + this.on_active_searchable_item_event(search_event, cx) + }); + } + }), + )); + + self.active_searchable_item = Some(searchable_item_handle); + let _ = self.update_matches(cx); + if !self.dismissed { + return ToolbarItemLocation::Secondary; + } + } + + ToolbarItemLocation::Hidden + } + + fn row_count(&self, _: &WindowContext<'_>) -> usize { + 1 + } +} + +impl BufferSearchBar { + pub fn register(workspace: &mut Workspace) { + workspace.register_action(|workspace, a: &Deploy, cx| { + dbg!("Setting"); + workspace.active_pane().update(cx, |this, cx| { + this.toolbar().update(cx, |this, cx| { + let view = cx.build_view(|cx| BufferSearchBar::new(cx)); + this.add_item(view.clone(), cx); + view.update(cx, |this, cx| this.deploy(a, cx)); + cx.notify(); + }) + }); + }); + } + pub fn new(cx: &mut ViewContext) -> Self { + dbg!("New"); + let query_editor = cx.build_view(|cx| Editor::single_line(cx)); + cx.subscribe(&query_editor, Self::on_query_editor_event) + .detach(); + let replacement_editor = cx.build_view(|cx| Editor::single_line(cx)); + cx.subscribe(&replacement_editor, Self::on_query_editor_event) + .detach(); + Self { + query_editor, + replacement_editor, + active_searchable_item: None, + active_searchable_item_subscription: None, + active_match_index: None, + searchable_items_with_matches: Default::default(), + default_options: SearchOptions::NONE, + search_options: SearchOptions::NONE, + pending_search: None, + query_contains_error: false, + dismissed: true, + search_history: SearchHistory::default(), + current_mode: SearchMode::default(), + active_search: None, + replace_enabled: false, + } + } + + pub fn is_dismissed(&self) -> bool { + self.dismissed + } + + pub fn dismiss(&mut self, _: &Dismiss, cx: &mut ViewContext) { + self.dismissed = true; + dbg!("Dismissed :("); + for searchable_item in self.searchable_items_with_matches.keys() { + if let Some(searchable_item) = + WeakSearchableItemHandle::upgrade(searchable_item.as_ref(), cx) + { + searchable_item.clear_matches(cx); + } + } + if let Some(active_editor) = self.active_searchable_item.as_ref() { + let handle = active_editor.focus_handle(cx); + cx.focus(&handle); + } + cx.emit(Event::UpdateLocation); + cx.notify(); + } + + pub fn deploy(&mut self, deploy: &Deploy, cx: &mut ViewContext) -> bool { + if self.show(cx) { + self.search_suggested(cx); + if deploy.focus { + self.select_query(cx); + let handle = cx.focus_handle(); + cx.focus(&handle); + } + return true; + } + + false + } + + pub fn show(&mut self, cx: &mut ViewContext) -> bool { + if self.active_searchable_item.is_none() { + dbg!("Hey"); + return false; + } + dbg!("not dismissed"); + self.dismissed = false; + cx.notify(); + cx.emit(Event::UpdateLocation); + true + } + + pub fn search_suggested(&mut self, cx: &mut ViewContext) { + let search = self + .query_suggestion(cx) + .map(|suggestion| self.search(&suggestion, Some(self.default_options), cx)); + + if let Some(search) = search { + cx.spawn(|this, mut cx| async move { + search.await?; + this.update(&mut cx, |this, cx| this.activate_current_match(cx)) + }) + .detach_and_log_err(cx); + } + } + + pub fn activate_current_match(&mut self, cx: &mut ViewContext) { + if let Some(match_ix) = self.active_match_index { + if let Some(active_searchable_item) = self.active_searchable_item.as_ref() { + if let Some(matches) = self + .searchable_items_with_matches + .get(&active_searchable_item.downgrade()) + { + active_searchable_item.activate_match(match_ix, matches, cx) + } + } + } + } + + pub fn select_query(&mut self, cx: &mut ViewContext) { + self.query_editor.update(cx, |query_editor, cx| { + query_editor.select_all(&Default::default(), cx); + }); + } + + pub fn query(&self, cx: &WindowContext) -> String { + self.query_editor.read(cx).text(cx) + } + pub fn replacement(&self, cx: &WindowContext) -> String { + self.replacement_editor.read(cx).text(cx) + } + pub fn query_suggestion(&mut self, cx: &mut ViewContext) -> Option { + self.active_searchable_item + .as_ref() + .map(|searchable_item| searchable_item.query_suggestion(cx)) + .filter(|suggestion| !suggestion.is_empty()) + } + + pub fn set_replacement(&mut self, replacement: Option<&str>, cx: &mut ViewContext) { + if replacement.is_none() { + self.replace_enabled = false; + return; + } + self.replace_enabled = true; + self.replacement_editor + .update(cx, |replacement_editor, cx| { + replacement_editor + .buffer() + .update(cx, |replacement_buffer, cx| { + let len = replacement_buffer.len(cx); + replacement_buffer.edit([(0..len, replacement.unwrap())], None, cx); + }); + }); + } + + pub fn search( + &mut self, + query: &str, + options: Option, + cx: &mut ViewContext, + ) -> oneshot::Receiver<()> { + let options = options.unwrap_or(self.default_options); + if query != self.query(cx) || self.search_options != options { + self.query_editor.update(cx, |query_editor, cx| { + query_editor.buffer().update(cx, |query_buffer, cx| { + let len = query_buffer.len(cx); + query_buffer.edit([(0..len, query)], None, cx); + }); + }); + self.search_options = options; + self.query_contains_error = false; + self.clear_matches(cx); + cx.notify(); + } + self.update_matches(cx) + } + + fn render_action_button( + &self, + icon: &'static str, + cx: &mut ViewContext, + ) -> impl Component { + let tooltip = "Select All Matches"; + let theme = cx.theme(); + // let tooltip_style = theme.tooltip.clone(); + + // let style = theme.search.action_button.clone(); + + IconButton::new(0, ui::Icon::SelectAll) + .on_click(|_: &mut Self, cx| cx.dispatch_action(Box::new(SelectAllMatches))) + } + + pub fn activate_search_mode(&mut self, mode: SearchMode, cx: &mut ViewContext) { + assert_ne!( + mode, + SearchMode::Semantic, + "Semantic search is not supported in buffer search" + ); + if mode == self.current_mode { + return; + } + self.current_mode = mode; + let _ = self.update_matches(cx); + cx.notify(); + } + + fn deploy_bar(pane: &mut Pane, action: &Deploy, cx: &mut ViewContext) { + let mut propagate_action = true; + if let Some(search_bar) = pane.toolbar().read(cx).item_of_type::() { + search_bar.update(cx, |search_bar, cx| { + if search_bar.deploy(action, cx) { + propagate_action = false; + } + }); + } + if !propagate_action { + cx.stop_propagation(); + } + } + + fn handle_editor_cancel(pane: &mut Pane, _: &editor::Cancel, cx: &mut ViewContext) { + if let Some(search_bar) = pane.toolbar().read(cx).item_of_type::() { + if !search_bar.read(cx).dismissed { + search_bar.update(cx, |search_bar, cx| search_bar.dismiss(&Dismiss, cx)); + cx.stop_propagation(); + return; + } + } + } + + pub fn focus_editor(&mut self, _: &FocusEditor, cx: &mut ViewContext) { + if let Some(active_editor) = self.active_searchable_item.as_ref() { + let handle = active_editor.focus_handle(cx); + cx.focus(&handle); + } + } + + fn toggle_search_option(&mut self, search_option: SearchOptions, cx: &mut ViewContext) { + self.search_options.toggle(search_option); + self.default_options = self.search_options; + let _ = self.update_matches(cx); + cx.notify(); + } + + pub fn set_search_options( + &mut self, + search_options: SearchOptions, + cx: &mut ViewContext, + ) { + self.search_options = search_options; + cx.notify(); + } + + fn select_next_match(&mut self, _: &SelectNextMatch, cx: &mut ViewContext) { + self.select_match(Direction::Next, 1, cx); + } + + fn select_prev_match(&mut self, _: &SelectPrevMatch, cx: &mut ViewContext) { + self.select_match(Direction::Prev, 1, cx); + } + + fn select_all_matches(&mut self, _: &SelectAllMatches, cx: &mut ViewContext) { + if !self.dismissed && self.active_match_index.is_some() { + if let Some(searchable_item) = self.active_searchable_item.as_ref() { + if let Some(matches) = self + .searchable_items_with_matches + .get(&searchable_item.downgrade()) + { + searchable_item.select_matches(matches, cx); + self.focus_editor(&FocusEditor, cx); + } + } + } + } + + pub fn select_match(&mut self, direction: Direction, count: usize, cx: &mut ViewContext) { + if let Some(index) = self.active_match_index { + if let Some(searchable_item) = self.active_searchable_item.as_ref() { + if let Some(matches) = self + .searchable_items_with_matches + .get(&searchable_item.downgrade()) + { + let new_match_index = searchable_item + .match_index_for_direction(matches, index, direction, count, cx); + searchable_item.update_matches(matches, cx); + searchable_item.activate_match(new_match_index, matches, cx); + } + } + } + } + + pub fn select_last_match(&mut self, cx: &mut ViewContext) { + if let Some(searchable_item) = self.active_searchable_item.as_ref() { + if let Some(matches) = self + .searchable_items_with_matches + .get(&searchable_item.downgrade()) + { + if matches.len() == 0 { + return; + } + let new_match_index = matches.len() - 1; + searchable_item.update_matches(matches, cx); + searchable_item.activate_match(new_match_index, matches, cx); + } + } + } + + fn select_next_match_on_pane( + pane: &mut Pane, + action: &SelectNextMatch, + cx: &mut ViewContext, + ) { + if let Some(search_bar) = pane.toolbar().read(cx).item_of_type::() { + search_bar.update(cx, |bar, cx| bar.select_next_match(action, cx)); + } + } + + fn select_prev_match_on_pane( + pane: &mut Pane, + action: &SelectPrevMatch, + cx: &mut ViewContext, + ) { + if let Some(search_bar) = pane.toolbar().read(cx).item_of_type::() { + search_bar.update(cx, |bar, cx| bar.select_prev_match(action, cx)); + } + } + + fn select_all_matches_on_pane( + pane: &mut Pane, + action: &SelectAllMatches, + cx: &mut ViewContext, + ) { + if let Some(search_bar) = pane.toolbar().read(cx).item_of_type::() { + search_bar.update(cx, |bar, cx| bar.select_all_matches(action, cx)); + } + } + + fn on_query_editor_event( + &mut self, + _: View, + event: &editor::Event, + cx: &mut ViewContext, + ) { + if let editor::Event::Edited { .. } = event { + self.query_contains_error = false; + self.clear_matches(cx); + let search = self.update_matches(cx); + cx.spawn(|this, mut cx| async move { + search.await?; + this.update(&mut cx, |this, cx| this.activate_current_match(cx)) + }) + .detach_and_log_err(cx); + } + } + + fn on_active_searchable_item_event(&mut self, event: &SearchEvent, cx: &mut ViewContext) { + match event { + SearchEvent::MatchesInvalidated => { + let _ = self.update_matches(cx); + } + SearchEvent::ActiveMatchChanged => self.update_match_index(cx), + } + } + + fn clear_matches(&mut self, cx: &mut ViewContext) { + let mut active_item_matches = None; + for (searchable_item, matches) in self.searchable_items_with_matches.drain() { + if let Some(searchable_item) = + WeakSearchableItemHandle::upgrade(searchable_item.as_ref(), cx) + { + if Some(&searchable_item) == self.active_searchable_item.as_ref() { + active_item_matches = Some((searchable_item.downgrade(), matches)); + } else { + searchable_item.clear_matches(cx); + } + } + } + + self.searchable_items_with_matches + .extend(active_item_matches); + } + + fn update_matches(&mut self, cx: &mut ViewContext) -> oneshot::Receiver<()> { + let (done_tx, done_rx) = oneshot::channel(); + let query = self.query(cx); + self.pending_search.take(); + dbg!("update_matches"); + if let Some(active_searchable_item) = self.active_searchable_item.as_ref() { + if query.is_empty() { + self.active_match_index.take(); + active_searchable_item.clear_matches(cx); + let _ = done_tx.send(()); + cx.notify(); + } else { + let query: Arc<_> = if self.current_mode == SearchMode::Regex { + match SearchQuery::regex( + query, + self.search_options.contains(SearchOptions::WHOLE_WORD), + self.search_options.contains(SearchOptions::CASE_SENSITIVE), + Vec::new(), + Vec::new(), + ) { + Ok(query) => query.with_replacement(self.replacement(cx)), + Err(_) => { + self.query_contains_error = true; + cx.notify(); + return done_rx; + } + } + } else { + match SearchQuery::text( + query, + self.search_options.contains(SearchOptions::WHOLE_WORD), + self.search_options.contains(SearchOptions::CASE_SENSITIVE), + Vec::new(), + Vec::new(), + ) { + Ok(query) => query.with_replacement(self.replacement(cx)), + Err(_) => { + self.query_contains_error = true; + cx.notify(); + return done_rx; + } + } + } + .into(); + self.active_search = Some(query.clone()); + let query_text = query.as_str().to_string(); + dbg!(&query_text); + let matches = active_searchable_item.find_matches(query, cx); + + let active_searchable_item = active_searchable_item.downgrade(); + self.pending_search = Some(cx.spawn(|this, mut cx| async move { + let matches = matches.await; + //dbg!(&matches); + this.update(&mut cx, |this, cx| { + dbg!("Updating!!"); + if let Some(active_searchable_item) = + WeakSearchableItemHandle::upgrade(active_searchable_item.as_ref(), cx) + { + dbg!("in if!!"); + this.searchable_items_with_matches + .insert(active_searchable_item.downgrade(), matches); + + this.update_match_index(cx); + this.search_history.add(query_text); + if !this.dismissed { + dbg!("Not dismissed"); + let matches = this + .searchable_items_with_matches + .get(&active_searchable_item.downgrade()) + .unwrap(); + active_searchable_item.update_matches(matches, cx); + let _ = done_tx.send(()); + } + cx.notify(); + } + }) + .log_err(); + })); + } + } + done_rx + } + + fn update_match_index(&mut self, cx: &mut ViewContext) { + let new_index = self + .active_searchable_item + .as_ref() + .and_then(|searchable_item| { + let matches = self + .searchable_items_with_matches + .get(&searchable_item.downgrade())?; + searchable_item.active_match_index(matches, cx) + }); + if new_index != self.active_match_index { + self.active_match_index = new_index; + cx.notify(); + } + } + + fn next_history_query(&mut self, _: &NextHistoryQuery, cx: &mut ViewContext) { + if let Some(new_query) = self.search_history.next().map(str::to_string) { + let _ = self.search(&new_query, Some(self.search_options), cx); + } else { + self.search_history.reset_selection(); + let _ = self.search("", Some(self.search_options), cx); + } + } + + fn previous_history_query(&mut self, _: &PreviousHistoryQuery, cx: &mut ViewContext) { + if self.query(cx).is_empty() { + if let Some(new_query) = self.search_history.current().map(str::to_string) { + let _ = self.search(&new_query, Some(self.search_options), cx); + return; + } + } + + if let Some(new_query) = self.search_history.previous().map(str::to_string) { + let _ = self.search(&new_query, Some(self.search_options), cx); + } + } + fn cycle_mode(&mut self, _: &CycleMode, cx: &mut ViewContext) { + self.activate_search_mode(next_mode(&self.current_mode, false), cx); + } + fn cycle_mode_on_pane(pane: &mut Pane, action: &CycleMode, cx: &mut ViewContext) { + let mut should_propagate = true; + if let Some(search_bar) = pane.toolbar().read(cx).item_of_type::() { + search_bar.update(cx, |bar, cx| { + if bar.show(cx) { + should_propagate = false; + bar.cycle_mode(action, cx); + false + } else { + true + } + }); + } + if !should_propagate { + cx.stop_propagation(); + } + } + fn toggle_replace(&mut self, _: &ToggleReplace, cx: &mut ViewContext) { + if let Some(_) = &self.active_searchable_item { + self.replace_enabled = !self.replace_enabled; + if !self.replace_enabled { + let handle = self.query_editor.focus_handle(cx); + cx.focus(&handle); + } + cx.notify(); + } + } + fn toggle_replace_on_a_pane(pane: &mut Pane, _: &ToggleReplace, cx: &mut ViewContext) { + let mut should_propagate = true; + if let Some(search_bar) = pane.toolbar().read(cx).item_of_type::() { + search_bar.update(cx, |bar, cx| { + if let Some(_) = &bar.active_searchable_item { + should_propagate = false; + bar.replace_enabled = !bar.replace_enabled; + if bar.dismissed { + bar.show(cx); + } + if !bar.replace_enabled { + let handle = bar.query_editor.focus_handle(cx); + cx.focus(&handle); + } + cx.notify(); + } + }); + } + if !should_propagate { + cx.stop_propagation(); + } + } + fn replace_next(&mut self, _: &ReplaceNext, cx: &mut ViewContext) { + let mut should_propagate = true; + if !self.dismissed && self.active_search.is_some() { + if let Some(searchable_item) = self.active_searchable_item.as_ref() { + if let Some(query) = self.active_search.as_ref() { + if let Some(matches) = self + .searchable_items_with_matches + .get(&searchable_item.downgrade()) + { + if let Some(active_index) = self.active_match_index { + let query = query + .as_ref() + .clone() + .with_replacement(self.replacement(cx)); + searchable_item.replace(&matches[active_index], &query, cx); + self.select_next_match(&SelectNextMatch, cx); + } + should_propagate = false; + self.focus_editor(&FocusEditor, cx); + } + } + } + } + if !should_propagate { + cx.stop_propagation(); + } + } + pub fn replace_all(&mut self, _: &ReplaceAll, cx: &mut ViewContext) { + if !self.dismissed && self.active_search.is_some() { + if let Some(searchable_item) = self.active_searchable_item.as_ref() { + if let Some(query) = self.active_search.as_ref() { + if let Some(matches) = self + .searchable_items_with_matches + .get(&searchable_item.downgrade()) + { + let query = query + .as_ref() + .clone() + .with_replacement(self.replacement(cx)); + for m in matches { + searchable_item.replace(m, &query, cx); + } + } + } + } + } + } + fn replace_next_on_pane(pane: &mut Pane, action: &ReplaceNext, cx: &mut ViewContext) { + if let Some(search_bar) = pane.toolbar().read(cx).item_of_type::() { + search_bar.update(cx, |bar, cx| bar.replace_next(action, cx)); + cx.stop_propagation(); + return; + } + } + fn replace_all_on_pane(pane: &mut Pane, action: &ReplaceAll, cx: &mut ViewContext) { + if let Some(search_bar) = pane.toolbar().read(cx).item_of_type::() { + search_bar.update(cx, |bar, cx| bar.replace_all(action, cx)); + cx.stop_propagation(); + return; + } + } +} + +#[cfg(test)] +mod tests { + use super::*; + use editor::{DisplayPoint, Editor}; + use gpui::{color::Color, test::EmptyView, TestAppContext}; + use language::Buffer; + use unindent::Unindent as _; + + fn init_test(cx: &mut TestAppContext) -> (ViewHandle, ViewHandle) { + crate::project_search::tests::init_test(cx); + + let buffer = cx.add_model(|cx| { + Buffer::new( + 0, + cx.model_id() as u64, + r#" + A regular expression (shortened as regex or regexp;[1] also referred to as + rational expression[2][3]) is a sequence of characters that specifies a search + pattern in text. Usually such patterns are used by string-searching algorithms + for "find" or "find and replace" operations on strings, or for input validation. + "# + .unindent(), + ) + }); + let window = cx.add_window(|_| EmptyView); + let editor = window.add_view(cx, |cx| Editor::for_buffer(buffer.clone(), None, cx)); + + let search_bar = window.add_view(cx, |cx| { + let mut search_bar = BufferSearchBar::new(cx); + search_bar.set_active_pane_item(Some(&editor), cx); + search_bar.show(cx); + search_bar + }); + + (editor, search_bar) + } + + #[gpui::test] + async fn test_search_simple(cx: &mut TestAppContext) { + let (editor, search_bar) = init_test(cx); + + // Search for a string that appears with different casing. + // By default, search is case-insensitive. + search_bar + .update(cx, |search_bar, cx| search_bar.search("us", None, cx)) + .await + .unwrap(); + editor.update(cx, |editor, cx| { + assert_eq!( + editor.all_text_background_highlights(cx), + &[ + ( + DisplayPoint::new(2, 17)..DisplayPoint::new(2, 19), + Color::red(), + ), + ( + DisplayPoint::new(2, 43)..DisplayPoint::new(2, 45), + Color::red(), + ), + ] + ); + }); + + // Switch to a case sensitive search. + search_bar.update(cx, |search_bar, cx| { + search_bar.toggle_search_option(SearchOptions::CASE_SENSITIVE, cx); + }); + editor.next_notification(cx).await; + editor.update(cx, |editor, cx| { + assert_eq!( + editor.all_text_background_highlights(cx), + &[( + DisplayPoint::new(2, 43)..DisplayPoint::new(2, 45), + Color::red(), + )] + ); + }); + + // Search for a string that appears both as a whole word and + // within other words. By default, all results are found. + search_bar + .update(cx, |search_bar, cx| search_bar.search("or", None, cx)) + .await + .unwrap(); + editor.update(cx, |editor, cx| { + assert_eq!( + editor.all_text_background_highlights(cx), + &[ + ( + DisplayPoint::new(0, 24)..DisplayPoint::new(0, 26), + Color::red(), + ), + ( + DisplayPoint::new(0, 41)..DisplayPoint::new(0, 43), + Color::red(), + ), + ( + DisplayPoint::new(2, 71)..DisplayPoint::new(2, 73), + Color::red(), + ), + ( + DisplayPoint::new(3, 1)..DisplayPoint::new(3, 3), + Color::red(), + ), + ( + DisplayPoint::new(3, 11)..DisplayPoint::new(3, 13), + Color::red(), + ), + ( + DisplayPoint::new(3, 56)..DisplayPoint::new(3, 58), + Color::red(), + ), + ( + DisplayPoint::new(3, 60)..DisplayPoint::new(3, 62), + Color::red(), + ), + ] + ); + }); + + // Switch to a whole word search. + search_bar.update(cx, |search_bar, cx| { + search_bar.toggle_search_option(SearchOptions::WHOLE_WORD, cx); + }); + editor.next_notification(cx).await; + editor.update(cx, |editor, cx| { + assert_eq!( + editor.all_text_background_highlights(cx), + &[ + ( + DisplayPoint::new(0, 41)..DisplayPoint::new(0, 43), + Color::red(), + ), + ( + DisplayPoint::new(3, 11)..DisplayPoint::new(3, 13), + Color::red(), + ), + ( + DisplayPoint::new(3, 56)..DisplayPoint::new(3, 58), + Color::red(), + ), + ] + ); + }); + + editor.update(cx, |editor, cx| { + editor.change_selections(None, cx, |s| { + s.select_display_ranges([DisplayPoint::new(0, 0)..DisplayPoint::new(0, 0)]) + }); + }); + search_bar.update(cx, |search_bar, cx| { + assert_eq!(search_bar.active_match_index, Some(0)); + search_bar.select_next_match(&SelectNextMatch, cx); + assert_eq!( + editor.update(cx, |editor, cx| editor.selections.display_ranges(cx)), + [DisplayPoint::new(0, 41)..DisplayPoint::new(0, 43)] + ); + }); + search_bar.read_with(cx, |search_bar, _| { + assert_eq!(search_bar.active_match_index, Some(0)); + }); + + search_bar.update(cx, |search_bar, cx| { + search_bar.select_next_match(&SelectNextMatch, cx); + assert_eq!( + editor.update(cx, |editor, cx| editor.selections.display_ranges(cx)), + [DisplayPoint::new(3, 11)..DisplayPoint::new(3, 13)] + ); + }); + search_bar.read_with(cx, |search_bar, _| { + assert_eq!(search_bar.active_match_index, Some(1)); + }); + + search_bar.update(cx, |search_bar, cx| { + search_bar.select_next_match(&SelectNextMatch, cx); + assert_eq!( + editor.update(cx, |editor, cx| editor.selections.display_ranges(cx)), + [DisplayPoint::new(3, 56)..DisplayPoint::new(3, 58)] + ); + }); + search_bar.read_with(cx, |search_bar, _| { + assert_eq!(search_bar.active_match_index, Some(2)); + }); + + search_bar.update(cx, |search_bar, cx| { + search_bar.select_next_match(&SelectNextMatch, cx); + assert_eq!( + editor.update(cx, |editor, cx| editor.selections.display_ranges(cx)), + [DisplayPoint::new(0, 41)..DisplayPoint::new(0, 43)] + ); + }); + search_bar.read_with(cx, |search_bar, _| { + assert_eq!(search_bar.active_match_index, Some(0)); + }); + + search_bar.update(cx, |search_bar, cx| { + search_bar.select_prev_match(&SelectPrevMatch, cx); + assert_eq!( + editor.update(cx, |editor, cx| editor.selections.display_ranges(cx)), + [DisplayPoint::new(3, 56)..DisplayPoint::new(3, 58)] + ); + }); + search_bar.read_with(cx, |search_bar, _| { + assert_eq!(search_bar.active_match_index, Some(2)); + }); + + search_bar.update(cx, |search_bar, cx| { + search_bar.select_prev_match(&SelectPrevMatch, cx); + assert_eq!( + editor.update(cx, |editor, cx| editor.selections.display_ranges(cx)), + [DisplayPoint::new(3, 11)..DisplayPoint::new(3, 13)] + ); + }); + search_bar.read_with(cx, |search_bar, _| { + assert_eq!(search_bar.active_match_index, Some(1)); + }); + + search_bar.update(cx, |search_bar, cx| { + search_bar.select_prev_match(&SelectPrevMatch, cx); + assert_eq!( + editor.update(cx, |editor, cx| editor.selections.display_ranges(cx)), + [DisplayPoint::new(0, 41)..DisplayPoint::new(0, 43)] + ); + }); + search_bar.read_with(cx, |search_bar, _| { + assert_eq!(search_bar.active_match_index, Some(0)); + }); + + // Park the cursor in between matches and ensure that going to the previous match selects + // the closest match to the left. + editor.update(cx, |editor, cx| { + editor.change_selections(None, cx, |s| { + s.select_display_ranges([DisplayPoint::new(1, 0)..DisplayPoint::new(1, 0)]) + }); + }); + search_bar.update(cx, |search_bar, cx| { + assert_eq!(search_bar.active_match_index, Some(1)); + search_bar.select_prev_match(&SelectPrevMatch, cx); + assert_eq!( + editor.update(cx, |editor, cx| editor.selections.display_ranges(cx)), + [DisplayPoint::new(0, 41)..DisplayPoint::new(0, 43)] + ); + }); + search_bar.read_with(cx, |search_bar, _| { + assert_eq!(search_bar.active_match_index, Some(0)); + }); + + // Park the cursor in between matches and ensure that going to the next match selects the + // closest match to the right. + editor.update(cx, |editor, cx| { + editor.change_selections(None, cx, |s| { + s.select_display_ranges([DisplayPoint::new(1, 0)..DisplayPoint::new(1, 0)]) + }); + }); + search_bar.update(cx, |search_bar, cx| { + assert_eq!(search_bar.active_match_index, Some(1)); + search_bar.select_next_match(&SelectNextMatch, cx); + assert_eq!( + editor.update(cx, |editor, cx| editor.selections.display_ranges(cx)), + [DisplayPoint::new(3, 11)..DisplayPoint::new(3, 13)] + ); + }); + search_bar.read_with(cx, |search_bar, _| { + assert_eq!(search_bar.active_match_index, Some(1)); + }); + + // Park the cursor after the last match and ensure that going to the previous match selects + // the last match. + editor.update(cx, |editor, cx| { + editor.change_selections(None, cx, |s| { + s.select_display_ranges([DisplayPoint::new(3, 60)..DisplayPoint::new(3, 60)]) + }); + }); + search_bar.update(cx, |search_bar, cx| { + assert_eq!(search_bar.active_match_index, Some(2)); + search_bar.select_prev_match(&SelectPrevMatch, cx); + assert_eq!( + editor.update(cx, |editor, cx| editor.selections.display_ranges(cx)), + [DisplayPoint::new(3, 56)..DisplayPoint::new(3, 58)] + ); + }); + search_bar.read_with(cx, |search_bar, _| { + assert_eq!(search_bar.active_match_index, Some(2)); + }); + + // Park the cursor after the last match and ensure that going to the next match selects the + // first match. + editor.update(cx, |editor, cx| { + editor.change_selections(None, cx, |s| { + s.select_display_ranges([DisplayPoint::new(3, 60)..DisplayPoint::new(3, 60)]) + }); + }); + search_bar.update(cx, |search_bar, cx| { + assert_eq!(search_bar.active_match_index, Some(2)); + search_bar.select_next_match(&SelectNextMatch, cx); + assert_eq!( + editor.update(cx, |editor, cx| editor.selections.display_ranges(cx)), + [DisplayPoint::new(0, 41)..DisplayPoint::new(0, 43)] + ); + }); + search_bar.read_with(cx, |search_bar, _| { + assert_eq!(search_bar.active_match_index, Some(0)); + }); + + // Park the cursor before the first match and ensure that going to the previous match + // selects the last match. + editor.update(cx, |editor, cx| { + editor.change_selections(None, cx, |s| { + s.select_display_ranges([DisplayPoint::new(0, 0)..DisplayPoint::new(0, 0)]) + }); + }); + search_bar.update(cx, |search_bar, cx| { + assert_eq!(search_bar.active_match_index, Some(0)); + search_bar.select_prev_match(&SelectPrevMatch, cx); + assert_eq!( + editor.update(cx, |editor, cx| editor.selections.display_ranges(cx)), + [DisplayPoint::new(3, 56)..DisplayPoint::new(3, 58)] + ); + }); + search_bar.read_with(cx, |search_bar, _| { + assert_eq!(search_bar.active_match_index, Some(2)); + }); + } + + #[gpui::test] + async fn test_search_option_handling(cx: &mut TestAppContext) { + let (editor, search_bar) = init_test(cx); + + // show with options should make current search case sensitive + search_bar + .update(cx, |search_bar, cx| { + search_bar.show(cx); + search_bar.search("us", Some(SearchOptions::CASE_SENSITIVE), cx) + }) + .await + .unwrap(); + editor.update(cx, |editor, cx| { + assert_eq!( + editor.all_text_background_highlights(cx), + &[( + DisplayPoint::new(2, 43)..DisplayPoint::new(2, 45), + Color::red(), + )] + ); + }); + + // search_suggested should restore default options + search_bar.update(cx, |search_bar, cx| { + search_bar.search_suggested(cx); + assert_eq!(search_bar.search_options, SearchOptions::NONE) + }); + + // toggling a search option should update the defaults + search_bar + .update(cx, |search_bar, cx| { + search_bar.search("regex", Some(SearchOptions::CASE_SENSITIVE), cx) + }) + .await + .unwrap(); + search_bar.update(cx, |search_bar, cx| { + search_bar.toggle_search_option(SearchOptions::WHOLE_WORD, cx) + }); + editor.next_notification(cx).await; + editor.update(cx, |editor, cx| { + assert_eq!( + editor.all_text_background_highlights(cx), + &[( + DisplayPoint::new(0, 35)..DisplayPoint::new(0, 40), + Color::red(), + ),] + ); + }); + + // defaults should still include whole word + search_bar.update(cx, |search_bar, cx| { + search_bar.search_suggested(cx); + assert_eq!( + search_bar.search_options, + SearchOptions::CASE_SENSITIVE | SearchOptions::WHOLE_WORD + ) + }); + } + + #[gpui::test] + async fn test_search_select_all_matches(cx: &mut TestAppContext) { + crate::project_search::tests::init_test(cx); + + let buffer_text = r#" + A regular expression (shortened as regex or regexp;[1] also referred to as + rational expression[2][3]) is a sequence of characters that specifies a search + pattern in text. Usually such patterns are used by string-searching algorithms + for "find" or "find and replace" operations on strings, or for input validation. + "# + .unindent(); + let expected_query_matches_count = buffer_text + .chars() + .filter(|c| c.to_ascii_lowercase() == 'a') + .count(); + assert!( + expected_query_matches_count > 1, + "Should pick a query with multiple results" + ); + let buffer = cx.add_model(|cx| Buffer::new(0, cx.model_id() as u64, buffer_text)); + let window = cx.add_window(|_| EmptyView); + let editor = window.add_view(cx, |cx| Editor::for_buffer(buffer.clone(), None, cx)); + + let search_bar = window.add_view(cx, |cx| { + let mut search_bar = BufferSearchBar::new(cx); + search_bar.set_active_pane_item(Some(&editor), cx); + search_bar.show(cx); + search_bar + }); + + search_bar + .update(cx, |search_bar, cx| search_bar.search("a", None, cx)) + .await + .unwrap(); + search_bar.update(cx, |search_bar, cx| { + cx.focus(search_bar.query_editor.as_any()); + search_bar.activate_current_match(cx); + }); + + window.read_with(cx, |cx| { + assert!( + !editor.is_focused(cx), + "Initially, the editor should not be focused" + ); + }); + + let initial_selections = editor.update(cx, |editor, cx| { + let initial_selections = editor.selections.display_ranges(cx); + assert_eq!( + initial_selections.len(), 1, + "Expected to have only one selection before adding carets to all matches, but got: {initial_selections:?}", + ); + initial_selections + }); + search_bar.update(cx, |search_bar, _| { + assert_eq!(search_bar.active_match_index, Some(0)); + }); + + search_bar.update(cx, |search_bar, cx| { + cx.focus(search_bar.query_editor.as_any()); + search_bar.select_all_matches(&SelectAllMatches, cx); + }); + window.read_with(cx, |cx| { + assert!( + editor.is_focused(cx), + "Should focus editor after successful SelectAllMatches" + ); + }); + search_bar.update(cx, |search_bar, cx| { + let all_selections = + editor.update(cx, |editor, cx| editor.selections.display_ranges(cx)); + assert_eq!( + all_selections.len(), + expected_query_matches_count, + "Should select all `a` characters in the buffer, but got: {all_selections:?}" + ); + assert_eq!( + search_bar.active_match_index, + Some(0), + "Match index should not change after selecting all matches" + ); + }); + + search_bar.update(cx, |search_bar, cx| { + search_bar.select_next_match(&SelectNextMatch, cx); + }); + window.read_with(cx, |cx| { + assert!( + editor.is_focused(cx), + "Should still have editor focused after SelectNextMatch" + ); + }); + search_bar.update(cx, |search_bar, cx| { + let all_selections = + editor.update(cx, |editor, cx| editor.selections.display_ranges(cx)); + assert_eq!( + all_selections.len(), + 1, + "On next match, should deselect items and select the next match" + ); + assert_ne!( + all_selections, initial_selections, + "Next match should be different from the first selection" + ); + assert_eq!( + search_bar.active_match_index, + Some(1), + "Match index should be updated to the next one" + ); + }); + + search_bar.update(cx, |search_bar, cx| { + cx.focus(search_bar.query_editor.as_any()); + search_bar.select_all_matches(&SelectAllMatches, cx); + }); + window.read_with(cx, |cx| { + assert!( + editor.is_focused(cx), + "Should focus editor after successful SelectAllMatches" + ); + }); + search_bar.update(cx, |search_bar, cx| { + let all_selections = + editor.update(cx, |editor, cx| editor.selections.display_ranges(cx)); + assert_eq!( + all_selections.len(), + expected_query_matches_count, + "Should select all `a` characters in the buffer, but got: {all_selections:?}" + ); + assert_eq!( + search_bar.active_match_index, + Some(1), + "Match index should not change after selecting all matches" + ); + }); + + search_bar.update(cx, |search_bar, cx| { + search_bar.select_prev_match(&SelectPrevMatch, cx); + }); + window.read_with(cx, |cx| { + assert!( + editor.is_focused(cx), + "Should still have editor focused after SelectPrevMatch" + ); + }); + let last_match_selections = search_bar.update(cx, |search_bar, cx| { + let all_selections = + editor.update(cx, |editor, cx| editor.selections.display_ranges(cx)); + assert_eq!( + all_selections.len(), + 1, + "On previous match, should deselect items and select the previous item" + ); + assert_eq!( + all_selections, initial_selections, + "Previous match should be the same as the first selection" + ); + assert_eq!( + search_bar.active_match_index, + Some(0), + "Match index should be updated to the previous one" + ); + all_selections + }); + + search_bar + .update(cx, |search_bar, cx| { + cx.focus(search_bar.query_editor.as_any()); + search_bar.search("abas_nonexistent_match", None, cx) + }) + .await + .unwrap(); + search_bar.update(cx, |search_bar, cx| { + search_bar.select_all_matches(&SelectAllMatches, cx); + }); + window.read_with(cx, |cx| { + assert!( + !editor.is_focused(cx), + "Should not switch focus to editor if SelectAllMatches does not find any matches" + ); + }); + search_bar.update(cx, |search_bar, cx| { + let all_selections = + editor.update(cx, |editor, cx| editor.selections.display_ranges(cx)); + assert_eq!( + all_selections, last_match_selections, + "Should not select anything new if there are no matches" + ); + assert!( + search_bar.active_match_index.is_none(), + "For no matches, there should be no active match index" + ); + }); + } + + #[gpui::test] + async fn test_search_query_history(cx: &mut TestAppContext) { + crate::project_search::tests::init_test(cx); + + let buffer_text = r#" + A regular expression (shortened as regex or regexp;[1] also referred to as + rational expression[2][3]) is a sequence of characters that specifies a search + pattern in text. Usually such patterns are used by string-searching algorithms + for "find" or "find and replace" operations on strings, or for input validation. + "# + .unindent(); + let buffer = cx.add_model(|cx| Buffer::new(0, cx.model_id() as u64, buffer_text)); + let window = cx.add_window(|_| EmptyView); + + let editor = window.add_view(cx, |cx| Editor::for_buffer(buffer.clone(), None, cx)); + + let search_bar = window.add_view(cx, |cx| { + let mut search_bar = BufferSearchBar::new(cx); + search_bar.set_active_pane_item(Some(&editor), cx); + search_bar.show(cx); + search_bar + }); + + // Add 3 search items into the history. + search_bar + .update(cx, |search_bar, cx| search_bar.search("a", None, cx)) + .await + .unwrap(); + search_bar + .update(cx, |search_bar, cx| search_bar.search("b", None, cx)) + .await + .unwrap(); + search_bar + .update(cx, |search_bar, cx| { + search_bar.search("c", Some(SearchOptions::CASE_SENSITIVE), cx) + }) + .await + .unwrap(); + // Ensure that the latest search is active. + search_bar.read_with(cx, |search_bar, cx| { + assert_eq!(search_bar.query(cx), "c"); + assert_eq!(search_bar.search_options, SearchOptions::CASE_SENSITIVE); + }); + + // Next history query after the latest should set the query to the empty string. + search_bar.update(cx, |search_bar, cx| { + search_bar.next_history_query(&NextHistoryQuery, cx); + }); + search_bar.read_with(cx, |search_bar, cx| { + assert_eq!(search_bar.query(cx), ""); + assert_eq!(search_bar.search_options, SearchOptions::CASE_SENSITIVE); + }); + search_bar.update(cx, |search_bar, cx| { + search_bar.next_history_query(&NextHistoryQuery, cx); + }); + search_bar.read_with(cx, |search_bar, cx| { + assert_eq!(search_bar.query(cx), ""); + assert_eq!(search_bar.search_options, SearchOptions::CASE_SENSITIVE); + }); + + // First previous query for empty current query should set the query to the latest. + search_bar.update(cx, |search_bar, cx| { + search_bar.previous_history_query(&PreviousHistoryQuery, cx); + }); + search_bar.read_with(cx, |search_bar, cx| { + assert_eq!(search_bar.query(cx), "c"); + assert_eq!(search_bar.search_options, SearchOptions::CASE_SENSITIVE); + }); + + // Further previous items should go over the history in reverse order. + search_bar.update(cx, |search_bar, cx| { + search_bar.previous_history_query(&PreviousHistoryQuery, cx); + }); + search_bar.read_with(cx, |search_bar, cx| { + assert_eq!(search_bar.query(cx), "b"); + assert_eq!(search_bar.search_options, SearchOptions::CASE_SENSITIVE); + }); + + // Previous items should never go behind the first history item. + search_bar.update(cx, |search_bar, cx| { + search_bar.previous_history_query(&PreviousHistoryQuery, cx); + }); + search_bar.read_with(cx, |search_bar, cx| { + assert_eq!(search_bar.query(cx), "a"); + assert_eq!(search_bar.search_options, SearchOptions::CASE_SENSITIVE); + }); + search_bar.update(cx, |search_bar, cx| { + search_bar.previous_history_query(&PreviousHistoryQuery, cx); + }); + search_bar.read_with(cx, |search_bar, cx| { + assert_eq!(search_bar.query(cx), "a"); + assert_eq!(search_bar.search_options, SearchOptions::CASE_SENSITIVE); + }); + + // Next items should go over the history in the original order. + search_bar.update(cx, |search_bar, cx| { + search_bar.next_history_query(&NextHistoryQuery, cx); + }); + search_bar.read_with(cx, |search_bar, cx| { + assert_eq!(search_bar.query(cx), "b"); + assert_eq!(search_bar.search_options, SearchOptions::CASE_SENSITIVE); + }); + + search_bar + .update(cx, |search_bar, cx| search_bar.search("ba", None, cx)) + .await + .unwrap(); + search_bar.read_with(cx, |search_bar, cx| { + assert_eq!(search_bar.query(cx), "ba"); + assert_eq!(search_bar.search_options, SearchOptions::NONE); + }); + + // New search input should add another entry to history and move the selection to the end of the history. + search_bar.update(cx, |search_bar, cx| { + search_bar.previous_history_query(&PreviousHistoryQuery, cx); + }); + search_bar.read_with(cx, |search_bar, cx| { + assert_eq!(search_bar.query(cx), "c"); + assert_eq!(search_bar.search_options, SearchOptions::NONE); + }); + search_bar.update(cx, |search_bar, cx| { + search_bar.previous_history_query(&PreviousHistoryQuery, cx); + }); + search_bar.read_with(cx, |search_bar, cx| { + assert_eq!(search_bar.query(cx), "b"); + assert_eq!(search_bar.search_options, SearchOptions::NONE); + }); + search_bar.update(cx, |search_bar, cx| { + search_bar.next_history_query(&NextHistoryQuery, cx); + }); + search_bar.read_with(cx, |search_bar, cx| { + assert_eq!(search_bar.query(cx), "c"); + assert_eq!(search_bar.search_options, SearchOptions::NONE); + }); + search_bar.update(cx, |search_bar, cx| { + search_bar.next_history_query(&NextHistoryQuery, cx); + }); + search_bar.read_with(cx, |search_bar, cx| { + assert_eq!(search_bar.query(cx), "ba"); + assert_eq!(search_bar.search_options, SearchOptions::NONE); + }); + search_bar.update(cx, |search_bar, cx| { + search_bar.next_history_query(&NextHistoryQuery, cx); + }); + search_bar.read_with(cx, |search_bar, cx| { + assert_eq!(search_bar.query(cx), ""); + assert_eq!(search_bar.search_options, SearchOptions::NONE); + }); + } + #[gpui::test] + async fn test_replace_simple(cx: &mut TestAppContext) { + let (editor, search_bar) = init_test(cx); + + search_bar + .update(cx, |search_bar, cx| { + search_bar.search("expression", None, cx) + }) + .await + .unwrap(); + + search_bar.update(cx, |search_bar, cx| { + search_bar.replacement_editor.update(cx, |editor, cx| { + // We use $1 here as initially we should be in Text mode, where `$1` should be treated literally. + editor.set_text("expr$1", cx); + }); + search_bar.replace_all(&ReplaceAll, cx) + }); + assert_eq!( + editor.read_with(cx, |this, cx| { this.text(cx) }), + r#" + A regular expr$1 (shortened as regex or regexp;[1] also referred to as + rational expr$1[2][3]) is a sequence of characters that specifies a search + pattern in text. Usually such patterns are used by string-searching algorithms + for "find" or "find and replace" operations on strings, or for input validation. + "# + .unindent() + ); + + // Search for word boundaries and replace just a single one. + search_bar + .update(cx, |search_bar, cx| { + search_bar.search("or", Some(SearchOptions::WHOLE_WORD), cx) + }) + .await + .unwrap(); + + search_bar.update(cx, |search_bar, cx| { + search_bar.replacement_editor.update(cx, |editor, cx| { + editor.set_text("banana", cx); + }); + search_bar.replace_next(&ReplaceNext, cx) + }); + // Notice how the first or in the text (shORtened) is not replaced. Neither are the remaining hits of `or` in the text. + assert_eq!( + editor.read_with(cx, |this, cx| { this.text(cx) }), + r#" + A regular expr$1 (shortened as regex banana regexp;[1] also referred to as + rational expr$1[2][3]) is a sequence of characters that specifies a search + pattern in text. Usually such patterns are used by string-searching algorithms + for "find" or "find and replace" operations on strings, or for input validation. + "# + .unindent() + ); + // Let's turn on regex mode. + search_bar + .update(cx, |search_bar, cx| { + search_bar.activate_search_mode(SearchMode::Regex, cx); + search_bar.search("\\[([^\\]]+)\\]", None, cx) + }) + .await + .unwrap(); + search_bar.update(cx, |search_bar, cx| { + search_bar.replacement_editor.update(cx, |editor, cx| { + editor.set_text("${1}number", cx); + }); + search_bar.replace_all(&ReplaceAll, cx) + }); + assert_eq!( + editor.read_with(cx, |this, cx| { this.text(cx) }), + r#" + A regular expr$1 (shortened as regex banana regexp;1number also referred to as + rational expr$12number3number) is a sequence of characters that specifies a search + pattern in text. Usually such patterns are used by string-searching algorithms + for "find" or "find and replace" operations on strings, or for input validation. + "# + .unindent() + ); + // Now with a whole-word twist. + search_bar + .update(cx, |search_bar, cx| { + search_bar.activate_search_mode(SearchMode::Regex, cx); + search_bar.search("a\\w+s", Some(SearchOptions::WHOLE_WORD), cx) + }) + .await + .unwrap(); + search_bar.update(cx, |search_bar, cx| { + search_bar.replacement_editor.update(cx, |editor, cx| { + editor.set_text("things", cx); + }); + search_bar.replace_all(&ReplaceAll, cx) + }); + // The only word affected by this edit should be `algorithms`, even though there's a bunch + // of words in this text that would match this regex if not for WHOLE_WORD. + assert_eq!( + editor.read_with(cx, |this, cx| { this.text(cx) }), + r#" + A regular expr$1 (shortened as regex banana regexp;1number also referred to as + rational expr$12number3number) is a sequence of characters that specifies a search + pattern in text. Usually such patterns are used by string-searching things + for "find" or "find and replace" operations on strings, or for input validation. + "# + .unindent() + ); + } +} diff --git a/crates/search2/src/history.rs b/crates/search2/src/history.rs new file mode 100644 index 0000000000000000000000000000000000000000..6b06c60293d4389693b9d3692a2649856076081f --- /dev/null +++ b/crates/search2/src/history.rs @@ -0,0 +1,184 @@ +use smallvec::SmallVec; +const SEARCH_HISTORY_LIMIT: usize = 20; + +#[derive(Default, Debug, Clone)] +pub struct SearchHistory { + history: SmallVec<[String; SEARCH_HISTORY_LIMIT]>, + selected: Option, +} + +impl SearchHistory { + pub fn add(&mut self, search_string: String) { + if let Some(i) = self.selected { + if search_string == self.history[i] { + return; + } + } + + if let Some(previously_searched) = self.history.last_mut() { + if search_string.find(previously_searched.as_str()).is_some() { + *previously_searched = search_string; + self.selected = Some(self.history.len() - 1); + return; + } + } + + self.history.push(search_string); + if self.history.len() > SEARCH_HISTORY_LIMIT { + self.history.remove(0); + } + self.selected = Some(self.history.len() - 1); + } + + pub fn next(&mut self) -> Option<&str> { + let history_size = self.history.len(); + if history_size == 0 { + return None; + } + + let selected = self.selected?; + if selected == history_size - 1 { + return None; + } + let next_index = selected + 1; + self.selected = Some(next_index); + Some(&self.history[next_index]) + } + + pub fn current(&self) -> Option<&str> { + Some(&self.history[self.selected?]) + } + + pub fn previous(&mut self) -> Option<&str> { + let history_size = self.history.len(); + if history_size == 0 { + return None; + } + + let prev_index = match self.selected { + Some(selected_index) => { + if selected_index == 0 { + return None; + } else { + selected_index - 1 + } + } + None => history_size - 1, + }; + + self.selected = Some(prev_index); + Some(&self.history[prev_index]) + } + + pub fn reset_selection(&mut self) { + self.selected = None; + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_add() { + let mut search_history = SearchHistory::default(); + assert_eq!( + search_history.current(), + None, + "No current selection should be set fo the default search history" + ); + + search_history.add("rust".to_string()); + assert_eq!( + search_history.current(), + Some("rust"), + "Newly added item should be selected" + ); + + // check if duplicates are not added + search_history.add("rust".to_string()); + assert_eq!( + search_history.history.len(), + 1, + "Should not add a duplicate" + ); + assert_eq!(search_history.current(), Some("rust")); + + // check if new string containing the previous string replaces it + search_history.add("rustlang".to_string()); + assert_eq!( + search_history.history.len(), + 1, + "Should replace previous item if it's a substring" + ); + assert_eq!(search_history.current(), Some("rustlang")); + + // push enough items to test SEARCH_HISTORY_LIMIT + for i in 0..SEARCH_HISTORY_LIMIT * 2 { + search_history.add(format!("item{i}")); + } + assert!(search_history.history.len() <= SEARCH_HISTORY_LIMIT); + } + + #[test] + fn test_next_and_previous() { + let mut search_history = SearchHistory::default(); + assert_eq!( + search_history.next(), + None, + "Default search history should not have a next item" + ); + + search_history.add("Rust".to_string()); + assert_eq!(search_history.next(), None); + search_history.add("JavaScript".to_string()); + assert_eq!(search_history.next(), None); + search_history.add("TypeScript".to_string()); + assert_eq!(search_history.next(), None); + + assert_eq!(search_history.current(), Some("TypeScript")); + + assert_eq!(search_history.previous(), Some("JavaScript")); + assert_eq!(search_history.current(), Some("JavaScript")); + + assert_eq!(search_history.previous(), Some("Rust")); + assert_eq!(search_history.current(), Some("Rust")); + + assert_eq!(search_history.previous(), None); + assert_eq!(search_history.current(), Some("Rust")); + + assert_eq!(search_history.next(), Some("JavaScript")); + assert_eq!(search_history.current(), Some("JavaScript")); + + assert_eq!(search_history.next(), Some("TypeScript")); + assert_eq!(search_history.current(), Some("TypeScript")); + + assert_eq!(search_history.next(), None); + assert_eq!(search_history.current(), Some("TypeScript")); + } + + #[test] + fn test_reset_selection() { + let mut search_history = SearchHistory::default(); + search_history.add("Rust".to_string()); + search_history.add("JavaScript".to_string()); + search_history.add("TypeScript".to_string()); + + assert_eq!(search_history.current(), Some("TypeScript")); + search_history.reset_selection(); + assert_eq!(search_history.current(), None); + assert_eq!( + search_history.previous(), + Some("TypeScript"), + "Should start from the end after reset on previous item query" + ); + + search_history.previous(); + assert_eq!(search_history.current(), Some("JavaScript")); + search_history.previous(); + assert_eq!(search_history.current(), Some("Rust")); + + search_history.reset_selection(); + assert_eq!(search_history.current(), None); + } +} diff --git a/crates/search2/src/mode.rs b/crates/search2/src/mode.rs new file mode 100644 index 0000000000000000000000000000000000000000..8afc2bd3f496cc502f5ffd53fec5b05973108501 --- /dev/null +++ b/crates/search2/src/mode.rs @@ -0,0 +1,65 @@ +use gpui::Action; + +use crate::{ActivateRegexMode, ActivateSemanticMode, ActivateTextMode}; +// TODO: Update the default search mode to get from config +#[derive(Copy, Clone, Debug, Default, PartialEq)] +pub enum SearchMode { + #[default] + Text, + Semantic, + Regex, +} + +#[derive(Copy, Clone, Debug, PartialEq)] +pub(crate) enum Side { + Left, + Right, +} + +impl SearchMode { + pub(crate) fn label(&self) -> &'static str { + match self { + SearchMode::Text => "Text", + SearchMode::Semantic => "Semantic", + SearchMode::Regex => "Regex", + } + } + + pub(crate) fn region_id(&self) -> usize { + match self { + SearchMode::Text => 3, + SearchMode::Semantic => 4, + SearchMode::Regex => 5, + } + } + + pub(crate) fn tooltip_text(&self) -> &'static str { + match self { + SearchMode::Text => "Activate Text Search", + SearchMode::Semantic => "Activate Semantic Search", + SearchMode::Regex => "Activate Regex Search", + } + } + + pub(crate) fn activate_action(&self) -> Box { + match self { + SearchMode::Text => Box::new(ActivateTextMode), + SearchMode::Semantic => Box::new(ActivateSemanticMode), + SearchMode::Regex => Box::new(ActivateRegexMode), + } + } +} + +pub(crate) fn next_mode(mode: &SearchMode, semantic_enabled: bool) -> SearchMode { + match mode { + SearchMode::Text => SearchMode::Regex, + SearchMode::Regex => { + if semantic_enabled { + SearchMode::Semantic + } else { + SearchMode::Text + } + } + SearchMode::Semantic => SearchMode::Text, + } +} diff --git a/crates/search2/src/project_search.rs b/crates/search2/src/project_search.rs new file mode 100644 index 0000000000000000000000000000000000000000..f6e17bbee5d12685385ca64de790d5f8217bb92d --- /dev/null +++ b/crates/search2/src/project_search.rs @@ -0,0 +1,2661 @@ +use crate::{ + history::SearchHistory, + mode::{SearchMode, Side}, + search_bar::{render_nav_button, render_option_button_icon, render_search_mode_button}, + ActivateRegexMode, ActivateSemanticMode, ActivateTextMode, CycleMode, NextHistoryQuery, + PreviousHistoryQuery, ReplaceAll, ReplaceNext, SearchOptions, SelectNextMatch, SelectPrevMatch, + ToggleCaseSensitive, ToggleReplace, ToggleWholeWord, +}; +use anyhow::{Context, Result}; +use collections::HashMap; +use editor::{ + items::active_match_index, scroll::autoscroll::Autoscroll, Anchor, Editor, MultiBuffer, + SelectAll, MAX_TAB_TITLE_LEN, +}; +use futures::StreamExt; +use gpui::{ + actions, + elements::*, + platform::{MouseButton, PromptLevel}, + Action, AnyElement, AnyViewHandle, AppContext, Entity, ModelContext, ModelHandle, Subscription, + Task, View, ViewContext, ViewHandle, WeakModelHandle, WeakViewHandle, +}; +use menu::Confirm; +use project::{ + search::{SearchInputs, SearchQuery}, + Entry, Project, +}; +use semantic_index::{SemanticIndex, SemanticIndexStatus}; +use smallvec::SmallVec; +use std::{ + any::{Any, TypeId}, + borrow::Cow, + collections::HashSet, + mem, + ops::{Not, Range}, + path::PathBuf, + sync::Arc, + time::{Duration, Instant}, +}; +use util::{paths::PathMatcher, ResultExt as _}; +use workspace::{ + item::{BreadcrumbText, Item, ItemEvent, ItemHandle}, + searchable::{Direction, SearchableItem, SearchableItemHandle}, + ItemNavHistory, Pane, ToolbarItemLocation, ToolbarItemView, Workspace, WorkspaceId, +}; + +actions!( + project_search, + [SearchInNew, ToggleFocus, NextField, ToggleFilters,] +); + +#[derive(Default)] +struct ActiveSearches(HashMap, WeakViewHandle>); + +#[derive(Default)] +struct ActiveSettings(HashMap, ProjectSearchSettings>); + +pub fn init(cx: &mut AppContext) { + cx.set_global(ActiveSearches::default()); + cx.set_global(ActiveSettings::default()); + cx.add_action(ProjectSearchView::deploy); + cx.add_action(ProjectSearchView::move_focus_to_results); + cx.add_action(ProjectSearchBar::confirm); + cx.add_action(ProjectSearchBar::search_in_new); + cx.add_action(ProjectSearchBar::select_next_match); + cx.add_action(ProjectSearchBar::select_prev_match); + cx.add_action(ProjectSearchBar::replace_next); + cx.add_action(ProjectSearchBar::replace_all); + cx.add_action(ProjectSearchBar::cycle_mode); + cx.add_action(ProjectSearchBar::next_history_query); + cx.add_action(ProjectSearchBar::previous_history_query); + cx.add_action(ProjectSearchBar::activate_regex_mode); + cx.add_action(ProjectSearchBar::toggle_replace); + cx.add_action(ProjectSearchBar::toggle_replace_on_a_pane); + 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_previous); + cx.capture_action(ProjectSearchView::replace_all); + cx.capture_action(ProjectSearchView::replace_next); + add_toggle_option_action::(SearchOptions::CASE_SENSITIVE, cx); + add_toggle_option_action::(SearchOptions::WHOLE_WORD, cx); + add_toggle_filters_action::(cx); +} + +fn add_toggle_filters_action(cx: &mut AppContext) { + cx.add_action(move |pane: &mut Pane, _: &A, cx: &mut ViewContext| { + if let Some(search_bar) = pane.toolbar().read(cx).item_of_type::() { + if search_bar.update(cx, |search_bar, cx| search_bar.toggle_filters(cx)) { + return; + } + } + cx.propagate_action(); + }); +} + +fn add_toggle_option_action(option: SearchOptions, cx: &mut AppContext) { + cx.add_action(move |pane: &mut Pane, _: &A, cx: &mut ViewContext| { + if let Some(search_bar) = pane.toolbar().read(cx).item_of_type::() { + if search_bar.update(cx, |search_bar, cx| { + search_bar.toggle_search_option(option, cx) + }) { + return; + } + } + cx.propagate_action(); + }); +} + +struct ProjectSearch { + project: ModelHandle, + excerpts: ModelHandle, + pending_search: Option>>, + match_ranges: Vec>, + active_query: Option, + search_id: usize, + search_history: SearchHistory, + no_results: Option, +} + +#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] +enum InputPanel { + Query, + Exclude, + Include, +} + +pub struct ProjectSearchView { + model: ModelHandle, + query_editor: ViewHandle, + replacement_editor: ViewHandle, + results_editor: ViewHandle, + semantic_state: Option, + semantic_permissioned: Option, + search_options: SearchOptions, + panels_with_errors: HashSet, + active_match_index: Option, + search_id: usize, + query_editor_was_focused: bool, + included_files_editor: ViewHandle, + excluded_files_editor: ViewHandle, + filters_enabled: bool, + replace_enabled: bool, + current_mode: SearchMode, +} + +struct SemanticState { + index_status: SemanticIndexStatus, + maintain_rate_limit: Option>, + _subscription: Subscription, +} + +#[derive(Debug, Clone)] +struct ProjectSearchSettings { + search_options: SearchOptions, + filters_enabled: bool, + current_mode: SearchMode, +} + +pub struct ProjectSearchBar { + active_project_search: Option>, + subscription: Option, +} + +impl Entity for ProjectSearch { + type Event = (); +} + +impl ProjectSearch { + fn new(project: ModelHandle, cx: &mut ModelContext) -> Self { + let replica_id = project.read(cx).replica_id(); + Self { + project, + excerpts: cx.add_model(|_| MultiBuffer::new(replica_id)), + pending_search: Default::default(), + match_ranges: Default::default(), + active_query: None, + search_id: 0, + search_history: SearchHistory::default(), + no_results: None, + } + } + + fn clone(&self, cx: &mut ModelContext) -> ModelHandle { + cx.add_model(|cx| Self { + project: self.project.clone(), + excerpts: self + .excerpts + .update(cx, |excerpts, cx| cx.add_model(|cx| excerpts.clone(cx))), + pending_search: Default::default(), + match_ranges: self.match_ranges.clone(), + active_query: self.active_query.clone(), + search_id: self.search_id, + search_history: self.search_history.clone(), + no_results: self.no_results.clone(), + }) + } + + fn search(&mut self, query: SearchQuery, cx: &mut ModelContext) { + let search = self + .project + .update(cx, |project, cx| project.search(query.clone(), cx)); + self.search_id += 1; + self.search_history.add(query.as_str().to_string()); + self.active_query = Some(query); + self.match_ranges.clear(); + self.pending_search = Some(cx.spawn_weak(|this, mut cx| async move { + let mut matches = search; + let this = this.upgrade(&cx)?; + this.update(&mut cx, |this, cx| { + this.match_ranges.clear(); + this.excerpts.update(cx, |this, cx| this.clear(cx)); + this.no_results = Some(true); + }); + + while let Some((buffer, anchors)) = matches.next().await { + let mut ranges = this.update(&mut cx, |this, cx| { + this.no_results = Some(false); + this.excerpts.update(cx, |excerpts, cx| { + excerpts.stream_excerpts_with_context_lines(buffer, anchors, 1, cx) + }) + }); + + while let Some(range) = ranges.next().await { + this.update(&mut cx, |this, _| this.match_ranges.push(range)); + } + this.update(&mut cx, |_, cx| cx.notify()); + } + + this.update(&mut cx, |this, cx| { + this.pending_search.take(); + cx.notify(); + }); + + None + })); + cx.notify(); + } + + fn semantic_search(&mut self, inputs: &SearchInputs, cx: &mut ModelContext) { + let search = SemanticIndex::global(cx).map(|index| { + index.update(cx, |semantic_index, cx| { + semantic_index.search_project( + self.project.clone(), + inputs.as_str().to_owned(), + 10, + inputs.files_to_include().to_vec(), + inputs.files_to_exclude().to_vec(), + cx, + ) + }) + }); + self.search_id += 1; + self.match_ranges.clear(); + self.search_history.add(inputs.as_str().to_string()); + self.no_results = None; + self.pending_search = Some(cx.spawn(|this, mut cx| async move { + let results = search?.await.log_err()?; + let matches = results + .into_iter() + .map(|result| (result.buffer, vec![result.range.start..result.range.start])); + + this.update(&mut cx, |this, cx| { + this.no_results = Some(true); + this.excerpts.update(cx, |excerpts, cx| { + excerpts.clear(cx); + }); + }); + for (buffer, ranges) in matches { + let mut match_ranges = this.update(&mut cx, |this, cx| { + this.no_results = Some(false); + this.excerpts.update(cx, |excerpts, cx| { + excerpts.stream_excerpts_with_context_lines(buffer, ranges, 3, cx) + }) + }); + while let Some(match_range) = match_ranges.next().await { + this.update(&mut cx, |this, cx| { + this.match_ranges.push(match_range); + while let Ok(Some(match_range)) = match_ranges.try_next() { + this.match_ranges.push(match_range); + } + cx.notify(); + }); + } + } + + this.update(&mut cx, |this, cx| { + this.pending_search.take(); + cx.notify(); + }); + + None + })); + cx.notify(); + } +} + +#[derive(Clone, Debug, PartialEq, Eq)] +pub enum ViewEvent { + UpdateTab, + Activate, + EditorEvent(editor::Event), + Dismiss, +} + +impl Entity for ProjectSearchView { + type Event = ViewEvent; +} + +impl View for ProjectSearchView { + fn ui_name() -> &'static str { + "ProjectSearchView" + } + + fn render(&mut self, cx: &mut ViewContext) -> AnyElement { + let model = &self.model.read(cx); + if model.match_ranges.is_empty() { + enum Status {} + + let theme = theme::current(cx).clone(); + + // If Search is Active -> Major: Searching..., Minor: None + // If Semantic -> Major: "Search using Natural Language", Minor: {Status}/n{ex...}/n{ex...} + // If Regex -> Major: "Search using Regex", Minor: {ex...} + // If Text -> Major: "Text search all files and folders", Minor: {...} + + let current_mode = self.current_mode; + let mut major_text = if model.pending_search.is_some() { + Cow::Borrowed("Searching...") + } else if model.no_results.is_some_and(|v| v) { + Cow::Borrowed("No Results") + } else { + match current_mode { + SearchMode::Text => Cow::Borrowed("Text search all files and folders"), + SearchMode::Semantic => { + Cow::Borrowed("Search all code objects using Natural Language") + } + SearchMode::Regex => Cow::Borrowed("Regex search all files and folders"), + } + }; + + let mut show_minor_text = true; + let semantic_status = self.semantic_state.as_ref().and_then(|semantic| { + let status = semantic.index_status; + match status { + SemanticIndexStatus::NotAuthenticated => { + major_text = Cow::Borrowed("Not Authenticated"); + show_minor_text = false; + Some(vec![ + "API Key Missing: Please set 'OPENAI_API_KEY' in Environment Variables." + .to_string(), "If you authenticated using the Assistant Panel, please restart Zed to Authenticate.".to_string()]) + } + SemanticIndexStatus::Indexed => Some(vec!["Indexing complete".to_string()]), + SemanticIndexStatus::Indexing { + remaining_files, + rate_limit_expiry, + } => { + if remaining_files == 0 { + Some(vec![format!("Indexing...")]) + } else { + if let Some(rate_limit_expiry) = rate_limit_expiry { + let remaining_seconds = + rate_limit_expiry.duration_since(Instant::now()); + if remaining_seconds > Duration::from_secs(0) { + Some(vec![format!( + "Remaining files to index (rate limit resets in {}s): {}", + remaining_seconds.as_secs(), + remaining_files + )]) + } else { + Some(vec![format!("Remaining files to index: {}", remaining_files)]) + } + } else { + Some(vec![format!("Remaining files to index: {}", remaining_files)]) + } + } + } + SemanticIndexStatus::NotIndexed => None, + } + }); + + let minor_text = if let Some(no_results) = model.no_results { + if model.pending_search.is_none() && no_results { + vec!["No results found in this project for the provided query".to_owned()] + } else { + vec![] + } + } else { + match current_mode { + SearchMode::Semantic => { + let mut minor_text: Vec = Vec::new(); + minor_text.push("".into()); + if let Some(semantic_status) = semantic_status { + minor_text.extend(semantic_status); + } + if show_minor_text { + minor_text + .push("Simply explain the code you are looking to find.".into()); + minor_text.push( + "ex. 'prompt user for permissions to index their project'".into(), + ); + } + minor_text + } + _ => vec![ + "".to_owned(), + "Include/exclude specific paths with the filter option.".to_owned(), + "Matching exact word and/or casing is available too.".to_owned(), + ], + } + }; + + let previous_query_keystrokes = + cx.binding_for_action(&PreviousHistoryQuery {}) + .map(|binding| { + binding + .keystrokes() + .iter() + .map(|k| k.to_string()) + .collect::>() + }); + let next_query_keystrokes = + cx.binding_for_action(&NextHistoryQuery {}).map(|binding| { + binding + .keystrokes() + .iter() + .map(|k| k.to_string()) + .collect::>() + }); + let new_placeholder_text = match (previous_query_keystrokes, next_query_keystrokes) { + (Some(previous_query_keystrokes), Some(next_query_keystrokes)) => { + format!( + "Search ({}/{} for previous/next query)", + previous_query_keystrokes.join(" "), + next_query_keystrokes.join(" ") + ) + } + (None, Some(next_query_keystrokes)) => { + format!( + "Search ({} for next query)", + next_query_keystrokes.join(" ") + ) + } + (Some(previous_query_keystrokes), None) => { + format!( + "Search ({} for previous query)", + previous_query_keystrokes.join(" ") + ) + } + (None, None) => String::new(), + }; + self.query_editor.update(cx, |editor, cx| { + editor.set_placeholder_text(new_placeholder_text, cx); + }); + + MouseEventHandler::new::(0, cx, |_, _| { + Flex::column() + .with_child(Flex::column().contained().flex(1., true)) + .with_child( + Flex::column() + .align_children_center() + .with_child(Label::new( + major_text, + theme.search.major_results_status.clone(), + )) + .with_children( + minor_text.into_iter().map(|x| { + Label::new(x, theme.search.minor_results_status.clone()) + }), + ) + .aligned() + .top() + .contained() + .flex(7., true), + ) + .contained() + .with_background_color(theme.editor.background) + }) + .on_down(MouseButton::Left, |_, _, cx| { + cx.focus_parent(); + }) + .into_any_named("project search view") + } else { + ChildView::new(&self.results_editor, cx) + .flex(1., true) + .into_any_named("project search view") + } + } + + fn focus_in(&mut self, _: AnyViewHandle, cx: &mut ViewContext) { + let handle = cx.weak_handle(); + cx.update_global(|state: &mut ActiveSearches, cx| { + state + .0 + .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 self.query_editor_was_focused { + cx.focus(&self.query_editor); + } else { + cx.focus(&self.results_editor); + } + } + } +} + +impl Item for ProjectSearchView { + fn tab_tooltip_text(&self, cx: &AppContext) -> Option> { + let query_text = self.query_editor.read(cx).text(cx); + + query_text + .is_empty() + .not() + .then(|| query_text.into()) + .or_else(|| Some("Project Search".into())) + } + fn should_close_item_on_event(event: &Self::Event) -> bool { + event == &Self::Event::Dismiss + } + + fn act_as_type<'a>( + &'a self, + type_id: TypeId, + self_handle: &'a ViewHandle, + _: &'a AppContext, + ) -> Option<&'a AnyViewHandle> { + if type_id == TypeId::of::() { + Some(self_handle) + } else if type_id == TypeId::of::() { + Some(&self.results_editor) + } else { + None + } + } + + fn deactivated(&mut self, cx: &mut ViewContext) { + self.results_editor + .update(cx, |editor, cx| editor.deactivated(cx)); + } + + fn tab_content( + &self, + _detail: Option, + tab_theme: &theme::Tab, + cx: &AppContext, + ) -> AnyElement { + Flex::row() + .with_child( + Svg::new("icons/magnifying_glass.svg") + .with_color(tab_theme.label.text.color) + .constrained() + .with_width(tab_theme.type_icon_width) + .aligned() + .contained() + .with_margin_right(tab_theme.spacing), + ) + .with_child({ + let tab_name: Option> = self + .model + .read(cx) + .search_history + .current() + .as_ref() + .map(|query| { + let query_text = util::truncate_and_trailoff(query, MAX_TAB_TITLE_LEN); + query_text.into() + }); + Label::new( + tab_name + .filter(|name| !name.is_empty()) + .unwrap_or("Project search".into()), + tab_theme.label.clone(), + ) + .aligned() + }) + .into_any() + } + + fn for_each_project_item(&self, cx: &AppContext, f: &mut dyn FnMut(usize, &dyn project::Item)) { + self.results_editor.for_each_project_item(cx, f) + } + + fn is_singleton(&self, _: &AppContext) -> bool { + false + } + + fn can_save(&self, _: &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, + cx: &mut ViewContext, + ) -> Task> { + self.results_editor + .update(cx, |editor, cx| editor.save(project, cx)) + } + + fn save_as( + &mut self, + _: ModelHandle, + _: PathBuf, + _: &mut ViewContext, + ) -> Task> { + unreachable!("save_as should not have been called") + } + + fn reload( + &mut self, + project: ModelHandle, + cx: &mut ViewContext, + ) -> Task> { + self.results_editor + .update(cx, |editor, cx| editor.reload(project, cx)) + } + + fn clone_on_split(&self, _workspace_id: WorkspaceId, cx: &mut ViewContext) -> Option + where + Self: Sized, + { + let model = self.model.update(cx, |model, cx| model.clone(cx)); + Some(Self::new(model, cx, None)) + } + + fn added_to_workspace(&mut self, workspace: &mut Workspace, cx: &mut ViewContext) { + self.results_editor + .update(cx, |editor, cx| editor.added_to_workspace(workspace, cx)); + } + + fn set_nav_history(&mut self, nav_history: ItemNavHistory, cx: &mut ViewContext) { + self.results_editor.update(cx, |editor, _| { + editor.set_nav_history(Some(nav_history)); + }); + } + + fn navigate(&mut self, data: Box, cx: &mut ViewContext) -> bool { + self.results_editor + .update(cx, |editor, cx| editor.navigate(data, cx)) + } + + fn to_item_events(event: &Self::Event) -> SmallVec<[ItemEvent; 2]> { + match event { + ViewEvent::UpdateTab => { + smallvec::smallvec![ItemEvent::UpdateBreadcrumbs, ItemEvent::UpdateTab] + } + ViewEvent::EditorEvent(editor_event) => Editor::to_item_events(editor_event), + ViewEvent::Dismiss => smallvec::smallvec![ItemEvent::CloseItem], + _ => SmallVec::new(), + } + } + + fn breadcrumb_location(&self) -> ToolbarItemLocation { + if self.has_matches() { + ToolbarItemLocation::Secondary + } else { + ToolbarItemLocation::Hidden + } + } + + fn breadcrumbs(&self, theme: &theme::Theme, cx: &AppContext) -> Option> { + self.results_editor.breadcrumbs(theme, cx) + } + + fn serialized_item_kind() -> Option<&'static str> { + None + } + + fn deserialize( + _project: ModelHandle, + _workspace: WeakViewHandle, + _workspace_id: workspace::WorkspaceId, + _item_id: workspace::ItemId, + _cx: &mut ViewContext, + ) -> Task>> { + unimplemented!() + } +} + +impl ProjectSearchView { + fn toggle_filters(&mut self, cx: &mut ViewContext) { + 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.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) { + if let Some(semantic_index) = SemanticIndex::global(cx) { + // Semantic search uses no options + self.search_options = SearchOptions::none(); + + let project = self.model.read(cx).project.clone(); + + semantic_index.update(cx, |semantic_index, cx| { + semantic_index + .index_project(project.clone(), cx) + .detach_and_log_err(cx); + }); + + self.semantic_state = Some(SemanticState { + index_status: semantic_index.read(cx).status(&project), + maintain_rate_limit: None, + _subscription: cx.observe(&semantic_index, Self::semantic_index_changed), + }); + self.semantic_index_changed(semantic_index, cx); + } + } + + fn semantic_index_changed( + &mut self, + semantic_index: ModelHandle, + cx: &mut ViewContext, + ) { + let project = self.model.read(cx).project.clone(); + if let Some(semantic_state) = self.semantic_state.as_mut() { + cx.notify(); + semantic_state.index_status = semantic_index.read(cx).status(&project); + if let SemanticIndexStatus::Indexing { + rate_limit_expiry: Some(_), + .. + } = &semantic_state.index_status + { + if semantic_state.maintain_rate_limit.is_none() { + semantic_state.maintain_rate_limit = + Some(cx.spawn(|this, mut cx| async move { + loop { + cx.background().timer(Duration::from_secs(1)).await; + this.update(&mut cx, |_, cx| cx.notify()).log_err(); + } + })); + return; + } + } else { + semantic_state.maintain_rate_limit = None; + } + } + } + + fn clear_search(&mut self, cx: &mut ViewContext) { + self.model.update(cx, |model, cx| { + model.pending_search = None; + model.no_results = None; + model.match_ranges.clear(); + + model.excerpts.update(cx, |excerpts, cx| { + excerpts.clear(cx); + }); + }); + } + + fn activate_search_mode(&mut self, mode: SearchMode, cx: &mut ViewContext) { + let previous_mode = self.current_mode; + if previous_mode == mode { + return; + } + + self.clear_search(cx); + self.current_mode = mode; + self.active_match_index = None; + + match mode { + SearchMode::Semantic => { + let has_permission = self.semantic_permissioned(cx); + self.active_match_index = None; + cx.spawn(|this, mut cx| async move { + let has_permission = has_permission.await?; + + if !has_permission { + let mut answer = this.update(&mut cx, |this, cx| { + let project = this.model.read(cx).project.clone(); + let project_name = project + .read(cx) + .worktree_root_names(cx) + .collect::>() + .join("/"); + let is_plural = + project_name.chars().filter(|letter| *letter == '/').count() > 0; + let prompt_text = format!("Would you like to index the '{}' project{} for semantic search? This requires sending code to the OpenAI API", project_name, + if is_plural { + "s" + } else {""}); + cx.prompt( + PromptLevel::Info, + prompt_text.as_str(), + &["Continue", "Cancel"], + ) + })?; + + if answer.next().await == Some(0) { + this.update(&mut cx, |this, _| { + this.semantic_permissioned = Some(true); + })?; + } else { + this.update(&mut cx, |this, cx| { + this.semantic_permissioned = Some(false); + debug_assert_ne!(previous_mode, SearchMode::Semantic, "Tried to re-enable semantic search mode after user modal was rejected"); + this.activate_search_mode(previous_mode, cx); + })?; + return anyhow::Ok(()); + } + } + + this.update(&mut cx, |this, cx| { + this.index_project(cx); + })?; + + anyhow::Ok(()) + }).detach_and_log_err(cx); + } + SearchMode::Regex | SearchMode::Text => { + self.semantic_state = None; + self.active_match_index = None; + self.search(cx); + } + } + + cx.update_global(|state: &mut ActiveSettings, cx| { + state.0.insert( + self.model.read(cx).project.downgrade(), + self.current_settings(), + ); + }); + + cx.notify(); + } + fn replace_next(&mut self, _: &ReplaceNext, cx: &mut ViewContext) { + let model = self.model.read(cx); + if let Some(query) = model.active_query.as_ref() { + if model.match_ranges.is_empty() { + return; + } + if let Some(active_index) = self.active_match_index { + let query = query.clone().with_replacement(self.replacement(cx)); + self.results_editor.replace( + &(Box::new(model.match_ranges[active_index].clone()) as _), + &query, + cx, + ); + self.select_match(Direction::Next, cx) + } + } + } + pub fn replacement(&self, cx: &AppContext) -> String { + self.replacement_editor.read(cx).text(cx) + } + fn replace_all(&mut self, _: &ReplaceAll, cx: &mut ViewContext) { + let model = self.model.read(cx); + if let Some(query) = model.active_query.as_ref() { + if model.match_ranges.is_empty() { + return; + } + if self.active_match_index.is_some() { + let query = query.clone().with_replacement(self.replacement(cx)); + let matches = model + .match_ranges + .iter() + .map(|item| Box::new(item.clone()) as _) + .collect::>(); + for item in matches { + self.results_editor.replace(&item, &query, cx); + } + } + } + } + + fn new( + model: ModelHandle, + cx: &mut ViewContext, + settings: Option, + ) -> Self { + let project; + let excerpts; + let mut replacement_text = None; + let mut query_text = String::new(); + + // 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); + 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(); + replacement_text = active_query.replacement().map(ToOwned::to_owned); + options = SearchOptions::from_query(active_query); + } + } + cx.observe(&model, |this, _, cx| this.model_changed(cx)) + .detach(); + + let query_editor = cx.add_view(|cx| { + let mut editor = Editor::single_line( + Some(Arc::new(|theme| theme.search.editor.input.clone())), + cx, + ); + editor.set_placeholder_text("Text search all files", cx); + editor.set_text(query_text, cx); + editor + }); + // Subscribe to query_editor in order to reraise editor events for workspace item activation purposes + cx.subscribe(&query_editor, |_, _, event, cx| { + cx.emit(ViewEvent::EditorEvent(event.clone())) + }) + .detach(); + let replacement_editor = cx.add_view(|cx| { + let mut editor = Editor::single_line( + Some(Arc::new(|theme| theme.search.editor.input.clone())), + cx, + ); + editor.set_placeholder_text("Replace in project..", cx); + if let Some(text) = replacement_text { + editor.set_text(text, cx); + } + editor + }); + let results_editor = cx.add_view(|cx| { + let mut editor = Editor::for_multibuffer(excerpts, Some(project.clone()), cx); + editor.set_searchable(false); + editor + }); + cx.observe(&results_editor, |_, _, cx| cx.emit(ViewEvent::UpdateTab)) + .detach(); + + cx.subscribe(&results_editor, |this, _, event, cx| { + if matches!(event, editor::Event::SelectionsChanged { .. }) { + this.update_match_index(cx); + } + // Reraise editor events for workspace item activation purposes + cx.emit(ViewEvent::EditorEvent(event.clone())); + }) + .detach(); + + let included_files_editor = cx.add_view(|cx| { + let mut editor = Editor::single_line( + Some(Arc::new(|theme| { + theme.search.include_exclude_editor.input.clone() + })), + cx, + ); + editor.set_placeholder_text("Include: crates/**/*.toml", cx); + + editor + }); + // Subscribe to include_files_editor in order to reraise editor events for workspace item activation purposes + cx.subscribe(&included_files_editor, |_, _, event, cx| { + cx.emit(ViewEvent::EditorEvent(event.clone())) + }) + .detach(); + + let excluded_files_editor = cx.add_view(|cx| { + let mut editor = Editor::single_line( + Some(Arc::new(|theme| { + theme.search.include_exclude_editor.input.clone() + })), + cx, + ); + editor.set_placeholder_text("Exclude: vendor/*, *.lock", cx); + + editor + }); + // Subscribe to excluded_files_editor in order to reraise editor events for workspace item activation purposes + cx.subscribe(&excluded_files_editor, |_, _, event, cx| { + cx.emit(ViewEvent::EditorEvent(event.clone())) + }) + .detach(); + + // Check if Worktrees have all been previously indexed + let mut this = ProjectSearchView { + replacement_editor, + search_id: model.read(cx).search_id, + model, + query_editor, + results_editor, + semantic_state: None, + semantic_permissioned: None, + search_options: options, + panels_with_errors: HashSet::new(), + active_match_index: None, + query_editor_was_focused: false, + included_files_editor, + excluded_files_editor, + filters_enabled, + current_mode, + replace_enabled: false, + }; + this.model_changed(cx); + this + } + + fn semantic_permissioned(&mut self, cx: &mut ViewContext) -> Task> { + if let Some(value) = self.semantic_permissioned { + return Task::ready(Ok(value)); + } + + SemanticIndex::global(cx) + .map(|semantic| { + let project = self.model.read(cx).project.clone(); + semantic.update(cx, |this, cx| this.project_previously_indexed(&project, cx)) + }) + .unwrap_or(Task::ready(Ok(false))) + } + pub fn new_search_in_directory( + workspace: &mut Workspace, + dir_entry: &Entry, + cx: &mut ViewContext, + ) { + if !dir_entry.is_dir() { + return; + } + let Some(filter_str) = dir_entry.path.to_str() else { + return; + }; + + let model = cx.add_model(|cx| ProjectSearch::new(workspace.project().clone(), cx)); + let search = cx.add_view(|cx| ProjectSearchView::new(model, cx, None)); + workspace.add_item(Box::new(search.clone()), cx); + search.update(cx, |search, cx| { + search + .included_files_editor + .update(cx, |editor, cx| editor.set_text(filter_str, cx)); + search.filters_enabled = true; + search.focus_query_editor(cx) + }); + } + + // 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. + fn deploy( + workspace: &mut Workspace, + _: &workspace::NewSearch, + cx: &mut ViewContext, + ) { + // Clean up entries for dropped projects + cx.update_global(|state: &mut ActiveSearches, cx| { + state.0.retain(|project, _| project.is_upgradable(cx)) + }); + + let active_search = cx + .global::() + .0 + .get(&workspace.project().downgrade()); + + let existing = active_search + .and_then(|active_search| { + workspace + .items_of_type::(cx) + .find(|search| search == active_search) + }) + .or_else(|| workspace.item_of_type::(cx)); + + let query = workspace.active_item(cx).and_then(|item| { + let editor = item.act_as::(cx)?; + let query = editor.query_suggestion(cx); + if query.is_empty() { + None + } else { + Some(query) + } + }); + + let search = if let Some(existing) = existing { + workspace.activate_item(&existing, cx); + existing + } else { + let settings = cx + .global::() + .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 view = cx.add_view(|cx| ProjectSearchView::new(model, cx, settings)); + + 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) + }); + } + + fn search(&mut self, cx: &mut ViewContext) { + let mode = self.current_mode; + match mode { + SearchMode::Semantic => { + if self.semantic_state.is_some() { + if let Some(query) = self.build_search_query(cx) { + self.model + .update(cx, |model, cx| model.semantic_search(query.as_inner(), cx)); + } + } + } + + _ => { + 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) -> Option { + let text = self.query_editor.read(cx).text(cx); + let included_files = + match Self::parse_path_matches(&self.included_files_editor.read(cx).text(cx)) { + Ok(included_files) => { + self.panels_with_errors.remove(&InputPanel::Include); + included_files + } + Err(_e) => { + self.panels_with_errors.insert(InputPanel::Include); + cx.notify(); + return None; + } + }; + let excluded_files = + match Self::parse_path_matches(&self.excluded_files_editor.read(cx).text(cx)) { + Ok(excluded_files) => { + self.panels_with_errors.remove(&InputPanel::Exclude); + excluded_files + } + Err(_e) => { + self.panels_with_errors.insert(InputPanel::Exclude); + cx.notify(); + return None; + } + }; + let current_mode = self.current_mode; + match current_mode { + SearchMode::Regex => { + match SearchQuery::regex( + text, + self.search_options.contains(SearchOptions::WHOLE_WORD), + self.search_options.contains(SearchOptions::CASE_SENSITIVE), + included_files, + excluded_files, + ) { + Ok(query) => { + self.panels_with_errors.remove(&InputPanel::Query); + Some(query) + } + Err(_e) => { + self.panels_with_errors.insert(InputPanel::Query); + cx.notify(); + None + } + } + } + _ => match SearchQuery::text( + text, + self.search_options.contains(SearchOptions::WHOLE_WORD), + self.search_options.contains(SearchOptions::CASE_SENSITIVE), + included_files, + excluded_files, + ) { + Ok(query) => { + self.panels_with_errors.remove(&InputPanel::Query); + Some(query) + } + Err(_e) => { + self.panels_with_errors.insert(InputPanel::Query); + cx.notify(); + None + } + }, + } + } + + fn parse_path_matches(text: &str) -> anyhow::Result> { + text.split(',') + .map(str::trim) + .filter(|maybe_glob_str| !maybe_glob_str.is_empty()) + .map(|maybe_glob_str| { + PathMatcher::new(maybe_glob_str) + .with_context(|| format!("parsing {maybe_glob_str} as path matcher")) + }) + .collect() + } + + fn select_match(&mut self, direction: Direction, cx: &mut ViewContext) { + if let Some(index) = self.active_match_index { + 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, 1, cx) + }); + + let range_to_select = match_ranges[new_index].clone(); + self.results_editor.update(cx, |editor, cx| { + let range_to_select = editor.range_for_match(&range_to_select); + editor.unfold_ranges([range_to_select.clone()], false, true, cx); + editor.change_selections(Some(Autoscroll::fit()), cx, |s| { + s.select_ranges([range_to_select]) + }); + }); + } + } + + fn focus_query_editor(&mut self, cx: &mut ViewContext) { + self.query_editor.update(cx, |query_editor, cx| { + query_editor.select_all(&SelectAll, cx); + }); + self.query_editor_was_focused = true; + cx.focus(&self.query_editor); + } + + fn set_query(&mut self, query: &str, cx: &mut ViewContext) { + self.query_editor + .update(cx, |query_editor, cx| query_editor.set_text(query, cx)); + } + + fn focus_results_editor(&mut self, cx: &mut ViewContext) { + self.query_editor.update(cx, |query_editor, cx| { + let cursor = query_editor.selections.newest_anchor().head(); + query_editor.change_selections(None, cx, |s| s.select_ranges([cursor.clone()..cursor])); + }); + self.query_editor_was_focused = false; + cx.focus(&self.results_editor); + } + + fn model_changed(&mut self, cx: &mut ViewContext) { + let match_ranges = self.model.read(cx).match_ranges.clone(); + if match_ranges.is_empty() { + self.active_match_index = None; + } else { + self.active_match_index = Some(0); + self.update_match_index(cx); + let prev_search_id = mem::replace(&mut self.search_id, self.model.read(cx).search_id); + let is_new_search = self.search_id != prev_search_id; + self.results_editor.update(cx, |editor, cx| { + if is_new_search { + let range_to_select = match_ranges + .first() + .clone() + .map(|range| editor.range_for_match(range)); + editor.change_selections(Some(Autoscroll::fit()), cx, |s| { + s.select_ranges(range_to_select) + }); + } + editor.highlight_background::( + match_ranges, + |theme| theme.search.match_background, + cx, + ); + }); + if is_new_search && self.query_editor.is_focused(cx) { + self.focus_results_editor(cx); + } + } + + cx.emit(ViewEvent::UpdateTab); + cx.notify(); + } + + fn update_match_index(&mut self, cx: &mut ViewContext) { + let results_editor = self.results_editor.read(cx); + let new_index = active_match_index( + &self.model.read(cx).match_ranges, + &results_editor.selections.newest_anchor().head(), + &results_editor.buffer().read(cx).snapshot(cx), + ); + if self.active_match_index != new_index { + self.active_match_index = new_index; + cx.notify(); + } + } + + pub fn has_matches(&self) -> bool { + self.active_match_index.is_some() + } + + fn move_focus_to_results(pane: &mut Pane, _: &ToggleFocus, cx: &mut ViewContext) { + if let Some(search_view) = pane + .active_item() + .and_then(|item| item.downcast::()) + { + search_view.update(cx, |search_view, cx| { + if !search_view.results_editor.is_focused(cx) + && !search_view.model.read(cx).match_ranges.is_empty() + { + return search_view.focus_results_editor(cx); + } + }); + } + + cx.propagate_action(); + } +} + +impl Default for ProjectSearchBar { + fn default() -> Self { + Self::new() + } +} + +impl ProjectSearchBar { + pub fn new() -> Self { + Self { + active_project_search: Default::default(), + subscription: Default::default(), + } + } + fn cycle_mode(workspace: &mut Workspace, _: &CycleMode, cx: &mut ViewContext) { + if let Some(search_view) = workspace + .active_item(cx) + .and_then(|item| item.downcast::()) + { + search_view.update(cx, |this, cx| { + let new_mode = + crate::mode::next_mode(&this.current_mode, SemanticIndex::enabled(cx)); + this.activate_search_mode(new_mode, cx); + cx.focus(&this.query_editor); + }) + } + } + fn confirm(&mut self, _: &Confirm, cx: &mut ViewContext) { + let mut should_propagate = true; + if let Some(search_view) = self.active_project_search.as_ref() { + search_view.update(cx, |search_view, cx| { + if !search_view.replacement_editor.is_focused(cx) { + should_propagate = false; + search_view.search(cx); + } + }); + } + if should_propagate { + cx.propagate_action(); + } + } + + fn search_in_new(workspace: &mut Workspace, _: &SearchInNew, cx: &mut ViewContext) { + if let Some(search_view) = workspace + .active_item(cx) + .and_then(|item| item.downcast::()) + { + 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.search_options = SearchOptions::from_query(&old_query); + } + } + 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, None))), + cx, + ); + } + } + } + + fn select_next_match(pane: &mut Pane, _: &SelectNextMatch, cx: &mut ViewContext) { + if let Some(search_view) = pane + .active_item() + .and_then(|item| item.downcast::()) + { + search_view.update(cx, |view, cx| view.select_match(Direction::Next, cx)); + } else { + cx.propagate_action(); + } + } + + fn replace_next(pane: &mut Pane, _: &ReplaceNext, cx: &mut ViewContext) { + if let Some(search_view) = pane + .active_item() + .and_then(|item| item.downcast::()) + { + search_view.update(cx, |view, cx| view.replace_next(&ReplaceNext, cx)); + } else { + cx.propagate_action(); + } + } + fn replace_all(pane: &mut Pane, _: &ReplaceAll, cx: &mut ViewContext) { + if let Some(search_view) = pane + .active_item() + .and_then(|item| item.downcast::()) + { + search_view.update(cx, |view, cx| view.replace_all(&ReplaceAll, cx)); + } else { + cx.propagate_action(); + } + } + fn select_prev_match(pane: &mut Pane, _: &SelectPrevMatch, cx: &mut ViewContext) { + if let Some(search_view) = pane + .active_item() + .and_then(|item| item.downcast::()) + { + search_view.update(cx, |view, cx| view.select_match(Direction::Prev, cx)); + } else { + cx.propagate_action(); + } + } + + fn tab(&mut self, _: &editor::Tab, cx: &mut ViewContext) { + self.cycle_field(Direction::Next, cx); + } + + fn tab_previous(&mut self, _: &editor::TabPrev, cx: &mut ViewContext) { + self.cycle_field(Direction::Prev, cx); + } + + fn cycle_field(&mut self, direction: Direction, cx: &mut ViewContext) { + let active_project_search = match &self.active_project_search { + Some(active_project_search) => active_project_search, + + None => { + cx.propagate_action(); + return; + } + }; + + active_project_search.update(cx, |project_view, cx| { + let mut views = vec![&project_view.query_editor]; + if project_view.filters_enabled { + views.extend([ + &project_view.included_files_editor, + &project_view.excluded_files_editor, + ]); + } + if project_view.replace_enabled { + views.push(&project_view.replacement_editor); + } + let current_index = match views + .iter() + .enumerate() + .find(|(_, view)| view.is_focused(cx)) + { + Some((index, _)) => index, + + None => { + cx.propagate_action(); + return; + } + }; + + let new_index = match direction { + Direction::Next => (current_index + 1) % views.len(), + Direction::Prev if current_index == 0 => views.len() - 1, + Direction::Prev => (current_index - 1) % views.len(), + }; + cx.focus(views[new_index]); + }); + } + + fn toggle_search_option(&mut self, option: SearchOptions, cx: &mut ViewContext) -> bool { + if let Some(search_view) = self.active_project_search.as_ref() { + search_view.update(cx, |search_view, cx| { + search_view.toggle_search_option(option, cx); + search_view.search(cx); + }); + + cx.notify(); + true + } else { + false + } + } + fn toggle_replace(&mut self, _: &ToggleReplace, cx: &mut ViewContext) { + if let Some(search) = &self.active_project_search { + search.update(cx, |this, cx| { + this.replace_enabled = !this.replace_enabled; + if !this.replace_enabled { + cx.focus(&this.query_editor); + } + cx.notify(); + }); + } + } + fn toggle_replace_on_a_pane(pane: &mut Pane, _: &ToggleReplace, cx: &mut ViewContext) { + let mut should_propagate = true; + if let Some(search_view) = pane + .active_item() + .and_then(|item| item.downcast::()) + { + search_view.update(cx, |this, cx| { + should_propagate = false; + this.replace_enabled = !this.replace_enabled; + if !this.replace_enabled { + cx.focus(&this.query_editor); + } + cx.notify(); + }); + } + if should_propagate { + cx.propagate_action(); + } + } + fn activate_text_mode(pane: &mut Pane, _: &ActivateTextMode, cx: &mut ViewContext) { + if let Some(search_view) = pane + .active_item() + .and_then(|item| item.downcast::()) + { + 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) { + if let Some(search_view) = pane + .active_item() + .and_then(|item| item.downcast::()) + { + search_view.update(cx, |view, cx| { + view.activate_search_mode(SearchMode::Regex, cx) + }); + } else { + cx.propagate_action(); + } + } + + fn activate_semantic_mode( + pane: &mut Pane, + _: &ActivateSemanticMode, + cx: &mut ViewContext, + ) { + if SemanticIndex::enabled(cx) { + if let Some(search_view) = pane + .active_item() + .and_then(|item| item.downcast::()) + { + 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) -> bool { + if let Some(search_view) = self.active_project_search.as_ref() { + search_view.update(cx, |search_view, cx| { + search_view.toggle_filters(cx); + search_view + .included_files_editor + .update(cx, |_, cx| cx.notify()); + search_view + .excluded_files_editor + .update(cx, |_, cx| cx.notify()); + cx.refresh_windows(); + cx.notify(); + }); + cx.notify(); + true + } else { + false + } + } + + fn activate_search_mode(&self, mode: SearchMode, cx: &mut ViewContext) { + // Update Current Mode + if let Some(search_view) = self.active_project_search.as_ref() { + search_view.update(cx, |search_view, cx| { + search_view.activate_search_mode(mode, cx); + }); + cx.notify(); + } + } + + fn is_option_enabled(&self, option: SearchOptions, cx: &AppContext) -> bool { + if let Some(search) = self.active_project_search.as_ref() { + search.read(cx).search_options.contains(option) + } else { + false + } + } + + fn next_history_query(&mut self, _: &NextHistoryQuery, cx: &mut ViewContext) { + if let Some(search_view) = self.active_project_search.as_ref() { + search_view.update(cx, |search_view, cx| { + let new_query = search_view.model.update(cx, |model, _| { + if let Some(new_query) = model.search_history.next().map(str::to_string) { + new_query + } else { + model.search_history.reset_selection(); + String::new() + } + }); + search_view.set_query(&new_query, cx); + }); + } + } + + fn previous_history_query(&mut self, _: &PreviousHistoryQuery, cx: &mut ViewContext) { + if let Some(search_view) = self.active_project_search.as_ref() { + search_view.update(cx, |search_view, cx| { + if search_view.query_editor.read(cx).text(cx).is_empty() { + if let Some(new_query) = search_view + .model + .read(cx) + .search_history + .current() + .map(str::to_string) + { + search_view.set_query(&new_query, cx); + return; + } + } + + if let Some(new_query) = search_view.model.update(cx, |model, _| { + model.search_history.previous().map(str::to_string) + }) { + search_view.set_query(&new_query, cx); + } + }); + } + } +} + +impl Entity for ProjectSearchBar { + type Event = (); +} + +impl View for ProjectSearchBar { + fn ui_name() -> &'static str { + "ProjectSearchBar" + } + + fn update_keymap_context( + &self, + keymap: &mut gpui::keymap_matcher::KeymapContext, + cx: &AppContext, + ) { + Self::reset_to_default_keymap_context(keymap); + let in_replace = self + .active_project_search + .as_ref() + .map(|search| { + search + .read(cx) + .replacement_editor + .read_with(cx, |_, cx| cx.is_self_focused()) + }) + .flatten() + .unwrap_or(false); + if in_replace { + keymap.add_identifier("in_replace"); + } + } + + fn render(&mut self, cx: &mut ViewContext) -> AnyElement { + if let Some(_search) = self.active_project_search.as_ref() { + let search = _search.read(cx); + let theme = theme::current(cx).clone(); + let query_container_style = if search.panels_with_errors.contains(&InputPanel::Query) { + theme.search.invalid_editor + } else { + theme.search.editor.input.container + }; + + let search = _search.read(cx); + let filter_button = render_option_button_icon( + search.filters_enabled, + "icons/filter.svg", + 0, + "Toggle filters", + Box::new(ToggleFilters), + move |_, this, cx| { + this.toggle_filters(cx); + }, + cx, + ); + + let search = _search.read(cx); + let is_semantic_available = SemanticIndex::enabled(cx); + let is_semantic_disabled = search.semantic_state.is_none(); + let icon_style = theme.search.editor_icon.clone(); + let is_active = search.active_match_index.is_some(); + + let render_option_button_icon = |path, option, cx: &mut ViewContext| { + crate::search_bar::render_option_button_icon( + self.is_option_enabled(option, cx), + path, + option.bits as usize, + format!("Toggle {}", option.label()), + option.to_toggle_action(), + move |_, this, cx| { + this.toggle_search_option(option, cx); + }, + cx, + ) + }; + let case_sensitive = is_semantic_disabled.then(|| { + render_option_button_icon( + "icons/case_insensitive.svg", + SearchOptions::CASE_SENSITIVE, + cx, + ) + }); + + let whole_word = is_semantic_disabled.then(|| { + render_option_button_icon("icons/word_search.svg", SearchOptions::WHOLE_WORD, cx) + }); + + let search_button_for_mode = |mode, side, cx: &mut ViewContext| { + let is_active = if let Some(search) = self.active_project_search.as_ref() { + let search = search.read(cx); + search.current_mode == mode + } else { + false + }; + render_search_mode_button( + mode, + side, + is_active, + move |_, this, cx| { + this.activate_search_mode(mode, cx); + }, + cx, + ) + }; + + let search = _search.read(cx); + + let include_container_style = + if search.panels_with_errors.contains(&InputPanel::Include) { + theme.search.invalid_include_exclude_editor + } else { + theme.search.include_exclude_editor.input.container + }; + + let exclude_container_style = + if search.panels_with_errors.contains(&InputPanel::Exclude) { + theme.search.invalid_include_exclude_editor + } else { + theme.search.include_exclude_editor.input.container + }; + + let matches = 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() + }); + let should_show_replace_input = search.replace_enabled; + let replacement = should_show_replace_input.then(|| { + Flex::row() + .with_child( + Svg::for_style(theme.search.replace_icon.clone().icon) + .contained() + .with_style(theme.search.replace_icon.clone().container), + ) + .with_child(ChildView::new(&search.replacement_editor, cx).flex(1., true)) + .align_children_center() + .flex(1., true) + .contained() + .with_style(query_container_style) + .constrained() + .with_min_width(theme.search.editor.min_width) + .with_max_width(theme.search.editor.max_width) + .with_height(theme.search.search_bar_row_height) + .flex(1., false) + }); + let replace_all = should_show_replace_input.then(|| { + super::replace_action( + ReplaceAll, + "Replace all", + "icons/replace_all.svg", + theme.tooltip.clone(), + theme.search.action_button.clone(), + ) + }); + let replace_next = should_show_replace_input.then(|| { + super::replace_action( + ReplaceNext, + "Replace next", + "icons/replace_next.svg", + theme.tooltip.clone(), + theme.search.action_button.clone(), + ) + }); + let query_column = Flex::column() + .with_spacing(theme.search.search_row_spacing) + .with_child( + Flex::row() + .with_child( + Svg::for_style(icon_style.icon) + .contained() + .with_style(icon_style.container), + ) + .with_child(ChildView::new(&search.query_editor, cx).flex(1., true)) + .with_child( + Flex::row() + .with_child(filter_button) + .with_children(case_sensitive) + .with_children(whole_word) + .flex(1., false) + .constrained() + .contained(), + ) + .align_children_center() + .contained() + .with_style(query_container_style) + .constrained() + .with_min_width(theme.search.editor.min_width) + .with_max_width(theme.search.editor.max_width) + .with_height(theme.search.search_bar_row_height) + .flex(1., false), + ) + .with_children(search.filters_enabled.then(|| { + Flex::row() + .with_child( + ChildView::new(&search.included_files_editor, cx) + .contained() + .with_style(include_container_style) + .constrained() + .with_height(theme.search.search_bar_row_height) + .flex(1., true), + ) + .with_child( + ChildView::new(&search.excluded_files_editor, cx) + .contained() + .with_style(exclude_container_style) + .constrained() + .with_height(theme.search.search_bar_row_height) + .flex(1., true), + ) + .constrained() + .with_min_width(theme.search.editor.min_width) + .with_max_width(theme.search.editor.max_width) + .flex(1., false) + })) + .flex(1., false); + let switches_column = Flex::row() + .align_children_center() + .with_child(super::toggle_replace_button( + search.replace_enabled, + theme.tooltip.clone(), + theme.search.option_button_component.clone(), + )) + .constrained() + .with_height(theme.search.search_bar_row_height) + .contained() + .with_style(theme.search.option_button_group); + let mode_column = + Flex::row() + .with_child(search_button_for_mode( + SearchMode::Text, + Some(Side::Left), + cx, + )) + .with_child(search_button_for_mode( + SearchMode::Regex, + if is_semantic_available { + None + } else { + Some(Side::Right) + }, + cx, + )) + .with_children(is_semantic_available.then(|| { + search_button_for_mode(SearchMode::Semantic, Some(Side::Right), cx) + })) + .contained() + .with_style(theme.search.modes_container); + + let nav_button_for_direction = |label, direction, cx: &mut ViewContext| { + render_nav_button( + label, + direction, + is_active, + move |_, this, cx| { + if let Some(search) = this.active_project_search.as_ref() { + search.update(cx, |search, cx| search.select_match(direction, cx)); + } + }, + cx, + ) + }; + + let nav_column = Flex::row() + .with_children(replace_next) + .with_children(replace_all) + .with_child(Flex::row().with_children(matches)) + .with_child(nav_button_for_direction("<", Direction::Prev, cx)) + .with_child(nav_button_for_direction(">", Direction::Next, cx)) + .constrained() + .with_height(theme.search.search_bar_row_height) + .flex_float(); + + Flex::row() + .with_child(query_column) + .with_child(mode_column) + .with_child(switches_column) + .with_children(replacement) + .with_child(nav_column) + .contained() + .with_style(theme.search.container) + .into_any_named("project search") + } else { + Empty::new().into_any() + } + } +} + +impl ToolbarItemView for ProjectSearchBar { + fn set_active_pane_item( + &mut self, + active_pane_item: Option<&dyn ItemHandle>, + cx: &mut ViewContext, + ) -> ToolbarItemLocation { + cx.notify(); + self.subscription = None; + self.active_project_search = None; + if let Some(search) = active_pane_item.and_then(|i| i.downcast::()) { + search.update(cx, |search, cx| { + if search.current_mode == SearchMode::Semantic { + search.index_project(cx); + } + }); + + self.subscription = Some(cx.observe(&search, |_, _, cx| cx.notify())); + self.active_project_search = Some(search); + ToolbarItemLocation::PrimaryLeft { + flex: Some((1., true)), + } + } else { + ToolbarItemLocation::Hidden + } + } + + fn row_count(&self, cx: &ViewContext) -> usize { + if let Some(search) = self.active_project_search.as_ref() { + if search.read(cx).filters_enabled { + return 2; + } + } + 1 + } +} + +#[cfg(test)] +pub mod tests { + use super::*; + use editor::DisplayPoint; + use gpui::{color::Color, executor::Deterministic, TestAppContext}; + use project::FakeFs; + use semantic_index::semantic_index_settings::SemanticIndexSettings; + use serde_json::json; + use settings::SettingsStore; + use std::sync::Arc; + use theme::ThemeSettings; + + #[gpui::test] + async fn test_project_search(deterministic: Arc, cx: &mut TestAppContext) { + init_test(cx); + + 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; + let project = Project::test(fs.clone(), ["/dir".as_ref()], cx).await; + let search = cx.add_model(|cx| ProjectSearch::new(project, cx)); + let search_view = cx + .add_window(|cx| ProjectSearchView::new(search.clone(), cx, None)) + .root(cx); + + search_view.update(cx, |search_view, cx| { + search_view + .query_editor + .update(cx, |query_editor, cx| query_editor.set_text("TWO", cx)); + search_view.search(cx); + }); + deterministic.run_until_parked(); + search_view.update(cx, |search_view, cx| { + 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_text_background_highlights(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.selections.display_ranges(cx)), + [DisplayPoint::new(2, 32)..DisplayPoint::new(2, 35)] + ); + + search_view.select_match(Direction::Next, cx); + }); + + search_view.update(cx, |search_view, cx| { + assert_eq!(search_view.active_match_index, Some(1)); + assert_eq!( + search_view + .results_editor + .update(cx, |editor, cx| editor.selections.display_ranges(cx)), + [DisplayPoint::new(2, 37)..DisplayPoint::new(2, 40)] + ); + search_view.select_match(Direction::Next, cx); + }); + + search_view.update(cx, |search_view, cx| { + assert_eq!(search_view.active_match_index, Some(2)); + assert_eq!( + search_view + .results_editor + .update(cx, |editor, cx| editor.selections.display_ranges(cx)), + [DisplayPoint::new(5, 6)..DisplayPoint::new(5, 9)] + ); + search_view.select_match(Direction::Next, cx); + }); + + search_view.update(cx, |search_view, cx| { + assert_eq!(search_view.active_match_index, Some(0)); + assert_eq!( + search_view + .results_editor + .update(cx, |editor, cx| editor.selections.display_ranges(cx)), + [DisplayPoint::new(2, 32)..DisplayPoint::new(2, 35)] + ); + search_view.select_match(Direction::Prev, cx); + }); + + search_view.update(cx, |search_view, cx| { + assert_eq!(search_view.active_match_index, Some(2)); + assert_eq!( + search_view + .results_editor + .update(cx, |editor, cx| editor.selections.display_ranges(cx)), + [DisplayPoint::new(5, 6)..DisplayPoint::new(5, 9)] + ); + search_view.select_match(Direction::Prev, cx); + }); + + search_view.update(cx, |search_view, cx| { + assert_eq!(search_view.active_match_index, Some(1)); + assert_eq!( + search_view + .results_editor + .update(cx, |editor, cx| editor.selections.display_ranges(cx)), + [DisplayPoint::new(2, 37)..DisplayPoint::new(2, 40)] + ); + }); + } + + #[gpui::test] + async fn test_project_search_focus(deterministic: Arc, cx: &mut TestAppContext) { + init_test(cx); + + 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; + let project = Project::test(fs.clone(), ["/dir".as_ref()], cx).await; + let window = cx.add_window(|cx| Workspace::test_new(project, cx)); + let workspace = window.root(cx); + + let active_item = cx.read(|cx| { + workspace + .read(cx) + .active_pane() + .read(cx) + .active_item() + .and_then(|item| item.downcast::()) + }); + assert!( + active_item.is_none(), + "Expected no search panel to be active, but got: {active_item:?}" + ); + + workspace.update(cx, |workspace, cx| { + ProjectSearchView::deploy(workspace, &workspace::NewSearch, cx) + }); + + let Some(search_view) = cx.read(|cx| { + workspace + .read(cx) + .active_pane() + .read(cx) + .active_item() + .and_then(|item| item.downcast::()) + }) else { + panic!("Search view expected to appear after new search event trigger") + }; + let search_view_id = search_view.id(); + + cx.spawn(|mut cx| async move { + window.dispatch_action(search_view_id, &ToggleFocus, &mut cx); + }) + .detach(); + deterministic.run_until_parked(); + search_view.update(cx, |search_view, cx| { + assert!( + search_view.query_editor.is_focused(cx), + "Empty search view should be focused after the toggle focus event: no results panel to focus on", + ); + }); + + search_view.update(cx, |search_view, cx| { + let query_editor = &search_view.query_editor; + assert!( + query_editor.is_focused(cx), + "Search view should be focused after the new search view is activated", + ); + let query_text = query_editor.read(cx).text(cx); + assert!( + query_text.is_empty(), + "New search query should be empty but got '{query_text}'", + ); + let results_text = search_view + .results_editor + .update(cx, |editor, cx| editor.display_text(cx)); + assert!( + results_text.is_empty(), + "Empty search view should have no results but got '{results_text}'" + ); + }); + + search_view.update(cx, |search_view, cx| { + search_view.query_editor.update(cx, |query_editor, cx| { + query_editor.set_text("sOMETHINGtHATsURELYdOESnOTeXIST", cx) + }); + search_view.search(cx); + }); + deterministic.run_until_parked(); + search_view.update(cx, |search_view, cx| { + let results_text = search_view + .results_editor + .update(cx, |editor, cx| editor.display_text(cx)); + assert!( + results_text.is_empty(), + "Search view for mismatching query should have no results but got '{results_text}'" + ); + assert!( + search_view.query_editor.is_focused(cx), + "Search view should be focused after mismatching query had been used in search", + ); + }); + cx.spawn( + |mut cx| async move { window.dispatch_action(search_view_id, &ToggleFocus, &mut cx) }, + ) + .detach(); + deterministic.run_until_parked(); + search_view.update(cx, |search_view, cx| { + assert!( + search_view.query_editor.is_focused(cx), + "Search view with mismatching query should be focused after the toggle focus event: still no results panel to focus on", + ); + }); + + search_view.update(cx, |search_view, cx| { + search_view + .query_editor + .update(cx, |query_editor, cx| query_editor.set_text("TWO", cx)); + search_view.search(cx); + }); + deterministic.run_until_parked(); + search_view.update(cx, |search_view, cx| { + 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;", + "Search view results should match the query" + ); + assert!( + search_view.results_editor.is_focused(cx), + "Search view with mismatching query should be focused after search results are available", + ); + }); + cx.spawn(|mut cx| async move { + window.dispatch_action(search_view_id, &ToggleFocus, &mut cx); + }) + .detach(); + deterministic.run_until_parked(); + search_view.update(cx, |search_view, cx| { + assert!( + search_view.results_editor.is_focused(cx), + "Search view with matching query should still have its results editor focused after the toggle focus event", + ); + }); + + workspace.update(cx, |workspace, cx| { + ProjectSearchView::deploy(workspace, &workspace::NewSearch, cx) + }); + search_view.update(cx, |search_view, cx| { + assert_eq!(search_view.query_editor.read(cx).text(cx), "two", "Query should be updated to first search result after search view 2nd open in a row"); + 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;", + "Results should be unchanged after search view 2nd open in a row" + ); + assert!( + search_view.query_editor.is_focused(cx), + "Focus should be moved into query editor again after search view 2nd open in a row" + ); + }); + + cx.spawn(|mut cx| async move { + window.dispatch_action(search_view_id, &ToggleFocus, &mut cx); + }) + .detach(); + deterministic.run_until_parked(); + search_view.update(cx, |search_view, cx| { + assert!( + search_view.results_editor.is_focused(cx), + "Search view with matching query should switch focus to the results editor after the toggle focus event", + ); + }); + } + + #[gpui::test] + async fn test_new_project_search_in_directory( + deterministic: Arc, + cx: &mut TestAppContext, + ) { + init_test(cx); + + let fs = FakeFs::new(cx.background()); + fs.insert_tree( + "/dir", + json!({ + "a": { + "one.rs": "const ONE: usize = 1;", + "two.rs": "const TWO: usize = one::ONE + one::ONE;", + }, + "b": { + "three.rs": "const THREE: usize = one::ONE + two::TWO;", + "four.rs": "const FOUR: usize = one::ONE + three::THREE;", + }, + }), + ) + .await; + let project = Project::test(fs.clone(), ["/dir".as_ref()], cx).await; + let worktree_id = project.read_with(cx, |project, cx| { + project.worktrees(cx).next().unwrap().read(cx).id() + }); + let workspace = cx + .add_window(|cx| Workspace::test_new(project, cx)) + .root(cx); + + let active_item = cx.read(|cx| { + workspace + .read(cx) + .active_pane() + .read(cx) + .active_item() + .and_then(|item| item.downcast::()) + }); + assert!( + active_item.is_none(), + "Expected no search panel to be active, but got: {active_item:?}" + ); + + let one_file_entry = cx.update(|cx| { + workspace + .read(cx) + .project() + .read(cx) + .entry_for_path(&(worktree_id, "a/one.rs").into(), cx) + .expect("no entry for /a/one.rs file") + }); + assert!(one_file_entry.is_file()); + workspace.update(cx, |workspace, cx| { + ProjectSearchView::new_search_in_directory(workspace, &one_file_entry, cx) + }); + let active_search_entry = cx.read(|cx| { + workspace + .read(cx) + .active_pane() + .read(cx) + .active_item() + .and_then(|item| item.downcast::()) + }); + assert!( + active_search_entry.is_none(), + "Expected no search panel to be active for file entry" + ); + + let a_dir_entry = cx.update(|cx| { + workspace + .read(cx) + .project() + .read(cx) + .entry_for_path(&(worktree_id, "a").into(), cx) + .expect("no entry for /a/ directory") + }); + assert!(a_dir_entry.is_dir()); + workspace.update(cx, |workspace, cx| { + ProjectSearchView::new_search_in_directory(workspace, &a_dir_entry, cx) + }); + + let Some(search_view) = cx.read(|cx| { + workspace + .read(cx) + .active_pane() + .read(cx) + .active_item() + .and_then(|item| item.downcast::()) + }) else { + panic!("Search view expected to appear after new search in directory event trigger") + }; + deterministic.run_until_parked(); + search_view.update(cx, |search_view, cx| { + assert!( + search_view.query_editor.is_focused(cx), + "On new search in directory, focus should be moved into query editor" + ); + search_view.excluded_files_editor.update(cx, |editor, cx| { + assert!( + editor.display_text(cx).is_empty(), + "New search in directory should not have any excluded files" + ); + }); + search_view.included_files_editor.update(cx, |editor, cx| { + assert_eq!( + editor.display_text(cx), + a_dir_entry.path.to_str().unwrap(), + "New search in directory should have included dir entry path" + ); + }); + }); + + search_view.update(cx, |search_view, cx| { + search_view + .query_editor + .update(cx, |query_editor, cx| query_editor.set_text("const", cx)); + search_view.search(cx); + }); + deterministic.run_until_parked(); + search_view.update(cx, |search_view, cx| { + assert_eq!( + search_view + .results_editor + .update(cx, |editor, cx| editor.display_text(cx)), + "\n\nconst ONE: usize = 1;\n\n\nconst TWO: usize = one::ONE + one::ONE;", + "New search in directory should have a filter that matches a certain directory" + ); + }); + } + + #[gpui::test] + async fn test_search_query_history(cx: &mut TestAppContext) { + init_test(cx); + + 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; + let project = Project::test(fs.clone(), ["/dir".as_ref()], cx).await; + let window = cx.add_window(|cx| Workspace::test_new(project, cx)); + let workspace = window.root(cx); + workspace.update(cx, |workspace, cx| { + ProjectSearchView::deploy(workspace, &workspace::NewSearch, cx) + }); + + let search_view = cx.read(|cx| { + workspace + .read(cx) + .active_pane() + .read(cx) + .active_item() + .and_then(|item| item.downcast::()) + .expect("Search view expected to appear after new search event trigger") + }); + + let search_bar = window.add_view(cx, |cx| { + let mut search_bar = ProjectSearchBar::new(); + search_bar.set_active_pane_item(Some(&search_view), cx); + // search_bar.show(cx); + search_bar + }); + + // Add 3 search items into the history + another unsubmitted one. + search_view.update(cx, |search_view, cx| { + search_view.search_options = SearchOptions::CASE_SENSITIVE; + search_view + .query_editor + .update(cx, |query_editor, cx| query_editor.set_text("ONE", cx)); + search_view.search(cx); + }); + cx.foreground().run_until_parked(); + search_view.update(cx, |search_view, cx| { + search_view + .query_editor + .update(cx, |query_editor, cx| query_editor.set_text("TWO", cx)); + search_view.search(cx); + }); + cx.foreground().run_until_parked(); + search_view.update(cx, |search_view, cx| { + search_view + .query_editor + .update(cx, |query_editor, cx| query_editor.set_text("THREE", cx)); + search_view.search(cx); + }); + cx.foreground().run_until_parked(); + search_view.update(cx, |search_view, cx| { + search_view.query_editor.update(cx, |query_editor, cx| { + query_editor.set_text("JUST_TEXT_INPUT", cx) + }); + }); + cx.foreground().run_until_parked(); + + // Ensure that the latest input with search settings is active. + search_view.update(cx, |search_view, cx| { + assert_eq!( + search_view.query_editor.read(cx).text(cx), + "JUST_TEXT_INPUT" + ); + assert_eq!(search_view.search_options, SearchOptions::CASE_SENSITIVE); + }); + + // Next history query after the latest should set the query to the empty string. + search_bar.update(cx, |search_bar, cx| { + search_bar.next_history_query(&NextHistoryQuery, cx); + }); + search_view.update(cx, |search_view, cx| { + assert_eq!(search_view.query_editor.read(cx).text(cx), ""); + assert_eq!(search_view.search_options, SearchOptions::CASE_SENSITIVE); + }); + search_bar.update(cx, |search_bar, cx| { + search_bar.next_history_query(&NextHistoryQuery, cx); + }); + search_view.update(cx, |search_view, cx| { + assert_eq!(search_view.query_editor.read(cx).text(cx), ""); + assert_eq!(search_view.search_options, SearchOptions::CASE_SENSITIVE); + }); + + // First previous query for empty current query should set the query to the latest submitted one. + search_bar.update(cx, |search_bar, cx| { + search_bar.previous_history_query(&PreviousHistoryQuery, cx); + }); + search_view.update(cx, |search_view, cx| { + assert_eq!(search_view.query_editor.read(cx).text(cx), "THREE"); + assert_eq!(search_view.search_options, SearchOptions::CASE_SENSITIVE); + }); + + // Further previous items should go over the history in reverse order. + search_bar.update(cx, |search_bar, cx| { + search_bar.previous_history_query(&PreviousHistoryQuery, cx); + }); + search_view.update(cx, |search_view, cx| { + assert_eq!(search_view.query_editor.read(cx).text(cx), "TWO"); + assert_eq!(search_view.search_options, SearchOptions::CASE_SENSITIVE); + }); + + // Previous items should never go behind the first history item. + search_bar.update(cx, |search_bar, cx| { + search_bar.previous_history_query(&PreviousHistoryQuery, cx); + }); + search_view.update(cx, |search_view, cx| { + assert_eq!(search_view.query_editor.read(cx).text(cx), "ONE"); + assert_eq!(search_view.search_options, SearchOptions::CASE_SENSITIVE); + }); + search_bar.update(cx, |search_bar, cx| { + search_bar.previous_history_query(&PreviousHistoryQuery, cx); + }); + search_view.update(cx, |search_view, cx| { + assert_eq!(search_view.query_editor.read(cx).text(cx), "ONE"); + assert_eq!(search_view.search_options, SearchOptions::CASE_SENSITIVE); + }); + + // Next items should go over the history in the original order. + search_bar.update(cx, |search_bar, cx| { + search_bar.next_history_query(&NextHistoryQuery, cx); + }); + search_view.update(cx, |search_view, cx| { + assert_eq!(search_view.query_editor.read(cx).text(cx), "TWO"); + assert_eq!(search_view.search_options, SearchOptions::CASE_SENSITIVE); + }); + + search_view.update(cx, |search_view, cx| { + search_view + .query_editor + .update(cx, |query_editor, cx| query_editor.set_text("TWO_NEW", cx)); + search_view.search(cx); + }); + cx.foreground().run_until_parked(); + search_view.update(cx, |search_view, cx| { + assert_eq!(search_view.query_editor.read(cx).text(cx), "TWO_NEW"); + assert_eq!(search_view.search_options, SearchOptions::CASE_SENSITIVE); + }); + + // New search input should add another entry to history and move the selection to the end of the history. + search_bar.update(cx, |search_bar, cx| { + search_bar.previous_history_query(&PreviousHistoryQuery, cx); + }); + search_view.update(cx, |search_view, cx| { + assert_eq!(search_view.query_editor.read(cx).text(cx), "THREE"); + assert_eq!(search_view.search_options, SearchOptions::CASE_SENSITIVE); + }); + search_bar.update(cx, |search_bar, cx| { + search_bar.previous_history_query(&PreviousHistoryQuery, cx); + }); + search_view.update(cx, |search_view, cx| { + assert_eq!(search_view.query_editor.read(cx).text(cx), "TWO"); + assert_eq!(search_view.search_options, SearchOptions::CASE_SENSITIVE); + }); + search_bar.update(cx, |search_bar, cx| { + search_bar.next_history_query(&NextHistoryQuery, cx); + }); + search_view.update(cx, |search_view, cx| { + assert_eq!(search_view.query_editor.read(cx).text(cx), "THREE"); + assert_eq!(search_view.search_options, SearchOptions::CASE_SENSITIVE); + }); + search_bar.update(cx, |search_bar, cx| { + search_bar.next_history_query(&NextHistoryQuery, cx); + }); + search_view.update(cx, |search_view, cx| { + assert_eq!(search_view.query_editor.read(cx).text(cx), "TWO_NEW"); + assert_eq!(search_view.search_options, SearchOptions::CASE_SENSITIVE); + }); + search_bar.update(cx, |search_bar, cx| { + search_bar.next_history_query(&NextHistoryQuery, cx); + }); + search_view.update(cx, |search_view, cx| { + assert_eq!(search_view.query_editor.read(cx).text(cx), ""); + assert_eq!(search_view.search_options, SearchOptions::CASE_SENSITIVE); + }); + } + + pub fn init_test(cx: &mut TestAppContext) { + cx.foreground().forbid_parking(); + let fonts = cx.font_cache(); + let mut theme = gpui::fonts::with_font_cache(fonts.clone(), theme::Theme::default); + theme.search.match_background = Color::red(); + + cx.update(|cx| { + cx.set_global(SettingsStore::test(cx)); + cx.set_global(ActiveSearches::default()); + settings::register::(cx); + + theme::init((), cx); + cx.update_global::(|store, _| { + let mut settings = store.get::(None).clone(); + settings.theme = Arc::new(theme); + store.override_global(settings) + }); + + language::init(cx); + client::init_settings(cx); + editor::init(cx); + workspace::init_settings(cx); + Project::init_settings(cx); + super::init(cx); + }); + } +} diff --git a/crates/search2/src/search.rs b/crates/search2/src/search.rs new file mode 100644 index 0000000000000000000000000000000000000000..9ae66462fce227222dfba882d3598d4091d6907e --- /dev/null +++ b/crates/search2/src/search.rs @@ -0,0 +1,115 @@ +use bitflags::bitflags; +pub use buffer_search::BufferSearchBar; +use gpui::{actions, Action, AnyElement, AppContext, Component, Element, Svg, View}; +pub use mode::SearchMode; +use project::search::SearchQuery; +use ui::ButtonVariant; +//pub use project_search::{ProjectSearchBar, ProjectSearchView}; +// use theme::components::{ +// action_button::Button, svg::Svg, ComponentExt, IconButtonStyle, ToggleIconButtonStyle, +// }; + +pub mod buffer_search; +mod history; +mod mode; +//pub mod project_search; +pub(crate) mod search_bar; + +pub fn init(cx: &mut AppContext) { + buffer_search::init(cx); + //project_search::init(cx); +} + +actions!( + CycleMode, + ToggleWholeWord, + ToggleCaseSensitive, + ToggleReplace, + SelectNextMatch, + SelectPrevMatch, + SelectAllMatches, + NextHistoryQuery, + PreviousHistoryQuery, + ActivateTextMode, + ActivateSemanticMode, + ActivateRegexMode, + ReplaceAll, + ReplaceNext, +); + +bitflags! { + #[derive(Default)] + pub struct SearchOptions: u8 { + const NONE = 0b000; + const WHOLE_WORD = 0b001; + const CASE_SENSITIVE = 0b010; + } +} + +impl SearchOptions { + pub fn label(&self) -> &'static str { + match *self { + SearchOptions::WHOLE_WORD => "Match Whole Word", + SearchOptions::CASE_SENSITIVE => "Match Case", + _ => panic!("{:?} is not a named SearchOption", self), + } + } + + pub fn icon(&self) -> ui::Icon { + match *self { + SearchOptions::WHOLE_WORD => ui::Icon::WholeWord, + SearchOptions::CASE_SENSITIVE => ui::Icon::CaseSensitive, + _ => panic!("{:?} is not a named SearchOption", self), + } + } + + pub fn to_toggle_action(&self) -> Box { + match *self { + SearchOptions::WHOLE_WORD => Box::new(ToggleWholeWord), + SearchOptions::CASE_SENSITIVE => Box::new(ToggleCaseSensitive), + _ => panic!("{:?} is not a named SearchOption", self), + } + } + + pub fn none() -> SearchOptions { + SearchOptions::NONE + } + + pub fn from_query(query: &SearchQuery) -> SearchOptions { + let mut options = SearchOptions::NONE; + options.set(SearchOptions::WHOLE_WORD, query.whole_word()); + options.set(SearchOptions::CASE_SENSITIVE, query.case_sensitive()); + options + } + + pub fn as_button(&self, active: bool) -> impl Component { + ui::IconButton::new(0, self.icon()) + .on_click({ + let action = self.to_toggle_action(); + move |_: &mut V, cx| { + cx.dispatch_action(action.boxed_clone()); + } + }) + .variant(ui::ButtonVariant::Ghost) + .when(active, |button| button.variant(ButtonVariant::Filled)) + } +} + +fn toggle_replace_button(active: bool) -> impl Component { + // todo: add toggle_replace button + ui::IconButton::new(0, ui::Icon::Replace) + .on_click(|_: &mut V, cx| { + cx.dispatch_action(Box::new(ToggleReplace)); + }) + .variant(ui::ButtonVariant::Ghost) + .when(active, |button| button.variant(ButtonVariant::Filled)) +} + +fn replace_action( + action: impl Action + 'static + Send + Sync, + name: &'static str, +) -> impl Component { + ui::IconButton::new(0, ui::Icon::Replace).on_click(move |_: &mut V, cx| { + cx.dispatch_action(action.boxed_clone()); + }) +} diff --git a/crates/search2/src/search_bar.rs b/crates/search2/src/search_bar.rs new file mode 100644 index 0000000000000000000000000000000000000000..150b2f2d899c295c6cde017e111ae6b9a6e0c88e --- /dev/null +++ b/crates/search2/src/search_bar.rs @@ -0,0 +1,177 @@ +use std::borrow::Cow; + +use gpui::{ + div, Action, AnyElement, Component, CursorStyle, Element, MouseButton, MouseDownEvent, Svg, + View, ViewContext, +}; +use theme::ActiveTheme; +use ui::Label; +use workspace::searchable::Direction; + +use crate::{ + mode::{SearchMode, Side}, + SelectNextMatch, SelectPrevMatch, +}; + +pub(super) fn render_nav_button( + icon: &'static str, + direction: Direction, + active: bool, + on_click: impl Fn(MouseDownEvent, &mut V, &mut ViewContext) + 'static, + cx: &mut ViewContext, +) -> impl Component { + let action: Box; + 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.theme().tooltip.clone(); + // let cursor_style = if active { + // CursorStyle::PointingHand + // } else { + // CursorStyle::default() + // }; + // enum NavButton {} + div() + // MouseEventHandler::new::(direction as usize, cx, |state, cx| { + // let theme = cx.theme(); + // let style = theme + // .search + // .nav_button + // .in_state(active) + // .style_for(state) + // .clone(); + // let mut container_style = style.container.clone(); + // let label = Label::new(icon, style.label.clone()).aligned().contained(); + // container_style.corner_radii = match direction { + // Direction::Prev => CornerRadii { + // bottom_right: 0., + // top_right: 0., + // ..container_style.corner_radii + // }, + // Direction::Next => CornerRadii { + // bottom_left: 0., + // top_left: 0., + // ..container_style.corner_radii + // }, + // }; + // if direction == Direction::Prev { + // // Remove right border so that when both Next and Prev buttons are + // // next to one another, there's no double border between them. + // container_style.border.right = false; + // } + // label.with_style(container_style) + // }) + // .on_click(MouseButton::Left, on_click) + // .with_cursor_style(cursor_style) + // .with_tooltip::( + // direction as usize, + // tooltip.to_string(), + // Some(action), + // tooltip_style, + // cx, + // ) + // .into_any() +} + +pub(crate) fn render_search_mode_button( + mode: SearchMode, + side: Option, + is_active: bool, + //on_click: impl Fn(MouseClick, &mut V, &mut ViewContext) + 'static, + cx: &mut ViewContext, +) -> impl Component { + //let tooltip_style = cx.theme().tooltip.clone(); + enum SearchModeButton {} + div() + // MouseEventHandler::new::(mode.region_id(), cx, |state, cx| { + // let theme = cx.theme(); + // let style = theme + // .search + // .mode_button + // .in_state(is_active) + // .style_for(state) + // .clone(); + + // let mut container_style = style.container; + // if let Some(button_side) = side { + // if button_side == Side::Left { + // container_style.border.left = true; + // container_style.corner_radii = CornerRadii { + // bottom_right: 0., + // top_right: 0., + // ..container_style.corner_radii + // }; + // } else { + // container_style.border.left = false; + // container_style.corner_radii = CornerRadii { + // bottom_left: 0., + // top_left: 0., + // ..container_style.corner_radii + // }; + // } + // } else { + // container_style.border.left = false; + // container_style.corner_radii = CornerRadii::default(); + // } + + // Label::new(mode.label(), style.text) + // .aligned() + // .contained() + // .with_style(container_style) + // .constrained() + // .with_height(theme.search.search_bar_row_height) + // }) + // .on_click(MouseButton::Left, on_click) + // .with_cursor_style(CursorStyle::PointingHand) + // .with_tooltip::( + // mode.region_id(), + // mode.tooltip_text().to_owned(), + // Some(mode.activate_action()), + // tooltip_style, + // cx, + // ) + // .into_any() +} + +pub(crate) fn render_option_button_icon( + is_active: bool, + icon: &'static str, + id: usize, + label: impl Into>, + action: Box, + //on_click: impl Fn(MouseClick, &mut V, &mut EventContext) + 'static, + cx: &mut ViewContext, +) -> impl Component { + //let tooltip_style = cx.theme().tooltip.clone(); + div() + // MouseEventHandler::new::(id, cx, |state, cx| { + // let theme = cx.theme(); + // let style = theme + // .search + // .option_button + // .in_state(is_active) + // .style_for(state); + // Svg::new(icon) + // .with_color(style.color.clone()) + // .constrained() + // .with_width(style.icon_width) + // .contained() + // .with_style(style.container) + // .constrained() + // .with_height(theme.search.option_button_height) + // .with_width(style.button_width) + // }) + // .on_click(MouseButton::Left, on_click) + // .with_cursor_style(CursorStyle::PointingHand) + // .with_tooltip::(id, label, Some(action), tooltip_style, cx) + // .into_any() +} diff --git a/crates/ui2/src/components/icon.rs b/crates/ui2/src/components/icon.rs index 907f3f91871b5c614944e821244d10228d2853bb..28c3c42e51c12b8e775b1202bd0fde2cd346cd50 100644 --- a/crates/ui2/src/components/icon.rs +++ b/crates/ui2/src/components/icon.rs @@ -97,6 +97,8 @@ pub enum Icon { BellRing, MailOpen, AtSign, + WholeWord, + CaseSensitive, } impl Icon { @@ -155,6 +157,8 @@ impl Icon { Icon::BellRing => "icons/bell-ring.svg", Icon::MailOpen => "icons/mail-open.svg", Icon::AtSign => "icons/at-sign.svg", + Icon::WholeWord => "icons/word_search.svg", + Icon::CaseSensitive => "icons/case_insensitive.svg", } } } diff --git a/crates/workspace2/src/pane.rs b/crates/workspace2/src/pane.rs index 2bba684d12c67c2477299ed5915b80a65e7de2d4..4698c15d57ca4a9f858b3887f6aee2f83f6570d5 100644 --- a/crates/workspace2/src/pane.rs +++ b/crates/workspace2/src/pane.rs @@ -1909,7 +1909,7 @@ impl Render for Pane { v_stack() .size_full() .child(self.render_tab_bar(cx)) - .child(div() /* todo!(toolbar) */) + .child(self.toolbar.clone()) .child(if let Some(item) = self.active_item() { div().flex_1().child(item.to_any()) } else { diff --git a/crates/workspace2/src/searchable.rs b/crates/workspace2/src/searchable.rs index 2a393a9f6de5e8c4d8295eb606b705273b8afe0d..ef3a5f08fc61c0fad70ec7b731257930e4ef8084 100644 --- a/crates/workspace2/src/searchable.rs +++ b/crates/workspace2/src/searchable.rs @@ -1,7 +1,8 @@ use std::{any::Any, sync::Arc}; use gpui::{ - AnyView, AppContext, EventEmitter, Subscription, Task, View, ViewContext, WindowContext, + AnyView, AppContext, EventEmitter, Subscription, Task, View, ViewContext, WeakView, + WindowContext, }; use project2::search::SearchQuery; @@ -129,8 +130,7 @@ pub trait SearchableItemHandle: ItemHandle { // todo!("here is where we need to use AnyWeakView"); impl SearchableItemHandle for View { fn downgrade(&self) -> Box { - // Box::new(self.downgrade()) - todo!() + Box::new(self.downgrade()) } fn boxed_clone(&self) -> Box { @@ -252,16 +252,15 @@ pub trait WeakSearchableItemHandle: WeakItemHandle { // fn into_any(self) -> AnyWeakView; } -// todo!() -// impl WeakSearchableItemHandle for WeakView { -// fn upgrade(&self, cx: &AppContext) -> Option> { -// Some(Box::new(self.upgrade(cx)?)) -// } +impl WeakSearchableItemHandle for WeakView { + fn upgrade(&self, cx: &AppContext) -> Option> { + Some(Box::new(self.upgrade()?)) + } -// // fn into_any(self) -> AnyView { -// // self.into_any() -// // } -// } + // fn into_any(self) -> AnyView { + // self.into_any() + // } +} impl PartialEq for Box { fn eq(&self, other: &Self) -> bool { diff --git a/crates/workspace2/src/toolbar.rs b/crates/workspace2/src/toolbar.rs index 1d67da06b223155d076c39e53492ce370391aa06..ddbb801890b4770b6ee28edf072fea8f349cf702 100644 --- a/crates/workspace2/src/toolbar.rs +++ b/crates/workspace2/src/toolbar.rs @@ -1,6 +1,7 @@ use crate::ItemHandle; use gpui::{ - AnyView, Div, Entity, EntityId, EventEmitter, Render, View, ViewContext, WindowContext, + div, AnyView, Div, Entity, EntityId, EventEmitter, ParentElement, Render, View, ViewContext, + WindowContext, }; pub enum ToolbarItemEvent { @@ -55,7 +56,8 @@ impl Render for Toolbar { type Element = Div; fn render(&mut self, cx: &mut ViewContext) -> Self::Element { - todo!() + //dbg!(&self.items.len()); + div().children(self.items.iter().map(|(child, _)| child.to_any())) } } diff --git a/crates/workspace2/src/workspace2.rs b/crates/workspace2/src/workspace2.rs index e4fc5d35c60ecb23977a48c6acfe6e85bae36376..beb4331f1e005a4071112e6bdde934d95e275851 100644 --- a/crates/workspace2/src/workspace2.rs +++ b/crates/workspace2/src/workspace2.rs @@ -68,7 +68,7 @@ use std::{ time::Duration, }; use theme2::ActiveTheme; -pub use toolbar::{ToolbarItemLocation, ToolbarItemView}; +pub use toolbar::{ToolbarItemEvent, ToolbarItemLocation, ToolbarItemView}; use ui::{h_stack, Label}; use util::ResultExt; use uuid::Uuid; diff --git a/crates/zed2/Cargo.toml b/crates/zed2/Cargo.toml index 570912abc579a7aad2461c4fe081354c97fcc478..9eaa80fab1eda80a0bc1041730c0dc88390505c9 100644 --- a/crates/zed2/Cargo.toml +++ b/crates/zed2/Cargo.toml @@ -37,7 +37,7 @@ db = { package = "db2", path = "../db2" } editor = { package="editor2", path = "../editor2" } # feedback = { path = "../feedback" } # file_finder = { path = "../file_finder" } -# search = { path = "../search" } +search = { package = "search2", path = "../search2" } fs = { package = "fs2", path = "../fs2" } fsevent = { path = "../fsevent" } fuzzy = { path = "../fuzzy" } diff --git a/crates/zed2/src/main.rs b/crates/zed2/src/main.rs index c9e7ee8c580eb4f4b694855376d3b632c4fb22a8..895f7fe3cf84c7decd9a9f77da63d376fb528caf 100644 --- a/crates/zed2/src/main.rs +++ b/crates/zed2/src/main.rs @@ -194,7 +194,7 @@ fn main() { // project_panel::init(Assets, cx); // channel::init(&client, user_store.clone(), cx); // diagnostics::init(cx); - // search::init(cx); + search::init(cx); // semantic_index::init(fs.clone(), http.clone(), languages.clone(), cx); // vim::init(cx); // terminal_view::init(cx); diff --git a/crates/zed2/src/zed2.rs b/crates/zed2/src/zed2.rs index 7368d3a5efe22319bc398d72af514a746a6fdc39..1c73076ba503d9b9f43ec023bfd63ffd2fea1469 100644 --- a/crates/zed2/src/zed2.rs +++ b/crates/zed2/src/zed2.rs @@ -8,8 +8,8 @@ mod open_listener; pub use assets::*; use gpui::{ - point, px, AppContext, AsyncWindowContext, Task, TitlebarOptions, WeakView, WindowBounds, - WindowKind, WindowOptions, + point, px, AppContext, AsyncWindowContext, Task, TitlebarOptions, VisualContext as _, WeakView, + WindowBounds, WindowKind, WindowOptions, }; pub use only_instance::*; pub use open_listener::*; @@ -64,8 +64,8 @@ pub fn initialize_workspace( // todo!() // let breadcrumbs = cx.add_view(|_| Breadcrumbs::new(workspace)); // toolbar.add_item(breadcrumbs, cx); - // let buffer_search_bar = cx.add_view(BufferSearchBar::new); - // toolbar.add_item(buffer_search_bar.clone(), cx); + let buffer_search_bar = cx.build_view(search::BufferSearchBar::new); + toolbar.add_item(buffer_search_bar.clone(), cx); // let quick_action_bar = cx.add_view(|_| { // QuickActionBar::new(buffer_search_bar, workspace) // }); From 6c69e40e5c5cfe77b50521ed733bdf42f015e280 Mon Sep 17 00:00:00 2001 From: Piotr Osiewicz <24362066+osiewicz@users.noreply.github.com> Date: Tue, 14 Nov 2023 16:56:31 +0100 Subject: [PATCH 02/69] WIP --- crates/editor2/src/items.rs | 1 - crates/search2/src/buffer_search.rs | 220 +++++++++++++++++----------- crates/search2/src/search.rs | 1 + crates/search2/src/search_bar.rs | 28 ++-- crates/workspace2/src/toolbar.rs | 4 +- 5 files changed, 159 insertions(+), 95 deletions(-) diff --git a/crates/editor2/src/items.rs b/crates/editor2/src/items.rs index 6b396278b693807ea39e94e25584e8215f4c1541..f385fd151cd9467a08bcf3f5d7f80760123c58f0 100644 --- a/crates/editor2/src/items.rs +++ b/crates/editor2/src/items.rs @@ -910,7 +910,6 @@ impl SearchableItem for Editor { } fn update_matches(&mut self, matches: Vec>, cx: &mut ViewContext) { - dbg!(&matches); self.highlight_background::( matches, |theme| theme.title_bar_background, // todo: update theme diff --git a/crates/search2/src/buffer_search.rs b/crates/search2/src/buffer_search.rs index fdd03f71c3beb42378592b0e99dfb6a4fb179924..297daebe58f34968efa3b69df22b0e9b6f7b8fb6 100644 --- a/crates/search2/src/buffer_search.rs +++ b/crates/search2/src/buffer_search.rs @@ -10,16 +10,16 @@ use collections::HashMap; use editor::Editor; use futures::channel::oneshot; use gpui::{ - action, actions, div, Action, AnyElement, AnyView, AppContext, Component, Div, Entity, - EventEmitter, ParentElement as _, Render, Subscription, Svg, Task, View, ViewContext, - VisualContext as _, WindowContext, + action, actions, blue, div, red, white, Action, AnyElement, AnyView, AppContext, Component, + Div, Entity, EventEmitter, Hsla, ParentElement as _, Render, Styled, Subscription, Svg, Task, + View, ViewContext, VisualContext as _, WindowContext, }; use project::search::SearchQuery; use serde::Deserialize; use std::{any::Any, sync::Arc}; use theme::ActiveTheme; -use ui::{IconButton, Label}; +use ui::{h_stack, Icon, IconButton, IconElement, Label, StyledExt}; use util::ResultExt; use workspace::{ item::ItemHandle, @@ -66,31 +66,8 @@ pub struct BufferSearchBar { impl EventEmitter for BufferSearchBar {} impl EventEmitter for BufferSearchBar {} impl Render for BufferSearchBar { - // fn ui_name() -> &'static str { - // "BufferSearchBar" - // } - - // fn update_keymap_context( - // &self, - // keymap: &mut gpui::keymap_matcher::KeymapContext, - // cx: &AppContext, - // ) { - // Self::reset_to_default_keymap_context(keymap); - // let in_replace = self - // .replacement_editor - // .read_with(cx, |_, cx| cx.is_self_focused()) - // .unwrap_or(false); - // if in_replace { - // keymap.add_identifier("in_replace"); - // } - // } - - // fn focus_in(&mut self, _: View, cx: &mut ViewContext) { - // cx.focus(&self.query_editor); - // } type Element = Div; fn render(&mut self, cx: &mut ViewContext) -> Self::Element { - let theme = cx.theme().clone(); // let query_container_style = if self.query_contains_error { // theme.search.invalid_editor // } else { @@ -147,61 +124,104 @@ impl Render for BufferSearchBar { self.replacement_editor.update(cx, |editor, cx| { editor.set_placeholder_text("Replace with...", cx); }); + + let search_button_for_mode = |mode, side, cx: &mut ViewContext| { + let is_active = self.current_mode == mode; + + render_search_mode_button( + mode, + side, + is_active, + move |this, cx| { + this.activate_search_mode(mode, cx); + }, + cx, + ) + }; + let search_option_button = |option| { + let is_active = self.search_options.contains(option); + option.as_button(is_active) + }; + let match_count = self + .active_searchable_item + .as_ref() + .and_then(|searchable_item| { + if self.query(cx).is_empty() { + return None; + } + let matches = self + .searchable_items_with_matches + .get(&searchable_item.downgrade())?; + let message = if let Some(match_ix) = self.active_match_index { + format!("{}/{}", match_ix + 1, matches.len()) + } else { + "No matches".to_string() + }; + + Some(ui::Label::new(message)) + }); + let nav_button_for_direction = |label, direction, cx: &mut ViewContext| { + render_nav_button( + label, + direction, + self.active_match_index.is_some(), + move |this, cx| match direction { + Direction::Prev => this.select_prev_match(&Default::default(), cx), + Direction::Next => this.select_next_match(&Default::default(), cx), + }, + cx, + ) + }; div() - .child(self.query_editor.clone()) - .child(self.replacement_editor.clone()) - // let search_button_for_mode = |mode, side, cx: &mut ViewContext| { - // let is_active = self.current_mode == mode; - - // render_search_mode_button( - // mode, - // side, - // is_active, - // move |_, this, cx| { - // this.activate_search_mode(mode, cx); - // }, - // cx, - // ) - // }; - // let search_option_button = |option| { - // let is_active = self.search_options.contains(option); - // option.as_button(is_active) - // }; - // let match_count = self - // .active_searchable_item - // .as_ref() - // .and_then(|searchable_item| { - // if self.query(cx).is_empty() { - // return None; - // } - // let matches = self - // .searchable_items_with_matches - // .get(&searchable_item.downgrade())?; - // let message = if let Some(match_ix) = self.active_match_index { - // format!("{}/{}", match_ix + 1, matches.len()) - // } else { - // "No matches".to_string() - // }; - - // Some( - // Label::new(message) - // .contained() - // .with_style(theme.search.match_index.container) - // .aligned(), - // ) - // }); - // let nav_button_for_direction = |label, direction, cx: &mut ViewContext| { - // render_nav_button( - // label, - // direction, - // self.active_match_index.is_some(), - // move |_, this, cx| match direction { - // Direction::Prev => this.select_prev_match(&Default::default(), cx), - // Direction::Next => this.select_next_match(&Default::default(), cx), - // }, - // cx, - // ) - // }; + .w_full() + .border() + .border_color(blue()) + .flex() // Make this div a flex container + .justify_between() + .child( + div() + .flex() + .border_1() + .border_color(red()) + .rounded_md() + .w_96() + .items_center() + .child(IconElement::new(Icon::MagnifyingGlass)) + .child(self.query_editor.clone()) + .children( + supported_options + .case + .then(|| search_option_button(SearchOptions::CASE_SENSITIVE)), + ) + .children( + supported_options + .word + .then(|| search_option_button(SearchOptions::WHOLE_WORD)), + ), + ) + .child(div().w_auto().flex_row()) + .child(search_button_for_mode( + SearchMode::Text, + Some(Side::Left), + cx, + )) + .child(search_button_for_mode( + SearchMode::Regex, + Some(Side::Right), + cx, + )) + .when(supported_options.replacement, |this| { + this.child(super::toggle_replace_button(self.replace_enabled)) + }) + .when(self.replace_enabled, |this| { + this.child(div().w_80().child(self.replacement_editor.clone())) + }) + .children(match_count) + .child(nav_button_for_direction("<", Direction::Prev, cx)) + .child(nav_button_for_direction(">", Direction::Next, cx)) + .flex() + .justify_between() + // let query_column = Flex::row() // .with_child( // Svg::for_style(theme.search.editor_icon.clone().icon) @@ -351,7 +371,6 @@ impl ToolbarItemView for BufferSearchBar { impl BufferSearchBar { pub fn register(workspace: &mut Workspace) { workspace.register_action(|workspace, a: &Deploy, cx| { - dbg!("Setting"); workspace.active_pane().update(cx, |this, cx| { this.toolbar().update(cx, |this, cx| { let view = cx.build_view(|cx| BufferSearchBar::new(cx)); @@ -361,6 +380,43 @@ impl BufferSearchBar { }) }); }); + fn register_action( + workspace: &mut Workspace, + update: fn(&mut BufferSearchBar, &mut ViewContext<'_, BufferSearchBar>), + ) { + workspace.register_action(move |workspace, _: &A, cx| { + workspace.active_pane().update(cx, move |this, cx| { + this.toolbar().update(cx, move |toolbar, cx| { + let Some(search_bar) = toolbar.item_of_type::() else { + return; + }; + search_bar.update(cx, |this, cx| update(this, cx)) + }) + }); + }); + } + register_action::(workspace, |this, cx| { + this.toggle_search_option(SearchOptions::CASE_SENSITIVE, cx) + }); + register_action::(workspace, |this: &mut BufferSearchBar, cx| { + this.toggle_search_option(SearchOptions::WHOLE_WORD, cx) + }); + register_action::(workspace, |this: &mut BufferSearchBar, cx| { + dbg!("Toggling"); + this.toggle_replace(&ToggleReplace, cx) + }); + // workspace.register_action(|workspace, _: &ToggleCaseSensitive, cx| { + // workspace.active_pane().update(cx, |this, cx| { + // this.toolbar().update(cx, |toolbar, cx| { + // let Some(search_bar) = toolbar.item_of_type::() else { + // return; + // }; + // search_bar.update(cx, |this, cx| { + // this.toggle_search_option(SearchOptions::CASE_SENSITIVE, cx); + // }) + // }) + // }); + // }); } pub fn new(cx: &mut ViewContext) -> Self { dbg!("New"); diff --git a/crates/search2/src/search.rs b/crates/search2/src/search.rs index 9ae66462fce227222dfba882d3598d4091d6907e..955328c48365b10aaab6093877a7059af77f5661 100644 --- a/crates/search2/src/search.rs +++ b/crates/search2/src/search.rs @@ -100,6 +100,7 @@ fn toggle_replace_button(active: bool) -> impl Component { ui::IconButton::new(0, ui::Icon::Replace) .on_click(|_: &mut V, cx| { cx.dispatch_action(Box::new(ToggleReplace)); + cx.notify(); }) .variant(ui::ButtonVariant::Ghost) .when(active, |button| button.variant(ButtonVariant::Filled)) diff --git a/crates/search2/src/search_bar.rs b/crates/search2/src/search_bar.rs index 150b2f2d899c295c6cde017e111ae6b9a6e0c88e..d9259edd3f8a24b14a70e6c81ab374d44c3713c4 100644 --- a/crates/search2/src/search_bar.rs +++ b/crates/search2/src/search_bar.rs @@ -1,11 +1,11 @@ -use std::borrow::Cow; +use std::{borrow::Cow, sync::Arc}; use gpui::{ - div, Action, AnyElement, Component, CursorStyle, Element, MouseButton, MouseDownEvent, Svg, - View, ViewContext, + div, Action, AnyElement, Component, CursorStyle, Element, MouseButton, MouseDownEvent, + ParentElement as _, StatelessInteractive, Styled, Svg, View, ViewContext, }; use theme::ActiveTheme; -use ui::Label; +use ui::{Button, Label}; use workspace::searchable::Direction; use crate::{ @@ -17,19 +17,16 @@ pub(super) fn render_nav_button( icon: &'static str, direction: Direction, active: bool, - on_click: impl Fn(MouseDownEvent, &mut V, &mut ViewContext) + 'static, + on_click: impl Fn(&mut V, &mut ViewContext) + 'static + Send + Sync, cx: &mut ViewContext, ) -> impl Component { - let action: Box; let tooltip; match direction { Direction::Prev => { - action = Box::new(SelectPrevMatch); tooltip = "Select Previous Match"; } Direction::Next => { - action = Box::new(SelectNextMatch); tooltip = "Select Next Match"; } }; @@ -40,7 +37,7 @@ pub(super) fn render_nav_button( // CursorStyle::default() // }; // enum NavButton {} - div() + Button::new(icon).on_click(Arc::new(on_click)) // MouseEventHandler::new::(direction as usize, cx, |state, cx| { // let theme = cx.theme(); // let style = theme @@ -86,12 +83,23 @@ pub(crate) fn render_search_mode_button( mode: SearchMode, side: Option, is_active: bool, - //on_click: impl Fn(MouseClick, &mut V, &mut ViewContext) + 'static, + on_click: impl Fn(&mut V, &mut ViewContext) + 'static, cx: &mut ViewContext, ) -> impl Component { //let tooltip_style = cx.theme().tooltip.clone(); enum SearchModeButton {} + div() + .border_2() + .rounded_md() + .when(side == Some(Side::Left), |this| { + this.border_r_0().rounded_tr_none().rounded_br_none() + }) + .when(side == Some(Side::Right), |this| { + this.border_l_0().rounded_bl_none().rounded_tl_none() + }) + .on_key_down(move |v, _, _, cx| on_click(v, cx)) + .child(Label::new(mode.label())) // MouseEventHandler::new::(mode.region_id(), cx, |state, cx| { // let theme = cx.theme(); // let style = theme diff --git a/crates/workspace2/src/toolbar.rs b/crates/workspace2/src/toolbar.rs index ddbb801890b4770b6ee28edf072fea8f349cf702..6ad03d8a84a108520d4b892be637bd41cc0a905c 100644 --- a/crates/workspace2/src/toolbar.rs +++ b/crates/workspace2/src/toolbar.rs @@ -1,7 +1,7 @@ use crate::ItemHandle; use gpui::{ - div, AnyView, Div, Entity, EntityId, EventEmitter, ParentElement, Render, View, ViewContext, - WindowContext, + div, AnyView, Div, Entity, EntityId, EventEmitter, ParentElement, Render, Styled, View, + ViewContext, WindowContext, }; pub enum ToolbarItemEvent { From 08dde94299a8f90fb8109bd4787ed526c0146f28 Mon Sep 17 00:00:00 2001 From: Piotr Osiewicz <24362066+osiewicz@users.noreply.github.com> Date: Tue, 14 Nov 2023 17:59:53 +0100 Subject: [PATCH 03/69] WIP styling Co-authored-by: Nate --- crates/editor2/src/items.rs | 2 +- crates/search2/src/buffer_search.rs | 29 +++++++--------- crates/search2/src/search_bar.rs | 38 +++++++++++++-------- crates/workspace2/src/toolbar.rs | 53 ++++++++++++++++++++++++++--- 4 files changed, 85 insertions(+), 37 deletions(-) diff --git a/crates/editor2/src/items.rs b/crates/editor2/src/items.rs index f385fd151cd9467a08bcf3f5d7f80760123c58f0..90b821f99dca970ac0ba2d1d314a4e49dde3b16f 100644 --- a/crates/editor2/src/items.rs +++ b/crates/editor2/src/items.rs @@ -760,7 +760,7 @@ impl Item for Editor { } fn breadcrumb_location(&self) -> ToolbarItemLocation { - ToolbarItemLocation::PrimaryLeft { flex: None } + ToolbarItemLocation::PrimaryLeft } fn breadcrumbs(&self, variant: &Theme, cx: &AppContext) -> Option> { diff --git a/crates/search2/src/buffer_search.rs b/crates/search2/src/buffer_search.rs index 297daebe58f34968efa3b69df22b0e9b6f7b8fb6..72c93905114ca79eb451546a9f09ae89f90bcdce 100644 --- a/crates/search2/src/buffer_search.rs +++ b/crates/search2/src/buffer_search.rs @@ -10,16 +10,16 @@ use collections::HashMap; use editor::Editor; use futures::channel::oneshot; use gpui::{ - action, actions, blue, div, red, white, Action, AnyElement, AnyView, AppContext, Component, - Div, Entity, EventEmitter, Hsla, ParentElement as _, Render, Styled, Subscription, Svg, Task, - View, ViewContext, VisualContext as _, WindowContext, + action, actions, blue, div, red, rems, white, Action, AnyElement, AnyView, AppContext, + Component, Div, Entity, EventEmitter, Hsla, ParentElement as _, Render, Styled, Subscription, + Svg, Task, View, ViewContext, VisualContext as _, WindowContext, }; use project::search::SearchQuery; use serde::Deserialize; use std::{any::Any, sync::Arc}; use theme::ActiveTheme; -use ui::{h_stack, Icon, IconButton, IconElement, Label, StyledExt}; +use ui::{h_stack, Button, ButtonGroup, Icon, IconButton, IconElement, Label, StyledExt}; use util::ResultExt; use workspace::{ item::ItemHandle, @@ -173,10 +173,9 @@ impl Render for BufferSearchBar { ) }; div() - .w_full() .border() .border_color(blue()) - .flex() // Make this div a flex container + .flex() .justify_between() .child( div() @@ -199,17 +198,10 @@ impl Render for BufferSearchBar { .then(|| search_option_button(SearchOptions::WHOLE_WORD)), ), ) - .child(div().w_auto().flex_row()) - .child(search_button_for_mode( - SearchMode::Text, - Some(Side::Left), - cx, - )) - .child(search_button_for_mode( - SearchMode::Regex, - Some(Side::Right), - cx, - )) + .child(ButtonGroup::new(vec![ + search_button_for_mode(SearchMode::Text, Some(Side::Left), cx), + search_button_for_mode(SearchMode::Regex, Some(Side::Right), cx), + ])) .when(supported_options.replacement, |this| { this.child(super::toggle_replace_button(self.replace_enabled)) }) @@ -373,6 +365,9 @@ impl BufferSearchBar { workspace.register_action(|workspace, a: &Deploy, cx| { workspace.active_pane().update(cx, |this, cx| { this.toolbar().update(cx, |this, cx| { + if this.item_of_type::().is_some() { + return; + } let view = cx.build_view(|cx| BufferSearchBar::new(cx)); this.add_item(view.clone(), cx); view.update(cx, |this, cx| this.deploy(a, cx)); diff --git a/crates/search2/src/search_bar.rs b/crates/search2/src/search_bar.rs index d9259edd3f8a24b14a70e6c81ab374d44c3713c4..8c3dc2e698d374e121569993fbe16a16434793be 100644 --- a/crates/search2/src/search_bar.rs +++ b/crates/search2/src/search_bar.rs @@ -5,7 +5,7 @@ use gpui::{ ParentElement as _, StatelessInteractive, Styled, Svg, View, ViewContext, }; use theme::ActiveTheme; -use ui::{Button, Label}; +use ui::{v_stack, Button, ButtonVariant, Label}; use workspace::searchable::Direction; use crate::{ @@ -83,23 +83,33 @@ pub(crate) fn render_search_mode_button( mode: SearchMode, side: Option, is_active: bool, - on_click: impl Fn(&mut V, &mut ViewContext) + 'static, + on_click: impl Fn(&mut V, &mut ViewContext) + 'static + Send + Sync, cx: &mut ViewContext, -) -> impl Component { +) -> Button { //let tooltip_style = cx.theme().tooltip.clone(); enum SearchModeButton {} - div() - .border_2() - .rounded_md() - .when(side == Some(Side::Left), |this| { - this.border_r_0().rounded_tr_none().rounded_br_none() - }) - .when(side == Some(Side::Right), |this| { - this.border_l_0().rounded_bl_none().rounded_tl_none() - }) - .on_key_down(move |v, _, _, cx| on_click(v, cx)) - .child(Label::new(mode.label())) + let button_variant = if is_active { + ButtonVariant::Filled + } else { + ButtonVariant::Ghost + }; + + Button::new(mode.label()) + .on_click(Arc::new(on_click)) + .variant(button_variant) + + // v_stack() + // .border_2() + // .rounded_md() + // .when(side == Some(Side::Left), |this| { + // this.border_r_0().rounded_tr_none().rounded_br_none() + // }) + // .when(side == Some(Side::Right), |this| { + // this.border_l_0().rounded_bl_none().rounded_tl_none() + // }) + // .on_key_down(move |v, _, _, cx| on_click(v, cx)) + // .child(Label::new(mode.label())) // MouseEventHandler::new::(mode.region_id(), cx, |state, cx| { // let theme = cx.theme(); // let style = theme diff --git a/crates/workspace2/src/toolbar.rs b/crates/workspace2/src/toolbar.rs index 6ad03d8a84a108520d4b892be637bd41cc0a905c..e03e53331465bdf8d22975ca5d90e1e22f7b1499 100644 --- a/crates/workspace2/src/toolbar.rs +++ b/crates/workspace2/src/toolbar.rs @@ -1,8 +1,9 @@ use crate::ItemHandle; use gpui::{ - div, AnyView, Div, Entity, EntityId, EventEmitter, ParentElement, Render, Styled, View, - ViewContext, WindowContext, + div, AnyView, Component, Div, Entity, EntityId, EventEmitter, ParentElement, Render, Styled, + View, ViewContext, WindowContext, }; +use ui::{h_stack, v_stack, Button, Icon, IconButton, Label, LabelColor, StyledExt}; pub enum ToolbarItemEvent { ChangeLocation(ToolbarItemLocation), @@ -40,8 +41,8 @@ trait ToolbarItemViewHandle: Send { #[derive(Copy, Clone, Debug, PartialEq)] pub enum ToolbarItemLocation { Hidden, - PrimaryLeft { flex: Option<(f32, bool)> }, - PrimaryRight { flex: Option<(f32, bool)> }, + PrimaryLeft, + PrimaryRight, Secondary, } @@ -52,12 +53,54 @@ pub struct Toolbar { items: Vec<(Box, ToolbarItemLocation)>, } +impl Toolbar { + fn left_items(&self) -> impl Iterator { + self.items.iter().filter_map(|(item, location)| { + if *location == ToolbarItemLocation::PrimaryLeft { + Some(item.as_ref()) + } else { + None + } + }) + } + + fn right_items(&self) -> impl Iterator { + self.items.iter().filter_map(|(item, location)| { + if *location == ToolbarItemLocation::PrimaryRight { + Some(item.as_ref()) + } else { + None + } + }) + } +} + impl Render for Toolbar { type Element = Div; fn render(&mut self, cx: &mut ViewContext) -> Self::Element { //dbg!(&self.items.len()); - div().children(self.items.iter().map(|(child, _)| child.to_any())) + v_stack() + .child( + h_stack() + .justify_between() + .child( + // Toolbar left side + h_stack() + .p_1() + .child(Button::new("crates")) + .child(Label::new("/").color(LabelColor::Muted)) + .child(Button::new("workspace2")), + ) + // Toolbar right side + .child( + h_stack() + .p_1() + .child(IconButton::new("buffer-search", Icon::MagnifyingGlass)) + .child(IconButton::new("inline-assist", Icon::MagicWand)), + ), + ) + .children(self.items.iter().map(|(child, _)| child.to_any())) } } From c14efb74d7477bbcc50e19b18c8988e2c3e571bf Mon Sep 17 00:00:00 2001 From: Piotr Osiewicz <24362066+osiewicz@users.noreply.github.com> Date: Tue, 14 Nov 2023 18:18:52 +0100 Subject: [PATCH 04/69] Finish up touchups for search UI. Co-authored-by: Nate --- crates/search2/src/buffer_search.rs | 62 +++++++++++++++++++---------- crates/search2/src/search.rs | 4 +- crates/workspace2/src/toolbar.rs | 3 ++ 3 files changed, 46 insertions(+), 23 deletions(-) diff --git a/crates/search2/src/buffer_search.rs b/crates/search2/src/buffer_search.rs index 72c93905114ca79eb451546a9f09ae89f90bcdce..83f1089887cce5548353636de8c2bb0baf5fd8b2 100644 --- a/crates/search2/src/buffer_search.rs +++ b/crates/search2/src/buffer_search.rs @@ -117,7 +117,7 @@ impl Render for BufferSearchBar { // } // (None, None) => String::new(), // }; - let new_placeholder_text = Arc::from("Fix this up!"); + let new_placeholder_text = Arc::from("Search for.."); self.query_editor.update(cx, |editor, cx| { editor.set_placeholder_text(new_placeholder_text, cx); }); @@ -172,18 +172,22 @@ impl Render for BufferSearchBar { cx, ) }; - div() - .border() - .border_color(blue()) - .flex() - .justify_between() + let should_show_replace_input = self.replace_enabled && supported_options.replacement; + let replace_all = should_show_replace_input.then(|| { + super::replace_action::(ReplaceAll, "Replace all", ui::Icon::ReplaceAll) + }); + let replace_next = should_show_replace_input + .then(|| super::replace_action::(ReplaceNext, "Replace next", ui::Icon::Replace)); + h_stack() + .w_full() + .p_1() .child( div() .flex() + .flex_1() .border_1() .border_color(red()) .rounded_md() - .w_96() .items_center() .child(IconElement::new(Icon::MagnifyingGlass)) .child(self.query_editor.clone()) @@ -198,21 +202,35 @@ impl Render for BufferSearchBar { .then(|| search_option_button(SearchOptions::WHOLE_WORD)), ), ) - .child(ButtonGroup::new(vec![ - search_button_for_mode(SearchMode::Text, Some(Side::Left), cx), - search_button_for_mode(SearchMode::Regex, Some(Side::Right), cx), - ])) - .when(supported_options.replacement, |this| { - this.child(super::toggle_replace_button(self.replace_enabled)) - }) - .when(self.replace_enabled, |this| { - this.child(div().w_80().child(self.replacement_editor.clone())) - }) - .children(match_count) - .child(nav_button_for_direction("<", Direction::Prev, cx)) - .child(nav_button_for_direction(">", Direction::Next, cx)) - .flex() - .justify_between() + .child( + h_stack() + .flex_none() + .child(ButtonGroup::new(vec![ + search_button_for_mode(SearchMode::Text, Some(Side::Left), cx), + search_button_for_mode(SearchMode::Regex, Some(Side::Right), cx), + ])) + .when(supported_options.replacement, |this| { + this.child(super::toggle_replace_button(self.replace_enabled)) + }), + ) + .child( + h_stack() + .gap_0p5() + .flex_1() + .when(self.replace_enabled, |this| { + this.child(self.replacement_editor.clone()) + .children(replace_next) + .children(replace_all) + }), + ) + .child( + h_stack() + .gap_0p5() + .flex_none() + .children(match_count) + .child(nav_button_for_direction("<", Direction::Prev, cx)) + .child(nav_button_for_direction(">", Direction::Next, cx)), + ) // let query_column = Flex::row() // .with_child( diff --git a/crates/search2/src/search.rs b/crates/search2/src/search.rs index 955328c48365b10aaab6093877a7059af77f5661..e4175e9e128ae785f378a8c1c2aecf92d601b3ae 100644 --- a/crates/search2/src/search.rs +++ b/crates/search2/src/search.rs @@ -109,8 +109,10 @@ fn toggle_replace_button(active: bool) -> impl Component { fn replace_action( action: impl Action + 'static + Send + Sync, name: &'static str, + icon: ui::Icon, ) -> impl Component { - ui::IconButton::new(0, ui::Icon::Replace).on_click(move |_: &mut V, cx| { + // todo: add tooltip + ui::IconButton::new(0, icon).on_click(move |_: &mut V, cx| { cx.dispatch_action(action.boxed_clone()); }) } diff --git a/crates/workspace2/src/toolbar.rs b/crates/workspace2/src/toolbar.rs index e03e53331465bdf8d22975ca5d90e1e22f7b1499..c5ef75a32d5d0855a85c7505b764b5ca57ed465f 100644 --- a/crates/workspace2/src/toolbar.rs +++ b/crates/workspace2/src/toolbar.rs @@ -3,6 +3,7 @@ use gpui::{ div, AnyView, Component, Div, Entity, EntityId, EventEmitter, ParentElement, Render, Styled, View, ViewContext, WindowContext, }; +use theme2::ActiveTheme; use ui::{h_stack, v_stack, Button, Icon, IconButton, Label, LabelColor, StyledExt}; pub enum ToolbarItemEvent { @@ -81,6 +82,8 @@ impl Render for Toolbar { fn render(&mut self, cx: &mut ViewContext) -> Self::Element { //dbg!(&self.items.len()); v_stack() + .border_b() + .border_color(cx.theme().colors().border) .child( h_stack() .justify_between() From c37faf0ab33c818398ba8de05be0fc5b7448890d Mon Sep 17 00:00:00 2001 From: Piotr Osiewicz <24362066+osiewicz@users.noreply.github.com> Date: Wed, 15 Nov 2023 15:05:48 +0100 Subject: [PATCH 05/69] Add query history and replace buttons --- crates/search2/src/buffer_search.rs | 215 ++++++++++++++-------------- crates/search2/src/search.rs | 5 +- crates/search2/src/search_bar.rs | 17 +-- 3 files changed, 113 insertions(+), 124 deletions(-) diff --git a/crates/search2/src/buffer_search.rs b/crates/search2/src/buffer_search.rs index ccacff12aee080af441b9af0846672ed84eb2135..4e8584b80441b716e7335b6f0e2d6a1f8889115d 100644 --- a/crates/search2/src/buffer_search.rs +++ b/crates/search2/src/buffer_search.rs @@ -11,8 +11,8 @@ use editor::Editor; use futures::channel::oneshot; use gpui::{ action, actions, div, red, Action, AppContext, Component, Div, EventEmitter, - ParentComponent as _, Render, Styled, Subscription, Task, View, ViewContext, - VisualContext as _, WindowContext, + InteractiveComponent, ParentComponent as _, Render, Styled, Subscription, Task, View, + ViewContext, VisualContext as _, WindowContext, }; use project::search::SearchQuery; use std::{any::Any, sync::Arc}; @@ -38,7 +38,6 @@ pub enum Event { } pub fn init(cx: &mut AppContext) { - dbg!("Registered"); cx.observe_new_views(|workspace: &mut Workspace, _| BufferSearchBar::register(workspace)) .detach(); } @@ -78,45 +77,51 @@ impl Render for BufferSearchBar { .map(|active_searchable_item| active_searchable_item.supported_options()) .unwrap_or_default(); - // let previous_query_keystrokes = - // cx.binding_for_action(&PreviousHistoryQuery {}) - // .map(|binding| { - // binding - // .keystrokes() - // .iter() - // .map(|k| k.to_string()) - // .collect::>() - // }); - // let next_query_keystrokes = cx.binding_for_action(&NextHistoryQuery {}).map(|binding| { - // binding - // .keystrokes() - // .iter() - // .map(|k| k.to_string()) - // .collect::>() - // }); - // let new_placeholder_text = match (previous_query_keystrokes, next_query_keystrokes) { - // (Some(previous_query_keystrokes), Some(next_query_keystrokes)) => { - // format!( - // "Search ({}/{} for previous/next query)", - // previous_query_keystrokes.join(" "), - // next_query_keystrokes.join(" ") - // ) - // } - // (None, Some(next_query_keystrokes)) => { - // format!( - // "Search ({} for next query)", - // next_query_keystrokes.join(" ") - // ) - // } - // (Some(previous_query_keystrokes), None) => { - // format!( - // "Search ({} for previous query)", - // previous_query_keystrokes.join(" ") - // ) - // } - // (None, None) => String::new(), - // }; - let new_placeholder_text = Arc::from("Search for.."); + let previous_query_keystrokes = cx + .bindings_for_action(&PreviousHistoryQuery {}) + .into_iter() + .next() + .map(|binding| { + binding + .keystrokes() + .iter() + .map(|k| k.to_string()) + .collect::>() + }); + let next_query_keystrokes = cx + .bindings_for_action(&NextHistoryQuery {}) + .into_iter() + .next() + .map(|binding| { + binding + .keystrokes() + .iter() + .map(|k| k.to_string()) + .collect::>() + }); + let new_placeholder_text = match (previous_query_keystrokes, next_query_keystrokes) { + (Some(previous_query_keystrokes), Some(next_query_keystrokes)) => { + format!( + "Search ({}/{} for previous/next query)", + previous_query_keystrokes.join(" "), + next_query_keystrokes.join(" ") + ) + } + (None, Some(next_query_keystrokes)) => { + format!( + "Search ({} for next query)", + next_query_keystrokes.join(" ") + ) + } + (Some(previous_query_keystrokes), None) => { + format!( + "Search ({} for previous query)", + previous_query_keystrokes.join(" ") + ) + } + (None, None) => String::new(), + }; + let new_placeholder_text = Arc::from(new_placeholder_text); self.query_editor.update(cx, |editor, cx| { editor.set_placeholder_text(new_placeholder_text, cx); }); @@ -159,9 +164,9 @@ impl Render for BufferSearchBar { Some(ui::Label::new(message)) }); - let nav_button_for_direction = |label, direction, cx: &mut ViewContext| { + let nav_button_for_direction = |icon, direction, cx: &mut ViewContext| { render_nav_button( - label, + icon, direction, self.active_match_index.is_some(), move |this, cx| match direction { @@ -172,12 +177,32 @@ impl Render for BufferSearchBar { ) }; let should_show_replace_input = self.replace_enabled && supported_options.replacement; - let replace_all = should_show_replace_input.then(|| { - super::replace_action::(ReplaceAll, "Replace all", ui::Icon::ReplaceAll) - }); + let replace_all = should_show_replace_input + .then(|| super::render_replace_button::(ReplaceAll, ui::Icon::ReplaceAll)); let replace_next = should_show_replace_input - .then(|| super::replace_action::(ReplaceNext, "Replace next", ui::Icon::Replace)); + .then(|| super::render_replace_button::(ReplaceNext, ui::Icon::Replace)); + let in_replace = self.replacement_editor.focus_handle(cx).is_focused(cx); + h_stack() + .key_context("BufferSearchBar") + .when(in_replace, |this| { + this.key_context("in_replace") + .on_action(Self::replace_next) + .on_action(Self::replace_all) + }) + .on_action(Self::previous_history_query) + .on_action(Self::next_history_query) + .when(supported_options.case, |this| { + this.on_action(Self::toggle_case_sensitive) + }) + .when(supported_options.word, |this| { + this.on_action(Self::toggle_whole_word) + }) + .when(supported_options.replacement, |this| { + this.on_action(Self::toggle_replace) + }) + .on_action(Self::select_next_match) + .on_action(Self::select_prev_match) .w_full() .p_1() .child( @@ -226,9 +251,18 @@ impl Render for BufferSearchBar { h_stack() .gap_0p5() .flex_none() + .child(self.render_action_button(cx)) .children(match_count) - .child(nav_button_for_direction("<", Direction::Prev, cx)) - .child(nav_button_for_direction(">", Direction::Next, cx)), + .child(nav_button_for_direction( + ui::Icon::ChevronLeft, + Direction::Prev, + cx, + )) + .child(nav_button_for_direction( + ui::Icon::ChevronRight, + Direction::Next, + cx, + )), ) // let query_column = Flex::row() @@ -343,7 +377,7 @@ impl ToolbarItemView for BufferSearchBar { cx.notify(); self.active_searchable_item_subscription.take(); self.active_searchable_item.take(); - dbg!("Take?"); + self.pending_search.take(); if let Some(searchable_item_handle) = @@ -382,7 +416,8 @@ impl BufferSearchBar { workspace.register_action(|workspace, a: &Deploy, cx| { workspace.active_pane().update(cx, |this, cx| { this.toolbar().update(cx, |this, cx| { - if this.item_of_type::().is_some() { + if let Some(search_bar) = this.item_of_type::() { + search_bar.update(cx, |this, cx| this.dismiss(&Dismiss, cx)); return; } let view = cx.build_view(|cx| BufferSearchBar::new(cx)); @@ -392,46 +427,8 @@ impl BufferSearchBar { }) }); }); - fn register_action( - workspace: &mut Workspace, - update: fn(&mut BufferSearchBar, &mut ViewContext<'_, BufferSearchBar>), - ) { - workspace.register_action(move |workspace, _: &A, cx| { - workspace.active_pane().update(cx, move |this, cx| { - this.toolbar().update(cx, move |toolbar, cx| { - let Some(search_bar) = toolbar.item_of_type::() else { - return; - }; - search_bar.update(cx, |this, cx| update(this, cx)) - }) - }); - }); - } - register_action::(workspace, |this, cx| { - this.toggle_search_option(SearchOptions::CASE_SENSITIVE, cx) - }); - register_action::(workspace, |this: &mut BufferSearchBar, cx| { - this.toggle_search_option(SearchOptions::WHOLE_WORD, cx) - }); - register_action::(workspace, |this: &mut BufferSearchBar, cx| { - dbg!("Toggling"); - this.toggle_replace(&ToggleReplace, cx) - }); - // workspace.register_action(|workspace, _: &ToggleCaseSensitive, cx| { - // workspace.active_pane().update(cx, |this, cx| { - // this.toolbar().update(cx, |toolbar, cx| { - // let Some(search_bar) = toolbar.item_of_type::() else { - // return; - // }; - // search_bar.update(cx, |this, cx| { - // this.toggle_search_option(SearchOptions::CASE_SENSITIVE, cx); - // }) - // }) - // }); - // }); } pub fn new(cx: &mut ViewContext) -> Self { - dbg!("New"); let query_editor = cx.build_view(|cx| Editor::single_line(cx)); cx.subscribe(&query_editor, Self::on_query_editor_event) .detach(); @@ -463,7 +460,7 @@ impl BufferSearchBar { pub fn dismiss(&mut self, _: &Dismiss, cx: &mut ViewContext) { self.dismissed = true; - dbg!("Dismissed :("); + for searchable_item in self.searchable_items_with_matches.keys() { if let Some(searchable_item) = WeakSearchableItemHandle::upgrade(searchable_item.as_ref(), cx) @@ -495,10 +492,9 @@ impl BufferSearchBar { pub fn show(&mut self, cx: &mut ViewContext) -> bool { if self.active_searchable_item.is_none() { - dbg!("Hey"); return false; } - dbg!("not dismissed"); + self.dismissed = false; cx.notify(); cx.emit(Event::UpdateLocation); @@ -590,13 +586,7 @@ impl BufferSearchBar { self.update_matches(cx) } - fn render_action_button( - &self, - icon: &'static str, - cx: &mut ViewContext, - ) -> impl Component { - let tooltip = "Select All Matches"; - let theme = cx.theme(); + fn render_action_button(&self, cx: &mut ViewContext) -> impl Component { // let tooltip_style = theme.tooltip.clone(); // let style = theme.search.action_button.clone(); @@ -695,8 +685,13 @@ impl BufferSearchBar { .searchable_items_with_matches .get(&searchable_item.downgrade()) { - let new_match_index = searchable_item - .match_index_for_direction(matches, index, direction, count, cx); + let new_match_index = searchable_item.match_index_for_direction( + matches, + index, + direction, + dbg!(count), + cx, + ); searchable_item.update_matches(matches, cx); searchable_item.activate_match(new_match_index, matches, cx); } @@ -769,6 +764,7 @@ impl BufferSearchBar { } fn on_active_searchable_item_event(&mut self, event: &SearchEvent, cx: &mut ViewContext) { + dbg!(&event); match event { SearchEvent::MatchesInvalidated => { let _ = self.update_matches(cx); @@ -777,6 +773,12 @@ impl BufferSearchBar { } } + fn toggle_case_sensitive(&mut self, _: &ToggleCaseSensitive, cx: &mut ViewContext) { + self.toggle_search_option(SearchOptions::CASE_SENSITIVE, cx) + } + fn toggle_whole_word(&mut self, _: &ToggleWholeWord, cx: &mut ViewContext) { + self.toggle_search_option(SearchOptions::WHOLE_WORD, cx) + } fn clear_matches(&mut self, cx: &mut ViewContext) { let mut active_item_matches = None; for (searchable_item, matches) in self.searchable_items_with_matches.drain() { @@ -799,7 +801,7 @@ impl BufferSearchBar { let (done_tx, done_rx) = oneshot::channel(); let query = self.query(cx); self.pending_search.take(); - dbg!("update_matches"); + if let Some(active_searchable_item) = self.active_searchable_item.as_ref() { if query.is_empty() { self.active_match_index.take(); @@ -841,26 +843,23 @@ impl BufferSearchBar { .into(); self.active_search = Some(query.clone()); let query_text = query.as_str().to_string(); - dbg!(&query_text); + let matches = active_searchable_item.find_matches(query, cx); let active_searchable_item = active_searchable_item.downgrade(); self.pending_search = Some(cx.spawn(|this, mut cx| async move { let matches = matches.await; - //dbg!(&matches); + this.update(&mut cx, |this, cx| { - dbg!("Updating!!"); if let Some(active_searchable_item) = WeakSearchableItemHandle::upgrade(active_searchable_item.as_ref(), cx) { - dbg!("in if!!"); this.searchable_items_with_matches .insert(active_searchable_item.downgrade(), matches); this.update_match_index(cx); this.search_history.add(query_text); if !this.dismissed { - dbg!("Not dismissed"); let matches = this .searchable_items_with_matches .get(&active_searchable_item.downgrade()) diff --git a/crates/search2/src/search.rs b/crates/search2/src/search.rs index e4175e9e128ae785f378a8c1c2aecf92d601b3ae..233975839f847583b5e3fac4998b4cfc95e2f7d3 100644 --- a/crates/search2/src/search.rs +++ b/crates/search2/src/search.rs @@ -1,6 +1,6 @@ use bitflags::bitflags; pub use buffer_search::BufferSearchBar; -use gpui::{actions, Action, AnyElement, AppContext, Component, Element, Svg, View}; +use gpui::{actions, Action, AppContext, Component}; pub use mode::SearchMode; use project::search::SearchQuery; use ui::ButtonVariant; @@ -106,9 +106,8 @@ fn toggle_replace_button(active: bool) -> impl Component { .when(active, |button| button.variant(ButtonVariant::Filled)) } -fn replace_action( +fn render_replace_button( action: impl Action + 'static + Send + Sync, - name: &'static str, icon: ui::Icon, ) -> impl Component { // todo: add tooltip diff --git a/crates/search2/src/search_bar.rs b/crates/search2/src/search_bar.rs index 99735c5898479c0d52d229a5efd84371a5c6db47..1c77a03741de5b2e9f47324e1311a7a5830a6c75 100644 --- a/crates/search2/src/search_bar.rs +++ b/crates/search2/src/search_bar.rs @@ -1,28 +1,19 @@ use std::{borrow::Cow, sync::Arc}; use gpui::{div, Action, Component, ViewContext}; -use ui::{Button, ButtonVariant}; +use ui::{Button, ButtonVariant, IconButton}; use workspace::searchable::Direction; use crate::mode::{SearchMode, Side}; pub(super) fn render_nav_button( - icon: &'static str, + icon: ui::Icon, direction: Direction, + active: bool, on_click: impl Fn(&mut V, &mut ViewContext) + 'static + Send + Sync, cx: &mut ViewContext, ) -> impl Component { - let tooltip; - - match direction { - Direction::Prev => { - tooltip = "Select Previous Match"; - } - Direction::Next => { - tooltip = "Select Next Match"; - } - }; // let tooltip_style = cx.theme().tooltip.clone(); // let cursor_style = if active { // CursorStyle::PointingHand @@ -30,7 +21,7 @@ pub(super) fn render_nav_button( // CursorStyle::default() // }; // enum NavButton {} - Button::new(icon).on_click(Arc::new(on_click)) + IconButton::new("search-nav-button", icon).on_click(on_click) // MouseEventHandler::new::(direction as usize, cx, |state, cx| { // let theme = cx.theme(); // let style = theme From 69e01e67dcba9b52bceeefe5ab049012d4069056 Mon Sep 17 00:00:00 2001 From: Piotr Osiewicz <24362066+osiewicz@users.noreply.github.com> Date: Wed, 15 Nov 2023 15:22:35 +0100 Subject: [PATCH 06/69] Bind cycle_mode action --- crates/search2/src/buffer_search.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/crates/search2/src/buffer_search.rs b/crates/search2/src/buffer_search.rs index 4e8584b80441b716e7335b6f0e2d6a1f8889115d..7f2d2b09101ca85683068b22f69dbba192507fcb 100644 --- a/crates/search2/src/buffer_search.rs +++ b/crates/search2/src/buffer_search.rs @@ -203,6 +203,7 @@ impl Render for BufferSearchBar { }) .on_action(Self::select_next_match) .on_action(Self::select_prev_match) + .on_action(Self::cycle_mode) .w_full() .p_1() .child( From 781a95d2e3e06d028f35c047b05cf023a4762049 Mon Sep 17 00:00:00 2001 From: Antonio Scandurra Date: Thu, 16 Nov 2023 14:47:45 +0100 Subject: [PATCH 07/69] Add back Completion::documentation --- crates/language2/src/buffer.rs | 41 ++++++++++++++++++++++++++++++ crates/language2/src/proto.rs | 1 + crates/project2/src/lsp_command.rs | 25 +++++++++++++++--- 3 files changed, 63 insertions(+), 4 deletions(-) diff --git a/crates/language2/src/buffer.rs b/crates/language2/src/buffer.rs index 2c8c55d5776d1970e4597f86372aef9d65627c16..cd77dc58590c99c6a0f649297ae70b6cafaa8ad5 100644 --- a/crates/language2/src/buffer.rs +++ b/crates/language2/src/buffer.rs @@ -7,6 +7,7 @@ pub use crate::{ use crate::{ diagnostic_set::{DiagnosticEntry, DiagnosticGroup}, language_settings::{language_settings, LanguageSettings}, + markdown::parse_markdown, outline::OutlineItem, syntax_map::{ SyntaxLayerInfo, SyntaxMap, SyntaxMapCapture, SyntaxMapCaptures, SyntaxMapMatches, @@ -144,12 +145,52 @@ pub struct Diagnostic { pub is_unnecessary: bool, } +pub async fn prepare_completion_documentation( + documentation: &lsp::Documentation, + language_registry: &Arc, + language: Option>, +) -> Documentation { + match documentation { + lsp::Documentation::String(text) => { + if text.lines().count() <= 1 { + Documentation::SingleLine(text.clone()) + } else { + Documentation::MultiLinePlainText(text.clone()) + } + } + + lsp::Documentation::MarkupContent(lsp::MarkupContent { kind, value }) => match kind { + lsp::MarkupKind::PlainText => { + if value.lines().count() <= 1 { + Documentation::SingleLine(value.clone()) + } else { + Documentation::MultiLinePlainText(value.clone()) + } + } + + lsp::MarkupKind::Markdown => { + let parsed = parse_markdown(value, language_registry, language).await; + Documentation::MultiLineMarkdown(parsed) + } + }, + } +} + +#[derive(Clone, Debug)] +pub enum Documentation { + Undocumented, + SingleLine(String), + MultiLinePlainText(String), + MultiLineMarkdown(ParsedMarkdown), +} + #[derive(Clone, Debug)] pub struct Completion { pub old_range: Range, pub new_text: String, pub label: CodeLabel, pub server_id: LanguageServerId, + pub documentation: Option, pub lsp_completion: lsp::CompletionItem, } diff --git a/crates/language2/src/proto.rs b/crates/language2/src/proto.rs index c4abe39d4782aafbe90594e3a0bc5de70787fa03..957f4ee7fbc4da251698fd640d33d37d5cf4a06b 100644 --- a/crates/language2/src/proto.rs +++ b/crates/language2/src/proto.rs @@ -482,6 +482,7 @@ pub async fn deserialize_completion( lsp_completion.filter_text.as_deref(), ) }), + documentation: None, server_id: LanguageServerId(completion.server_id as usize), lsp_completion, }) diff --git a/crates/project2/src/lsp_command.rs b/crates/project2/src/lsp_command.rs index cc1821d3fff744b557568f9e11d1fd1b98c4acdb..94c277db1e0277c56493837d45e37a9c0b38e4c0 100644 --- a/crates/project2/src/lsp_command.rs +++ b/crates/project2/src/lsp_command.rs @@ -10,7 +10,7 @@ use futures::future; use gpui::{AppContext, AsyncAppContext, Model}; use language::{ language_settings::{language_settings, InlayHintKind}, - point_from_lsp, point_to_lsp, + point_from_lsp, point_to_lsp, prepare_completion_documentation, proto::{deserialize_anchor, deserialize_version, serialize_anchor, serialize_version}, range_from_lsp, range_to_lsp, Anchor, Bias, Buffer, BufferSnapshot, CachedLspAdapter, CharKind, CodeAction, Completion, OffsetRangeExt, PointUtf16, ToOffset, ToPointUtf16, Transaction, @@ -1339,7 +1339,7 @@ impl LspCommand for GetCompletions { async fn response_from_lsp( self, completions: Option, - _: Model, + project: Model, buffer: Model, server_id: LanguageServerId, mut cx: AsyncAppContext, @@ -1359,7 +1359,8 @@ impl LspCommand for GetCompletions { Default::default() }; - let completions = buffer.update(&mut cx, |buffer, _| { + let completions = buffer.update(&mut cx, |buffer, cx| { + let language_registry = project.read(cx).languages().clone(); let language = buffer.language().cloned(); let snapshot = buffer.snapshot(); let clipped_position = buffer.clip_point_utf16(Unclipped(self.position), Bias::Left); @@ -1443,14 +1444,29 @@ impl LspCommand for GetCompletions { } }; + let language_registry = language_registry.clone(); let language = language.clone(); LineEnding::normalize(&mut new_text); Some(async move { let mut label = None; - if let Some(language) = language { + if let Some(language) = language.as_ref() { language.process_completion(&mut lsp_completion).await; label = language.label_for_completion(&lsp_completion).await; } + + let documentation = if let Some(lsp_docs) = &lsp_completion.documentation { + Some( + prepare_completion_documentation( + lsp_docs, + &language_registry, + language.clone(), + ) + .await, + ) + } else { + None + }; + Completion { old_range, new_text, @@ -1460,6 +1476,7 @@ impl LspCommand for GetCompletions { lsp_completion.filter_text.as_deref(), ) }), + documentation, server_id, lsp_completion, } From c08ce1c3b86e95603da73d226c0945278b03ac17 Mon Sep 17 00:00:00 2001 From: Antonio Scandurra Date: Thu, 16 Nov 2023 14:55:06 +0100 Subject: [PATCH 08/69] Start rendering autocompletion menu --- crates/editor2/src/editor.rs | 467 +++++++++++++++++------------------ 1 file changed, 228 insertions(+), 239 deletions(-) diff --git a/crates/editor2/src/editor.rs b/crates/editor2/src/editor.rs index 8e7bd5876f7d69c3254464ff9d32b31ad5877037..48c61c0f32ee3a1369d14d73ae170a9d4994bf00 100644 --- a/crates/editor2/src/editor.rs +++ b/crates/editor2/src/editor.rs @@ -54,13 +54,13 @@ use itertools::Itertools; pub use language::{char_kind, CharKind}; use language::{ language_settings::{self, all_language_settings, InlayHintSettings}, - point_from_lsp, AutoindentMode, BracketPair, Buffer, CodeAction, Completion, CursorShape, - Diagnostic, IndentKind, IndentSize, Language, LanguageRegistry, LanguageServerName, - OffsetRangeExt, Point, Selection, SelectionGoal, TransactionId, + point_from_lsp, AutoindentMode, BracketPair, Buffer, CodeAction, CodeLabel, Completion, + CursorShape, Diagnostic, Documentation, IndentKind, IndentSize, Language, LanguageRegistry, + LanguageServerName, OffsetRangeExt, Point, Selection, SelectionGoal, TransactionId, }; use lazy_static::lazy_static; use link_go_to_definition::{GoToDefinitionLink, InlayHighlight, LinkGoToDefinitionState}; -use lsp::{DiagnosticSeverity, Documentation, LanguageServerId}; +use lsp::{DiagnosticSeverity, LanguageServerId}; use movement::TextLayoutDetails; use multi_buffer::ToOffsetUtf16; pub use multi_buffer::{ @@ -97,7 +97,7 @@ use text::{OffsetUtf16, Rope}; use theme::{ ActiveTheme, DiagnosticStyle, PlayerColor, SyntaxTheme, Theme, ThemeColors, ThemeSettings, }; -use ui::{v_stack, HighlightedLabel, IconButton, StyledExt, Tooltip}; +use ui::{h_stack, v_stack, HighlightedLabel, IconButton, StyledExt, Tooltip}; use util::{post_inc, RangeExt, ResultExt, TryFutureExt}; use workspace::{ item::{ItemEvent, ItemHandle}, @@ -1224,207 +1224,195 @@ impl CompletionsMenu { workspace: Option>, cx: &mut ViewContext, ) -> AnyElement { - todo!("old implementation below") - } + // enum CompletionTag {} - // enum CompletionTag {} + let settings = EditorSettings::get_global(cx); + let show_completion_documentation = settings.show_completion_documentation; - // let settings = EditorSettings>(cx); - // let show_completion_documentation = settings.show_completion_documentation; + let widest_completion_ix = self + .matches + .iter() + .enumerate() + .max_by_key(|(_, mat)| { + let completions = self.completions.read(); + let completion = &completions[mat.candidate_id]; + let documentation = &completion.documentation; + + let mut len = completion.label.text.chars().count(); + if let Some(Documentation::SingleLine(text)) = documentation { + if show_completion_documentation { + len += text.chars().count(); + } + } - // let widest_completion_ix = self - // .matches - // .iter() - // .enumerate() - // .max_by_key(|(_, mat)| { - // let completions = self.completions.read(); - // let completion = &completions[mat.candidate_id]; - // let documentation = &completion.documentation; + len + }) + .map(|(ix, _)| ix); - // let mut len = completion.label.text.chars().count(); - // if let Some(Documentation::SingleLine(text)) = documentation { - // if show_completion_documentation { - // len += text.chars().count(); - // } - // } + let completions = self.completions.clone(); + let matches = self.matches.clone(); + let selected_item = self.selected_item; - // len - // }) - // .map(|(ix, _)| ix); + let list = uniform_list("completions", matches.len(), move |editor, range, cx| { + let start_ix = range.start; + let completions_guard = completions.read(); - // let completions = self.completions.clone(); - // let matches = self.matches.clone(); - // let selected_item = self.selected_item; - - // let list = UniformList::new(self.list.clone(), matches.len(), cx, { - // let style = style.clone(); - // move |_, range, items, cx| { - // let start_ix = range.start; - // let completions_guard = completions.read(); - - // for (ix, mat) in matches[range].iter().enumerate() { - // let item_ix = start_ix + ix; - // let candidate_id = mat.candidate_id; - // let completion = &completions_guard[candidate_id]; - - // let documentation = if show_completion_documentation { - // &completion.documentation - // } else { - // &None - // }; + matches[range] + .iter() + .enumerate() + .map(|(ix, mat)| { + let item_ix = start_ix + ix; + let candidate_id = mat.candidate_id; + let completion = &completions_guard[candidate_id]; - // items.push( - // MouseEventHandler::new::( - // mat.candidate_id, - // cx, - // |state, _| { - // let item_style = if item_ix == selected_item { - // style.autocomplete.selected_item - // } else if state.hovered() { - // style.autocomplete.hovered_item - // } else { - // style.autocomplete.item - // }; - - // let completion_label = - // Text::new(completion.label.text.clone(), style.text.clone()) - // .with_soft_wrap(false) - // .with_highlights( - // combine_syntax_and_fuzzy_match_highlights( - // &completion.label.text, - // style.text.color.into(), - // styled_runs_for_code_label( - // &completion.label, - // &style.syntax, - // ), - // &mat.positions, - // ), - // ); - - // if let Some(Documentation::SingleLine(text)) = documentation { - // Flex::row() - // .with_child(completion_label) - // .with_children((|| { - // let text_style = TextStyle { - // color: style.autocomplete.inline_docs_color, - // font_size: style.text.font_size - // * style.autocomplete.inline_docs_size_percent, - // ..style.text.clone() - // }; - - // let label = Text::new(text.clone(), text_style) - // .aligned() - // .constrained() - // .dynamically(move |constraint, _, _| { - // gpui::SizeConstraint { - // min: constraint.min, - // max: vec2f( - // constraint.max.x(), - // constraint.min.y(), - // ), - // } - // }); - - // if Some(item_ix) == widest_completion_ix { - // Some( - // label - // .contained() - // .with_style( - // style - // .autocomplete - // .inline_docs_container, - // ) - // .into_any(), - // ) - // } else { - // Some(label.flex_float().into_any()) - // } - // })()) - // .into_any() - // } else { - // completion_label.into_any() - // } - // .contained() - // .with_style(item_style) - // .constrained() - // .dynamically( - // move |constraint, _, _| { - // if Some(item_ix) == widest_completion_ix { - // constraint - // } else { - // gpui::SizeConstraint { - // min: constraint.min, - // max: constraint.min, - // } - // } - // }, - // ) - // }, - // ) - // .with_cursor_style(CursorStyle::PointingHand) - // .on_down(MouseButton::Left, move |_, this, cx| { - // this.confirm_completion( - // &ConfirmCompletion { - // item_ix: Some(item_ix), - // }, - // cx, - // ) - // .map(|task| task.detach()); - // }) - // .constrained() - // .with_min_width(style.autocomplete.completion_min_width) - // .with_max_width(style.autocomplete.completion_max_width) - // .into_any(), - // ); - // } - // } - // }) - // .with_width_from_item(widest_completion_ix); - - // enum MultiLineDocumentation {} - - // Flex::row() - // .with_child(list.flex(1., false)) - // .with_children({ - // let mat = &self.matches[selected_item]; - // let completions = self.completions.read(); - // let completion = &completions[mat.candidate_id]; - // let documentation = &completion.documentation; - - // match documentation { - // Some(Documentation::MultiLinePlainText(text)) => Some( - // Flex::column() - // .scrollable::(0, None, cx) - // .with_child( - // Text::new(text.clone(), style.text.clone()).with_soft_wrap(true), - // ) - // .contained() - // .with_style(style.autocomplete.alongside_docs_container) - // .constrained() - // .with_max_width(style.autocomplete.alongside_docs_max_width) - // .flex(1., false), - // ), - - // Some(Documentation::MultiLineMarkdown(parsed)) => Some( - // Flex::column() - // .scrollable::(0, None, cx) - // .with_child(render_parsed_markdown::( - // parsed, &style, workspace, cx, - // )) - // .contained() - // .with_style(style.autocomplete.alongside_docs_container) - // .constrained() - // .with_max_width(style.autocomplete.alongside_docs_max_width) - // .flex(1., false), - // ), - - // _ => None, - // } - // }) - // .contained() - // .with_style(style.autocomplete.container) - // .into_any() - // } + let documentation = if show_completion_documentation { + &completion.documentation + } else { + &None + }; + + // todo!("highlights") + // let highlights = combine_syntax_and_fuzzy_match_highlights( + // &completion.label.text, + // style.text.color.into(), + // styled_runs_for_code_label(&completion.label, &style.syntax), + // &mat.positions, + // ) + + // todo!("documentation") + // MouseEventHandler::new::(mat.candidate_id, cx, |state, _| { + // let completion_label = HighlightedLabel::new( + // completion.label.text.clone(), + // combine_syntax_and_fuzzy_match_highlights( + // &completion.label.text, + // style.text.color.into(), + // styled_runs_for_code_label(&completion.label, &style.syntax), + // &mat.positions, + // ), + // ); + // Text::new(completion.label.text.clone(), style.text.clone()) + // .with_soft_wrap(false) + // .with_highlights(); + + // if let Some(Documentation::SingleLine(text)) = documentation { + // h_stack() + // .child(completion_label) + // .with_children((|| { + // let text_style = TextStyle { + // color: style.autocomplete.inline_docs_color, + // font_size: style.text.font_size + // * style.autocomplete.inline_docs_size_percent, + // ..style.text.clone() + // }; + + // let label = Text::new(text.clone(), text_style) + // .aligned() + // .constrained() + // .dynamically(move |constraint, _, _| gpui::SizeConstraint { + // min: constraint.min, + // max: vec2f(constraint.max.x(), constraint.min.y()), + // }); + + // if Some(item_ix) == widest_completion_ix { + // Some( + // label + // .contained() + // .with_style(style.autocomplete.inline_docs_container) + // .into_any(), + // ) + // } else { + // Some(label.flex_float().into_any()) + // } + // })()) + // .into_any() + // } else { + // completion_label.into_any() + // } + // .contained() + // .with_style(item_style) + // .constrained() + // .dynamically(move |constraint, _, _| { + // if Some(item_ix) == widest_completion_ix { + // constraint + // } else { + // gpui::SizeConstraint { + // min: constraint.min, + // max: constraint.min, + // } + // } + // }) + // }) + // .with_cursor_style(CursorStyle::PointingHand) + // .on_down(MouseButton::Left, move |_, this, cx| { + // this.confirm_completion( + // &ConfirmCompletion { + // item_ix: Some(item_ix), + // }, + // cx, + // ) + // .map(|task| task.detach()); + // }) + // .constrained() + // + div() + .id(mat.candidate_id) + .bg(gpui::green()) + .hover(|style| style.bg(gpui::blue())) + .when(item_ix == selected_item, |div| div.bg(gpui::blue())) + .child(completion.label.text.clone()) + .min_w(px(300.)) + .max_w(px(700.)) + }) + .collect() + }) + .with_width_from_item(widest_completion_ix); + + list.render() + // todo!("multiline documentation") + // enum MultiLineDocumentation {} + + // Flex::row() + // .with_child(list.flex(1., false)) + // .with_children({ + // let mat = &self.matches[selected_item]; + // let completions = self.completions.read(); + // let completion = &completions[mat.candidate_id]; + // let documentation = &completion.documentation; + + // match documentation { + // Some(Documentation::MultiLinePlainText(text)) => Some( + // Flex::column() + // .scrollable::(0, None, cx) + // .with_child( + // Text::new(text.clone(), style.text.clone()).with_soft_wrap(true), + // ) + // .contained() + // .with_style(style.autocomplete.alongside_docs_container) + // .constrained() + // .with_max_width(style.autocomplete.alongside_docs_max_width) + // .flex(1., false), + // ), + + // Some(Documentation::MultiLineMarkdown(parsed)) => Some( + // Flex::column() + // .scrollable::(0, None, cx) + // .with_child(render_parsed_markdown::( + // parsed, &style, workspace, cx, + // )) + // .contained() + // .with_style(style.autocomplete.alongside_docs_container) + // .constrained() + // .with_max_width(style.autocomplete.alongside_docs_max_width) + // .flex(1., false), + // ), + + // _ => None, + // } + // }) + // .contained() + // .with_style(style.autocomplete.container) + // .into_any() + } pub async fn filter(&mut self, query: Option<&str>, executor: BackgroundExecutor) { let mut matches = if let Some(query) = query { @@ -10110,49 +10098,50 @@ pub fn combine_syntax_and_fuzzy_match_highlights( result } -// pub fn styled_runs_for_code_label<'a>( -// label: &'a CodeLabel, -// syntax_theme: &'a theme::SyntaxTheme, -// ) -> impl 'a + Iterator, HighlightStyle)> { -// let fade_out = HighlightStyle { -// fade_out: Some(0.35), -// ..Default::default() -// }; +pub fn styled_runs_for_code_label<'a>( + label: &'a CodeLabel, + syntax_theme: &'a theme::SyntaxTheme, +) -> impl 'a + Iterator, HighlightStyle)> { + let fade_out = HighlightStyle { + fade_out: Some(0.35), + ..Default::default() + }; + + let mut prev_end = label.filter_range.end; + label + .runs + .iter() + .enumerate() + .flat_map(move |(ix, (range, highlight_id))| { + let style = if let Some(style) = highlight_id.style(syntax_theme) { + style + } else { + return Default::default(); + }; + let mut muted_style = style; + muted_style.highlight(fade_out); -// let mut prev_end = label.filter_range.end; -// label -// .runs -// .iter() -// .enumerate() -// .flat_map(move |(ix, (range, highlight_id))| { -// let style = if let Some(style) = highlight_id.style(syntax_theme) { -// style -// } else { -// return Default::default(); -// }; -// let mut muted_style = style; -// muted_style.highlight(fade_out); - -// let mut runs = SmallVec::<[(Range, HighlightStyle); 3]>::new(); -// if range.start >= label.filter_range.end { -// if range.start > prev_end { -// runs.push((prev_end..range.start, fade_out)); -// } -// runs.push((range.clone(), muted_style)); -// } else if range.end <= label.filter_range.end { -// runs.push((range.clone(), style)); -// } else { -// runs.push((range.start..label.filter_range.end, style)); -// runs.push((label.filter_range.end..range.end, muted_style)); -// } -// prev_end = cmp::max(prev_end, range.end); + let mut runs = SmallVec::<[(Range, HighlightStyle); 3]>::new(); + if range.start >= label.filter_range.end { + if range.start > prev_end { + runs.push((prev_end..range.start, fade_out)); + } + runs.push((range.clone(), muted_style)); + } else if range.end <= label.filter_range.end { + runs.push((range.clone(), style)); + } else { + runs.push((range.start..label.filter_range.end, style)); + runs.push((label.filter_range.end..range.end, muted_style)); + } + prev_end = cmp::max(prev_end, range.end); -// if ix + 1 == label.runs.len() && label.text.len() > prev_end { -// runs.push((prev_end..label.text.len(), fade_out)); -// } + if ix + 1 == label.runs.len() && label.text.len() > prev_end { + runs.push((prev_end..label.text.len(), fade_out)); + } -// runs -// }) + runs + }) +} pub fn split_words<'a>(text: &'a str) -> impl std::iter::Iterator + 'a { let mut index = 0; From f8b91bd0f0d64b41e18af9121dd4864bfddb52f2 Mon Sep 17 00:00:00 2001 From: Piotr Osiewicz <24362066+osiewicz@users.noreply.github.com> Date: Thu, 16 Nov 2023 18:36:19 +0100 Subject: [PATCH 09/69] Fix some of the warnings --- crates/search2/src/buffer_search.rs | 89 +++++++++++------ crates/search2/src/mode.rs | 6 -- crates/search2/src/search_bar.rs | 111 +-------------------- crates/terminal_view2/src/terminal_view.rs | 2 +- 4 files changed, 58 insertions(+), 150 deletions(-) diff --git a/crates/search2/src/buffer_search.rs b/crates/search2/src/buffer_search.rs index 7f2d2b09101ca85683068b22f69dbba192507fcb..021cc570158d9b31bac1b56f58bac3e6247f2749 100644 --- a/crates/search2/src/buffer_search.rs +++ b/crates/search2/src/buffer_search.rs @@ -1,10 +1,10 @@ use crate::{ history::SearchHistory, - mode::{next_mode, SearchMode, Side}, + mode::{next_mode, SearchMode}, search_bar::{render_nav_button, render_search_mode_button}, - CycleMode, NextHistoryQuery, PreviousHistoryQuery, ReplaceAll, ReplaceNext, SearchOptions, - SelectAllMatches, SelectNextMatch, SelectPrevMatch, ToggleCaseSensitive, ToggleReplace, - ToggleWholeWord, + ActivateRegexMode, ActivateTextMode, CycleMode, NextHistoryQuery, PreviousHistoryQuery, + ReplaceAll, ReplaceNext, SearchOptions, SelectAllMatches, SelectNextMatch, SelectPrevMatch, + ToggleCaseSensitive, ToggleReplace, ToggleWholeWord, }; use collections::HashMap; use editor::Editor; @@ -16,7 +16,6 @@ use gpui::{ }; use project::search::SearchQuery; use std::{any::Any, sync::Arc}; -use theme::ActiveTheme; use ui::{h_stack, ButtonGroup, Icon, IconButton, IconElement}; use util::ResultExt; @@ -129,18 +128,12 @@ impl Render for BufferSearchBar { editor.set_placeholder_text("Replace with...", cx); }); - let search_button_for_mode = |mode, side, cx: &mut ViewContext| { + let search_button_for_mode = |mode| { let is_active = self.current_mode == mode; - render_search_mode_button( - mode, - side, - is_active, - move |this, cx| { - this.activate_search_mode(mode, cx); - }, - cx, - ) + render_search_mode_button(mode, is_active, move |this: &mut Self, cx| { + this.activate_search_mode(mode, cx); + }) }; let search_option_button = |option| { let is_active = self.search_options.contains(option); @@ -164,16 +157,14 @@ impl Render for BufferSearchBar { Some(ui::Label::new(message)) }); - let nav_button_for_direction = |icon, direction, cx: &mut ViewContext| { + let nav_button_for_direction = |icon, direction| { render_nav_button( icon, - direction, self.active_match_index.is_some(), - move |this, cx| match direction { + move |this: &mut Self, cx| match direction { Direction::Prev => this.select_prev_match(&Default::default(), cx), Direction::Next => this.select_next_match(&Default::default(), cx), }, - cx, ) }; let should_show_replace_input = self.replace_enabled && supported_options.replacement; @@ -231,8 +222,8 @@ impl Render for BufferSearchBar { h_stack() .flex_none() .child(ButtonGroup::new(vec![ - search_button_for_mode(SearchMode::Text, Some(Side::Left), cx), - search_button_for_mode(SearchMode::Regex, Some(Side::Right), cx), + search_button_for_mode(SearchMode::Text), + search_button_for_mode(SearchMode::Regex), ])) .when(supported_options.replacement, |this| { this.child(super::toggle_replace_button(self.replace_enabled)) @@ -252,17 +243,15 @@ impl Render for BufferSearchBar { h_stack() .gap_0p5() .flex_none() - .child(self.render_action_button(cx)) + .child(self.render_action_button()) .children(match_count) .child(nav_button_for_direction( ui::Icon::ChevronLeft, Direction::Prev, - cx, )) .child(nav_button_for_direction( ui::Icon::ChevronRight, Direction::Next, - cx, )), ) @@ -428,6 +417,46 @@ impl BufferSearchBar { }) }); }); + fn register_action( + workspace: &mut Workspace, + update: fn(&mut BufferSearchBar, &A, &mut ViewContext), + ) { + workspace.register_action(move |workspace, action: &A, cx| { + workspace.active_pane().update(cx, move |this, cx| { + this.toolbar().update(cx, move |this, cx| { + if let Some(search_bar) = this.item_of_type::() { + search_bar.update(cx, move |this, cx| update(this, action, cx)); + cx.notify(); + } + }) + }); + }); + } + + register_action(workspace, |this, action: &ToggleCaseSensitive, cx| { + this.toggle_case_sensitive(action, cx); + }); + register_action(workspace, |this, action: &ToggleWholeWord, cx| { + this.toggle_whole_word(action, cx); + }); + register_action(workspace, |this, action: &ToggleReplace, cx| { + this.toggle_replace(action, cx); + }); + register_action(workspace, |this, action: &ActivateRegexMode, cx| { + this.activate_search_mode(SearchMode::Regex, cx); + }); + register_action(workspace, |this, action: &ActivateTextMode, cx| { + this.activate_search_mode(SearchMode::Text, cx); + }); + register_action(workspace, |this, action: &SelectNextMatch, cx| { + this.select_next_match(action, cx); + }); + register_action(workspace, |this, action: &SelectPrevMatch, cx| { + this.select_prev_match(action, cx); + }); + register_action(workspace, |this, action: &SelectAllMatches, cx| { + this.select_all_matches(action, cx); + }); } pub fn new(cx: &mut ViewContext) -> Self { let query_editor = cx.build_view(|cx| Editor::single_line(cx)); @@ -587,7 +616,7 @@ impl BufferSearchBar { self.update_matches(cx) } - fn render_action_button(&self, cx: &mut ViewContext) -> impl Component { + fn render_action_button(&self) -> impl Component { // let tooltip_style = theme.tooltip.clone(); // let style = theme.search.action_button.clone(); @@ -686,13 +715,8 @@ impl BufferSearchBar { .searchable_items_with_matches .get(&searchable_item.downgrade()) { - let new_match_index = searchable_item.match_index_for_direction( - matches, - index, - direction, - dbg!(count), - cx, - ); + let new_match_index = searchable_item + .match_index_for_direction(matches, index, direction, count, cx); searchable_item.update_matches(matches, cx); searchable_item.activate_match(new_match_index, matches, cx); } @@ -765,7 +789,6 @@ impl BufferSearchBar { } fn on_active_searchable_item_event(&mut self, event: &SearchEvent, cx: &mut ViewContext) { - dbg!(&event); match event { SearchEvent::MatchesInvalidated => { let _ = self.update_matches(cx); diff --git a/crates/search2/src/mode.rs b/crates/search2/src/mode.rs index 8afc2bd3f496cc502f5ffd53fec5b05973108501..bb729cb6c0c198cb1034f04a31125b6df7325e96 100644 --- a/crates/search2/src/mode.rs +++ b/crates/search2/src/mode.rs @@ -10,12 +10,6 @@ pub enum SearchMode { Regex, } -#[derive(Copy, Clone, Debug, PartialEq)] -pub(crate) enum Side { - Left, - Right, -} - impl SearchMode { pub(crate) fn label(&self) -> &'static str { match self { diff --git a/crates/search2/src/search_bar.rs b/crates/search2/src/search_bar.rs index 1c77a03741de5b2e9f47324e1311a7a5830a6c75..46a3357763cbe6188ade787fdf45417a022d39e6 100644 --- a/crates/search2/src/search_bar.rs +++ b/crates/search2/src/search_bar.rs @@ -4,15 +4,12 @@ use gpui::{div, Action, Component, ViewContext}; use ui::{Button, ButtonVariant, IconButton}; use workspace::searchable::Direction; -use crate::mode::{SearchMode, Side}; +use crate::mode::SearchMode; pub(super) fn render_nav_button( icon: ui::Icon, - direction: Direction, - active: bool, on_click: impl Fn(&mut V, &mut ViewContext) + 'static + Send + Sync, - cx: &mut ViewContext, ) -> impl Component { // let tooltip_style = cx.theme().tooltip.clone(); // let cursor_style = if active { @@ -22,57 +19,13 @@ pub(super) fn render_nav_button( // }; // enum NavButton {} IconButton::new("search-nav-button", icon).on_click(on_click) - // MouseEventHandler::new::(direction as usize, cx, |state, cx| { - // let theme = cx.theme(); - // let style = theme - // .search - // .nav_button - // .in_state(active) - // .style_for(state) - // .clone(); - // let mut container_style = style.container.clone(); - // let label = Label::new(icon, style.label.clone()).aligned().contained(); - // container_style.corner_radii = match direction { - // Direction::Prev => CornerRadii { - // bottom_right: 0., - // top_right: 0., - // ..container_style.corner_radii - // }, - // Direction::Next => CornerRadii { - // bottom_left: 0., - // top_left: 0., - // ..container_style.corner_radii - // }, - // }; - // if direction == Direction::Prev { - // // Remove right border so that when both Next and Prev buttons are - // // next to one another, there's no double border between them. - // container_style.border.right = false; - // } - // label.with_style(container_style) - // }) - // .on_click(MouseButton::Left, on_click) - // .with_cursor_style(cursor_style) - // .with_tooltip::( - // direction as usize, - // tooltip.to_string(), - // Some(action), - // tooltip_style, - // cx, - // ) - // .into_any() } pub(crate) fn render_search_mode_button( mode: SearchMode, - side: Option, is_active: bool, on_click: impl Fn(&mut V, &mut ViewContext) + 'static + Send + Sync, - cx: &mut ViewContext, ) -> Button { - //let tooltip_style = cx.theme().tooltip.clone(); - enum SearchModeButton {} - let button_variant = if is_active { ButtonVariant::Filled } else { @@ -82,66 +35,6 @@ pub(crate) fn render_search_mode_button( Button::new(mode.label()) .on_click(Arc::new(on_click)) .variant(button_variant) - - // v_stack() - // .border_2() - // .rounded_md() - // .when(side == Some(Side::Left), |this| { - // this.border_r_0().rounded_tr_none().rounded_br_none() - // }) - // .when(side == Some(Side::Right), |this| { - // this.border_l_0().rounded_bl_none().rounded_tl_none() - // }) - // .on_key_down(move |v, _, _, cx| on_click(v, cx)) - // .child(Label::new(mode.label())) - // MouseEventHandler::new::(mode.region_id(), cx, |state, cx| { - // let theme = cx.theme(); - // let style = theme - // .search - // .mode_button - // .in_state(is_active) - // .style_for(state) - // .clone(); - - // let mut container_style = style.container; - // if let Some(button_side) = side { - // if button_side == Side::Left { - // container_style.border.left = true; - // container_style.corner_radii = CornerRadii { - // bottom_right: 0., - // top_right: 0., - // ..container_style.corner_radii - // }; - // } else { - // container_style.border.left = false; - // container_style.corner_radii = CornerRadii { - // bottom_left: 0., - // top_left: 0., - // ..container_style.corner_radii - // }; - // } - // } else { - // container_style.border.left = false; - // container_style.corner_radii = CornerRadii::default(); - // } - - // Label::new(mode.label(), style.text) - // .aligned() - // .contained() - // .with_style(container_style) - // .constrained() - // .with_height(theme.search.search_bar_row_height) - // }) - // .on_click(MouseButton::Left, on_click) - // .with_cursor_style(CursorStyle::PointingHand) - // .with_tooltip::( - // mode.region_id(), - // mode.tooltip_text().to_owned(), - // Some(mode.activate_action()), - // tooltip_style, - // cx, - // ) - // .into_any() } pub(crate) fn render_option_button_icon( @@ -150,8 +43,6 @@ pub(crate) fn render_option_button_icon( id: usize, label: impl Into>, action: Box, - //on_click: impl Fn(MouseClick, &mut V, &mut EventContext) + 'static, - cx: &mut ViewContext, ) -> impl Component { //let tooltip_style = cx.theme().tooltip.clone(); div() diff --git a/crates/terminal_view2/src/terminal_view.rs b/crates/terminal_view2/src/terminal_view.rs index 14391ca2b2f357f56f084a5688601182bad780cf..2ed7c8f47247cb4fe63e825ca00f4911c7cdfbf0 100644 --- a/crates/terminal_view2/src/terminal_view.rs +++ b/crates/terminal_view2/src/terminal_view.rs @@ -792,7 +792,7 @@ impl Item for TerminalView { // } fn breadcrumb_location(&self) -> ToolbarItemLocation { - ToolbarItemLocation::PrimaryLeft { flex: None } + ToolbarItemLocation::PrimaryLeft } fn breadcrumbs(&self, _: &theme::Theme, cx: &AppContext) -> Option> { From 8845f5a1831bf83421fd6e888fb510110e4e36e7 Mon Sep 17 00:00:00 2001 From: Piotr Osiewicz <24362066+osiewicz@users.noreply.github.com> Date: Fri, 17 Nov 2023 13:22:07 +0100 Subject: [PATCH 10/69] Clean up warnings --- crates/search2/src/buffer_search.rs | 183 +--------------------------- crates/search2/src/search_bar.rs | 14 +-- 2 files changed, 12 insertions(+), 185 deletions(-) diff --git a/crates/search2/src/buffer_search.rs b/crates/search2/src/buffer_search.rs index 021cc570158d9b31bac1b56f58bac3e6247f2749..3c995af20bb29815924d7e6c26a6933e0b220fcd 100644 --- a/crates/search2/src/buffer_search.rs +++ b/crates/search2/src/buffer_search.rs @@ -254,107 +254,6 @@ impl Render for BufferSearchBar { Direction::Next, )), ) - - // let query_column = Flex::row() - // .with_child( - // Svg::for_style(theme.search.editor_icon.clone().icon) - // .contained() - // .with_style(theme.search.editor_icon.clone().container), - // ) - // .with_child(ChildView::new(&self.query_editor, cx).flex(1., true)) - // .with_child( - // Flex::row() - // .with_children( - // supported_options - // .case - // .then(|| search_option_button(SearchOptions::CASE_SENSITIVE)), - // ) - // .with_children( - // supported_options - // .word - // .then(|| search_option_button(SearchOptions::WHOLE_WORD)), - // ) - // .flex_float() - // .contained(), - // ) - // .align_children_center() - // .contained() - // .with_style(query_container_style) - // .constrained() - // .with_min_width(theme.search.editor.min_width) - // .with_max_width(theme.search.editor.max_width) - // .with_height(theme.search.search_bar_row_height) - // .flex(1., false); - // let should_show_replace_input = self.replace_enabled && supported_options.replacement; - - // let replacement = should_show_replace_input.then(|| { - // div() - // .child( - // Svg::for_style(theme.search.replace_icon.clone().icon) - // .contained() - // .with_style(theme.search.replace_icon.clone().container), - // ) - // .child(self.replacement_editor) - // .align_children_center() - // .flex(1., true) - // .contained() - // .with_style(query_container_style) - // .constrained() - // .with_min_width(theme.search.editor.min_width) - // .with_max_width(theme.search.editor.max_width) - // .with_height(theme.search.search_bar_row_height) - // .flex(1., false) - // }); - // let replace_all = - // should_show_replace_input.then(|| super::replace_action(ReplaceAll, "Replace all")); - // let replace_next = - // should_show_replace_input.then(|| super::replace_action(ReplaceNext, "Replace next")); - // let switches_column = supported_options.replacement.then(|| { - // Flex::row() - // .align_children_center() - // .with_child(super::toggle_replace_button(self.replace_enabled)) - // .constrained() - // .with_height(theme.search.search_bar_row_height) - // .contained() - // .with_style(theme.search.option_button_group) - // }); - // let mode_column = div() - // .child(search_button_for_mode( - // SearchMode::Text, - // Some(Side::Left), - // cx, - // )) - // .child(search_button_for_mode( - // SearchMode::Regex, - // Some(Side::Right), - // cx, - // )) - // .contained() - // .with_style(theme.search.modes_container) - // .constrained() - // .with_height(theme.search.search_bar_row_height); - - // let nav_column = div() - // .align_children_center() - // .with_children(replace_next) - // .with_children(replace_all) - // .with_child(self.render_action_button("icons/select-all.svg", cx)) - // .with_child(div().children(match_count)) - // .with_child(nav_button_for_direction("<", Direction::Prev, cx)) - // .with_child(nav_button_for_direction(">", Direction::Next, cx)) - // .constrained() - // .with_height(theme.search.search_bar_row_height) - // .flex_float(); - - // div() - // .child(query_column) - // .child(mode_column) - // .children(switches_column) - // .children(replacement) - // .child(nav_column) - // .contained() - // .with_style(theme.search.container) - // .into_any_named("search bar") } } @@ -442,12 +341,15 @@ impl BufferSearchBar { register_action(workspace, |this, action: &ToggleReplace, cx| { this.toggle_replace(action, cx); }); - register_action(workspace, |this, action: &ActivateRegexMode, cx| { + register_action(workspace, |this, _: &ActivateRegexMode, cx| { this.activate_search_mode(SearchMode::Regex, cx); }); - register_action(workspace, |this, action: &ActivateTextMode, cx| { + register_action(workspace, |this, _: &ActivateTextMode, cx| { this.activate_search_mode(SearchMode::Text, cx); }); + register_action(workspace, |this, action: &CycleMode, cx| { + this.cycle_mode(action, cx) + }); register_action(workspace, |this, action: &SelectNextMatch, cx| { this.select_next_match(action, cx); }); @@ -639,20 +541,6 @@ impl BufferSearchBar { cx.notify(); } - fn deploy_bar(pane: &mut Pane, action: &Deploy, cx: &mut ViewContext) { - let mut propagate_action = true; - if let Some(search_bar) = pane.toolbar().read(cx).item_of_type::() { - search_bar.update(cx, |search_bar, cx| { - if search_bar.deploy(action, cx) { - propagate_action = false; - } - }); - } - if !propagate_action { - cx.stop_propagation(); - } - } - fn handle_editor_cancel(pane: &mut Pane, _: &editor::Cancel, cx: &mut ViewContext) { if let Some(search_bar) = pane.toolbar().read(cx).item_of_type::() { if !search_bar.read(cx).dismissed { @@ -740,36 +628,6 @@ impl BufferSearchBar { } } - fn select_next_match_on_pane( - pane: &mut Pane, - action: &SelectNextMatch, - cx: &mut ViewContext, - ) { - if let Some(search_bar) = pane.toolbar().read(cx).item_of_type::() { - search_bar.update(cx, |bar, cx| bar.select_next_match(action, cx)); - } - } - - fn select_prev_match_on_pane( - pane: &mut Pane, - action: &SelectPrevMatch, - cx: &mut ViewContext, - ) { - if let Some(search_bar) = pane.toolbar().read(cx).item_of_type::() { - search_bar.update(cx, |bar, cx| bar.select_prev_match(action, cx)); - } - } - - fn select_all_matches_on_pane( - pane: &mut Pane, - action: &SelectAllMatches, - cx: &mut ViewContext, - ) { - if let Some(search_bar) = pane.toolbar().read(cx).item_of_type::() { - search_bar.update(cx, |bar, cx| bar.select_all_matches(action, cx)); - } - } - fn on_query_editor_event( &mut self, _: View, @@ -941,23 +799,6 @@ impl BufferSearchBar { fn cycle_mode(&mut self, _: &CycleMode, cx: &mut ViewContext) { self.activate_search_mode(next_mode(&self.current_mode, false), cx); } - fn cycle_mode_on_pane(pane: &mut Pane, action: &CycleMode, cx: &mut ViewContext) { - let mut should_propagate = true; - if let Some(search_bar) = pane.toolbar().read(cx).item_of_type::() { - search_bar.update(cx, |bar, cx| { - if bar.show(cx) { - should_propagate = false; - bar.cycle_mode(action, cx); - false - } else { - true - } - }); - } - if !should_propagate { - cx.stop_propagation(); - } - } fn toggle_replace(&mut self, _: &ToggleReplace, cx: &mut ViewContext) { if let Some(_) = &self.active_searchable_item { self.replace_enabled = !self.replace_enabled; @@ -1037,20 +878,6 @@ impl BufferSearchBar { } } } - fn replace_next_on_pane(pane: &mut Pane, action: &ReplaceNext, cx: &mut ViewContext) { - if let Some(search_bar) = pane.toolbar().read(cx).item_of_type::() { - search_bar.update(cx, |bar, cx| bar.replace_next(action, cx)); - cx.stop_propagation(); - return; - } - } - fn replace_all_on_pane(pane: &mut Pane, action: &ReplaceAll, cx: &mut ViewContext) { - if let Some(search_bar) = pane.toolbar().read(cx).item_of_type::() { - search_bar.update(cx, |bar, cx| bar.replace_all(action, cx)); - cx.stop_propagation(); - return; - } - } } #[cfg(test)] diff --git a/crates/search2/src/search_bar.rs b/crates/search2/src/search_bar.rs index 46a3357763cbe6188ade787fdf45417a022d39e6..ddd844be1dbcd361881ee9204f78258e721e6a3d 100644 --- a/crates/search2/src/search_bar.rs +++ b/crates/search2/src/search_bar.rs @@ -2,13 +2,13 @@ use std::{borrow::Cow, sync::Arc}; use gpui::{div, Action, Component, ViewContext}; use ui::{Button, ButtonVariant, IconButton}; -use workspace::searchable::Direction; + use crate::mode::SearchMode; pub(super) fn render_nav_button( icon: ui::Icon, - active: bool, + _active: bool, on_click: impl Fn(&mut V, &mut ViewContext) + 'static + Send + Sync, ) -> impl Component { // let tooltip_style = cx.theme().tooltip.clone(); @@ -38,11 +38,11 @@ pub(crate) fn render_search_mode_button( } pub(crate) fn render_option_button_icon( - is_active: bool, - icon: &'static str, - id: usize, - label: impl Into>, - action: Box, + _is_active: bool, + _icon: &'static str, + _id: usize, + _label: impl Into>, + _action: Box, ) -> impl Component { //let tooltip_style = cx.theme().tooltip.clone(); div() From f23cc724d44c08ebc63ebfcfdb57560208073a27 Mon Sep 17 00:00:00 2001 From: Piotr Osiewicz <24362066+osiewicz@users.noreply.github.com> Date: Fri, 17 Nov 2023 13:23:42 +0100 Subject: [PATCH 11/69] chore: cargo fmt --- crates/search2/src/search_bar.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/crates/search2/src/search_bar.rs b/crates/search2/src/search_bar.rs index ddd844be1dbcd361881ee9204f78258e721e6a3d..8879644eabe938c3307e83e1e12a5cf270bcbcb9 100644 --- a/crates/search2/src/search_bar.rs +++ b/crates/search2/src/search_bar.rs @@ -3,7 +3,6 @@ use std::{borrow::Cow, sync::Arc}; use gpui::{div, Action, Component, ViewContext}; use ui::{Button, ButtonVariant, IconButton}; - use crate::mode::SearchMode; pub(super) fn render_nav_button( From 741e11cc11df7f5b84d7dce8db7f20610655bd9b Mon Sep 17 00:00:00 2001 From: Piotr Osiewicz <24362066+osiewicz@users.noreply.github.com> Date: Fri, 17 Nov 2023 13:27:33 +0100 Subject: [PATCH 12/69] Fix up action derive --- crates/search2/src/buffer_search.rs | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/crates/search2/src/buffer_search.rs b/crates/search2/src/buffer_search.rs index 3c995af20bb29815924d7e6c26a6933e0b220fcd..5e2138062944a598ad18f2ad4173a837853f76f4 100644 --- a/crates/search2/src/buffer_search.rs +++ b/crates/search2/src/buffer_search.rs @@ -10,11 +10,12 @@ use collections::HashMap; use editor::Editor; use futures::channel::oneshot; use gpui::{ - action, actions, div, red, Action, AppContext, Component, Div, EventEmitter, - InteractiveComponent, ParentComponent as _, Render, Styled, Subscription, Task, View, - ViewContext, VisualContext as _, WindowContext, + actions, div, red, Action, AppContext, Component, Div, EventEmitter, InteractiveComponent, + ParentComponent as _, Render, Styled, Subscription, Task, View, ViewContext, + VisualContext as _, WindowContext, }; use project::search::SearchQuery; +use serde::Deserialize; use std::{any::Any, sync::Arc}; use ui::{h_stack, ButtonGroup, Icon, IconButton, IconElement}; @@ -25,7 +26,7 @@ use workspace::{ Pane, ToolbarItemLocation, ToolbarItemView, Workspace, }; -#[action] +#[derive(PartialEq, Clone, Deserialize, Default, Action)] pub struct Deploy { pub focus: bool, } From 27600b6b8d921bc2584b7ef21f6a2b2b07ba3c48 Mon Sep 17 00:00:00 2001 From: Piotr Osiewicz <24362066+osiewicz@users.noreply.github.com> Date: Fri, 17 Nov 2023 14:42:20 +0100 Subject: [PATCH 13/69] Remove dead code (for now). Ensure actions are registed just once (previously some were registered on both Workspace and search bar itself). --- crates/search2/src/buffer_search.rs | 88 ++++++++++------------------- crates/search2/src/mode.rs | 24 -------- crates/search2/src/search_bar.rs | 32 ----------- 3 files changed, 30 insertions(+), 114 deletions(-) diff --git a/crates/search2/src/buffer_search.rs b/crates/search2/src/buffer_search.rs index 5e2138062944a598ad18f2ad4173a837853f76f4..d1f9103b671e2639ec6e2eb6b4db5d09f94af866 100644 --- a/crates/search2/src/buffer_search.rs +++ b/crates/search2/src/buffer_search.rs @@ -23,7 +23,7 @@ use util::ResultExt; use workspace::{ item::ItemHandle, searchable::{Direction, SearchEvent, SearchableItemHandle, WeakSearchableItemHandle}, - Pane, ToolbarItemLocation, ToolbarItemView, Workspace, + ToolbarItemLocation, ToolbarItemView, Workspace, }; #[derive(PartialEq, Clone, Deserialize, Default, Action)] @@ -71,11 +71,7 @@ impl Render for BufferSearchBar { // } else { // theme.search.editor.input.container // }; - let supported_options = self - .active_searchable_item - .as_ref() - .map(|active_searchable_item| active_searchable_item.supported_options()) - .unwrap_or_default(); + let supported_options = self.supported_options(); let previous_query_keystrokes = cx .bindings_for_action(&PreviousHistoryQuery {}) @@ -184,18 +180,6 @@ impl Render for BufferSearchBar { }) .on_action(Self::previous_history_query) .on_action(Self::next_history_query) - .when(supported_options.case, |this| { - this.on_action(Self::toggle_case_sensitive) - }) - .when(supported_options.word, |this| { - this.on_action(Self::toggle_whole_word) - }) - .when(supported_options.replacement, |this| { - this.on_action(Self::toggle_replace) - }) - .on_action(Self::select_next_match) - .on_action(Self::select_prev_match) - .on_action(Self::cycle_mode) .w_full() .p_1() .child( @@ -292,7 +276,6 @@ impl ToolbarItemView for BufferSearchBar { return ToolbarItemLocation::Secondary; } } - ToolbarItemLocation::Hidden } @@ -334,22 +317,34 @@ impl BufferSearchBar { } register_action(workspace, |this, action: &ToggleCaseSensitive, cx| { - this.toggle_case_sensitive(action, cx); + if this.supported_options().case { + this.toggle_case_sensitive(action, cx); + } }); register_action(workspace, |this, action: &ToggleWholeWord, cx| { - this.toggle_whole_word(action, cx); + if this.supported_options().word { + this.toggle_whole_word(action, cx); + } }); register_action(workspace, |this, action: &ToggleReplace, cx| { - this.toggle_replace(action, cx); + if this.supported_options().replacement { + this.toggle_replace(action, cx); + } }); register_action(workspace, |this, _: &ActivateRegexMode, cx| { - this.activate_search_mode(SearchMode::Regex, cx); + if this.supported_options().regex { + this.activate_search_mode(SearchMode::Regex, cx); + } }); register_action(workspace, |this, _: &ActivateTextMode, cx| { this.activate_search_mode(SearchMode::Text, cx); }); register_action(workspace, |this, action: &CycleMode, cx| { - this.cycle_mode(action, cx) + if this.supported_options().regex { + // If regex is not supported then search has just one mode (text) - in that case there's no point in supporting + // cycling. + this.cycle_mode(action, cx) + } }); register_action(workspace, |this, action: &SelectNextMatch, cx| { this.select_next_match(action, cx); @@ -360,6 +355,11 @@ impl BufferSearchBar { register_action(workspace, |this, action: &SelectAllMatches, cx| { this.select_all_matches(action, cx); }); + register_action(workspace, |this, _: &editor::Cancel, cx| { + if !this.dismissed { + this.dismiss(&Dismiss, cx); + } + }); } pub fn new(cx: &mut ViewContext) -> Self { let query_editor = cx.build_view(|cx| Editor::single_line(cx)); @@ -393,7 +393,6 @@ impl BufferSearchBar { pub fn dismiss(&mut self, _: &Dismiss, cx: &mut ViewContext) { self.dismissed = true; - for searchable_item in self.searchable_items_with_matches.keys() { if let Some(searchable_item) = WeakSearchableItemHandle::upgrade(searchable_item.as_ref(), cx) @@ -427,13 +426,18 @@ impl BufferSearchBar { if self.active_searchable_item.is_none() { return false; } - self.dismissed = false; cx.notify(); cx.emit(Event::UpdateLocation); true } + fn supported_options(&self) -> workspace::searchable::SearchOptions { + self.active_searchable_item + .as_deref() + .map(SearchableItemHandle::supported_options) + .unwrap_or_default() + } pub fn search_suggested(&mut self, cx: &mut ViewContext) { let search = self .query_suggestion(cx) @@ -542,16 +546,6 @@ impl BufferSearchBar { cx.notify(); } - fn handle_editor_cancel(pane: &mut Pane, _: &editor::Cancel, cx: &mut ViewContext) { - if let Some(search_bar) = pane.toolbar().read(cx).item_of_type::() { - if !search_bar.read(cx).dismissed { - search_bar.update(cx, |search_bar, cx| search_bar.dismiss(&Dismiss, cx)); - cx.stop_propagation(); - return; - } - } - } - pub fn focus_editor(&mut self, _: &FocusEditor, cx: &mut ViewContext) { if let Some(active_editor) = self.active_searchable_item.as_ref() { let handle = active_editor.focus_handle(cx); @@ -810,28 +804,6 @@ impl BufferSearchBar { cx.notify(); } } - fn toggle_replace_on_a_pane(pane: &mut Pane, _: &ToggleReplace, cx: &mut ViewContext) { - let mut should_propagate = true; - if let Some(search_bar) = pane.toolbar().read(cx).item_of_type::() { - search_bar.update(cx, |bar, cx| { - if let Some(_) = &bar.active_searchable_item { - should_propagate = false; - bar.replace_enabled = !bar.replace_enabled; - if bar.dismissed { - bar.show(cx); - } - if !bar.replace_enabled { - let handle = bar.query_editor.focus_handle(cx); - cx.focus(&handle); - } - cx.notify(); - } - }); - } - if !should_propagate { - cx.stop_propagation(); - } - } fn replace_next(&mut self, _: &ReplaceNext, cx: &mut ViewContext) { let mut should_propagate = true; if !self.dismissed && self.active_search.is_some() { diff --git a/crates/search2/src/mode.rs b/crates/search2/src/mode.rs index bb729cb6c0c198cb1034f04a31125b6df7325e96..4b036d29a5aeb419a808b05ad4a31bd023313431 100644 --- a/crates/search2/src/mode.rs +++ b/crates/search2/src/mode.rs @@ -11,30 +11,6 @@ pub enum SearchMode { } impl SearchMode { - pub(crate) fn label(&self) -> &'static str { - match self { - SearchMode::Text => "Text", - SearchMode::Semantic => "Semantic", - SearchMode::Regex => "Regex", - } - } - - pub(crate) fn region_id(&self) -> usize { - match self { - SearchMode::Text => 3, - SearchMode::Semantic => 4, - SearchMode::Regex => 5, - } - } - - pub(crate) fn tooltip_text(&self) -> &'static str { - match self { - SearchMode::Text => "Activate Text Search", - SearchMode::Semantic => "Activate Semantic Search", - SearchMode::Regex => "Activate Regex Search", - } - } - pub(crate) fn activate_action(&self) -> Box { match self { SearchMode::Text => Box::new(ActivateTextMode), diff --git a/crates/search2/src/search_bar.rs b/crates/search2/src/search_bar.rs index 8879644eabe938c3307e83e1e12a5cf270bcbcb9..cd3b5e474bb6ee448e6bd39aa4d05e621c5126ae 100644 --- a/crates/search2/src/search_bar.rs +++ b/crates/search2/src/search_bar.rs @@ -35,35 +35,3 @@ pub(crate) fn render_search_mode_button( .on_click(Arc::new(on_click)) .variant(button_variant) } - -pub(crate) fn render_option_button_icon( - _is_active: bool, - _icon: &'static str, - _id: usize, - _label: impl Into>, - _action: Box, -) -> impl Component { - //let tooltip_style = cx.theme().tooltip.clone(); - div() - // MouseEventHandler::new::(id, cx, |state, cx| { - // let theme = cx.theme(); - // let style = theme - // .search - // .option_button - // .in_state(is_active) - // .style_for(state); - // Svg::new(icon) - // .with_color(style.color.clone()) - // .constrained() - // .with_width(style.icon_width) - // .contained() - // .with_style(style.container) - // .constrained() - // .with_height(theme.search.option_button_height) - // .with_width(style.button_width) - // }) - // .on_click(MouseButton::Left, on_click) - // .with_cursor_style(CursorStyle::PointingHand) - // .with_tooltip::(id, label, Some(action), tooltip_style, cx) - // .into_any() -} From ae1ebc68583f19cb23c679cb6f2dd62653b464a2 Mon Sep 17 00:00:00 2001 From: Piotr Osiewicz <24362066+osiewicz@users.noreply.github.com> Date: Fri, 17 Nov 2023 17:22:52 +0100 Subject: [PATCH 14/69] fixup! Remove dead code (for now). --- crates/search2/src/mode.rs | 11 ++++------- crates/search2/src/search_bar.rs | 4 ++-- 2 files changed, 6 insertions(+), 9 deletions(-) diff --git a/crates/search2/src/mode.rs b/crates/search2/src/mode.rs index 4b036d29a5aeb419a808b05ad4a31bd023313431..817fb454d2dcb08953d012fbb9814874c786cb78 100644 --- a/crates/search2/src/mode.rs +++ b/crates/search2/src/mode.rs @@ -1,6 +1,3 @@ -use gpui::Action; - -use crate::{ActivateRegexMode, ActivateSemanticMode, ActivateTextMode}; // TODO: Update the default search mode to get from config #[derive(Copy, Clone, Debug, Default, PartialEq)] pub enum SearchMode { @@ -11,11 +8,11 @@ pub enum SearchMode { } impl SearchMode { - pub(crate) fn activate_action(&self) -> Box { + pub(crate) fn label(&self) -> &'static str { match self { - SearchMode::Text => Box::new(ActivateTextMode), - SearchMode::Semantic => Box::new(ActivateSemanticMode), - SearchMode::Regex => Box::new(ActivateRegexMode), + SearchMode::Text => "Text", + SearchMode::Semantic => "Semantic", + SearchMode::Regex => "Regex", } } } diff --git a/crates/search2/src/search_bar.rs b/crates/search2/src/search_bar.rs index cd3b5e474bb6ee448e6bd39aa4d05e621c5126ae..1c4f2a17a6d1c5e13ee6905378272d24f3c96ac8 100644 --- a/crates/search2/src/search_bar.rs +++ b/crates/search2/src/search_bar.rs @@ -1,6 +1,6 @@ -use std::{borrow::Cow, sync::Arc}; +use std::sync::Arc; -use gpui::{div, Action, Component, ViewContext}; +use gpui::{Component, ViewContext}; use ui::{Button, ButtonVariant, IconButton}; use crate::mode::SearchMode; From eb9959a0cf589f09bdf9bc43bf942e17c1b55199 Mon Sep 17 00:00:00 2001 From: Piotr Osiewicz <24362066+osiewicz@users.noreply.github.com> Date: Fri, 17 Nov 2023 17:23:05 +0100 Subject: [PATCH 15/69] gpui: notifications now takes an entity instead of a model --- crates/gpui2/src/app/test_context.rs | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) diff --git a/crates/gpui2/src/app/test_context.rs b/crates/gpui2/src/app/test_context.rs index 940492573f0ed504defc711f559375fc3686c0ce..7b6acad586e56ddd8084c2ccd352d9edc2e96672 100644 --- a/crates/gpui2/src/app/test_context.rs +++ b/crates/gpui2/src/app/test_context.rs @@ -1,8 +1,9 @@ use crate::{ div, Action, AnyView, AnyWindowHandle, AppCell, AppContext, AsyncAppContext, - BackgroundExecutor, Context, Div, EventEmitter, ForegroundExecutor, InputEvent, KeyDownEvent, - Keystroke, Model, ModelContext, Render, Result, Task, TestDispatcher, TestPlatform, TestWindow, - View, ViewContext, VisualContext, WindowContext, WindowHandle, WindowOptions, + BackgroundExecutor, Context, Div, Entity, EventEmitter, ForegroundExecutor, InputEvent, + KeyDownEvent, Keystroke, Model, ModelContext, Render, Result, Task, TestDispatcher, + TestPlatform, TestWindow, View, ViewContext, VisualContext, WindowContext, WindowHandle, + WindowOptions, }; use anyhow::{anyhow, bail}; use futures::{Stream, StreamExt}; @@ -296,21 +297,19 @@ impl TestAppContext { .unwrap() } - pub fn notifications(&mut self, entity: &Model) -> impl Stream { + pub fn notifications(&mut self, entity: &impl Entity) -> impl Stream { let (tx, rx) = futures::channel::mpsc::unbounded(); - - entity.update(self, move |_, cx: &mut ModelContext| { + self.update(|cx| { cx.observe(entity, { let tx = tx.clone(); - move |_, _, _| { + move |_, _| { let _ = tx.unbounded_send(()); } }) .detach(); - - cx.on_release(move |_, _| tx.close_channel()).detach(); + cx.observe_release(entity, move |_, _| tx.close_channel()) + .detach() }); - rx } From 3b5754a77e97f81987b30bfa420555f91ca696df Mon Sep 17 00:00:00 2001 From: Piotr Osiewicz <24362066+osiewicz@users.noreply.github.com> Date: Fri, 17 Nov 2023 17:23:47 +0100 Subject: [PATCH 16/69] Clean up tests (they compile now) --- crates/search2/src/buffer_search.rs | 462 ++++++++++++++-------------- 1 file changed, 237 insertions(+), 225 deletions(-) diff --git a/crates/search2/src/buffer_search.rs b/crates/search2/src/buffer_search.rs index d1f9103b671e2639ec6e2eb6b4db5d09f94af866..1c1c88277459c201e79e810df7000e781a086a14 100644 --- a/crates/search2/src/buffer_search.rs +++ b/crates/search2/src/buffer_search.rs @@ -857,17 +857,33 @@ impl BufferSearchBar { mod tests { use super::*; use editor::{DisplayPoint, Editor}; - use gpui::{color::Color, test::EmptyView, TestAppContext}; + use gpui::{Context, EmptyView, Hsla, TestAppContext, VisualTestContext}; use language::Buffer; + use smol::stream::StreamExt as _; use unindent::Unindent as _; - fn init_test(cx: &mut TestAppContext) -> (ViewHandle, ViewHandle) { - crate::project_search::tests::init_test(cx); - - let buffer = cx.add_model(|cx| { + fn init_globals(cx: &mut TestAppContext) { + cx.update(|cx| { + let store = settings::SettingsStore::test(cx); + cx.set_global(store); + editor::init(cx); + ui::init(cx); + language::init(cx); + theme::init(theme::LoadThemes::JustBase, cx); + }); + } + fn init_test( + cx: &mut TestAppContext, + ) -> ( + View, + View, + &mut VisualTestContext<'_>, + ) { + init_globals(cx); + let buffer = cx.build_model(|cx| { Buffer::new( 0, - cx.model_id() as u64, + cx.entity_id().as_u64(), r#" A regular expression (shortened as regex or regexp;[1] also referred to as rational expression[2][3]) is a sequence of characters that specifies a search @@ -877,22 +893,22 @@ mod tests { .unindent(), ) }); - let window = cx.add_window(|_| EmptyView); - let editor = window.add_view(cx, |cx| Editor::for_buffer(buffer.clone(), None, cx)); + let (window, cx) = cx.add_window_view(|_| EmptyView {}); + let editor = cx.build_view(|cx| Editor::for_buffer(buffer.clone(), None, cx)); - let search_bar = window.add_view(cx, |cx| { + let search_bar = cx.build_view(|cx| { let mut search_bar = BufferSearchBar::new(cx); search_bar.set_active_pane_item(Some(&editor), cx); search_bar.show(cx); search_bar }); - (editor, search_bar) + (editor, search_bar, cx) } #[gpui::test] async fn test_search_simple(cx: &mut TestAppContext) { - let (editor, search_bar) = init_test(cx); + let (editor, search_bar, cx) = init_test(cx); // Search for a string that appears with different casing. // By default, search is case-insensitive. @@ -906,11 +922,11 @@ mod tests { &[ ( DisplayPoint::new(2, 17)..DisplayPoint::new(2, 19), - Color::red(), + Hsla::red(), ), ( DisplayPoint::new(2, 43)..DisplayPoint::new(2, 45), - Color::red(), + Hsla::red(), ), ] ); @@ -920,13 +936,14 @@ mod tests { search_bar.update(cx, |search_bar, cx| { search_bar.toggle_search_option(SearchOptions::CASE_SENSITIVE, cx); }); - editor.next_notification(cx).await; + let mut editor_notifications = cx.notifications(&editor); + editor_notifications.next().await; editor.update(cx, |editor, cx| { assert_eq!( editor.all_text_background_highlights(cx), &[( DisplayPoint::new(2, 43)..DisplayPoint::new(2, 45), - Color::red(), + Hsla::red(), )] ); }); @@ -943,31 +960,31 @@ mod tests { &[ ( DisplayPoint::new(0, 24)..DisplayPoint::new(0, 26), - Color::red(), + Hsla::red(), ), ( DisplayPoint::new(0, 41)..DisplayPoint::new(0, 43), - Color::red(), + Hsla::red(), ), ( DisplayPoint::new(2, 71)..DisplayPoint::new(2, 73), - Color::red(), + Hsla::red(), ), ( DisplayPoint::new(3, 1)..DisplayPoint::new(3, 3), - Color::red(), + Hsla::red(), ), ( DisplayPoint::new(3, 11)..DisplayPoint::new(3, 13), - Color::red(), + Hsla::red(), ), ( DisplayPoint::new(3, 56)..DisplayPoint::new(3, 58), - Color::red(), + Hsla::red(), ), ( DisplayPoint::new(3, 60)..DisplayPoint::new(3, 62), - Color::red(), + Hsla::red(), ), ] ); @@ -977,22 +994,23 @@ mod tests { search_bar.update(cx, |search_bar, cx| { search_bar.toggle_search_option(SearchOptions::WHOLE_WORD, cx); }); - editor.next_notification(cx).await; + let mut editor_notifications = cx.notifications(&editor); + editor_notifications.next().await; editor.update(cx, |editor, cx| { assert_eq!( editor.all_text_background_highlights(cx), &[ ( DisplayPoint::new(0, 41)..DisplayPoint::new(0, 43), - Color::red(), + Hsla::red(), ), ( DisplayPoint::new(3, 11)..DisplayPoint::new(3, 13), - Color::red(), + Hsla::red(), ), ( DisplayPoint::new(3, 56)..DisplayPoint::new(3, 58), - Color::red(), + Hsla::red(), ), ] ); @@ -1011,7 +1029,7 @@ mod tests { [DisplayPoint::new(0, 41)..DisplayPoint::new(0, 43)] ); }); - search_bar.read_with(cx, |search_bar, _| { + search_bar.update(cx, |search_bar, _| { assert_eq!(search_bar.active_match_index, Some(0)); }); @@ -1022,7 +1040,7 @@ mod tests { [DisplayPoint::new(3, 11)..DisplayPoint::new(3, 13)] ); }); - search_bar.read_with(cx, |search_bar, _| { + search_bar.update(cx, |search_bar, _| { assert_eq!(search_bar.active_match_index, Some(1)); }); @@ -1033,7 +1051,7 @@ mod tests { [DisplayPoint::new(3, 56)..DisplayPoint::new(3, 58)] ); }); - search_bar.read_with(cx, |search_bar, _| { + search_bar.update(cx, |search_bar, _| { assert_eq!(search_bar.active_match_index, Some(2)); }); @@ -1044,7 +1062,7 @@ mod tests { [DisplayPoint::new(0, 41)..DisplayPoint::new(0, 43)] ); }); - search_bar.read_with(cx, |search_bar, _| { + search_bar.update(cx, |search_bar, _| { assert_eq!(search_bar.active_match_index, Some(0)); }); @@ -1055,7 +1073,7 @@ mod tests { [DisplayPoint::new(3, 56)..DisplayPoint::new(3, 58)] ); }); - search_bar.read_with(cx, |search_bar, _| { + search_bar.update(cx, |search_bar, _| { assert_eq!(search_bar.active_match_index, Some(2)); }); @@ -1066,7 +1084,7 @@ mod tests { [DisplayPoint::new(3, 11)..DisplayPoint::new(3, 13)] ); }); - search_bar.read_with(cx, |search_bar, _| { + search_bar.update(cx, |search_bar, _| { assert_eq!(search_bar.active_match_index, Some(1)); }); @@ -1077,7 +1095,7 @@ mod tests { [DisplayPoint::new(0, 41)..DisplayPoint::new(0, 43)] ); }); - search_bar.read_with(cx, |search_bar, _| { + search_bar.update(cx, |search_bar, _| { assert_eq!(search_bar.active_match_index, Some(0)); }); @@ -1096,7 +1114,7 @@ mod tests { [DisplayPoint::new(0, 41)..DisplayPoint::new(0, 43)] ); }); - search_bar.read_with(cx, |search_bar, _| { + search_bar.update(cx, |search_bar, _| { assert_eq!(search_bar.active_match_index, Some(0)); }); @@ -1115,7 +1133,7 @@ mod tests { [DisplayPoint::new(3, 11)..DisplayPoint::new(3, 13)] ); }); - search_bar.read_with(cx, |search_bar, _| { + search_bar.update(cx, |search_bar, _| { assert_eq!(search_bar.active_match_index, Some(1)); }); @@ -1134,7 +1152,7 @@ mod tests { [DisplayPoint::new(3, 56)..DisplayPoint::new(3, 58)] ); }); - search_bar.read_with(cx, |search_bar, _| { + search_bar.update(cx, |search_bar, _| { assert_eq!(search_bar.active_match_index, Some(2)); }); @@ -1153,7 +1171,7 @@ mod tests { [DisplayPoint::new(0, 41)..DisplayPoint::new(0, 43)] ); }); - search_bar.read_with(cx, |search_bar, _| { + search_bar.update(cx, |search_bar, _| { assert_eq!(search_bar.active_match_index, Some(0)); }); @@ -1172,14 +1190,14 @@ mod tests { [DisplayPoint::new(3, 56)..DisplayPoint::new(3, 58)] ); }); - search_bar.read_with(cx, |search_bar, _| { + search_bar.update(cx, |search_bar, _| { assert_eq!(search_bar.active_match_index, Some(2)); }); } #[gpui::test] async fn test_search_option_handling(cx: &mut TestAppContext) { - let (editor, search_bar) = init_test(cx); + let (editor, search_bar, cx) = init_test(cx); // show with options should make current search case sensitive search_bar @@ -1194,7 +1212,7 @@ mod tests { editor.all_text_background_highlights(cx), &[( DisplayPoint::new(2, 43)..DisplayPoint::new(2, 45), - Color::red(), + Hsla::red(), )] ); }); @@ -1215,13 +1233,14 @@ mod tests { search_bar.update(cx, |search_bar, cx| { search_bar.toggle_search_option(SearchOptions::WHOLE_WORD, cx) }); - editor.next_notification(cx).await; + let mut editor_notifications = cx.notifications(&editor); + editor_notifications.next().await; editor.update(cx, |editor, cx| { assert_eq!( editor.all_text_background_highlights(cx), &[( DisplayPoint::new(0, 35)..DisplayPoint::new(0, 40), - Color::red(), + Hsla::red(), ),] ); }); @@ -1238,8 +1257,7 @@ mod tests { #[gpui::test] async fn test_search_select_all_matches(cx: &mut TestAppContext) { - crate::project_search::tests::init_test(cx); - + init_globals(cx); let buffer_text = r#" A regular expression (shortened as regex or regexp;[1] also referred to as rational expression[2][3]) is a sequence of characters that specifies a search @@ -1255,186 +1273,180 @@ mod tests { expected_query_matches_count > 1, "Should pick a query with multiple results" ); - let buffer = cx.add_model(|cx| Buffer::new(0, cx.model_id() as u64, buffer_text)); - let window = cx.add_window(|_| EmptyView); - let editor = window.add_view(cx, |cx| Editor::for_buffer(buffer.clone(), None, cx)); + let buffer = cx.build_model(|cx| Buffer::new(0, cx.entity_id().as_u64(), buffer_text)); + let window = cx.add_window(|_| EmptyView {}); + + let editor = window.build_view(cx, |cx| Editor::for_buffer(buffer.clone(), None, cx)); - let search_bar = window.add_view(cx, |cx| { + let search_bar = window.build_view(cx, |cx| { let mut search_bar = BufferSearchBar::new(cx); search_bar.set_active_pane_item(Some(&editor), cx); search_bar.show(cx); search_bar }); - search_bar - .update(cx, |search_bar, cx| search_bar.search("a", None, cx)) + window + .update(cx, |_, cx| { + search_bar.update(cx, |search_bar, cx| search_bar.search("a", None, cx)) + }) + .unwrap() .await .unwrap(); - search_bar.update(cx, |search_bar, cx| { - cx.focus(search_bar.query_editor.as_any()); - search_bar.activate_current_match(cx); - }); - - window.read_with(cx, |cx| { - assert!( - !editor.is_focused(cx), - "Initially, the editor should not be focused" - ); - }); - - let initial_selections = editor.update(cx, |editor, cx| { - let initial_selections = editor.selections.display_ranges(cx); - assert_eq!( - initial_selections.len(), 1, - "Expected to have only one selection before adding carets to all matches, but got: {initial_selections:?}", - ); - initial_selections - }); - search_bar.update(cx, |search_bar, _| { - assert_eq!(search_bar.active_match_index, Some(0)); - }); - - search_bar.update(cx, |search_bar, cx| { - cx.focus(search_bar.query_editor.as_any()); - search_bar.select_all_matches(&SelectAllMatches, cx); - }); - window.read_with(cx, |cx| { - assert!( - editor.is_focused(cx), - "Should focus editor after successful SelectAllMatches" - ); - }); - search_bar.update(cx, |search_bar, cx| { - let all_selections = - editor.update(cx, |editor, cx| editor.selections.display_ranges(cx)); - assert_eq!( - all_selections.len(), - expected_query_matches_count, - "Should select all `a` characters in the buffer, but got: {all_selections:?}" - ); - assert_eq!( - search_bar.active_match_index, - Some(0), - "Match index should not change after selecting all matches" - ); - }); - search_bar.update(cx, |search_bar, cx| { - search_bar.select_next_match(&SelectNextMatch, cx); - }); - window.read_with(cx, |cx| { - assert!( - editor.is_focused(cx), - "Should still have editor focused after SelectNextMatch" - ); - }); - search_bar.update(cx, |search_bar, cx| { - let all_selections = - editor.update(cx, |editor, cx| editor.selections.display_ranges(cx)); - assert_eq!( - all_selections.len(), - 1, - "On next match, should deselect items and select the next match" - ); - assert_ne!( - all_selections, initial_selections, - "Next match should be different from the first selection" - ); - assert_eq!( - search_bar.active_match_index, - Some(1), - "Match index should be updated to the next one" - ); - }); - - search_bar.update(cx, |search_bar, cx| { - cx.focus(search_bar.query_editor.as_any()); - search_bar.select_all_matches(&SelectAllMatches, cx); - }); - window.read_with(cx, |cx| { - assert!( - editor.is_focused(cx), - "Should focus editor after successful SelectAllMatches" - ); - }); - search_bar.update(cx, |search_bar, cx| { - let all_selections = - editor.update(cx, |editor, cx| editor.selections.display_ranges(cx)); - assert_eq!( - all_selections.len(), - expected_query_matches_count, - "Should select all `a` characters in the buffer, but got: {all_selections:?}" - ); - assert_eq!( - search_bar.active_match_index, - Some(1), - "Match index should not change after selecting all matches" - ); - }); - - search_bar.update(cx, |search_bar, cx| { - search_bar.select_prev_match(&SelectPrevMatch, cx); - }); - window.read_with(cx, |cx| { - assert!( - editor.is_focused(cx), - "Should still have editor focused after SelectPrevMatch" - ); - }); - let last_match_selections = search_bar.update(cx, |search_bar, cx| { - let all_selections = - editor.update(cx, |editor, cx| editor.selections.display_ranges(cx)); - assert_eq!( - all_selections.len(), - 1, - "On previous match, should deselect items and select the previous item" - ); - assert_eq!( - all_selections, initial_selections, - "Previous match should be the same as the first selection" - ); - assert_eq!( - search_bar.active_match_index, - Some(0), - "Match index should be updated to the previous one" - ); - all_selections - }); + let last_match_selections = window + .update(cx, |_, cx| { + search_bar.update(cx, |search_bar, cx| { + let handle = search_bar.query_editor.focus_handle(cx); + cx.focus(&handle); + search_bar.activate_current_match(cx); + }); + assert!( + !editor.read(cx).is_focused(cx), + "Initially, the editor should not be focused" + ); + let initial_selections = editor.update(cx, |editor, cx| { + let initial_selections = editor.selections.display_ranges(cx); + assert_eq!( + initial_selections.len(), 1, + "Expected to have only one selection before adding carets to all matches, but got: {initial_selections:?}", + ); + initial_selections + }); + search_bar.update(cx, |search_bar, cx| { + assert_eq!(search_bar.active_match_index, Some(0)); + let handle = search_bar.query_editor.focus_handle(cx); + cx.focus(&handle); + search_bar.select_all_matches(&SelectAllMatches, cx); + }); + assert!( + editor.read(cx).is_focused(cx), + "Should focus editor after successful SelectAllMatches" + ); + search_bar.update(cx, |search_bar, cx| { + let all_selections = + editor.update(cx, |editor, cx| editor.selections.display_ranges(cx)); + assert_eq!( + all_selections.len(), + expected_query_matches_count, + "Should select all `a` characters in the buffer, but got: {all_selections:?}" + ); + assert_eq!( + search_bar.active_match_index, + Some(0), + "Match index should not change after selecting all matches" + ); + }); + search_bar.update(cx, |this, cx| this.select_next_match(&SelectNextMatch, cx)); + assert!( + editor.read(cx).is_focused(cx), + "Should still have editor focused after SelectNextMatch" + ); + search_bar.update(cx, |search_bar, cx| { + let all_selections = + editor.update(cx, |editor, cx| editor.selections.display_ranges(cx)); + assert_eq!( + all_selections.len(), + 1, + "On next match, should deselect items and select the next match" + ); + assert_ne!( + all_selections, initial_selections, + "Next match should be different from the first selection" + ); + assert_eq!( + search_bar.active_match_index, + Some(1), + "Match index should be updated to the next one" + ); + let handle = search_bar.query_editor.focus_handle(cx); + cx.focus(&handle); + search_bar.select_all_matches(&SelectAllMatches, cx); + }); + assert!( + editor.read(cx).is_focused(cx), + "Should focus editor after successful SelectAllMatches" + ); + search_bar.update(cx, |search_bar, cx| { + let all_selections = + editor.update(cx, |editor, cx| editor.selections.display_ranges(cx)); + assert_eq!( + all_selections.len(), + expected_query_matches_count, + "Should select all `a` characters in the buffer, but got: {all_selections:?}" + ); + assert_eq!( + search_bar.active_match_index, + Some(1), + "Match index should not change after selecting all matches" + ); + }); + search_bar.update(cx, |search_bar, cx| { + search_bar.select_prev_match(&SelectPrevMatch, cx); + }); + assert!( + editor.read(cx).is_focused(&cx), + "Should still have editor focused after SelectPrevMatch" + ); + search_bar.update(cx, |search_bar, cx| { + let all_selections = + editor.update(cx, |editor, cx| editor.selections.display_ranges(cx)); + assert_eq!( + all_selections.len(), + 1, + "On previous match, should deselect items and select the previous item" + ); + assert_eq!( + all_selections, initial_selections, + "Previous match should be the same as the first selection" + ); + assert_eq!( + search_bar.active_match_index, + Some(0), + "Match index should be updated to the previous one" + ); + all_selections + }) + }) + .unwrap(); - search_bar - .update(cx, |search_bar, cx| { - cx.focus(search_bar.query_editor.as_any()); - search_bar.search("abas_nonexistent_match", None, cx) + window + .update(cx, |_, cx| { + search_bar.update(cx, |search_bar, cx| { + let handle = search_bar.query_editor.focus_handle(cx); + cx.focus(&handle); + search_bar.search("abas_nonexistent_match", None, cx) + }) }) + .unwrap() .await .unwrap(); - search_bar.update(cx, |search_bar, cx| { - search_bar.select_all_matches(&SelectAllMatches, cx); - }); - window.read_with(cx, |cx| { + window.update(cx, |_, cx| { + search_bar.update(cx, |search_bar, cx| { + search_bar.select_all_matches(&SelectAllMatches, cx); + }); assert!( - !editor.is_focused(cx), + editor.update(cx, |this, cx| !this.is_focused(cx.window_context())), "Should not switch focus to editor if SelectAllMatches does not find any matches" ); - }); - search_bar.update(cx, |search_bar, cx| { - let all_selections = - editor.update(cx, |editor, cx| editor.selections.display_ranges(cx)); - assert_eq!( - all_selections, last_match_selections, - "Should not select anything new if there are no matches" - ); - assert!( - search_bar.active_match_index.is_none(), - "For no matches, there should be no active match index" - ); + search_bar.update(cx, |search_bar, cx| { + let all_selections = + editor.update(cx, |editor, cx| editor.selections.display_ranges(cx)); + assert_eq!( + all_selections, last_match_selections, + "Should not select anything new if there are no matches" + ); + assert!( + search_bar.active_match_index.is_none(), + "For no matches, there should be no active match index" + ); + }); }); } #[gpui::test] async fn test_search_query_history(cx: &mut TestAppContext) { - crate::project_search::tests::init_test(cx); - + //crate::project_search::tests::init_test(cx); + init_globals(cx); let buffer_text = r#" A regular expression (shortened as regex or regexp;[1] also referred to as rational expression[2][3]) is a sequence of characters that specifies a search @@ -1442,12 +1454,12 @@ mod tests { for "find" or "find and replace" operations on strings, or for input validation. "# .unindent(); - let buffer = cx.add_model(|cx| Buffer::new(0, cx.model_id() as u64, buffer_text)); - let window = cx.add_window(|_| EmptyView); + let buffer = cx.build_model(|cx| Buffer::new(0, cx.entity_id().as_u64(), buffer_text)); + let (window, cx) = cx.add_window_view(|_| EmptyView {}); - let editor = window.add_view(cx, |cx| Editor::for_buffer(buffer.clone(), None, cx)); + let editor = cx.build_view(|cx| Editor::for_buffer(buffer.clone(), None, cx)); - let search_bar = window.add_view(cx, |cx| { + let search_bar = cx.build_view(|cx| { let mut search_bar = BufferSearchBar::new(cx); search_bar.set_active_pane_item(Some(&editor), cx); search_bar.show(cx); @@ -1470,7 +1482,7 @@ mod tests { .await .unwrap(); // Ensure that the latest search is active. - search_bar.read_with(cx, |search_bar, cx| { + search_bar.update(cx, |search_bar, cx| { assert_eq!(search_bar.query(cx), "c"); assert_eq!(search_bar.search_options, SearchOptions::CASE_SENSITIVE); }); @@ -1479,14 +1491,14 @@ mod tests { search_bar.update(cx, |search_bar, cx| { search_bar.next_history_query(&NextHistoryQuery, cx); }); - search_bar.read_with(cx, |search_bar, cx| { + search_bar.update(cx, |search_bar, cx| { assert_eq!(search_bar.query(cx), ""); assert_eq!(search_bar.search_options, SearchOptions::CASE_SENSITIVE); }); search_bar.update(cx, |search_bar, cx| { search_bar.next_history_query(&NextHistoryQuery, cx); }); - search_bar.read_with(cx, |search_bar, cx| { + search_bar.update(cx, |search_bar, cx| { assert_eq!(search_bar.query(cx), ""); assert_eq!(search_bar.search_options, SearchOptions::CASE_SENSITIVE); }); @@ -1495,7 +1507,7 @@ mod tests { search_bar.update(cx, |search_bar, cx| { search_bar.previous_history_query(&PreviousHistoryQuery, cx); }); - search_bar.read_with(cx, |search_bar, cx| { + search_bar.update(cx, |search_bar, cx| { assert_eq!(search_bar.query(cx), "c"); assert_eq!(search_bar.search_options, SearchOptions::CASE_SENSITIVE); }); @@ -1504,7 +1516,7 @@ mod tests { search_bar.update(cx, |search_bar, cx| { search_bar.previous_history_query(&PreviousHistoryQuery, cx); }); - search_bar.read_with(cx, |search_bar, cx| { + search_bar.update(cx, |search_bar, cx| { assert_eq!(search_bar.query(cx), "b"); assert_eq!(search_bar.search_options, SearchOptions::CASE_SENSITIVE); }); @@ -1513,14 +1525,14 @@ mod tests { search_bar.update(cx, |search_bar, cx| { search_bar.previous_history_query(&PreviousHistoryQuery, cx); }); - search_bar.read_with(cx, |search_bar, cx| { + search_bar.update(cx, |search_bar, cx| { assert_eq!(search_bar.query(cx), "a"); assert_eq!(search_bar.search_options, SearchOptions::CASE_SENSITIVE); }); search_bar.update(cx, |search_bar, cx| { search_bar.previous_history_query(&PreviousHistoryQuery, cx); }); - search_bar.read_with(cx, |search_bar, cx| { + search_bar.update(cx, |search_bar, cx| { assert_eq!(search_bar.query(cx), "a"); assert_eq!(search_bar.search_options, SearchOptions::CASE_SENSITIVE); }); @@ -1529,7 +1541,7 @@ mod tests { search_bar.update(cx, |search_bar, cx| { search_bar.next_history_query(&NextHistoryQuery, cx); }); - search_bar.read_with(cx, |search_bar, cx| { + search_bar.update(cx, |search_bar, cx| { assert_eq!(search_bar.query(cx), "b"); assert_eq!(search_bar.search_options, SearchOptions::CASE_SENSITIVE); }); @@ -1538,7 +1550,7 @@ mod tests { .update(cx, |search_bar, cx| search_bar.search("ba", None, cx)) .await .unwrap(); - search_bar.read_with(cx, |search_bar, cx| { + search_bar.update(cx, |search_bar, cx| { assert_eq!(search_bar.query(cx), "ba"); assert_eq!(search_bar.search_options, SearchOptions::NONE); }); @@ -1547,42 +1559,42 @@ mod tests { search_bar.update(cx, |search_bar, cx| { search_bar.previous_history_query(&PreviousHistoryQuery, cx); }); - search_bar.read_with(cx, |search_bar, cx| { + search_bar.update(cx, |search_bar, cx| { assert_eq!(search_bar.query(cx), "c"); assert_eq!(search_bar.search_options, SearchOptions::NONE); }); search_bar.update(cx, |search_bar, cx| { search_bar.previous_history_query(&PreviousHistoryQuery, cx); }); - search_bar.read_with(cx, |search_bar, cx| { + search_bar.update(cx, |search_bar, cx| { assert_eq!(search_bar.query(cx), "b"); assert_eq!(search_bar.search_options, SearchOptions::NONE); }); search_bar.update(cx, |search_bar, cx| { search_bar.next_history_query(&NextHistoryQuery, cx); }); - search_bar.read_with(cx, |search_bar, cx| { + search_bar.update(cx, |search_bar, cx| { assert_eq!(search_bar.query(cx), "c"); assert_eq!(search_bar.search_options, SearchOptions::NONE); }); search_bar.update(cx, |search_bar, cx| { search_bar.next_history_query(&NextHistoryQuery, cx); }); - search_bar.read_with(cx, |search_bar, cx| { + search_bar.update(cx, |search_bar, cx| { assert_eq!(search_bar.query(cx), "ba"); assert_eq!(search_bar.search_options, SearchOptions::NONE); }); search_bar.update(cx, |search_bar, cx| { search_bar.next_history_query(&NextHistoryQuery, cx); }); - search_bar.read_with(cx, |search_bar, cx| { + search_bar.update(cx, |search_bar, cx| { assert_eq!(search_bar.query(cx), ""); assert_eq!(search_bar.search_options, SearchOptions::NONE); }); } #[gpui::test] async fn test_replace_simple(cx: &mut TestAppContext) { - let (editor, search_bar) = init_test(cx); + let (editor, search_bar, cx) = init_test(cx); search_bar .update(cx, |search_bar, cx| { @@ -1599,7 +1611,7 @@ mod tests { search_bar.replace_all(&ReplaceAll, cx) }); assert_eq!( - editor.read_with(cx, |this, cx| { this.text(cx) }), + editor.update(cx, |this, cx| { this.text(cx) }), r#" A regular expr$1 (shortened as regex or regexp;[1] also referred to as rational expr$1[2][3]) is a sequence of characters that specifies a search @@ -1625,7 +1637,7 @@ mod tests { }); // Notice how the first or in the text (shORtened) is not replaced. Neither are the remaining hits of `or` in the text. assert_eq!( - editor.read_with(cx, |this, cx| { this.text(cx) }), + editor.update(cx, |this, cx| { this.text(cx) }), r#" A regular expr$1 (shortened as regex banana regexp;[1] also referred to as rational expr$1[2][3]) is a sequence of characters that specifies a search @@ -1649,7 +1661,7 @@ mod tests { search_bar.replace_all(&ReplaceAll, cx) }); assert_eq!( - editor.read_with(cx, |this, cx| { this.text(cx) }), + editor.update(cx, |this, cx| { this.text(cx) }), r#" A regular expr$1 (shortened as regex banana regexp;1number also referred to as rational expr$12number3number) is a sequence of characters that specifies a search @@ -1675,7 +1687,7 @@ mod tests { // The only word affected by this edit should be `algorithms`, even though there's a bunch // of words in this text that would match this regex if not for WHOLE_WORD. assert_eq!( - editor.read_with(cx, |this, cx| { this.text(cx) }), + editor.update(cx, |this, cx| { this.text(cx) }), r#" A regular expr$1 (shortened as regex banana regexp;1number also referred to as rational expr$12number3number) is a sequence of characters that specifies a search From 88024ca7c93d6e60fd5e1b4f90287406b01cb123 Mon Sep 17 00:00:00 2001 From: Mikayla Date: Sun, 19 Nov 2023 18:32:31 -0800 Subject: [PATCH 17/69] Remove V parameter on elements --- crates/gpui2/src/app/test_context.rs | 2 +- crates/gpui2/src/element.rs | 144 +++--- crates/gpui2/src/elements/div.rs | 599 ++++++++++------------ crates/gpui2/src/elements/img.rs | 36 +- crates/gpui2/src/elements/overlay.rs | 30 +- crates/gpui2/src/elements/svg.rs | 28 +- crates/gpui2/src/elements/text.rs | 43 +- crates/gpui2/src/elements/uniform_list.rs | 74 ++- crates/gpui2/src/gpui2.rs | 24 + crates/gpui2/src/interactive.rs | 55 +- crates/gpui2/src/style.rs | 4 +- crates/gpui2/src/view.rs | 81 ++- crates/gpui2/src/window.rs | 123 +++-- 13 files changed, 578 insertions(+), 665 deletions(-) diff --git a/crates/gpui2/src/app/test_context.rs b/crates/gpui2/src/app/test_context.rs index 0315e362b0f6c2dd3f6a8cb4356345b1d567e9ad..8687646b4b7706c35a67ceb7a29fd276ab897c73 100644 --- a/crates/gpui2/src/app/test_context.rs +++ b/crates/gpui2/src/app/test_context.rs @@ -631,7 +631,7 @@ impl AnyWindowHandle { pub struct EmptyView {} impl Render for EmptyView { - type Element = Div; + type Element = Div; fn render(&mut self, _cx: &mut crate::ViewContext) -> Self::Element { div() diff --git a/crates/gpui2/src/element.rs b/crates/gpui2/src/element.rs index c773bb6f65207c1d3a2ef4bc8643072def5a30ce..d36f5f1635f043b1c1ab49fb4edce8465fa2c71d 100644 --- a/crates/gpui2/src/element.rs +++ b/crates/gpui2/src/element.rs @@ -1,37 +1,34 @@ use crate::{ - AvailableSpace, BorrowWindow, Bounds, ElementId, LayoutId, Pixels, Point, Size, ViewContext, + AvailableSpace, BorrowWindow, Bounds, ElementId, LayoutId, Pixels, Point, Size, WindowContext, }; use derive_more::{Deref, DerefMut}; pub(crate) use smallvec::SmallVec; use std::{any::Any, fmt::Debug, mem}; -pub trait Element { +pub trait Element { type ElementState: 'static; fn element_id(&self) -> Option; fn layout( &mut self, - view_state: &mut V, element_state: Option, - cx: &mut ViewContext, + cx: &mut WindowContext, ) -> (LayoutId, Self::ElementState); fn paint( &mut self, bounds: Bounds, - view_state: &mut V, element_state: &mut Self::ElementState, - cx: &mut ViewContext, + cx: &mut WindowContext, ); fn draw( self, origin: Point, available_space: Size, - view_state: &mut V, - cx: &mut ViewContext, - f: impl FnOnce(&Self::ElementState, &mut ViewContext) -> R, + cx: &mut WindowContext, + f: impl FnOnce(&Self::ElementState, &mut WindowContext) -> R, ) -> R where Self: Sized, @@ -41,7 +38,7 @@ pub trait Element { element: self, phase: ElementRenderPhase::Start, }; - element.draw(origin, available_space.map(Into::into), view_state, cx); + element.draw(origin, available_space.map(Into::into), cx); if let ElementRenderPhase::Painted { frame_state } = &element.phase { if let Some(frame_state) = frame_state.as_ref() { f(&frame_state, cx) @@ -65,10 +62,10 @@ pub trait Element { #[derive(Deref, DerefMut, Default, Clone, Debug, Eq, PartialEq, Hash)] pub struct GlobalElementId(SmallVec<[ElementId; 32]>); -pub trait ParentComponent { - fn children_mut(&mut self) -> &mut SmallVec<[AnyElement; 2]>; +pub trait ParentComponent { + fn children_mut(&mut self) -> &mut SmallVec<[AnyElement; 2]>; - fn child(mut self, child: impl Component) -> Self + fn child(mut self, child: impl Component) -> Self where Self: Sized, { @@ -76,7 +73,7 @@ pub trait ParentComponent { self } - fn children(mut self, iter: impl IntoIterator>) -> Self + fn children(mut self, iter: impl IntoIterator) -> Self where Self: Sized, { @@ -86,26 +83,24 @@ pub trait ParentComponent { } } -trait ElementObject { +trait ElementObject { fn element_id(&self) -> Option; - fn layout(&mut self, view_state: &mut V, cx: &mut ViewContext) -> LayoutId; - fn paint(&mut self, view_state: &mut V, cx: &mut ViewContext); + fn layout(&mut self, cx: &mut WindowContext) -> LayoutId; + fn paint(&mut self, cx: &mut WindowContext); fn measure( &mut self, available_space: Size, - view_state: &mut V, - cx: &mut ViewContext, + cx: &mut WindowContext, ) -> Size; fn draw( &mut self, origin: Point, available_space: Size, - view_state: &mut V, - cx: &mut ViewContext, + cx: &mut WindowContext, ); } -struct RenderedElement> { +struct RenderedElement { element: E, phase: ElementRenderPhase, } @@ -131,7 +126,7 @@ enum ElementRenderPhase { /// Internal struct that wraps an element to store Layout and ElementState after the element is rendered. /// It's allocated as a trait object to erase the element type and wrapped in AnyElement for /// improved usability. -impl> RenderedElement { +impl RenderedElement { fn new(element: E) -> Self { RenderedElement { element, @@ -140,25 +135,25 @@ impl> RenderedElement { } } -impl ElementObject for RenderedElement +impl ElementObject for RenderedElement where - E: Element, + E: Element, E::ElementState: 'static, { fn element_id(&self) -> Option { self.element.element_id() } - fn layout(&mut self, state: &mut V, cx: &mut ViewContext) -> LayoutId { + fn layout(&mut self, cx: &mut WindowContext) -> LayoutId { let (layout_id, frame_state) = match mem::take(&mut self.phase) { ElementRenderPhase::Start => { if let Some(id) = self.element.element_id() { let layout_id = cx.with_element_state(id, |element_state, cx| { - self.element.layout(state, element_state, cx) + self.element.layout(element_state, cx) }); (layout_id, None) } else { - let (layout_id, frame_state) = self.element.layout(state, None, cx); + let (layout_id, frame_state) = self.element.layout(None, cx); (layout_id, Some(frame_state)) } } @@ -176,7 +171,7 @@ where layout_id } - fn paint(&mut self, view_state: &mut V, cx: &mut ViewContext) { + fn paint(&mut self, cx: &mut WindowContext) { self.phase = match mem::take(&mut self.phase) { ElementRenderPhase::LayoutRequested { layout_id, @@ -191,13 +186,12 @@ where if let Some(id) = self.element.element_id() { cx.with_element_state(id, |element_state, cx| { let mut element_state = element_state.unwrap(); - self.element - .paint(bounds, view_state, &mut element_state, cx); + self.element.paint(bounds, &mut element_state, cx); ((), element_state) }); } else { self.element - .paint(bounds, view_state, frame_state.as_mut().unwrap(), cx); + .paint(bounds, frame_state.as_mut().unwrap(), cx); } ElementRenderPhase::Painted { frame_state } } @@ -209,11 +203,10 @@ where fn measure( &mut self, available_space: Size, - view_state: &mut V, - cx: &mut ViewContext, + cx: &mut WindowContext, ) -> Size { if matches!(&self.phase, ElementRenderPhase::Start) { - self.layout(view_state, cx); + self.layout(cx); } let layout_id = match &mut self.phase { @@ -251,21 +244,19 @@ where &mut self, origin: Point, available_space: Size, - view_state: &mut V, - cx: &mut ViewContext, + cx: &mut WindowContext, ) { - self.measure(available_space, view_state, cx); - cx.with_absolute_element_offset(origin, |cx| self.paint(view_state, cx)) + self.measure(available_space, cx); + cx.with_absolute_element_offset(origin, |cx| self.paint(cx)) } } -pub struct AnyElement(Box>); +pub struct AnyElement(Box); -impl AnyElement { +impl AnyElement { pub fn new(element: E) -> Self where - V: 'static, - E: 'static + Element, + E: 'static + Element, E::ElementState: Any, { AnyElement(Box::new(RenderedElement::new(element))) @@ -275,22 +266,21 @@ impl AnyElement { self.0.element_id() } - pub fn layout(&mut self, view_state: &mut V, cx: &mut ViewContext) -> LayoutId { - self.0.layout(view_state, cx) + pub fn layout(&mut self, cx: &mut WindowContext) -> LayoutId { + self.0.layout(cx) } - pub fn paint(&mut self, view_state: &mut V, cx: &mut ViewContext) { - self.0.paint(view_state, cx) + pub fn paint(&mut self, cx: &mut WindowContext) { + self.0.paint(cx) } /// Initializes this element and performs layout within the given available space to determine its size. pub fn measure( &mut self, available_space: Size, - view_state: &mut V, - cx: &mut ViewContext, + cx: &mut WindowContext, ) -> Size { - self.0.measure(available_space, view_state, cx) + self.0.measure(available_space, cx) } /// Initializes this element and performs layout in the available space, then paints it at the given origin. @@ -298,20 +288,19 @@ impl AnyElement { &mut self, origin: Point, available_space: Size, - view_state: &mut V, - cx: &mut ViewContext, + cx: &mut WindowContext, ) { - self.0.draw(origin, available_space, view_state, cx) + self.0.draw(origin, available_space, cx) } } -pub trait Component { - fn render(self) -> AnyElement; +pub trait Component { + fn render(self) -> AnyElement; fn map(self, f: impl FnOnce(Self) -> U) -> U where Self: Sized, - U: Component, + U: Component, { f(self) } @@ -337,19 +326,18 @@ pub trait Component { } } -impl Component for AnyElement { - fn render(self) -> AnyElement { +impl Component for AnyElement { + fn render(self) -> AnyElement { self } } -impl Element for Option +impl Element for Option where - V: 'static, - E: 'static + Component, - F: FnOnce(&mut V, &mut ViewContext<'_, V>) -> E + 'static, + E: 'static + Component, + F: FnOnce(&mut WindowContext) -> E + 'static, { - type ElementState = AnyElement; + type ElementState = AnyElement; fn element_id(&self) -> Option { None @@ -357,45 +345,41 @@ where fn layout( &mut self, - view_state: &mut V, _: Option, - cx: &mut ViewContext, + cx: &mut WindowContext, ) -> (LayoutId, Self::ElementState) { let render = self.take().unwrap(); - let mut rendered_element = (render)(view_state, cx).render(); - let layout_id = rendered_element.layout(view_state, cx); + let mut rendered_element = (render)(cx).render(); + let layout_id = rendered_element.layout(cx); (layout_id, rendered_element) } fn paint( &mut self, _bounds: Bounds, - view_state: &mut V, rendered_element: &mut Self::ElementState, - cx: &mut ViewContext, + cx: &mut WindowContext, ) { - rendered_element.paint(view_state, cx) + rendered_element.paint(cx) } } -impl Component for Option +impl Component for Option where - V: 'static, - E: 'static + Component, - F: FnOnce(&mut V, &mut ViewContext<'_, V>) -> E + 'static, + E: 'static + Component, + F: FnOnce(&mut WindowContext) -> E + 'static, { - fn render(self) -> AnyElement { + fn render(self) -> AnyElement { AnyElement::new(self) } } -impl Component for F +impl Component for F where - V: 'static, - E: 'static + Component, - F: FnOnce(&mut V, &mut ViewContext<'_, V>) -> E + 'static, + E: 'static + Component, + F: FnOnce(&mut WindowContext) -> E + 'static, { - fn render(self) -> AnyElement { + fn render(self) -> AnyElement { AnyElement::new(Some(self)) } } diff --git a/crates/gpui2/src/elements/div.rs b/crates/gpui2/src/elements/div.rs index a37e3dee2adb5898b1526e3fd944491ed637c6b2..6587f22552dda74bfd1a38bc146777e3338f7be6 100644 --- a/crates/gpui2/src/elements/div.rs +++ b/crates/gpui2/src/elements/div.rs @@ -1,9 +1,10 @@ use crate::{ point, px, Action, AnyDrag, AnyElement, AnyTooltip, AnyView, AppContext, BorrowAppContext, - BorrowWindow, Bounds, ClickEvent, Component, DispatchPhase, Element, ElementId, FocusEvent, - FocusHandle, KeyContext, KeyDownEvent, KeyUpEvent, LayoutId, MouseButton, MouseDownEvent, - MouseMoveEvent, MouseUpEvent, ParentComponent, Pixels, Point, Render, ScrollWheelEvent, - SharedString, Size, Style, StyleRefinement, Styled, Task, View, ViewContext, Visibility, + BorrowWindow, Bounds, CallbackHandle, ClickEvent, Component, ConstructorHandle, DispatchPhase, + Element, ElementId, FocusEvent, FocusHandle, KeyContext, KeyDownEvent, KeyUpEvent, LayoutId, + MouseButton, MouseDownEvent, MouseMoveEvent, MouseUpEvent, ParentComponent, Pixels, Point, + Render, ScrollWheelEvent, SharedString, Size, Style, StyleRefinement, Styled, Task, View, + Visibility, WindowContext, }; use collections::HashMap; use refineable::Refineable; @@ -12,7 +13,6 @@ use std::{ any::{Any, TypeId}, cell::RefCell, fmt::Debug, - marker::PhantomData, mem, rc::Rc, time::Duration, @@ -28,30 +28,24 @@ pub struct GroupStyle { pub style: StyleRefinement, } -pub trait InteractiveComponent: Sized + Element { - fn interactivity(&mut self) -> &mut Interactivity; +pub trait InteractiveComponent: Sized + Element { + fn interactivity(&mut self) -> &mut Interactivity; fn group(mut self, group: impl Into) -> Self { self.interactivity().group = Some(group.into()); self } - fn id(mut self, id: impl Into) -> Stateful { + fn id(mut self, id: impl Into) -> Stateful { self.interactivity().element_id = Some(id.into()); - Stateful { - element: self, - view_type: PhantomData, - } + Stateful { element: self } } - fn track_focus(mut self, focus_handle: &FocusHandle) -> Focusable { + fn track_focus(mut self, focus_handle: &FocusHandle) -> Focusable { self.interactivity().focusable = true; self.interactivity().tracked_focus_handle = Some(focus_handle.clone()); - Focusable { - element: self, - view_type: PhantomData, - } + Focusable { element: self } } fn key_context(mut self, key_context: C) -> Self @@ -85,29 +79,28 @@ pub trait InteractiveComponent: Sized + Element { fn on_mouse_down( mut self, button: MouseButton, - handler: impl Fn(&mut V, &MouseDownEvent, &mut ViewContext) + 'static, + handler: impl Into>, ) -> Self { + let handler = handler.into(); self.interactivity().mouse_down_listeners.push(Box::new( - move |view, event, bounds, phase, cx| { + move |event, bounds, phase, cx| { if phase == DispatchPhase::Bubble && event.button == button && bounds.contains_point(&event.position) { - handler(view, event, cx) + (handler.callback)(event, cx) } }, )); self } - fn on_any_mouse_down( - mut self, - handler: impl Fn(&mut V, &MouseDownEvent, &mut ViewContext) + 'static, - ) -> Self { + fn on_any_mouse_down(mut self, handler: impl Into>) -> Self { + let handler = handler.into(); self.interactivity().mouse_down_listeners.push(Box::new( - move |view, event, bounds, phase, cx| { + move |event, bounds, phase, cx| { if phase == DispatchPhase::Bubble && bounds.contains_point(&event.position) { - handler(view, event, cx) + (handler.callback)(event, cx) } }, )); @@ -117,43 +110,40 @@ pub trait InteractiveComponent: Sized + Element { fn on_mouse_up( mut self, button: MouseButton, - handler: impl Fn(&mut V, &MouseUpEvent, &mut ViewContext) + 'static, + handler: impl Into>, ) -> Self { - self.interactivity().mouse_up_listeners.push(Box::new( - move |view, event, bounds, phase, cx| { + let handler = handler.into(); + self.interactivity() + .mouse_up_listeners + .push(Box::new(move |event, bounds, phase, cx| { if phase == DispatchPhase::Bubble && event.button == button && bounds.contains_point(&event.position) { - handler(view, event, cx) + (handler.callback)(event, cx) } - }, - )); + })); self } - fn on_any_mouse_up( - mut self, - handler: impl Fn(&mut V, &MouseUpEvent, &mut ViewContext) + 'static, - ) -> Self { - self.interactivity().mouse_up_listeners.push(Box::new( - move |view, event, bounds, phase, cx| { + fn on_any_mouse_up(mut self, handler: impl Into>) -> Self { + let handler = handler.into(); + self.interactivity() + .mouse_up_listeners + .push(Box::new(move |event, bounds, phase, cx| { if phase == DispatchPhase::Bubble && bounds.contains_point(&event.position) { - handler(view, event, cx) + (handler.callback)(event, cx) } - }, - )); + })); self } - fn on_mouse_down_out( - mut self, - handler: impl Fn(&mut V, &MouseDownEvent, &mut ViewContext) + 'static, - ) -> Self { + fn on_mouse_down_out(mut self, handler: impl Into>) -> Self { + let handler = handler.into(); self.interactivity().mouse_down_listeners.push(Box::new( - move |view, event, bounds, phase, cx| { + move |event, bounds, phase, cx| { if phase == DispatchPhase::Capture && !bounds.contains_point(&event.position) { - handler(view, event, cx) + (handler.callback)(event, cx) } }, )); @@ -163,60 +153,55 @@ pub trait InteractiveComponent: Sized + Element { fn on_mouse_up_out( mut self, button: MouseButton, - handler: impl Fn(&mut V, &MouseUpEvent, &mut ViewContext) + 'static, + handler: impl Into>, ) -> Self { - self.interactivity().mouse_up_listeners.push(Box::new( - move |view, event, bounds, phase, cx| { + let handler = handler.into(); + self.interactivity() + .mouse_up_listeners + .push(Box::new(move |event, bounds, phase, cx| { if phase == DispatchPhase::Capture && event.button == button && !bounds.contains_point(&event.position) { - handler(view, event, cx); + (handler.callback)(event, cx); } - }, - )); + })); self } - fn on_mouse_move( - mut self, - handler: impl Fn(&mut V, &MouseMoveEvent, &mut ViewContext) + 'static, - ) -> Self { + fn on_mouse_move(mut self, handler: impl Into>) -> Self { + let handler = handler.into(); self.interactivity().mouse_move_listeners.push(Box::new( - move |view, event, bounds, phase, cx| { + move |event, bounds, phase, cx| { if phase == DispatchPhase::Bubble && bounds.contains_point(&event.position) { - handler(view, event, cx); + (handler.callback)(event, cx); } }, )); self } - fn on_scroll_wheel( - mut self, - handler: impl Fn(&mut V, &ScrollWheelEvent, &mut ViewContext) + 'static, - ) -> Self { + fn on_scroll_wheel(mut self, handler: impl Into>) -> Self { + let handler = handler.into(); self.interactivity().scroll_wheel_listeners.push(Box::new( - move |view, event, bounds, phase, cx| { + move |event, bounds, phase, cx| { if phase == DispatchPhase::Bubble && bounds.contains_point(&event.position) { - handler(view, event, cx); + (handler.callback)(event, cx); } }, )); self } - /// Capture the given action, fires during the capture phase - fn capture_action( - mut self, - listener: impl Fn(&mut V, &A, &mut ViewContext) + 'static, - ) -> Self { + /// Capture the given action, before normal action dispatch can fire + fn capture_action(mut self, listener: impl Into>) -> Self { + let listener = listener.into(); self.interactivity().action_listeners.push(( TypeId::of::(), - Box::new(move |view, action, phase, cx| { + Box::new(move |action, phase, cx| { let action = action.downcast_ref().unwrap(); if phase == DispatchPhase::Capture { - listener(view, action, cx) + (listener.callback)(action, cx) } }), )); @@ -224,10 +209,8 @@ pub trait InteractiveComponent: Sized + Element { } /// Add a listener for the given action, fires during the bubble event phase - fn on_action( - mut self, - listener: impl Fn(&mut V, &A, &mut ViewContext) + 'static, - ) -> Self { + fn on_action(mut self, listener: impl Into> + 'static) -> Self { + let handle = listener.into(); // NOTE: this debug assert has the side-effect of working around // a bug where a crate consisting only of action definitions does // not register the actions in debug builds: @@ -244,36 +227,60 @@ pub trait InteractiveComponent: Sized + Element { // ); self.interactivity().action_listeners.push(( TypeId::of::(), - Box::new(move |view, action, phase, cx| { + Box::new(move |action, phase, cx| { let action = action.downcast_ref().unwrap(); if phase == DispatchPhase::Bubble { - listener(view, action, cx) + (handle.callback)(action, cx) } }), )); self } - fn on_key_down( - mut self, - listener: impl Fn(&mut V, &KeyDownEvent, DispatchPhase, &mut ViewContext) + 'static, - ) -> Self { + fn on_key_down(mut self, listener: impl Into>) -> Self { + let listener = listener.into(); self.interactivity() .key_down_listeners - .push(Box::new(move |view, event, phase, cx| { - listener(view, event, phase, cx) + .push(Box::new(move |event, phase, cx| { + if phase == DispatchPhase::Bubble { + (listener.callback)(event, cx) + } })); self } - fn on_key_up( - mut self, - listener: impl Fn(&mut V, &KeyUpEvent, DispatchPhase, &mut ViewContext) + 'static, - ) -> Self { + fn capture_key_down(mut self, listener: impl Into>) -> Self { + let listener = listener.into(); + self.interactivity() + .key_down_listeners + .push(Box::new(move |event, phase, cx| { + if phase == DispatchPhase::Capture { + (listener.callback)(event, cx) + } + })); + self + } + + fn on_key_up(mut self, listener: impl Into>) -> Self { + let listener = listener.into(); self.interactivity() .key_up_listeners - .push(Box::new(move |view, event, phase, cx| { - listener(view, event, phase, cx) + .push(Box::new(move |event, phase, cx| { + if phase == DispatchPhase::Bubble { + (listener.callback)(event, cx) + } + })); + self + } + + fn capture_key_up(mut self, listener: impl Into>) -> Self { + let listener = listener.into(); + self.interactivity() + .key_up_listeners + .push(Box::new(move |event, phase, cx| { + if phase == DispatchPhase::Capture { + (listener.callback)(event, cx) + } })); self } @@ -300,27 +307,22 @@ pub trait InteractiveComponent: Sized + Element { self } - fn on_drop( - mut self, - listener: impl Fn(&mut V, View, &mut ViewContext) + 'static, - ) -> Self { + fn on_drop(mut self, listener: impl Into>>) -> Self { + let listener = listener.into(); self.interactivity().drop_listeners.push(( TypeId::of::(), - Box::new(move |view, dragged_view, cx| { - listener(view, dragged_view.downcast().unwrap(), cx); + Box::new(move |dragged_view, cx| { + (listener.callback)(&dragged_view.downcast().unwrap(), cx); }), )); self } } -pub trait StatefulInteractiveComponent>: InteractiveComponent { - fn focusable(mut self) -> Focusable { +pub trait StatefulInteractiveComponent: InteractiveComponent { + fn focusable(mut self) -> Focusable { self.interactivity().focusable = true; - Focusable { - element: self, - view_type: PhantomData, - } + Focusable { element: self } } fn overflow_scroll(mut self) -> Self { @@ -362,70 +364,64 @@ pub trait StatefulInteractiveComponent>: InteractiveCo self } - fn on_click( - mut self, - listener: impl Fn(&mut V, &ClickEvent, &mut ViewContext) + 'static, - ) -> Self + fn on_click(mut self, listener: impl Into>) -> Self where Self: Sized, { + let listener = listener.into(); self.interactivity() .click_listeners - .push(Box::new(move |view, event, cx| listener(view, event, cx))); + .push(Box::new(move |event, cx| (listener.callback)(event, cx))); self } - fn on_drag( - mut self, - listener: impl Fn(&mut V, &mut ViewContext) -> View + 'static, - ) -> Self + fn on_drag(mut self, listener: impl Into>>) -> Self where Self: Sized, W: 'static + Render, { + let listener = listener.into(); debug_assert!( self.interactivity().drag_listener.is_none(), "calling on_drag more than once on the same element is not supported" ); - self.interactivity().drag_listener = - Some(Box::new(move |view_state, cursor_offset, cx| AnyDrag { - view: listener(view_state, cx).into(), - cursor_offset, - })); + self.interactivity().drag_listener = Some(Box::new(move |cursor_offset, cx| AnyDrag { + view: (listener.callback)(cx).into(), + cursor_offset, + })); self } - fn on_hover(mut self, listener: impl 'static + Fn(&mut V, bool, &mut ViewContext)) -> Self + fn on_hover(mut self, listener: impl Into>) -> Self where Self: Sized, { + let listener = listener.into(); debug_assert!( self.interactivity().hover_listener.is_none(), "calling on_hover more than once on the same element is not supported" ); - self.interactivity().hover_listener = Some(Box::new(listener)); + self.interactivity().hover_listener = Some(listener); self } - fn tooltip( - mut self, - build_tooltip: impl Fn(&mut V, &mut ViewContext) -> AnyView + 'static, - ) -> Self + fn tooltip(mut self, build_tooltip: impl Into>) -> Self where Self: Sized, { + let build_tooltip = build_tooltip.into(); debug_assert!( self.interactivity().tooltip_builder.is_none(), "calling tooltip more than once on the same element is not supported" ); self.interactivity().tooltip_builder = - Some(Rc::new(move |view_state, cx| build_tooltip(view_state, cx))); + Some(Rc::new(move |cx| (build_tooltip.callback)(cx))); self } } -pub trait FocusableComponent: InteractiveComponent { +pub trait FocusableComponent: InteractiveComponent { fn focus(mut self, f: impl FnOnce(StyleRefinement) -> StyleRefinement) -> Self where Self: Sized, @@ -442,49 +438,44 @@ pub trait FocusableComponent: InteractiveComponent { self } - fn on_focus( - mut self, - listener: impl Fn(&mut V, &FocusEvent, &mut ViewContext) + 'static, - ) -> Self + fn on_focus(mut self, listener: impl Into>) -> Self where Self: Sized, { - self.interactivity().focus_listeners.push(Box::new( - move |view, focus_handle, event, cx| { + let listener = listener.into(); + self.interactivity() + .focus_listeners + .push(Box::new(move |focus_handle, event, cx| { if event.focused.as_ref() == Some(focus_handle) { - listener(view, event, cx) + (listener.callback)(event, cx) } - }, - )); + })); self } - fn on_blur( - mut self, - listener: impl Fn(&mut V, &FocusEvent, &mut ViewContext) + 'static, - ) -> Self + fn on_blur(mut self, listener: impl Into>) -> Self where Self: Sized, { - self.interactivity().focus_listeners.push(Box::new( - move |view, focus_handle, event, cx| { + let listener = listener.into(); + self.interactivity() + .focus_listeners + .push(Box::new(move |focus_handle, event, cx| { if event.blurred.as_ref() == Some(focus_handle) { - listener(view, event, cx) + (listener.callback)(event, cx) } - }, - )); + })); self } - fn on_focus_in( - mut self, - listener: impl Fn(&mut V, &FocusEvent, &mut ViewContext) + 'static, - ) -> Self + fn on_focus_in(mut self, listener: impl Into>) -> Self where Self: Sized, { - self.interactivity().focus_listeners.push(Box::new( - move |view, focus_handle, event, cx| { + let listener = listener.into(); + self.interactivity() + .focus_listeners + .push(Box::new(move |focus_handle, event, cx| { let descendant_blurred = event .blurred .as_ref() @@ -495,22 +486,20 @@ pub trait FocusableComponent: InteractiveComponent { .map_or(false, |focused| focus_handle.contains(focused, cx)); if !descendant_blurred && descendant_focused { - listener(view, event, cx) + (listener.callback)(event, cx) } - }, - )); + })); self } - fn on_focus_out( - mut self, - listener: impl Fn(&mut V, &FocusEvent, &mut ViewContext) + 'static, - ) -> Self + fn on_focus_out(mut self, listener: impl Into>) -> Self where Self: Sized, { - self.interactivity().focus_listeners.push(Box::new( - move |view, focus_handle, event, cx| { + let listener = listener.into(); + self.interactivity() + .focus_listeners + .push(Box::new(move |focus_handle, event, cx| { let descendant_blurred = event .blurred .as_ref() @@ -520,86 +509,73 @@ pub trait FocusableComponent: InteractiveComponent { .as_ref() .map_or(false, |focused| focus_handle.contains(focused, cx)); if descendant_blurred && !descendant_focused { - listener(view, event, cx) + (listener.callback)(event, cx) } - }, - )); + })); self } } -pub type FocusListeners = SmallVec<[FocusListener; 2]>; +pub type FocusListeners = SmallVec<[FocusListener; 2]>; -pub type FocusListener = - Box) + 'static>; +pub type FocusListener = Box; -pub type MouseDownListener = Box< - dyn Fn(&mut V, &MouseDownEvent, &Bounds, DispatchPhase, &mut ViewContext) + 'static, ->; -pub type MouseUpListener = Box< - dyn Fn(&mut V, &MouseUpEvent, &Bounds, DispatchPhase, &mut ViewContext) + 'static, ->; +pub type MouseDownListener = + Box, DispatchPhase, &mut WindowContext) + 'static>; +pub type MouseUpListener = + Box, DispatchPhase, &mut WindowContext) + 'static>; -pub type MouseMoveListener = Box< - dyn Fn(&mut V, &MouseMoveEvent, &Bounds, DispatchPhase, &mut ViewContext) + 'static, ->; +pub type MouseMoveListener = + Box, DispatchPhase, &mut WindowContext) + 'static>; -pub type ScrollWheelListener = Box< - dyn Fn(&mut V, &ScrollWheelEvent, &Bounds, DispatchPhase, &mut ViewContext) - + 'static, ->; +pub type ScrollWheelListener = + Box, DispatchPhase, &mut WindowContext) + 'static>; -pub type ClickListener = Box) + 'static>; +pub type ClickListener = Box; -pub type DragListener = - Box, &mut ViewContext) -> AnyDrag + 'static>; +pub type DragListener = Box, &mut WindowContext) -> AnyDrag + 'static>; -type DropListener = dyn Fn(&mut V, AnyView, &mut ViewContext) + 'static; +type DropListener = dyn Fn(AnyView, &mut WindowContext) + 'static; -pub type HoverListener = Box) + 'static>; +pub type TooltipBuilder = Rc AnyView + 'static>; -pub type TooltipBuilder = Rc) -> AnyView + 'static>; +pub type KeyDownListener = Box; -pub type KeyDownListener = - Box) + 'static>; +pub type KeyUpListener = Box; -pub type KeyUpListener = - Box) + 'static>; +pub type ActionListener = Box; -pub type ActionListener = - Box) + 'static>; - -pub fn div() -> Div { +pub fn div() -> Div { Div { interactivity: Interactivity::default(), children: SmallVec::default(), } } -pub struct Div { - interactivity: Interactivity, - children: SmallVec<[AnyElement; 2]>, +pub struct Div { + interactivity: Interactivity, + children: SmallVec<[AnyElement; 2]>, } -impl Styled for Div { +impl Styled for Div { fn style(&mut self) -> &mut StyleRefinement { &mut self.interactivity.base_style } } -impl InteractiveComponent for Div { - fn interactivity(&mut self) -> &mut Interactivity { +impl InteractiveComponent for Div { + fn interactivity(&mut self) -> &mut Interactivity { &mut self.interactivity } } -impl ParentComponent for Div { - fn children_mut(&mut self) -> &mut SmallVec<[AnyElement; 2]> { +impl ParentComponent for Div { + fn children_mut(&mut self) -> &mut SmallVec<[AnyElement; 2]> { &mut self.children } } -impl Element for Div { +impl Element for Div { type ElementState = DivState; fn element_id(&self) -> Option { @@ -608,9 +584,8 @@ impl Element for Div { fn layout( &mut self, - view_state: &mut V, element_state: Option, - cx: &mut ViewContext, + cx: &mut WindowContext, ) -> (LayoutId, Self::ElementState) { let mut child_layout_ids = SmallVec::new(); let mut interactivity = mem::take(&mut self.interactivity); @@ -622,7 +597,7 @@ impl Element for Div { child_layout_ids = self .children .iter_mut() - .map(|child| child.layout(view_state, cx)) + .map(|child| child.layout(cx)) .collect::>(); cx.request_layout(&style, child_layout_ids.iter().copied()) }) @@ -641,9 +616,8 @@ impl Element for Div { fn paint( &mut self, bounds: Bounds, - view_state: &mut V, element_state: &mut Self::ElementState, - cx: &mut ViewContext, + cx: &mut WindowContext, ) { let mut child_min = point(Pixels::MAX, Pixels::MAX); let mut child_max = Point::default(); @@ -680,7 +654,7 @@ impl Element for Div { cx.with_content_mask(style.overflow_mask(bounds), |cx| { cx.with_element_offset(scroll_offset, |cx| { for child in &mut self.children { - child.paint(view_state, cx); + child.paint(cx); } }) }) @@ -693,8 +667,8 @@ impl Element for Div { } } -impl Component for Div { - fn render(self) -> AnyElement { +impl Component for Div { + fn render(self) -> AnyElement { AnyElement::new(self) } } @@ -710,12 +684,12 @@ impl DivState { } } -pub struct Interactivity { +pub struct Interactivity { pub element_id: Option, pub key_context: KeyContext, pub focusable: bool, pub tracked_focus_handle: Option, - pub focus_listeners: FocusListeners, + pub focus_listeners: FocusListeners, pub group: Option, pub base_style: StyleRefinement, pub focus_style: StyleRefinement, @@ -726,29 +700,26 @@ pub struct Interactivity { pub group_active_style: Option, pub drag_over_styles: SmallVec<[(TypeId, StyleRefinement); 2]>, pub group_drag_over_styles: SmallVec<[(TypeId, GroupStyle); 2]>, - pub mouse_down_listeners: SmallVec<[MouseDownListener; 2]>, - pub mouse_up_listeners: SmallVec<[MouseUpListener; 2]>, - pub mouse_move_listeners: SmallVec<[MouseMoveListener; 2]>, - pub scroll_wheel_listeners: SmallVec<[ScrollWheelListener; 2]>, - pub key_down_listeners: SmallVec<[KeyDownListener; 2]>, - pub key_up_listeners: SmallVec<[KeyUpListener; 2]>, - pub action_listeners: SmallVec<[(TypeId, ActionListener); 8]>, - pub drop_listeners: SmallVec<[(TypeId, Box>); 2]>, - pub click_listeners: SmallVec<[ClickListener; 2]>, - pub drag_listener: Option>, - pub hover_listener: Option>, - pub tooltip_builder: Option>, + pub mouse_down_listeners: SmallVec<[MouseDownListener; 2]>, + pub mouse_up_listeners: SmallVec<[MouseUpListener; 2]>, + pub mouse_move_listeners: SmallVec<[MouseMoveListener; 2]>, + pub scroll_wheel_listeners: SmallVec<[ScrollWheelListener; 2]>, + pub key_down_listeners: SmallVec<[KeyDownListener; 2]>, + pub key_up_listeners: SmallVec<[KeyUpListener; 2]>, + pub action_listeners: SmallVec<[(TypeId, ActionListener); 8]>, + pub drop_listeners: SmallVec<[(TypeId, Box); 2]>, + pub click_listeners: SmallVec<[ClickListener; 2]>, + pub drag_listener: Option, + pub hover_listener: Option>, + pub tooltip_builder: Option, } -impl Interactivity -where - V: 'static, -{ +impl Interactivity { pub fn layout( &mut self, element_state: Option, - cx: &mut ViewContext, - f: impl FnOnce(Style, &mut ViewContext) -> LayoutId, + cx: &mut WindowContext, + f: impl FnOnce(Style, &mut WindowContext) -> LayoutId, ) -> (LayoutId, InteractiveElementState) { let mut element_state = element_state.unwrap_or_default(); @@ -774,8 +745,8 @@ where bounds: Bounds, content_size: Size, element_state: &mut InteractiveElementState, - cx: &mut ViewContext, - f: impl FnOnce(Style, Point, &mut ViewContext), + cx: &mut WindowContext, + f: impl FnOnce(Style, Point, &mut WindowContext), ) { let style = self.compute_style(Some(bounds), element_state, cx); @@ -787,26 +758,26 @@ where } for listener in self.mouse_down_listeners.drain(..) { - cx.on_mouse_event(move |state, event: &MouseDownEvent, phase, cx| { - listener(state, event, &bounds, phase, cx); + cx.on_mouse_event(move |event: &MouseDownEvent, phase, cx| { + listener(event, &bounds, phase, cx); }) } for listener in self.mouse_up_listeners.drain(..) { - cx.on_mouse_event(move |state, event: &MouseUpEvent, phase, cx| { - listener(state, event, &bounds, phase, cx); + cx.on_mouse_event(move |event: &MouseUpEvent, phase, cx| { + listener(event, &bounds, phase, cx); }) } for listener in self.mouse_move_listeners.drain(..) { - cx.on_mouse_event(move |state, event: &MouseMoveEvent, phase, cx| { - listener(state, event, &bounds, phase, cx); + cx.on_mouse_event(move |event: &MouseMoveEvent, phase, cx| { + listener(event, &bounds, phase, cx); }) } for listener in self.scroll_wheel_listeners.drain(..) { - cx.on_mouse_event(move |state, event: &ScrollWheelEvent, phase, cx| { - listener(state, event, &bounds, phase, cx); + cx.on_mouse_event(move |event: &ScrollWheelEvent, phase, cx| { + listener(event, &bounds, phase, cx); }) } @@ -817,7 +788,7 @@ where if let Some(group_bounds) = hover_group_bounds { let hovered = group_bounds.contains_point(&cx.mouse_position()); - cx.on_mouse_event(move |_, event: &MouseMoveEvent, phase, cx| { + cx.on_mouse_event(move |event: &MouseMoveEvent, phase, cx| { if phase == DispatchPhase::Capture { if group_bounds.contains_point(&event.position) != hovered { cx.notify(); @@ -830,7 +801,7 @@ where || (cx.active_drag.is_some() && !self.drag_over_styles.is_empty()) { let hovered = bounds.contains_point(&cx.mouse_position()); - cx.on_mouse_event(move |_, event: &MouseMoveEvent, phase, cx| { + cx.on_mouse_event(move |event: &MouseMoveEvent, phase, cx| { if phase == DispatchPhase::Capture { if bounds.contains_point(&event.position) != hovered { cx.notify(); @@ -841,7 +812,7 @@ where if cx.active_drag.is_some() { let drop_listeners = mem::take(&mut self.drop_listeners); - cx.on_mouse_event(move |view, event: &MouseUpEvent, phase, cx| { + cx.on_mouse_event(move |event: &MouseUpEvent, phase, cx| { if phase == DispatchPhase::Bubble && bounds.contains_point(&event.position) { if let Some(drag_state_type) = cx.active_drag.as_ref().map(|drag| drag.view.entity_type()) @@ -852,7 +823,7 @@ where .active_drag .take() .expect("checked for type drag state type above"); - listener(view, drag.view.clone(), cx); + listener(drag.view.clone(), cx); cx.notify(); cx.stop_propagation(); } @@ -872,7 +843,7 @@ where if let Some(drag_listener) = drag_listener { let active_state = element_state.clicked_state.clone(); - cx.on_mouse_event(move |view_state, event: &MouseMoveEvent, phase, cx| { + cx.on_mouse_event(move |event: &MouseMoveEvent, phase, cx| { if cx.active_drag.is_some() { if phase == DispatchPhase::Capture { cx.notify(); @@ -883,7 +854,7 @@ where { *active_state.borrow_mut() = ElementClickedState::default(); let cursor_offset = event.position - bounds.origin; - let drag = drag_listener(view_state, cursor_offset, cx); + let drag = drag_listener(cursor_offset, cx); cx.active_drag = Some(drag); cx.notify(); cx.stop_propagation(); @@ -891,21 +862,21 @@ where }); } - cx.on_mouse_event(move |view_state, event: &MouseUpEvent, phase, cx| { + cx.on_mouse_event(move |event: &MouseUpEvent, phase, cx| { if phase == DispatchPhase::Bubble && bounds.contains_point(&event.position) { let mouse_click = ClickEvent { down: mouse_down.clone(), up: event.clone(), }; for listener in &click_listeners { - listener(view_state, &mouse_click, cx); + listener(&mouse_click, cx); } } *pending_mouse_down.borrow_mut() = None; cx.notify(); }); } else { - cx.on_mouse_event(move |_view_state, event: &MouseDownEvent, phase, cx| { + cx.on_mouse_event(move |event: &MouseDownEvent, phase, cx| { if phase == DispatchPhase::Bubble && bounds.contains_point(&event.position) { *pending_mouse_down.borrow_mut() = Some(event.clone()); cx.notify(); @@ -918,7 +889,7 @@ where let was_hovered = element_state.hover_state.clone(); let has_mouse_down = element_state.pending_mouse_down.clone(); - cx.on_mouse_event(move |view_state, event: &MouseMoveEvent, phase, cx| { + cx.on_mouse_event(move |event: &MouseMoveEvent, phase, cx| { if phase != DispatchPhase::Bubble { return; } @@ -930,7 +901,7 @@ where *was_hovered = is_hovered; drop(was_hovered); - hover_listener(view_state, is_hovered, cx); + (hover_listener.callback)(&is_hovered, cx); } }); } @@ -939,7 +910,7 @@ where let active_tooltip = element_state.active_tooltip.clone(); let pending_mouse_down = element_state.pending_mouse_down.clone(); - cx.on_mouse_event(move |_, event: &MouseMoveEvent, phase, cx| { + cx.on_mouse_event(move |event: &MouseMoveEvent, phase, cx| { if phase != DispatchPhase::Bubble { return; } @@ -956,12 +927,12 @@ where let active_tooltip = active_tooltip.clone(); let tooltip_builder = tooltip_builder.clone(); - move |view, mut cx| async move { + move |mut cx| async move { cx.background_executor().timer(TOOLTIP_DELAY).await; - view.update(&mut cx, move |view_state, cx| { + cx.update(|_, cx| { active_tooltip.borrow_mut().replace(ActiveTooltip { tooltip: Some(AnyTooltip { - view: tooltip_builder(view_state, cx), + view: tooltip_builder(cx), cursor_offset: cx.mouse_position(), }), _task: None, @@ -979,7 +950,7 @@ where }); let active_tooltip = element_state.active_tooltip.clone(); - cx.on_mouse_event(move |_, _: &MouseDownEvent, _, _| { + cx.on_mouse_event(move |_: &MouseDownEvent, _, _| { active_tooltip.borrow_mut().take(); }); @@ -992,7 +963,7 @@ where let active_state = element_state.clicked_state.clone(); if !active_state.borrow().is_clicked() { - cx.on_mouse_event(move |_, _: &MouseUpEvent, phase, cx| { + cx.on_mouse_event(move |_: &MouseUpEvent, phase, cx| { if phase == DispatchPhase::Capture { *active_state.borrow_mut() = ElementClickedState::default(); cx.notify(); @@ -1003,7 +974,7 @@ where .group_active_style .as_ref() .and_then(|group_active| GroupBounds::get(&group_active.group, cx)); - cx.on_mouse_event(move |_view, down: &MouseDownEvent, phase, cx| { + cx.on_mouse_event(move |down: &MouseDownEvent, phase, cx| { if phase == DispatchPhase::Bubble { let group = active_group_bounds .map_or(false, |bounds| bounds.contains_point(&down.position)); @@ -1025,7 +996,7 @@ where let line_height = cx.line_height(); let scroll_max = (content_size - bounds.size).max(&Size::default()); - cx.on_mouse_event(move |_, event: &ScrollWheelEvent, phase, cx| { + cx.on_mouse_event(move |event: &ScrollWheelEvent, phase, cx| { if phase == DispatchPhase::Bubble && bounds.contains_point(&event.position) { let mut scroll_offset = scroll_offset.borrow_mut(); let old_scroll_offset = *scroll_offset; @@ -1063,14 +1034,14 @@ where element_state.focus_handle.clone(), |_, cx| { for listener in self.key_down_listeners.drain(..) { - cx.on_key_event(move |state, event: &KeyDownEvent, phase, cx| { - listener(state, event, phase, cx); + cx.on_key_event(move |event: &KeyDownEvent, phase, cx| { + listener(event, phase, cx); }) } for listener in self.key_up_listeners.drain(..) { - cx.on_key_event(move |state, event: &KeyUpEvent, phase, cx| { - listener(state, event, phase, cx); + cx.on_key_event(move |event: &KeyUpEvent, phase, cx| { + listener(event, phase, cx); }) } @@ -1081,9 +1052,7 @@ where if let Some(focus_handle) = element_state.focus_handle.as_ref() { for listener in self.focus_listeners.drain(..) { let focus_handle = focus_handle.clone(); - cx.on_focus_changed(move |view, event, cx| { - listener(view, &focus_handle, event, cx) - }); + cx.on_focus_changed(move |event, cx| listener(&focus_handle, event, cx)); } } @@ -1100,7 +1069,7 @@ where &self, bounds: Option>, element_state: &mut InteractiveElementState, - cx: &mut ViewContext, + cx: &mut WindowContext, ) -> Style { let mut style = Style::default(); style.refine(&self.base_style); @@ -1171,7 +1140,7 @@ where } } -impl Default for Interactivity { +impl Default for Interactivity { fn default() -> Self { Self { element_id: None, @@ -1259,31 +1228,25 @@ impl GroupBounds { } } -pub struct Focusable { +pub struct Focusable { element: E, - view_type: PhantomData, } -impl> FocusableComponent for Focusable {} +impl FocusableComponent for Focusable {} -impl InteractiveComponent for Focusable +impl InteractiveComponent for Focusable where - V: 'static, - E: InteractiveComponent, + E: InteractiveComponent, { - fn interactivity(&mut self) -> &mut Interactivity { + fn interactivity(&mut self) -> &mut Interactivity { self.element.interactivity() } } -impl> StatefulInteractiveComponent - for Focusable -{ -} +impl StatefulInteractiveComponent for Focusable {} -impl Styled for Focusable +impl Styled for Focusable where - V: 'static, E: Styled, { fn style(&mut self) -> &mut StyleRefinement { @@ -1291,10 +1254,9 @@ where } } -impl Element for Focusable +impl Element for Focusable where - V: 'static, - E: Element, + E: Element, { type ElementState = E::ElementState; @@ -1304,52 +1266,46 @@ where fn layout( &mut self, - view_state: &mut V, element_state: Option, - cx: &mut ViewContext, + cx: &mut WindowContext, ) -> (LayoutId, Self::ElementState) { - self.element.layout(view_state, element_state, cx) + self.element.layout(element_state, cx) } fn paint( &mut self, bounds: Bounds, - view_state: &mut V, element_state: &mut Self::ElementState, - cx: &mut ViewContext, + cx: &mut WindowContext, ) { - self.element.paint(bounds, view_state, element_state, cx); + self.element.paint(bounds, element_state, cx); } } -impl Component for Focusable +impl Component for Focusable where - V: 'static, - E: 'static + Element, + E: 'static + Element, { - fn render(self) -> AnyElement { + fn render(self) -> AnyElement { AnyElement::new(self) } } -impl ParentComponent for Focusable +impl ParentComponent for Focusable where - V: 'static, - E: ParentComponent, + E: ParentComponent, { - fn children_mut(&mut self) -> &mut SmallVec<[AnyElement; 2]> { + fn children_mut(&mut self) -> &mut SmallVec<[AnyElement; 2]> { self.element.children_mut() } } -pub struct Stateful { +pub struct Stateful { element: E, - view_type: PhantomData, } -impl Styled for Stateful +impl Styled for Stateful where - V: 'static, E: Styled, { fn style(&mut self) -> &mut StyleRefinement { @@ -1357,30 +1313,27 @@ where } } -impl StatefulInteractiveComponent for Stateful +impl StatefulInteractiveComponent for Stateful where - V: 'static, - E: Element, - Self: InteractiveComponent, + E: Element, + Self: InteractiveComponent, { } -impl InteractiveComponent for Stateful +impl InteractiveComponent for Stateful where - V: 'static, - E: InteractiveComponent, + E: InteractiveComponent, { - fn interactivity(&mut self) -> &mut Interactivity { + fn interactivity(&mut self) -> &mut Interactivity { self.element.interactivity() } } -impl> FocusableComponent for Stateful {} +impl FocusableComponent for Stateful {} -impl Element for Stateful +impl Element for Stateful where - V: 'static, - E: Element, + E: Element, { type ElementState = E::ElementState; @@ -1390,40 +1343,36 @@ where fn layout( &mut self, - view_state: &mut V, element_state: Option, - cx: &mut ViewContext, + cx: &mut WindowContext, ) -> (LayoutId, Self::ElementState) { - self.element.layout(view_state, element_state, cx) + self.element.layout(element_state, cx) } fn paint( &mut self, bounds: Bounds, - view_state: &mut V, element_state: &mut Self::ElementState, - cx: &mut ViewContext, + cx: &mut WindowContext, ) { - self.element.paint(bounds, view_state, element_state, cx) + self.element.paint(bounds, element_state, cx) } } -impl Component for Stateful +impl Component for Stateful where - V: 'static, - E: 'static + Element, + E: 'static + Element, { - fn render(self) -> AnyElement { + fn render(self) -> AnyElement { AnyElement::new(self) } } -impl ParentComponent for Stateful +impl ParentComponent for Stateful where - V: 'static, - E: ParentComponent, + E: ParentComponent, { - fn children_mut(&mut self) -> &mut SmallVec<[AnyElement; 2]> { + fn children_mut(&mut self) -> &mut SmallVec<[AnyElement; 2]> { self.element.children_mut() } } diff --git a/crates/gpui2/src/elements/img.rs b/crates/gpui2/src/elements/img.rs index 5376c4001227f952709d3075e8149c0f1fe3008c..023811dcd254c3b38e73afd991b207ad81157801 100644 --- a/crates/gpui2/src/elements/img.rs +++ b/crates/gpui2/src/elements/img.rs @@ -1,18 +1,17 @@ use crate::{ - AnyElement, BorrowWindow, Bounds, Component, Element, InteractiveComponent, - InteractiveElementState, Interactivity, LayoutId, Pixels, SharedString, StyleRefinement, - Styled, ViewContext, + AnyElement, Bounds, Component, Element, InteractiveComponent, InteractiveElementState, + Interactivity, LayoutId, Pixels, SharedString, StyleRefinement, Styled, WindowContext, }; use futures::FutureExt; use util::ResultExt; -pub struct Img { - interactivity: Interactivity, +pub struct Img { + interactivity: Interactivity, uri: Option, grayscale: bool, } -pub fn img() -> Img { +pub fn img() -> Img { Img { interactivity: Interactivity::default(), uri: None, @@ -20,10 +19,7 @@ pub fn img() -> Img { } } -impl Img -where - V: 'static, -{ +impl Img { pub fn uri(mut self, uri: impl Into) -> Self { self.uri = Some(uri.into()); self @@ -35,13 +31,13 @@ where } } -impl Component for Img { - fn render(self) -> AnyElement { +impl Component for Img { + fn render(self) -> AnyElement { AnyElement::new(self) } } -impl Element for Img { +impl Element for Img { type ElementState = InteractiveElementState; fn element_id(&self) -> Option { @@ -50,9 +46,8 @@ impl Element for Img { fn layout( &mut self, - _view_state: &mut V, element_state: Option, - cx: &mut ViewContext, + cx: &mut WindowContext, ) -> (LayoutId, Self::ElementState) { self.interactivity.layout(element_state, cx, |style, cx| { cx.request_layout(&style, None) @@ -62,9 +57,8 @@ impl Element for Img { fn paint( &mut self, bounds: Bounds, - _view_state: &mut V, element_state: &mut Self::ElementState, - cx: &mut ViewContext, + cx: &mut WindowContext, ) { self.interactivity.paint( bounds, @@ -89,7 +83,7 @@ impl Element for Img { .log_err() }); } else { - cx.spawn(|_, mut cx| async move { + cx.spawn(|mut cx| async move { if image_future.await.ok().is_some() { cx.on_next_frame(|cx| cx.notify()); } @@ -102,14 +96,14 @@ impl Element for Img { } } -impl Styled for Img { +impl Styled for Img { fn style(&mut self) -> &mut StyleRefinement { &mut self.interactivity.base_style } } -impl InteractiveComponent for Img { - fn interactivity(&mut self) -> &mut Interactivity { +impl InteractiveComponent for Img { + fn interactivity(&mut self) -> &mut Interactivity { &mut self.interactivity } } diff --git a/crates/gpui2/src/elements/overlay.rs b/crates/gpui2/src/elements/overlay.rs index 14a8048d398176bbd8bec49b37bc96b261450ed9..19dab158cc0aaba1cff1d8f93ad39ace7be3b0d0 100644 --- a/crates/gpui2/src/elements/overlay.rs +++ b/crates/gpui2/src/elements/overlay.rs @@ -3,15 +3,15 @@ use taffy::style::{Display, Position}; use crate::{ point, AnyElement, BorrowWindow, Bounds, Component, Element, LayoutId, ParentComponent, Pixels, - Point, Size, Style, + Point, Size, Style, WindowContext, }; pub struct OverlayState { child_layout_ids: SmallVec<[LayoutId; 4]>, } -pub struct Overlay { - children: SmallVec<[AnyElement; 2]>, +pub struct Overlay { + children: SmallVec<[AnyElement; 2]>, anchor_corner: AnchorCorner, fit_mode: OverlayFitMode, // todo!(); @@ -21,7 +21,7 @@ pub struct Overlay { /// overlay gives you a floating element that will avoid overflowing the window bounds. /// Its children should have no margin to avoid measurement issues. -pub fn overlay() -> Overlay { +pub fn overlay() -> Overlay { Overlay { children: SmallVec::new(), anchor_corner: AnchorCorner::TopLeft, @@ -30,7 +30,7 @@ pub fn overlay() -> Overlay { } } -impl Overlay { +impl Overlay { /// Sets which corner of the overlay should be anchored to the current position. pub fn anchor(mut self, anchor: AnchorCorner) -> Self { self.anchor_corner = anchor; @@ -51,19 +51,19 @@ impl Overlay { } } -impl ParentComponent for Overlay { - fn children_mut(&mut self) -> &mut SmallVec<[AnyElement; 2]> { +impl ParentComponent for Overlay { + fn children_mut(&mut self) -> &mut SmallVec<[AnyElement; 2]> { &mut self.children } } -impl Component for Overlay { - fn render(self) -> AnyElement { +impl Component for Overlay { + fn render(self) -> AnyElement { AnyElement::new(self) } } -impl Element for Overlay { +impl Element for Overlay { type ElementState = OverlayState; fn element_id(&self) -> Option { @@ -72,14 +72,13 @@ impl Element for Overlay { fn layout( &mut self, - view_state: &mut V, _: Option, - cx: &mut crate::ViewContext, + cx: &mut WindowContext, ) -> (crate::LayoutId, Self::ElementState) { let child_layout_ids = self .children .iter_mut() - .map(|child| child.layout(view_state, cx)) + .map(|child| child.layout(cx)) .collect::>(); let mut overlay_style = Style::default(); @@ -94,9 +93,8 @@ impl Element for Overlay { fn paint( &mut self, bounds: crate::Bounds, - view_state: &mut V, element_state: &mut Self::ElementState, - cx: &mut crate::ViewContext, + cx: &mut WindowContext, ) { if element_state.child_layout_ids.is_empty() { return; @@ -157,7 +155,7 @@ impl Element for Overlay { cx.with_element_offset(desired.origin - bounds.origin, |cx| { for child in &mut self.children { - child.paint(view_state, cx); + child.paint(cx); } }) } diff --git a/crates/gpui2/src/elements/svg.rs b/crates/gpui2/src/elements/svg.rs index c1c7691fbf41371cca9cba173485523df2dd7fb3..2a0df36c942384e921bb635fb60f8cfb92461b2c 100644 --- a/crates/gpui2/src/elements/svg.rs +++ b/crates/gpui2/src/elements/svg.rs @@ -1,36 +1,36 @@ use crate::{ AnyElement, Bounds, Component, Element, ElementId, InteractiveComponent, InteractiveElementState, Interactivity, LayoutId, Pixels, SharedString, StyleRefinement, - Styled, ViewContext, + Styled, WindowContext, }; use util::ResultExt; -pub struct Svg { - interactivity: Interactivity, +pub struct Svg { + interactivity: Interactivity, path: Option, } -pub fn svg() -> Svg { +pub fn svg() -> Svg { Svg { interactivity: Interactivity::default(), path: None, } } -impl Svg { +impl Svg { pub fn path(mut self, path: impl Into) -> Self { self.path = Some(path.into()); self } } -impl Component for Svg { - fn render(self) -> AnyElement { +impl Component for Svg { + fn render(self) -> AnyElement { AnyElement::new(self) } } -impl Element for Svg { +impl Element for Svg { type ElementState = InteractiveElementState; fn element_id(&self) -> Option { @@ -39,9 +39,8 @@ impl Element for Svg { fn layout( &mut self, - _view_state: &mut V, element_state: Option, - cx: &mut ViewContext, + cx: &mut WindowContext, ) -> (LayoutId, Self::ElementState) { self.interactivity.layout(element_state, cx, |style, cx| { cx.request_layout(&style, None) @@ -51,9 +50,8 @@ impl Element for Svg { fn paint( &mut self, bounds: Bounds, - _view_state: &mut V, element_state: &mut Self::ElementState, - cx: &mut ViewContext, + cx: &mut WindowContext, ) where Self: Sized, { @@ -66,14 +64,14 @@ impl Element for Svg { } } -impl Styled for Svg { +impl Styled for Svg { fn style(&mut self) -> &mut StyleRefinement { &mut self.interactivity.base_style } } -impl InteractiveComponent for Svg { - fn interactivity(&mut self) -> &mut Interactivity { +impl InteractiveComponent for Svg { + fn interactivity(&mut self) -> &mut Interactivity { &mut self.interactivity } } diff --git a/crates/gpui2/src/elements/text.rs b/crates/gpui2/src/elements/text.rs index e3c63523e1244b3005fd1803e6af2fef4e3c5576..99226baaa715014181b2e808c6b4e9c848a84ce5 100644 --- a/crates/gpui2/src/elements/text.rs +++ b/crates/gpui2/src/elements/text.rs @@ -1,6 +1,6 @@ use crate::{ - AnyElement, BorrowWindow, Bounds, Component, Element, ElementId, LayoutId, Pixels, - SharedString, Size, TextRun, ViewContext, WrappedLine, + AnyElement, Bounds, Component, Element, ElementId, LayoutId, Pixels, SharedString, Size, + TextRun, WindowContext, WrappedLine, }; use parking_lot::{Mutex, MutexGuard}; use smallvec::SmallVec; @@ -26,13 +26,13 @@ impl Text { } } -impl Component for Text { - fn render(self) -> AnyElement { +impl Component for Text { + fn render(self) -> AnyElement { AnyElement::new(self) } } -impl Element for Text { +impl Element for Text { type ElementState = TextState; fn element_id(&self) -> Option { @@ -41,9 +41,8 @@ impl Element for Text { fn layout( &mut self, - _view: &mut V, element_state: Option, - cx: &mut ViewContext, + cx: &mut WindowContext, ) -> (LayoutId, Self::ElementState) { let element_state = element_state.unwrap_or_default(); let text_system = cx.text_system().clone(); @@ -120,9 +119,8 @@ impl Element for Text { fn paint( &mut self, bounds: Bounds, - _: &mut V, element_state: &mut Self::ElementState, - cx: &mut ViewContext, + cx: &mut WindowContext, ) { let element_state = element_state.lock(); let element_state = element_state @@ -165,7 +163,7 @@ struct InteractiveTextState { clicked_range_ixs: Rc>>, } -impl Element for InteractiveText { +impl Element for InteractiveText { type ElementState = InteractiveTextState; fn element_id(&self) -> Option { @@ -174,23 +172,22 @@ impl Element for InteractiveText { fn layout( &mut self, - view_state: &mut V, element_state: Option, - cx: &mut ViewContext, + cx: &mut WindowContext, ) -> (LayoutId, Self::ElementState) { if let Some(InteractiveTextState { text_state, clicked_range_ixs, }) = element_state { - let (layout_id, text_state) = self.text.layout(view_state, Some(text_state), cx); + let (layout_id, text_state) = self.text.layout(Some(text_state), cx); let element_state = InteractiveTextState { text_state, clicked_range_ixs, }; (layout_id, element_state) } else { - let (layout_id, text_state) = self.text.layout(view_state, None, cx); + let (layout_id, text_state) = self.text.layout(None, cx); let element_state = InteractiveTextState { text_state, clicked_range_ixs: Rc::default(), @@ -202,17 +199,15 @@ impl Element for InteractiveText { fn paint( &mut self, bounds: Bounds, - view_state: &mut V, element_state: &mut Self::ElementState, - cx: &mut ViewContext, + cx: &mut WindowContext, ) { - self.text - .paint(bounds, view_state, &mut element_state.text_state, cx) + self.text.paint(bounds, &mut element_state.text_state, cx) } } -impl Component for SharedString { - fn render(self) -> AnyElement { +impl Component for SharedString { + fn render(self) -> AnyElement { Text { text: self, runs: None, @@ -221,8 +216,8 @@ impl Component for SharedString { } } -impl Component for &'static str { - fn render(self) -> AnyElement { +impl Component for &'static str { + fn render(self) -> AnyElement { Text { text: self.into(), runs: None, @@ -233,8 +228,8 @@ impl Component for &'static str { // TODO: Figure out how to pass `String` to `child` without this. // This impl doesn't exist in the `gpui2` crate. -impl Component for String { - fn render(self) -> AnyElement { +impl Component for String { + fn render(self) -> AnyElement { Text { text: self.into(), runs: None, diff --git a/crates/gpui2/src/elements/uniform_list.rs b/crates/gpui2/src/elements/uniform_list.rs index 773f9ec8aacfb2ce0ac77cc96b4c30311a406ba5..48d666383fcdecf959a89e6168b746004082c938 100644 --- a/crates/gpui2/src/elements/uniform_list.rs +++ b/crates/gpui2/src/elements/uniform_list.rs @@ -1,7 +1,7 @@ use crate::{ - point, px, size, AnyElement, AvailableSpace, BorrowWindow, Bounds, Component, Element, - ElementId, InteractiveComponent, InteractiveElementState, Interactivity, LayoutId, Pixels, - Point, Size, StyleRefinement, Styled, ViewContext, + point, px, size, AnyElement, AvailableSpace, Bounds, Component, Element, ElementId, + InteractiveComponent, InteractiveElementState, Interactivity, LayoutId, Pixels, Point, Size, + StyleRefinement, Styled, WindowContext, }; use smallvec::SmallVec; use std::{cell::RefCell, cmp, mem, ops::Range, rc::Rc}; @@ -10,15 +10,14 @@ use taffy::style::Overflow; /// uniform_list provides lazy rendering for a set of items that are of uniform height. /// When rendered into a container with overflow-y: hidden and a fixed (or max) height, /// uniform_list will only render the visibile subset of items. -pub fn uniform_list( +pub fn uniform_list( id: I, item_count: usize, - f: impl 'static + Fn(&mut V, Range, &mut ViewContext) -> Vec, -) -> UniformList + f: impl 'static + Fn(Range, &mut WindowContext) -> Vec, +) -> UniformList where I: Into, - V: 'static, - C: Component, + C: Component, { let id = id.into(); let mut style = StyleRefinement::default(); @@ -29,8 +28,8 @@ where style, item_count, item_to_measure_index: 0, - render_items: Box::new(move |view, visible_range, cx| { - f(view, visible_range, cx) + render_items: Box::new(move |visible_range, cx| { + f(visible_range, cx) .into_iter() .map(|component| component.render()) .collect() @@ -43,19 +42,14 @@ where } } -pub struct UniformList { +pub struct UniformList { id: ElementId, style: StyleRefinement, item_count: usize, item_to_measure_index: usize, - render_items: Box< - dyn for<'a> Fn( - &'a mut V, - Range, - &'a mut ViewContext, - ) -> SmallVec<[AnyElement; 64]>, - >, - interactivity: Interactivity, + render_items: + Box Fn(Range, &'a mut WindowContext) -> SmallVec<[AnyElement; 64]>>, + interactivity: Interactivity, scroll_handle: Option, } @@ -89,7 +83,7 @@ impl UniformListScrollHandle { } } -impl Styled for UniformList { +impl Styled for UniformList { fn style(&mut self) -> &mut StyleRefinement { &mut self.style } @@ -101,7 +95,7 @@ pub struct UniformListState { item_size: Size, } -impl Element for UniformList { +impl Element for UniformList { type ElementState = UniformListState; fn element_id(&self) -> Option { @@ -110,16 +104,15 @@ impl Element for UniformList { fn layout( &mut self, - view_state: &mut V, element_state: Option, - cx: &mut ViewContext, + cx: &mut WindowContext, ) -> (LayoutId, Self::ElementState) { let max_items = self.item_count; let rem_size = cx.rem_size(); let item_size = element_state .as_ref() .map(|s| s.item_size) - .unwrap_or_else(|| self.measure_item(view_state, None, cx)); + .unwrap_or_else(|| self.measure_item(None, cx)); let (layout_id, interactive) = self.interactivity @@ -161,9 +154,8 @@ impl Element for UniformList { fn paint( &mut self, bounds: Bounds, - view_state: &mut V, element_state: &mut Self::ElementState, - cx: &mut ViewContext, + cx: &mut WindowContext, ) { let style = self.interactivity @@ -209,9 +201,8 @@ impl Element for UniformList { style.paint(bounds, cx); if self.item_count > 0 { - let item_height = self - .measure_item(view_state, Some(padded_bounds.size.width), cx) - .height; + let item_height = + self.measure_item(Some(padded_bounds.size.width), cx).height; if let Some(scroll_handle) = self.scroll_handle.clone() { scroll_handle.0.borrow_mut().replace(ScrollHandleState { item_height, @@ -233,7 +224,7 @@ impl Element for UniformList { self.item_count, ); - let mut items = (self.render_items)(view_state, visible_range.clone(), cx); + let mut items = (self.render_items)(visible_range.clone(), cx); cx.with_z_index(1, |cx| { for (item, ix) in items.iter_mut().zip(visible_range) { let item_origin = padded_bounds.origin @@ -242,7 +233,7 @@ impl Element for UniformList { AvailableSpace::Definite(padded_bounds.size.width), AvailableSpace::Definite(item_height), ); - item.draw(item_origin, available_space, view_state, cx); + item.draw(item_origin, available_space, cx); } }); } @@ -253,24 +244,19 @@ impl Element for UniformList { } } -impl UniformList { +impl UniformList { pub fn with_width_from_item(mut self, item_index: Option) -> Self { self.item_to_measure_index = item_index.unwrap_or(0); self } - fn measure_item( - &self, - view_state: &mut V, - list_width: Option, - cx: &mut ViewContext, - ) -> Size { + fn measure_item(&self, list_width: Option, cx: &mut WindowContext) -> Size { if self.item_count == 0 { return Size::default(); } let item_ix = cmp::min(self.item_to_measure_index, self.item_count - 1); - let mut items = (self.render_items)(view_state, item_ix..item_ix + 1, cx); + let mut items = (self.render_items)(item_ix..item_ix + 1, cx); let mut item_to_measure = items.pop().unwrap(); let available_space = size( list_width.map_or(AvailableSpace::MinContent, |width| { @@ -278,7 +264,7 @@ impl UniformList { }), AvailableSpace::MinContent, ); - item_to_measure.measure(available_space, view_state, cx) + item_to_measure.measure(available_space, cx) } pub fn track_scroll(mut self, handle: UniformListScrollHandle) -> Self { @@ -287,14 +273,14 @@ impl UniformList { } } -impl InteractiveComponent for UniformList { - fn interactivity(&mut self) -> &mut crate::Interactivity { +impl InteractiveComponent for UniformList { + fn interactivity(&mut self) -> &mut crate::Interactivity { &mut self.interactivity } } -impl Component for UniformList { - fn render(self) -> AnyElement { +impl Component for UniformList { + fn render(self) -> AnyElement { AnyElement::new(self) } } diff --git a/crates/gpui2/src/gpui2.rs b/crates/gpui2/src/gpui2.rs index a24509386b5a229102cc483b64084b5c2319849e..39b148b15bd0ba625178545a6db49a8d1574f492 100644 --- a/crates/gpui2/src/gpui2.rs +++ b/crates/gpui2/src/gpui2.rs @@ -196,6 +196,30 @@ where } } +pub struct CallbackHandle { + callback: Box, +} + +impl From for CallbackHandle { + fn from(value: F) -> Self { + CallbackHandle { + callback: Box::new(value), + } + } +} + +pub struct ConstructorHandle { + callback: Box R + 'static>, +} + +impl R + 'static> From for ConstructorHandle { + fn from(value: F) -> Self { + ConstructorHandle { + callback: Box::new(value), + } + } +} + pub trait Flatten { fn flatten(self) -> Result; } diff --git a/crates/gpui2/src/interactive.rs b/crates/gpui2/src/interactive.rs index 80a89ef6257497459c848df60e68187a01e7142d..2604d24f89afa23cada8d0bc72840b8cac1f6149 100644 --- a/crates/gpui2/src/interactive.rs +++ b/crates/gpui2/src/interactive.rs @@ -1,9 +1,8 @@ use crate::{ - div, point, Component, Div, FocusHandle, Keystroke, Modifiers, Pixels, Point, Render, - ViewContext, + div, point, Div, FocusHandle, Keystroke, Modifiers, Pixels, Point, Render, ViewContext, }; use smallvec::SmallVec; -use std::{any::Any, fmt::Debug, marker::PhantomData, ops::Deref, path::PathBuf}; +use std::{any::Any, fmt::Debug, ops::Deref, path::PathBuf}; #[derive(Clone, Debug, Eq, PartialEq)] pub struct KeyDownEvent { @@ -60,32 +59,6 @@ pub struct ClickEvent { pub up: MouseUpEvent, } -pub struct Drag -where - R: Fn(&mut V, &mut ViewContext) -> E, - V: 'static, - E: Component<()>, -{ - pub state: S, - pub render_drag_handle: R, - view_type: PhantomData, -} - -impl Drag -where - R: Fn(&mut V, &mut ViewContext) -> E, - V: 'static, - E: Component<()>, -{ - pub fn new(state: S, render_drag_handle: R) -> Self { - Drag { - state, - render_drag_handle, - view_type: PhantomData, - } - } -} - #[derive(Hash, PartialEq, Eq, Copy, Clone, Debug)] pub enum MouseButton { Left, @@ -194,7 +167,7 @@ impl Deref for MouseExitEvent { pub struct ExternalPaths(pub(crate) SmallVec<[PathBuf; 2]>); impl Render for ExternalPaths { - type Element = Div; + type Element = Div; fn render(&mut self, _: &mut ViewContext) -> Self::Element { div() // Intentionally left empty because the platform will render icons for the dragged files @@ -287,7 +260,7 @@ pub struct FocusEvent { mod test { use crate::{ self as gpui, div, Component, Div, FocusHandle, InteractiveComponent, KeyBinding, - Keystroke, ParentComponent, Render, Stateful, TestAppContext, ViewContext, VisualContext, + Keystroke, ParentComponent, Render, Stateful, TestAppContext, VisualContext, }; struct TestView { @@ -299,20 +272,24 @@ mod test { actions!(TestAction); impl Render for TestView { - type Element = Stateful>; + type Element = Stateful
; - fn render(&mut self, _: &mut gpui::ViewContext) -> Self::Element { + fn render(&mut self, cx: &mut gpui::ViewContext) -> Self::Element { div().id("testview").child( div() .key_context("parent") - .on_key_down(|this: &mut TestView, _, _, _| this.saw_key_down = true) - .on_action(|this: &mut TestView, _: &TestAction, _| this.saw_action = true) - .child(|this: &mut Self, _cx: &mut ViewContext| { + .on_key_down(cx.callback(|this, _, _| this.saw_key_down = true)) + .on_action( + cx.callback(|this: &mut TestView, _: &TestAction, _| { + this.saw_action = true + }), + ) + .child( div() .key_context("nested") - .track_focus(&this.focus_handle) - .render() - }), + .track_focus(&self.focus_handle) + .render(), + ), ) } } diff --git a/crates/gpui2/src/style.rs b/crates/gpui2/src/style.rs index 1b0cabb40154575f60ead9d8404bfa7c0fb5ee34..f958b8b44cc3fbfbdcae462905c936d234643581 100644 --- a/crates/gpui2/src/style.rs +++ b/crates/gpui2/src/style.rs @@ -2,7 +2,7 @@ use crate::{ black, phi, point, rems, AbsoluteLength, BorrowAppContext, BorrowWindow, Bounds, ContentMask, Corners, CornersRefinement, CursorStyle, DefiniteLength, Edges, EdgesRefinement, Font, FontFeatures, FontStyle, FontWeight, Hsla, Length, Pixels, Point, PointRefinement, Rgba, - SharedString, Size, SizeRefinement, Styled, TextRun, ViewContext, + SharedString, Size, SizeRefinement, Styled, TextRun, WindowContext, }; use refineable::{Cascade, Refineable}; use smallvec::SmallVec; @@ -313,7 +313,7 @@ impl Style { } /// Paints the background of an element styled with this style. - pub fn paint(&self, bounds: Bounds, cx: &mut ViewContext) { + pub fn paint(&self, bounds: Bounds, cx: &mut WindowContext) { let rem_size = cx.rem_size(); cx.with_z_index(0, |cx| { diff --git a/crates/gpui2/src/view.rs b/crates/gpui2/src/view.rs index c1750d6dc54bf3c33005d49b8b57d5e453b837de..7bbbbce7aff0b632140e73bab12a92a4a20f1a86 100644 --- a/crates/gpui2/src/view.rs +++ b/crates/gpui2/src/view.rs @@ -11,7 +11,7 @@ use std::{ }; pub trait Render: 'static + Sized { - type Element: Element + 'static; + type Element: Element + 'static; fn render(&mut self, cx: &mut ViewContext) -> Self::Element; } @@ -67,7 +67,7 @@ impl View { pub fn render_with(&self, component: C) -> RenderViewWith where - C: 'static + Component, + C: 'static + Component, { RenderViewWith { view: self.clone(), @@ -105,8 +105,8 @@ impl PartialEq for View { impl Eq for View {} -impl Component for View { - fn render(self) -> AnyElement { +impl Component for View { + fn render(self) -> AnyElement { AnyElement::new(AnyView::from(self)) } } @@ -211,8 +211,8 @@ impl AnyView { } } -impl Component for AnyView { - fn render(self) -> AnyElement { +impl Component for AnyView { + fn render(self) -> AnyElement { AnyElement::new(self) } } @@ -227,7 +227,7 @@ impl From> for AnyView { } } -impl Element for AnyView { +impl Element for AnyView { type ElementState = Box; fn element_id(&self) -> Option { @@ -236,9 +236,8 @@ impl Element for AnyView { fn layout( &mut self, - _view_state: &mut ParentViewState, _element_state: Option, - cx: &mut ViewContext, + cx: &mut WindowContext, ) -> (LayoutId, Self::ElementState) { (self.layout)(self, cx) } @@ -246,9 +245,8 @@ impl Element for AnyView { fn paint( &mut self, _bounds: Bounds, - _view_state: &mut ParentViewState, rendered_element: &mut Self::ElementState, - cx: &mut ViewContext, + cx: &mut WindowContext, ) { (self.paint)(self, rendered_element, cx) } @@ -281,41 +279,39 @@ impl From> for AnyWeakView { } } -// impl Render for T -// where -// T: 'static + FnMut(&mut WindowContext) -> E, -// E: 'static + Send + Element, -// { -// type Element = E; +impl Render for T +where + T: 'static + FnMut(&mut WindowContext) -> E, + E: 'static + Send + Element, +{ + type Element = E; -// fn render(&mut self, cx: &mut ViewContext) -> Self::Element { -// (self)(cx) -// } -// } + fn render(&mut self, cx: &mut ViewContext) -> Self::Element { + (self)(cx) + } +} pub struct RenderViewWith { view: View, component: Option, } -impl Component for RenderViewWith +impl Component for RenderViewWith where - C: 'static + Component, - ParentViewState: 'static, - ViewState: 'static, + V: 'static + Render, + C: 'static + Component, { - fn render(self) -> AnyElement { + fn render(self) -> AnyElement { AnyElement::new(self) } } -impl Element for RenderViewWith +impl Element for RenderViewWith where - C: 'static + Component, - ParentViewState: 'static, - ViewState: 'static, + V: 'static + Render, + C: 'static + Component, { - type ElementState = AnyElement; + type ElementState = AnyElement; fn element_id(&self) -> Option { Some(self.view.entity_id().into()) @@ -323,25 +319,21 @@ where fn layout( &mut self, - _: &mut ParentViewState, _: Option, - cx: &mut ViewContext, + cx: &mut WindowContext, ) -> (LayoutId, Self::ElementState) { - self.view.update(cx, |view, cx| { - let mut element = self.component.take().unwrap().render(); - let layout_id = element.layout(view, cx); - (layout_id, element) - }) + let mut element = self.component.take().unwrap().render(); + let layout_id = element.layout(cx); + (layout_id, element) } fn paint( &mut self, _: Bounds, - _: &mut ParentViewState, element: &mut Self::ElementState, - cx: &mut ViewContext, + cx: &mut WindowContext, ) { - self.view.update(cx, |view, cx| element.paint(view, cx)) + element.paint(cx) } } @@ -357,7 +349,7 @@ mod any_view { let view = view.clone().downcast::().unwrap(); view.update(cx, |view, cx| { let mut element = AnyElement::new(view.render(cx)); - let layout_id = element.layout(view, cx); + let layout_id = element.layout(cx); (layout_id, Box::new(element) as Box) }) }) @@ -369,9 +361,8 @@ mod any_view { cx: &mut WindowContext, ) { cx.with_element_id(Some(view.model.entity_id), |cx| { - let view = view.clone().downcast::().unwrap(); - let element = element.downcast_mut::>().unwrap(); - view.update(cx, |view, cx| element.paint(view, cx)) + let element = element.downcast_mut::().unwrap(); + element.paint(cx); }) } } diff --git a/crates/gpui2/src/window.rs b/crates/gpui2/src/window.rs index a3fe05d39fe38fb0df0388a6731559f43a5b0638..d3517996c800dd789b32180e29fb591db94fe0cf 100644 --- a/crates/gpui2/src/window.rs +++ b/crates/gpui2/src/window.rs @@ -1,15 +1,15 @@ use crate::{ key_dispatch::DispatchActionListener, px, size, Action, AnyBox, AnyDrag, AnyView, AppContext, - AsyncWindowContext, AvailableSpace, Bounds, BoxShadow, Context, Corners, CursorStyle, - DevicePixels, DispatchNodeId, DispatchTree, DisplayId, Edges, Effect, Entity, EntityId, - EventEmitter, FileDropEvent, Flatten, FocusEvent, FontId, GlobalElementId, GlyphId, Hsla, - ImageData, InputEvent, IsZero, KeyBinding, KeyContext, KeyDownEvent, LayoutId, Model, - ModelContext, Modifiers, MonochromeSprite, MouseButton, MouseDownEvent, MouseMoveEvent, - MouseUpEvent, Path, Pixels, PlatformAtlas, PlatformDisplay, PlatformInputHandler, - PlatformWindow, Point, PolychromeSprite, PromptLevel, Quad, Render, RenderGlyphParams, - RenderImageParams, RenderSvgParams, ScaledPixels, SceneBuilder, Shadow, SharedString, Size, - Style, SubscriberSet, Subscription, TaffyLayoutEngine, Task, Underline, UnderlineStyle, View, - VisualContext, WeakView, WindowBounds, WindowOptions, SUBPIXEL_VARIANTS, + AsyncWindowContext, AvailableSpace, Bounds, BoxShadow, CallbackHandle, ConstructorHandle, + Context, Corners, CursorStyle, DevicePixels, DispatchNodeId, DispatchTree, DisplayId, Edges, + Effect, Entity, EntityId, EventEmitter, FileDropEvent, Flatten, FocusEvent, FontId, + GlobalElementId, GlyphId, Hsla, ImageData, InputEvent, IsZero, KeyBinding, KeyContext, + KeyDownEvent, LayoutId, Model, ModelContext, Modifiers, MonochromeSprite, MouseButton, + MouseDownEvent, MouseMoveEvent, MouseUpEvent, Path, Pixels, PlatformAtlas, PlatformDisplay, + PlatformInputHandler, PlatformWindow, Point, PolychromeSprite, PromptLevel, Quad, Render, + RenderGlyphParams, RenderImageParams, RenderSvgParams, ScaledPixels, SceneBuilder, Shadow, + SharedString, Size, Style, SubscriberSet, Subscription, TaffyLayoutEngine, Task, Underline, + UnderlineStyle, View, VisualContext, WeakView, WindowBounds, WindowOptions, SUBPIXEL_VARIANTS, }; use anyhow::{anyhow, Context as _, Result}; use collections::HashMap; @@ -1436,6 +1436,47 @@ impl<'a> WindowContext<'a> { .dispatch_tree .bindings_for_action(action) } + + ///========== ELEMENT RELATED FUNCTIONS =========== + pub fn with_key_dispatch( + &mut self, + context: KeyContext, + focus_handle: Option, + f: impl FnOnce(Option, &mut Self) -> R, + ) -> R { + let window = &mut self.window; + window + .current_frame + .dispatch_tree + .push_node(context.clone()); + if let Some(focus_handle) = focus_handle.as_ref() { + window + .current_frame + .dispatch_tree + .make_focusable(focus_handle.id); + } + let result = f(focus_handle, self); + + self.window.current_frame.dispatch_tree.pop_node(); + + result + } + + /// Register a focus listener for the current frame only. It will be cleared + /// on the next frame render. You should use this method only from within elements, + /// and we may want to enforce that better via a different context type. + // todo!() Move this to `FrameContext` to emphasize its individuality? + pub fn on_focus_changed( + &mut self, + listener: impl Fn(&FocusEvent, &mut WindowContext) + 'static, + ) { + self.window + .current_frame + .focus_listeners + .push(Box::new(move |event, cx| { + listener(event, cx); + })); + } } impl Context for WindowContext<'_> { @@ -2124,49 +2165,6 @@ impl<'a, V: 'static> ViewContext<'a, V> { ) } - /// Register a focus listener for the current frame only. It will be cleared - /// on the next frame render. You should use this method only from within elements, - /// and we may want to enforce that better via a different context type. - // todo!() Move this to `FrameContext` to emphasize its individuality? - pub fn on_focus_changed( - &mut self, - listener: impl Fn(&mut V, &FocusEvent, &mut ViewContext) + 'static, - ) { - let handle = self.view().downgrade(); - self.window - .current_frame - .focus_listeners - .push(Box::new(move |event, cx| { - handle - .update(cx, |view, cx| listener(view, event, cx)) - .log_err(); - })); - } - - pub fn with_key_dispatch( - &mut self, - context: KeyContext, - focus_handle: Option, - f: impl FnOnce(Option, &mut Self) -> R, - ) -> R { - let window = &mut self.window; - window - .current_frame - .dispatch_tree - .push_node(context.clone()); - if let Some(focus_handle) = focus_handle.as_ref() { - window - .current_frame - .dispatch_tree - .make_focusable(focus_handle.id); - } - let result = f(focus_handle, self); - - self.window.current_frame.dispatch_tree.pop_node(); - - result - } - pub fn spawn( &mut self, f: impl FnOnce(WeakView, AsyncWindowContext) -> Fut, @@ -2284,6 +2282,25 @@ impl<'a, V: 'static> ViewContext<'a, V> { { self.defer(|_, cx| cx.emit(Manager::Dismiss)) } + + pub fn callback( + &self, + f: impl Fn(&mut V, &E, &mut ViewContext) + 'static, + ) -> CallbackHandle { + let view = self.view().clone(); + (move |e: &E, cx: &mut WindowContext| { + view.update(cx, |view, cx| f(view, e, cx)); + }) + .into() + } + + pub fn constructor( + &self, + f: impl Fn(&mut V, &mut ViewContext) -> R + 'static, + ) -> ConstructorHandle { + let view = self.view().clone(); + (move |cx: &mut WindowContext| view.update(cx, |view, cx| f(view, cx))).into() + } } impl Context for ViewContext<'_, V> { From f3eb1d4abf192c55c84f3ff4307b162ba69bad8f Mon Sep 17 00:00:00 2001 From: Mikayla Date: Sun, 19 Nov 2023 19:11:51 -0800 Subject: [PATCH 18/69] Add button port example --- crates/ui2/src/components/button.rs | 66 ++++++++++++----------------- 1 file changed, 26 insertions(+), 40 deletions(-) diff --git a/crates/ui2/src/components/button.rs b/crates/ui2/src/components/button.rs index 1bb611a86e7559b544d8a49731fe4c85d5128ead..fdfad8a435ec44c15357f57e57d27c3999253bcf 100644 --- a/crates/ui2/src/components/button.rs +++ b/crates/ui2/src/components/button.rs @@ -1,25 +1,27 @@ use std::sync::Arc; -use gpui::{DefiniteLength, Hsla, MouseButton, StatefulInteractiveComponent, WindowContext}; +use gpui::{ + CallbackHandle, DefiniteLength, Hsla, MouseButton, StatefulInteractiveComponent, WindowContext, +}; use crate::prelude::*; use crate::{h_stack, Icon, IconButton, IconElement, Label, LineHeightStyle, TextColor}; /// Provides the flexibility to use either a standard /// button or an icon button in a given context. -pub enum ButtonOrIconButton { - Button(Button), - IconButton(IconButton), +pub enum ButtonOrIconButton { + Button(Button), + IconButton(IconButton), } -impl From> for ButtonOrIconButton { - fn from(value: Button) -> Self { +impl From