Skip to content

armgit / zed

Public Imported from GitHub
0

Code at the speed of thought – Zed is a high-performance, multiplayer code editor from the creators of Atom and Tree-sitter.

git_ui: Restore search in solo diff views (#60936)

saberoueslati777aa2ee85a6
diff --git a/crates/git_ui/src/git_panel.rs b/crates/git_ui/src/git_panel.rs
index 36d07381d81..a5b311fd52b 100644
--- a/crates/git_ui/src/git_panel.rs
+++ b/crates/git_ui/src/git_panel.rs
@@ -8741,6 +8741,7 @@ pub(crate) fn commit_title_exceeds_limit(title: &str, max_length: usize) -> bool
#[cfg(test)]
mod tests {
+ use editor::SplittableEditor;
use git::{
repository::repo_path,
status::{StatusCode, TrackedStatus, UnmergedStatus, UnmergedStatusCode},
@@ -8748,13 +8749,15 @@ mod tests {
use gpui::{TestAppContext, UpdateGlobal, VisualTestContext, px};
use indoc::indoc;
use project::FakeFs;
+ use search::{BufferSearchBar, buffer_search::Deploy};
use serde_json::json;
use settings::SettingsStore;
+ use std::any::TypeId;
use theme::LoadThemes;
use util::path;
use util::rel_path::rel_path;
- use workspace::MultiWorkspace;
+ use workspace::{MultiWorkspace, ToolbarItemEvent, ToolbarItemLocation};
use super::*;
@@ -10238,6 +10241,7 @@ mod tests {
#[gpui::test]
async fn test_group_by_staging_open_diff_uses_section_diff(cx: &mut TestAppContext) {
init_test(cx);
+ cx.update(search::buffer_search::init);
let fs = FakeFs::new(cx.background_executor.clone());
fs.insert_tree(
path!("/project"),
@@ -10313,10 +10317,54 @@ mod tests {
});
cx.run_until_parked();
- workspace.read_with(&cx, |workspace, cx| {
- assert!(workspace.active_item_as::<SoloDiffView>(cx).is_some());
+ let search_bar = workspace.update_in(&mut cx, |workspace, window, cx| {
+ let search_bar = cx.new(|cx| BufferSearchBar::new(None, window, cx));
+ workspace.active_pane().update(cx, |pane, cx| {
+ pane.toolbar().update(cx, |toolbar, cx| {
+ toolbar.add_item(search_bar.clone(), window, cx)
+ });
+ });
+ search_bar
+ });
+
+ let split_editor = workspace.read_with(&cx, |workspace, cx| {
+ let solo_diff = workspace
+ .active_item_as::<SoloDiffView>(cx)
+ .expect("SoloDiffView should be active");
+ let searchable = solo_diff
+ .read(cx)
+ .as_searchable(&solo_diff, cx)
+ .expect("SoloDiffView should expose its editor to buffer search");
+ let split_editor = searchable
+ .act_as_type(TypeId::of::<SplittableEditor>(), cx)
+ .and_then(|entity| entity.downcast::<SplittableEditor>().ok())
+ .expect("the split editor should be the searchable item");
assert_eq!(workspace.items_of_type::<StagedDiff>(cx).count(), 1);
assert_eq!(workspace.items_of_type::<SoloDiffView>(cx).count(), 1);
+ split_editor
+ });
+
+ let mut search_bar_events = cx.events::<ToolbarItemEvent, BufferSearchBar>(&search_bar);
+ cx.dispatch_action(Deploy::find());
+ cx.run_until_parked();
+ cx.read(|cx| assert!(!search_bar.read(cx).is_dismissed()));
+ assert_eq!(
+ search_bar_events
+ .try_recv()
+ .expect("search bar location event"),
+ ToolbarItemEvent::ChangeLocation(ToolbarItemLocation::Secondary)
+ );
+
+ search_bar
+ .update_in(&mut cx, |search_bar, window, cx| {
+ search_bar.search("partial", None, false, window, cx)
+ })
+ .await
+ .expect("buffer search should complete");
+
+ let focused_editor = cx.read(|cx| split_editor.read(cx).focused_editor().clone());
+ focused_editor.update_in(&mut cx, |editor, _window, cx| {
+ assert_eq!(editor.search_background_highlights(cx).len(), 1);
});
panel.update_in(&mut cx, |panel, window, cx| {
diff --git a/crates/git_ui/src/solo_diff_view.rs b/crates/git_ui/src/solo_diff_view.rs
index 9ada614cfcf..038bde7d078 100644
--- a/crates/git_ui/src/solo_diff_view.rs
+++ b/crates/git_ui/src/solo_diff_view.rs
@@ -435,7 +435,7 @@ impl Item for SoloDiffView {
}
fn as_searchable(&self, _: &Entity<Self>, _: &App) -> Option<Box<dyn SearchableItemHandle>> {
- None
+ Some(Box::new(self.editor.clone()))
}
fn for_each_project_item(
diff --git a/crates/search/src/buffer_search.rs b/crates/search/src/buffer_search.rs
index 2c51c97a0eb..f0fa4acafd0 100644
--- a/crates/search/src/buffer_search.rs
+++ b/crates/search/src/buffer_search.rs
@@ -608,13 +608,13 @@ impl ToolbarItemView for BufferSearchBar {
let is_project_search = searchable_item_handle.supported_options(cx).find_in_results;
self.active_searchable_item = Some(searchable_item_handle);
drop(self.update_matches(true, false, window, cx));
- if self.needs_expand_collapse_option(cx) {
+ if self.needs_expand_collapse_option(cx) && self.is_dismissed() {
return ToolbarItemLocation::PrimaryLeft;
} else if !self.is_dismissed() {
if is_project_search {
self.dismiss(&Default::default(), window, cx);
} else {
- return ToolbarItemLocation::Secondary;
+ return self.deployed_toolbar_location(cx);
}
}
}
@@ -944,15 +944,27 @@ impl BufferSearchBar {
cx.notify();
cx.emit(Event::UpdateLocation);
cx.emit(ToolbarItemEvent::ChangeLocation(
- if self.needs_expand_collapse_option(cx) {
- ToolbarItemLocation::PrimaryLeft
- } else {
- ToolbarItemLocation::Secondary
- },
+ self.deployed_toolbar_location(cx),
));
true
}
+ fn deployed_toolbar_location(&self, cx: &App) -> ToolbarItemLocation {
+ if self.needs_expand_collapse_option(cx) && !self.uses_hidden_splittable_editor(cx) {
+ ToolbarItemLocation::PrimaryLeft
+ } else {
+ ToolbarItemLocation::Secondary
+ }
+ }
+
+ fn uses_hidden_splittable_editor(&self, cx: &App) -> bool {
+ self.splittable_editor.is_none()
+ && self.active_searchable_item.as_ref().is_some_and(|item| {
+ item.act_as_type(TypeId::of::<SplittableEditor>(), cx)
+ .is_some()
+ })
+ }
+
fn supported_options(&self, cx: &mut Context<Self>) -> workspace::searchable::SearchOptions {
self.active_searchable_item
.as_ref()