Code at the speed of thought – Zed is a high-performance, multiplayer code editor from the creators of Atom and Tree-sitter.
ui: Add buttons to switch diff view when diffing buffers (#64529)
rn23thakuraf431aeef427
diff --git a/crates/git_ui_core/src/file_diff_view.rs b/crates/git_ui_core/src/file_diff_view.rsindex 555fca0a4d5..d5fa761b8ff 100644--- a/crates/git_ui_core/src/file_diff_view.rs+++ b/crates/git_ui_core/src/file_diff_view.rs@@ -3,13 +3,13 @@use anyhow::Result;use buffer_diff::BufferDiff;use editor::{- Editor, EditorEvent, EditorSettings, HiddenUnstagedDiffHunkRenderer, MultiBuffer,- SplittableEditor,+ DiffStyleControls, Editor, EditorEvent, EditorSettings, HiddenUnstagedDiffHunkRenderer,+ MultiBuffer, SplittableEditor,};use futures::{FutureExt, select_biased};use gpui::{- App, AppContext as _, AsyncApp, Context, Entity, EventEmitter, FocusHandle, Focusable, Font,- IntoElement, Render, Task, WeakEntity, Window,+ App, AppContext as _, AsyncApp, Context, Empty, Entity, EventEmitter, FocusHandle, Focusable,+ Font, IntoElement, Render, Task, WeakEntity, Window,};use language::{Buffer, HighlightedText, Point};use project::{Project, ProjectPath};@@ -21,10 +21,11 @@ use std::{sync::Arc,time::Duration,};-use ui::{Color, Icon, IconName, SharedString};+use ui::{Color, Icon, IconName, SharedString, prelude::*};use util::paths::PathExt as _;use workspace::{- Item, ItemHandle as _, ItemNavHistory, ToolbarItemLocation, Workspace,+ Item, ItemHandle, ItemNavHistory, ToolbarItemEvent, ToolbarItemLocation, ToolbarItemView,+ Workspace,item::{ItemEvent, SaveOptions},searchable::SearchableItemHandle,};@@ -128,6 +129,22 @@ impl FileDiffView {splittable});+ let include_root = project.read(cx).visible_worktrees(cx).count() > 1;+ let breadcrumb_path = |buffer: &Entity<Buffer>| {+ buffer+ .read(cx)+ .snapshot()+ .resolve_file_path(include_root, cx)+ .unwrap_or_else(|| MultiBuffer::DEFAULT_TITLE.to_string())+ };+ let old_path = breadcrumb_path(&old_buffer);+ let new_path = breadcrumb_path(&new_buffer);+ editor.update(cx, |editor, cx| {+ editor.rhs_editor().update(cx, |editor, _| {+ editor.set_breadcrumb_header(format!("{old_path} ↔ {new_path}"));+ });+ });+let (buffer_changes_tx, mut buffer_changes_rx) = watch::channel(());// The buffers' languages may load after the diff was built, e.g. when@@ -394,6 +411,57 @@ impl Render for FileDiffView {}}+pub struct FileDiffStyleToolbar {+ file_diff: Option<WeakEntity<FileDiffView>>,+}++impl FileDiffStyleToolbar {+ pub fn new(_: &mut Context<Self>) -> Self {+ Self { file_diff: None }+ }++ fn file_diff(&self) -> Option<Entity<FileDiffView>> {+ self.file_diff.as_ref()?.upgrade()+ }+}++impl EventEmitter<ToolbarItemEvent> for FileDiffStyleToolbar {}++impl ToolbarItemView for FileDiffStyleToolbar {+ fn set_active_pane_item(+ &mut self,+ active_pane_item: Option<&dyn ItemHandle>,+ _: &mut Window,+ cx: &mut Context<Self>,+ ) -> ToolbarItemLocation {+ self.file_diff = active_pane_item+ .and_then(|item| item.act_as::<FileDiffView>(cx))+ .map(|entity| entity.downgrade());++ if self.file_diff.is_some() {+ ToolbarItemLocation::PrimaryLeft+ } else {+ ToolbarItemLocation::Hidden+ }+ }+}++impl Render for FileDiffStyleToolbar {+ fn render(&mut self, _: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {+ let Some(file_diff) = self.file_diff() else {+ return Empty.into_any_element();+ };++ let editor = file_diff.read(cx).editor.clone();++ h_flex()+ .pl_0p5()+ .gap_1()+ .child(DiffStyleControls::new(editor))+ .into_any_element()+ }+}+#[cfg(test)]mod tests {use super::*;@@ -560,7 +628,34 @@ mod tests {path!("test/new_file.txt")));- })+ let (breadcrumbs, _) = diff_view.breadcrumbs(cx).unwrap();+ assert_eq!(+ breadcrumbs+ .iter()+ .map(|crumb| crumb.text.as_ref())+ .collect::<Vec<_>>(),+ vec!["old_file.txt ↔ new_file.txt"]+ );+ });++ let toolbar = cx.new(FileDiffStyleToolbar::new);+ let location = toolbar.update_in(cx, |toolbar, window, cx| {+ toolbar.set_active_pane_item(Some(&diff_view), window, cx)+ });+ assert_eq!(location, ToolbarItemLocation::PrimaryLeft);+ assert_eq!(+ toolbar.read_with(cx, |toolbar, _| toolbar.file_diff()),+ Some(diff_view.clone())+ );++ let location = toolbar.update_in(cx, |toolbar, window, cx| {+ toolbar.set_active_pane_item(None, window, cx)+ });+ assert_eq!(location, ToolbarItemLocation::Hidden);+ assert_eq!(+ toolbar.read_with(cx, |toolbar, _| toolbar.file_diff()),+ None+ );}#[gpui::test]diff --git a/crates/zed/src/zed.rs b/crates/zed/src/zed.rsindex c4bf14ed8e2..4034efd8998 100644--- a/crates/zed/src/zed.rs+++ b/crates/zed/src/zed.rs@@ -38,6 +38,7 @@ use git_ui::project_diff::ProjectDiffToolbar;use git_ui::solo_diff_view::{SoloDiffGitToolbar, SoloDiffStyleToolbar};use git_ui::staged_diff::StagedDiffToolbar;use git_ui::unstaged_diff::UnstagedDiffToolbar;+use git_ui_core::file_diff_view::FileDiffStyleToolbar;use gpui::{Action, App, AppContext as _, AsyncWindowContext, ClipboardItem, Context, DismissEvent,Element, Entity, FocusHandle, Focusable, Image, ImageFormat, KeyBinding, ParentElement,@@ -1466,6 +1467,8 @@ fn initialize_pane(toolbar.add_item(multibuffer_hint, window, cx);let solo_diff_style_toolbar = cx.new(SoloDiffStyleToolbar::new);toolbar.add_item(solo_diff_style_toolbar, window, cx);+ let file_diff_style_toolbar = cx.new(FileDiffStyleToolbar::new);+ toolbar.add_item(file_diff_style_toolbar, window, cx);let breadcrumbs = cx.new(|_| Breadcrumbs::new());toolbar.add_item(breadcrumbs, window, cx);let buffer_search_bar = cx.new(|cx| {diff --git a/script/bundle-mac b/script/bundle-macindex 12deaa7ede1..44f36aa1b69 100755--- a/script/bundle-mac+++ b/script/bundle-mac@@ -4,6 +4,8 @@ set -euo pipefailsource script/lib/blob-store.shbuild_flag="--release"+zed_features=""+remote_server_features=""target_dir="release"open_result=falselocal_install=false@@ -34,6 +36,8 @@ dod)export CARGO_INCREMENTAL=truebuild_flag="";+ zed_features="--features util/debug-embed"+ remote_server_features="--features debug-embed"target_dir="debug";;i) local_install=true;;@@ -87,10 +91,10 @@ script/generate-licensesrustup target add $target_tripleecho "Compiling zed binaries"-cargo --config .cargo/bundle-config.toml build ${build_flag} --package zed --package cli --target $target_triple+cargo --config .cargo/bundle-config.toml build ${build_flag} ${zed_features} --package zed --package cli --target $target_triple# Build remote_server in separate invocation to prevent feature unification from other crates# from influencing dynamic libraries required by it.-cargo --config .cargo/bundle-config.toml build ${build_flag} --package remote_server --target $target_triple+cargo --config .cargo/bundle-config.toml build ${build_flag} ${remote_server_features} --package remote_server --target $target_tripleecho "Creating application bundle"pushd crates/zed