1use crate::{
2 conflict_view::ConflictAddon,
3 git_panel::{GitPanel, GitPanelAddon, GitStatusEntry},
4 git_panel_settings::GitPanelSettings,
5 remote_button::{render_publish_button, render_push_button},
6};
7use anyhow::Result;
8use buffer_diff::{BufferDiff, DiffHunkSecondaryStatus};
9use collections::HashSet;
10use editor::{
11 Editor, EditorEvent,
12 actions::{GoToHunk, GoToPreviousHunk},
13 scroll::Autoscroll,
14};
15use futures::StreamExt;
16use git::{
17 Commit, StageAll, StageAndNext, ToggleStaged, UnstageAll, UnstageAndNext,
18 repository::{Branch, Upstream, UpstreamTracking, UpstreamTrackingStatus},
19 status::FileStatus,
20};
21use gpui::{
22 Action, AnyElement, AnyView, App, AppContext as _, AsyncWindowContext, Entity, EventEmitter,
23 FocusHandle, Focusable, Render, Subscription, Task, WeakEntity, actions,
24};
25use language::{Anchor, Buffer, Capability, OffsetRangeExt};
26use multi_buffer::{MultiBuffer, PathKey};
27use project::{
28 Project, ProjectPath,
29 git_store::{GitStore, GitStoreEvent, RepositoryEvent},
30};
31use settings::{Settings, SettingsStore};
32use std::any::{Any, TypeId};
33use std::ops::Range;
34use theme::ActiveTheme;
35use ui::{KeyBinding, Tooltip, prelude::*, vertical_divider};
36use util::ResultExt as _;
37use workspace::{
38 CloseActiveItem, ItemNavHistory, SerializableItem, ToolbarItemEvent, ToolbarItemLocation,
39 ToolbarItemView, Workspace,
40 item::{BreadcrumbText, Item, ItemEvent, ItemHandle, TabContentParams},
41 searchable::SearchableItemHandle,
42};
43
44actions!(git, [Diff, Add]);
45
46pub struct ProjectDiff {
47 project: Entity<Project>,
48 multibuffer: Entity<MultiBuffer>,
49 editor: Entity<Editor>,
50 git_store: Entity<GitStore>,
51 workspace: WeakEntity<Workspace>,
52 focus_handle: FocusHandle,
53 update_needed: postage::watch::Sender<()>,
54 pending_scroll: Option<PathKey>,
55 _task: Task<Result<()>>,
56 _subscription: Subscription,
57}
58
59#[derive(Debug)]
60struct DiffBuffer {
61 path_key: PathKey,
62 buffer: Entity<Buffer>,
63 diff: Entity<BufferDiff>,
64 file_status: FileStatus,
65}
66
67const CONFLICT_NAMESPACE: u32 = 1;
68const TRACKED_NAMESPACE: u32 = 2;
69const NEW_NAMESPACE: u32 = 3;
70
71impl ProjectDiff {
72 pub(crate) fn register(workspace: &mut Workspace, cx: &mut Context<Workspace>) {
73 workspace.register_action(Self::deploy);
74 workspace.register_action(|workspace, _: &Add, window, cx| {
75 Self::deploy(workspace, &Diff, window, cx);
76 });
77 workspace::register_serializable_item::<ProjectDiff>(cx);
78 }
79
80 fn deploy(
81 workspace: &mut Workspace,
82 _: &Diff,
83 window: &mut Window,
84 cx: &mut Context<Workspace>,
85 ) {
86 Self::deploy_at(workspace, None, window, cx)
87 }
88
89 pub fn deploy_at(
90 workspace: &mut Workspace,
91 entry: Option<GitStatusEntry>,
92 window: &mut Window,
93 cx: &mut Context<Workspace>,
94 ) {
95 telemetry::event!(
96 "Git Diff Opened",
97 source = if entry.is_some() {
98 "Git Panel"
99 } else {
100 "Action"
101 }
102 );
103 let project_diff = if let Some(existing) = workspace.item_of_type::<Self>(cx) {
104 workspace.activate_item(&existing, true, true, window, cx);
105 existing
106 } else {
107 let workspace_handle = cx.entity();
108 let project_diff =
109 cx.new(|cx| Self::new(workspace.project().clone(), workspace_handle, window, cx));
110 workspace.add_item_to_active_pane(
111 Box::new(project_diff.clone()),
112 None,
113 true,
114 window,
115 cx,
116 );
117 project_diff
118 };
119 if let Some(entry) = entry {
120 project_diff.update(cx, |project_diff, cx| {
121 project_diff.move_to_entry(entry, window, cx);
122 })
123 }
124 }
125
126 pub fn autoscroll(&self, cx: &mut Context<Self>) {
127 self.editor.update(cx, |editor, cx| {
128 editor.request_autoscroll(Autoscroll::fit(), cx);
129 })
130 }
131
132 fn new(
133 project: Entity<Project>,
134 workspace: Entity<Workspace>,
135 window: &mut Window,
136 cx: &mut Context<Self>,
137 ) -> Self {
138 let focus_handle = cx.focus_handle();
139 let multibuffer = cx.new(|_| MultiBuffer::new(Capability::ReadWrite));
140
141 let editor = cx.new(|cx| {
142 let mut diff_display_editor =
143 Editor::for_multibuffer(multibuffer.clone(), Some(project.clone()), window, cx);
144 diff_display_editor.disable_inline_diagnostics();
145 diff_display_editor.set_expand_all_diff_hunks(cx);
146 diff_display_editor.register_addon(GitPanelAddon {
147 workspace: workspace.downgrade(),
148 });
149 diff_display_editor
150 });
151 cx.subscribe_in(&editor, window, Self::handle_editor_event)
152 .detach();
153
154 let git_store = project.read(cx).git_store().clone();
155 let git_store_subscription = cx.subscribe_in(
156 &git_store,
157 window,
158 move |this, _git_store, event, _window, _cx| match event {
159 GitStoreEvent::ActiveRepositoryChanged(_)
160 | GitStoreEvent::RepositoryUpdated(_, RepositoryEvent::Updated { .. }, true)
161 | GitStoreEvent::ConflictsUpdated => {
162 *this.update_needed.borrow_mut() = ();
163 }
164 _ => {}
165 },
166 );
167
168 let mut was_sort_by_path = GitPanelSettings::get_global(cx).sort_by_path;
169 cx.observe_global::<SettingsStore>(move |this, cx| {
170 let is_sort_by_path = GitPanelSettings::get_global(cx).sort_by_path;
171 if is_sort_by_path != was_sort_by_path {
172 *this.update_needed.borrow_mut() = ();
173 }
174 was_sort_by_path = is_sort_by_path
175 })
176 .detach();
177
178 let (mut send, recv) = postage::watch::channel::<()>();
179 let worker = window.spawn(cx, {
180 let this = cx.weak_entity();
181 async |cx| Self::handle_status_updates(this, recv, cx).await
182 });
183 // Kick off a refresh immediately
184 *send.borrow_mut() = ();
185
186 Self {
187 project,
188 git_store: git_store.clone(),
189 workspace: workspace.downgrade(),
190 focus_handle,
191 editor,
192 multibuffer,
193 pending_scroll: None,
194 update_needed: send,
195 _task: worker,
196 _subscription: git_store_subscription,
197 }
198 }
199
200 pub fn move_to_entry(
201 &mut self,
202 entry: GitStatusEntry,
203 window: &mut Window,
204 cx: &mut Context<Self>,
205 ) {
206 let Some(git_repo) = self.git_store.read(cx).active_repository() else {
207 return;
208 };
209 let repo = git_repo.read(cx);
210
211 let namespace = if repo.has_conflict(&entry.repo_path) {
212 CONFLICT_NAMESPACE
213 } else if entry.status.is_created() {
214 NEW_NAMESPACE
215 } else {
216 TRACKED_NAMESPACE
217 };
218
219 let path_key = PathKey::namespaced(namespace, entry.repo_path.0.clone());
220
221 self.move_to_path(path_key, window, cx)
222 }
223
224 pub fn active_path(&self, cx: &App) -> Option<ProjectPath> {
225 let editor = self.editor.read(cx);
226 let position = editor.selections.newest_anchor().head();
227 let multi_buffer = editor.buffer().read(cx);
228 let (_, buffer, _) = multi_buffer.excerpt_containing(position, cx)?;
229
230 let file = buffer.read(cx).file()?;
231 Some(ProjectPath {
232 worktree_id: file.worktree_id(cx),
233 path: file.path().clone(),
234 })
235 }
236
237 fn move_to_path(&mut self, path_key: PathKey, window: &mut Window, cx: &mut Context<Self>) {
238 if let Some(position) = self.multibuffer.read(cx).location_for_path(&path_key, cx) {
239 self.editor.update(cx, |editor, cx| {
240 editor.change_selections(Some(Autoscroll::focused()), window, cx, |s| {
241 s.select_ranges([position..position]);
242 })
243 });
244 } else {
245 self.pending_scroll = Some(path_key);
246 }
247 }
248
249 fn button_states(&self, cx: &App) -> ButtonStates {
250 let editor = self.editor.read(cx);
251 let snapshot = self.multibuffer.read(cx).snapshot(cx);
252 let prev_next = snapshot.diff_hunks().skip(1).next().is_some();
253 let mut selection = true;
254
255 let mut ranges = editor
256 .selections
257 .disjoint_anchor_ranges()
258 .collect::<Vec<_>>();
259 if !ranges.iter().any(|range| range.start != range.end) {
260 selection = false;
261 if let Some((excerpt_id, buffer, range)) = self.editor.read(cx).active_excerpt(cx) {
262 ranges = vec![multi_buffer::Anchor::range_in_buffer(
263 excerpt_id,
264 buffer.read(cx).remote_id(),
265 range,
266 )];
267 } else {
268 ranges = Vec::default();
269 }
270 }
271 let mut has_staged_hunks = false;
272 let mut has_unstaged_hunks = false;
273 for hunk in editor.diff_hunks_in_ranges(&ranges, &snapshot) {
274 match hunk.secondary_status {
275 DiffHunkSecondaryStatus::HasSecondaryHunk
276 | DiffHunkSecondaryStatus::SecondaryHunkAdditionPending => {
277 has_unstaged_hunks = true;
278 }
279 DiffHunkSecondaryStatus::OverlapsWithSecondaryHunk => {
280 has_staged_hunks = true;
281 has_unstaged_hunks = true;
282 }
283 DiffHunkSecondaryStatus::NoSecondaryHunk
284 | DiffHunkSecondaryStatus::SecondaryHunkRemovalPending => {
285 has_staged_hunks = true;
286 }
287 }
288 }
289 let mut stage_all = false;
290 let mut unstage_all = false;
291 self.workspace
292 .read_with(cx, |workspace, cx| {
293 if let Some(git_panel) = workspace.panel::<GitPanel>(cx) {
294 let git_panel = git_panel.read(cx);
295 stage_all = git_panel.can_stage_all();
296 unstage_all = git_panel.can_unstage_all();
297 }
298 })
299 .ok();
300
301 return ButtonStates {
302 stage: has_unstaged_hunks,
303 unstage: has_staged_hunks,
304 prev_next,
305 selection,
306 stage_all,
307 unstage_all,
308 };
309 }
310
311 fn handle_editor_event(
312 &mut self,
313 editor: &Entity<Editor>,
314 event: &EditorEvent,
315 window: &mut Window,
316 cx: &mut Context<Self>,
317 ) {
318 match event {
319 EditorEvent::SelectionsChanged { local: true } => {
320 let Some(project_path) = self.active_path(cx) else {
321 return;
322 };
323 self.workspace
324 .update(cx, |workspace, cx| {
325 if let Some(git_panel) = workspace.panel::<GitPanel>(cx) {
326 git_panel.update(cx, |git_panel, cx| {
327 git_panel.select_entry_by_path(project_path, window, cx)
328 })
329 }
330 })
331 .ok();
332 }
333 _ => {}
334 }
335 if editor.focus_handle(cx).contains_focused(window, cx) {
336 if self.multibuffer.read(cx).is_empty() {
337 self.focus_handle.focus(window)
338 }
339 }
340 }
341
342 fn load_buffers(&mut self, cx: &mut Context<Self>) -> Vec<Task<Result<DiffBuffer>>> {
343 let Some(repo) = self.git_store.read(cx).active_repository() else {
344 self.multibuffer.update(cx, |multibuffer, cx| {
345 multibuffer.clear(cx);
346 });
347 return vec![];
348 };
349
350 let mut previous_paths = self.multibuffer.read(cx).paths().collect::<HashSet<_>>();
351
352 let mut result = vec![];
353 repo.update(cx, |repo, cx| {
354 for entry in repo.cached_status() {
355 if !entry.status.has_changes() {
356 continue;
357 }
358 let Some(project_path) = repo.repo_path_to_project_path(&entry.repo_path, cx)
359 else {
360 continue;
361 };
362 let namespace = if GitPanelSettings::get_global(cx).sort_by_path {
363 TRACKED_NAMESPACE
364 } else if repo.has_conflict(&entry.repo_path) {
365 CONFLICT_NAMESPACE
366 } else if entry.status.is_created() {
367 NEW_NAMESPACE
368 } else {
369 TRACKED_NAMESPACE
370 };
371 let path_key = PathKey::namespaced(namespace, entry.repo_path.0.clone());
372
373 previous_paths.remove(&path_key);
374 let load_buffer = self
375 .project
376 .update(cx, |project, cx| project.open_buffer(project_path, cx));
377
378 let project = self.project.clone();
379 result.push(cx.spawn(async move |_, cx| {
380 let buffer = load_buffer.await?;
381 let changes = project
382 .update(cx, |project, cx| {
383 project.open_uncommitted_diff(buffer.clone(), cx)
384 })?
385 .await?;
386 Ok(DiffBuffer {
387 path_key,
388 buffer,
389 diff: changes,
390 file_status: entry.status,
391 })
392 }));
393 }
394 });
395 self.multibuffer.update(cx, |multibuffer, cx| {
396 for path in previous_paths {
397 multibuffer.remove_excerpts_for_path(path, cx);
398 }
399 });
400 result
401 }
402
403 fn register_buffer(
404 &mut self,
405 diff_buffer: DiffBuffer,
406 window: &mut Window,
407 cx: &mut Context<Self>,
408 ) {
409 let path_key = diff_buffer.path_key;
410 let buffer = diff_buffer.buffer;
411 let diff = diff_buffer.diff;
412
413 let conflict_addon = self
414 .editor
415 .read(cx)
416 .addon::<ConflictAddon>()
417 .expect("project diff editor should have a conflict addon");
418
419 let snapshot = buffer.read(cx).snapshot();
420 let diff = diff.read(cx);
421 let diff_hunk_ranges = diff
422 .hunks_intersecting_range(Anchor::MIN..Anchor::MAX, &snapshot, cx)
423 .map(|diff_hunk| diff_hunk.buffer_range.clone());
424 let conflicts = conflict_addon
425 .conflict_set(snapshot.remote_id())
426 .map(|conflict_set| conflict_set.read(cx).snapshot().conflicts.clone())
427 .unwrap_or_default();
428 let conflicts = conflicts.iter().map(|conflict| conflict.range.clone());
429
430 let excerpt_ranges = merge_anchor_ranges(diff_hunk_ranges, conflicts, &snapshot)
431 .map(|range| range.to_point(&snapshot))
432 .collect::<Vec<_>>();
433
434 let (was_empty, is_excerpt_newly_added) = self.multibuffer.update(cx, |multibuffer, cx| {
435 let was_empty = multibuffer.is_empty();
436 let (_, is_newly_added) = multibuffer.set_excerpts_for_path(
437 path_key.clone(),
438 buffer,
439 excerpt_ranges,
440 editor::DEFAULT_MULTIBUFFER_CONTEXT,
441 cx,
442 );
443 (was_empty, is_newly_added)
444 });
445
446 self.editor.update(cx, |editor, cx| {
447 if was_empty {
448 editor.change_selections(None, window, cx, |selections| {
449 // TODO select the very beginning (possibly inside a deletion)
450 selections.select_ranges([0..0])
451 });
452 }
453 if is_excerpt_newly_added && diff_buffer.file_status.is_deleted() {
454 editor.fold_buffer(snapshot.text.remote_id(), cx)
455 }
456 });
457
458 if self.multibuffer.read(cx).is_empty()
459 && self
460 .editor
461 .read(cx)
462 .focus_handle(cx)
463 .contains_focused(window, cx)
464 {
465 self.focus_handle.focus(window);
466 } else if self.focus_handle.is_focused(window) && !self.multibuffer.read(cx).is_empty() {
467 self.editor.update(cx, |editor, cx| {
468 editor.focus_handle(cx).focus(window);
469 });
470 }
471 if self.pending_scroll.as_ref() == Some(&path_key) {
472 self.move_to_path(path_key, window, cx);
473 }
474 }
475
476 pub async fn handle_status_updates(
477 this: WeakEntity<Self>,
478 mut recv: postage::watch::Receiver<()>,
479 cx: &mut AsyncWindowContext,
480 ) -> Result<()> {
481 while let Some(_) = recv.next().await {
482 let buffers_to_load = this.update(cx, |this, cx| this.load_buffers(cx))?;
483 for buffer_to_load in buffers_to_load {
484 if let Some(buffer) = buffer_to_load.await.log_err() {
485 cx.update(|window, cx| {
486 this.update(cx, |this, cx| this.register_buffer(buffer, window, cx))
487 .ok();
488 })?;
489 }
490 }
491 this.update(cx, |this, cx| {
492 this.pending_scroll.take();
493 cx.notify();
494 })?;
495 }
496
497 Ok(())
498 }
499
500 #[cfg(any(test, feature = "test-support"))]
501 pub fn excerpt_paths(&self, cx: &App) -> Vec<String> {
502 self.multibuffer
503 .read(cx)
504 .excerpt_paths()
505 .map(|key| key.path().to_string_lossy().to_string())
506 .collect()
507 }
508}
509
510impl EventEmitter<EditorEvent> for ProjectDiff {}
511
512impl Focusable for ProjectDiff {
513 fn focus_handle(&self, cx: &App) -> FocusHandle {
514 if self.multibuffer.read(cx).is_empty() {
515 self.focus_handle.clone()
516 } else {
517 self.editor.focus_handle(cx)
518 }
519 }
520}
521
522impl Item for ProjectDiff {
523 type Event = EditorEvent;
524
525 fn tab_icon(&self, _window: &Window, _cx: &App) -> Option<Icon> {
526 Some(Icon::new(IconName::GitBranch).color(Color::Muted))
527 }
528
529 fn to_item_events(event: &EditorEvent, f: impl FnMut(ItemEvent)) {
530 Editor::to_item_events(event, f)
531 }
532
533 fn deactivated(&mut self, window: &mut Window, cx: &mut Context<Self>) {
534 self.editor
535 .update(cx, |editor, cx| editor.deactivated(window, cx));
536 }
537
538 fn navigate(
539 &mut self,
540 data: Box<dyn Any>,
541 window: &mut Window,
542 cx: &mut Context<Self>,
543 ) -> bool {
544 self.editor
545 .update(cx, |editor, cx| editor.navigate(data, window, cx))
546 }
547
548 fn tab_tooltip_text(&self, _: &App) -> Option<SharedString> {
549 Some("Project Diff".into())
550 }
551
552 fn tab_content(&self, params: TabContentParams, _window: &Window, _: &App) -> AnyElement {
553 Label::new("Uncommitted Changes")
554 .color(if params.selected {
555 Color::Default
556 } else {
557 Color::Muted
558 })
559 .into_any_element()
560 }
561
562 fn tab_content_text(&self, _detail: usize, _: &App) -> SharedString {
563 "Uncommitted Changes".into()
564 }
565
566 fn telemetry_event_text(&self) -> Option<&'static str> {
567 Some("Project Diff Opened")
568 }
569
570 fn as_searchable(&self, _: &Entity<Self>) -> Option<Box<dyn SearchableItemHandle>> {
571 Some(Box::new(self.editor.clone()))
572 }
573
574 fn for_each_project_item(
575 &self,
576 cx: &App,
577 f: &mut dyn FnMut(gpui::EntityId, &dyn project::ProjectItem),
578 ) {
579 self.editor.for_each_project_item(cx, f)
580 }
581
582 fn is_singleton(&self, _: &App) -> bool {
583 false
584 }
585
586 fn set_nav_history(
587 &mut self,
588 nav_history: ItemNavHistory,
589 _: &mut Window,
590 cx: &mut Context<Self>,
591 ) {
592 self.editor.update(cx, |editor, _| {
593 editor.set_nav_history(Some(nav_history));
594 });
595 }
596
597 fn clone_on_split(
598 &self,
599 _workspace_id: Option<workspace::WorkspaceId>,
600 window: &mut Window,
601 cx: &mut Context<Self>,
602 ) -> Option<Entity<Self>>
603 where
604 Self: Sized,
605 {
606 let workspace = self.workspace.upgrade()?;
607 Some(cx.new(|cx| ProjectDiff::new(self.project.clone(), workspace, window, cx)))
608 }
609
610 fn is_dirty(&self, cx: &App) -> bool {
611 self.multibuffer.read(cx).is_dirty(cx)
612 }
613
614 fn has_conflict(&self, cx: &App) -> bool {
615 self.multibuffer.read(cx).has_conflict(cx)
616 }
617
618 fn can_save(&self, _: &App) -> bool {
619 true
620 }
621
622 fn save(
623 &mut self,
624 format: bool,
625 project: Entity<Project>,
626 window: &mut Window,
627 cx: &mut Context<Self>,
628 ) -> Task<Result<()>> {
629 self.editor.save(format, project, window, cx)
630 }
631
632 fn save_as(
633 &mut self,
634 _: Entity<Project>,
635 _: ProjectPath,
636 _window: &mut Window,
637 _: &mut Context<Self>,
638 ) -> Task<Result<()>> {
639 unreachable!()
640 }
641
642 fn reload(
643 &mut self,
644 project: Entity<Project>,
645 window: &mut Window,
646 cx: &mut Context<Self>,
647 ) -> Task<Result<()>> {
648 self.editor.reload(project, window, cx)
649 }
650
651 fn act_as_type<'a>(
652 &'a self,
653 type_id: TypeId,
654 self_handle: &'a Entity<Self>,
655 _: &'a App,
656 ) -> Option<AnyView> {
657 if type_id == TypeId::of::<Self>() {
658 Some(self_handle.to_any())
659 } else if type_id == TypeId::of::<Editor>() {
660 Some(self.editor.to_any())
661 } else {
662 None
663 }
664 }
665
666 fn breadcrumb_location(&self, _: &App) -> ToolbarItemLocation {
667 ToolbarItemLocation::PrimaryLeft
668 }
669
670 fn breadcrumbs(&self, theme: &theme::Theme, cx: &App) -> Option<Vec<BreadcrumbText>> {
671 self.editor.breadcrumbs(theme, cx)
672 }
673
674 fn added_to_workspace(
675 &mut self,
676 workspace: &mut Workspace,
677 window: &mut Window,
678 cx: &mut Context<Self>,
679 ) {
680 self.editor.update(cx, |editor, cx| {
681 editor.added_to_workspace(workspace, window, cx)
682 });
683 }
684}
685
686impl Render for ProjectDiff {
687 fn render(&mut self, window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
688 let is_empty = self.multibuffer.read(cx).is_empty();
689
690 div()
691 .track_focus(&self.focus_handle)
692 .key_context(if is_empty { "EmptyPane" } else { "GitDiff" })
693 .bg(cx.theme().colors().editor_background)
694 .flex()
695 .items_center()
696 .justify_center()
697 .size_full()
698 .when(is_empty, |el| {
699 let remote_button = if let Some(panel) = self
700 .workspace
701 .upgrade()
702 .and_then(|workspace| workspace.read(cx).panel::<GitPanel>(cx))
703 {
704 panel.update(cx, |panel, cx| panel.render_remote_button(cx))
705 } else {
706 None
707 };
708 let keybinding_focus_handle = self.focus_handle(cx).clone();
709 el.child(
710 v_flex()
711 .gap_1()
712 .child(
713 h_flex()
714 .justify_around()
715 .child(Label::new("No uncommitted changes")),
716 )
717 .map(|el| match remote_button {
718 Some(button) => el.child(h_flex().justify_around().child(button)),
719 None => el.child(
720 h_flex()
721 .justify_around()
722 .child(Label::new("Remote up to date")),
723 ),
724 })
725 .child(
726 h_flex().justify_around().mt_1().child(
727 Button::new("project-diff-close-button", "Close")
728 // .style(ButtonStyle::Transparent)
729 .key_binding(KeyBinding::for_action_in(
730 &CloseActiveItem::default(),
731 &keybinding_focus_handle,
732 window,
733 cx,
734 ))
735 .on_click(move |_, window, cx| {
736 window.focus(&keybinding_focus_handle);
737 window.dispatch_action(
738 Box::new(CloseActiveItem::default()),
739 cx,
740 );
741 }),
742 ),
743 ),
744 )
745 })
746 .when(!is_empty, |el| el.child(self.editor.clone()))
747 }
748}
749
750impl SerializableItem for ProjectDiff {
751 fn serialized_item_kind() -> &'static str {
752 "ProjectDiff"
753 }
754
755 fn cleanup(
756 _: workspace::WorkspaceId,
757 _: Vec<workspace::ItemId>,
758 _: &mut Window,
759 _: &mut App,
760 ) -> Task<Result<()>> {
761 Task::ready(Ok(()))
762 }
763
764 fn deserialize(
765 _project: Entity<Project>,
766 workspace: WeakEntity<Workspace>,
767 _workspace_id: workspace::WorkspaceId,
768 _item_id: workspace::ItemId,
769 window: &mut Window,
770 cx: &mut App,
771 ) -> Task<Result<Entity<Self>>> {
772 window.spawn(cx, async move |cx| {
773 workspace.update_in(cx, |workspace, window, cx| {
774 let workspace_handle = cx.entity();
775 cx.new(|cx| Self::new(workspace.project().clone(), workspace_handle, window, cx))
776 })
777 })
778 }
779
780 fn serialize(
781 &mut self,
782 _workspace: &mut Workspace,
783 _item_id: workspace::ItemId,
784 _closing: bool,
785 _window: &mut Window,
786 _cx: &mut Context<Self>,
787 ) -> Option<Task<Result<()>>> {
788 None
789 }
790
791 fn should_serialize(&self, _: &Self::Event) -> bool {
792 false
793 }
794}
795
796pub struct ProjectDiffToolbar {
797 project_diff: Option<WeakEntity<ProjectDiff>>,
798 workspace: WeakEntity<Workspace>,
799}
800
801impl ProjectDiffToolbar {
802 pub fn new(workspace: &Workspace, _: &mut Context<Self>) -> Self {
803 Self {
804 project_diff: None,
805 workspace: workspace.weak_handle(),
806 }
807 }
808
809 fn project_diff(&self, _: &App) -> Option<Entity<ProjectDiff>> {
810 self.project_diff.as_ref()?.upgrade()
811 }
812
813 fn dispatch_action(&self, action: &dyn Action, window: &mut Window, cx: &mut Context<Self>) {
814 if let Some(project_diff) = self.project_diff(cx) {
815 project_diff.focus_handle(cx).focus(window);
816 }
817 let action = action.boxed_clone();
818 cx.defer(move |cx| {
819 cx.dispatch_action(action.as_ref());
820 })
821 }
822
823 fn stage_all(&mut self, window: &mut Window, cx: &mut Context<Self>) {
824 self.workspace
825 .update(cx, |workspace, cx| {
826 if let Some(panel) = workspace.panel::<GitPanel>(cx) {
827 panel.update(cx, |panel, cx| {
828 panel.stage_all(&Default::default(), window, cx);
829 });
830 }
831 })
832 .ok();
833 }
834
835 fn unstage_all(&mut self, window: &mut Window, cx: &mut Context<Self>) {
836 self.workspace
837 .update(cx, |workspace, cx| {
838 let Some(panel) = workspace.panel::<GitPanel>(cx) else {
839 return;
840 };
841 panel.update(cx, |panel, cx| {
842 panel.unstage_all(&Default::default(), window, cx);
843 });
844 })
845 .ok();
846 }
847}
848
849impl EventEmitter<ToolbarItemEvent> for ProjectDiffToolbar {}
850
851impl ToolbarItemView for ProjectDiffToolbar {
852 fn set_active_pane_item(
853 &mut self,
854 active_pane_item: Option<&dyn ItemHandle>,
855 _: &mut Window,
856 cx: &mut Context<Self>,
857 ) -> ToolbarItemLocation {
858 self.project_diff = active_pane_item
859 .and_then(|item| item.act_as::<ProjectDiff>(cx))
860 .map(|entity| entity.downgrade());
861 if self.project_diff.is_some() {
862 ToolbarItemLocation::PrimaryRight
863 } else {
864 ToolbarItemLocation::Hidden
865 }
866 }
867
868 fn pane_focus_update(
869 &mut self,
870 _pane_focused: bool,
871 _window: &mut Window,
872 _cx: &mut Context<Self>,
873 ) {
874 }
875}
876
877struct ButtonStates {
878 stage: bool,
879 unstage: bool,
880 prev_next: bool,
881 selection: bool,
882 stage_all: bool,
883 unstage_all: bool,
884}
885
886impl Render for ProjectDiffToolbar {
887 fn render(&mut self, _: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
888 let Some(project_diff) = self.project_diff(cx) else {
889 return div();
890 };
891 let focus_handle = project_diff.focus_handle(cx);
892 let button_states = project_diff.read(cx).button_states(cx);
893
894 h_group_xl()
895 .my_neg_1()
896 .items_center()
897 .py_1()
898 .px_2()
899 .flex_wrap()
900 .justify_between()
901 .child(
902 h_group_sm()
903 .when(button_states.selection, |el| {
904 el.child(
905 Button::new("stage", "Toggle Staged")
906 .tooltip(Tooltip::for_action_title_in(
907 "Toggle Staged",
908 &ToggleStaged,
909 &focus_handle,
910 ))
911 .disabled(!button_states.stage && !button_states.unstage)
912 .on_click(cx.listener(|this, _, window, cx| {
913 this.dispatch_action(&ToggleStaged, window, cx)
914 })),
915 )
916 })
917 .when(!button_states.selection, |el| {
918 el.child(
919 Button::new("stage", "Stage")
920 .tooltip(Tooltip::for_action_title_in(
921 "Stage and go to next hunk",
922 &StageAndNext,
923 &focus_handle,
924 ))
925 .on_click(cx.listener(|this, _, window, cx| {
926 this.dispatch_action(&StageAndNext, window, cx)
927 })),
928 )
929 .child(
930 Button::new("unstage", "Unstage")
931 .tooltip(Tooltip::for_action_title_in(
932 "Unstage and go to next hunk",
933 &UnstageAndNext,
934 &focus_handle,
935 ))
936 .on_click(cx.listener(|this, _, window, cx| {
937 this.dispatch_action(&UnstageAndNext, window, cx)
938 })),
939 )
940 }),
941 )
942 // n.b. the only reason these arrows are here is because we don't
943 // support "undo" for staging so we need a way to go back.
944 .child(
945 h_group_sm()
946 .child(
947 IconButton::new("up", IconName::ArrowUp)
948 .shape(ui::IconButtonShape::Square)
949 .tooltip(Tooltip::for_action_title_in(
950 "Go to previous hunk",
951 &GoToPreviousHunk,
952 &focus_handle,
953 ))
954 .disabled(!button_states.prev_next)
955 .on_click(cx.listener(|this, _, window, cx| {
956 this.dispatch_action(&GoToPreviousHunk, window, cx)
957 })),
958 )
959 .child(
960 IconButton::new("down", IconName::ArrowDown)
961 .shape(ui::IconButtonShape::Square)
962 .tooltip(Tooltip::for_action_title_in(
963 "Go to next hunk",
964 &GoToHunk,
965 &focus_handle,
966 ))
967 .disabled(!button_states.prev_next)
968 .on_click(cx.listener(|this, _, window, cx| {
969 this.dispatch_action(&GoToHunk, window, cx)
970 })),
971 ),
972 )
973 .child(vertical_divider())
974 .child(
975 h_group_sm()
976 .when(
977 button_states.unstage_all && !button_states.stage_all,
978 |el| {
979 el.child(
980 Button::new("unstage-all", "Unstage All")
981 .tooltip(Tooltip::for_action_title_in(
982 "Unstage all changes",
983 &UnstageAll,
984 &focus_handle,
985 ))
986 .on_click(cx.listener(|this, _, window, cx| {
987 this.unstage_all(window, cx)
988 })),
989 )
990 },
991 )
992 .when(
993 !button_states.unstage_all || button_states.stage_all,
994 |el| {
995 el.child(
996 // todo make it so that changing to say "Unstaged"
997 // doesn't change the position.
998 div().child(
999 Button::new("stage-all", "Stage All")
1000 .disabled(!button_states.stage_all)
1001 .tooltip(Tooltip::for_action_title_in(
1002 "Stage all changes",
1003 &StageAll,
1004 &focus_handle,
1005 ))
1006 .on_click(cx.listener(|this, _, window, cx| {
1007 this.stage_all(window, cx)
1008 })),
1009 ),
1010 )
1011 },
1012 )
1013 .child(
1014 Button::new("commit", "Commit")
1015 .tooltip(Tooltip::for_action_title_in(
1016 "Commit",
1017 &Commit,
1018 &focus_handle,
1019 ))
1020 .on_click(cx.listener(|this, _, window, cx| {
1021 this.dispatch_action(&Commit, window, cx);
1022 })),
1023 ),
1024 )
1025 }
1026}
1027
1028#[derive(IntoElement, RegisterComponent)]
1029pub struct ProjectDiffEmptyState {
1030 pub no_repo: bool,
1031 pub can_push_and_pull: bool,
1032 pub focus_handle: Option<FocusHandle>,
1033 pub current_branch: Option<Branch>,
1034 // has_pending_commits: bool,
1035 // ahead_of_remote: bool,
1036 // no_git_repository: bool,
1037}
1038
1039impl RenderOnce for ProjectDiffEmptyState {
1040 fn render(self, _window: &mut Window, cx: &mut App) -> impl IntoElement {
1041 let status_against_remote = |ahead_by: usize, behind_by: usize| -> bool {
1042 match self.current_branch {
1043 Some(Branch {
1044 upstream:
1045 Some(Upstream {
1046 tracking:
1047 UpstreamTracking::Tracked(UpstreamTrackingStatus {
1048 ahead, behind, ..
1049 }),
1050 ..
1051 }),
1052 ..
1053 }) if (ahead > 0) == (ahead_by > 0) && (behind > 0) == (behind_by > 0) => true,
1054 _ => false,
1055 }
1056 };
1057
1058 let change_count = |current_branch: &Branch| -> (usize, usize) {
1059 match current_branch {
1060 Branch {
1061 upstream:
1062 Some(Upstream {
1063 tracking:
1064 UpstreamTracking::Tracked(UpstreamTrackingStatus {
1065 ahead, behind, ..
1066 }),
1067 ..
1068 }),
1069 ..
1070 } => (*ahead as usize, *behind as usize),
1071 _ => (0, 0),
1072 }
1073 };
1074
1075 let not_ahead_or_behind = status_against_remote(0, 0);
1076 let ahead_of_remote = status_against_remote(1, 0);
1077 let branch_not_on_remote = if let Some(branch) = self.current_branch.as_ref() {
1078 branch.upstream.is_none()
1079 } else {
1080 false
1081 };
1082
1083 let has_branch_container = |branch: &Branch| {
1084 h_flex()
1085 .max_w(px(420.))
1086 .bg(cx.theme().colors().text.opacity(0.05))
1087 .border_1()
1088 .border_color(cx.theme().colors().border)
1089 .rounded_sm()
1090 .gap_8()
1091 .px_6()
1092 .py_4()
1093 .map(|this| {
1094 if ahead_of_remote {
1095 let ahead_count = change_count(branch).0;
1096 let ahead_string = format!("{} Commits Ahead", ahead_count);
1097 this.child(
1098 v_flex()
1099 .child(Headline::new(ahead_string).size(HeadlineSize::Small))
1100 .child(
1101 Label::new(format!("Push your changes to {}", branch.name()))
1102 .color(Color::Muted),
1103 ),
1104 )
1105 .child(div().child(render_push_button(
1106 self.focus_handle,
1107 "push".into(),
1108 ahead_count as u32,
1109 )))
1110 } else if branch_not_on_remote {
1111 this.child(
1112 v_flex()
1113 .child(Headline::new("Publish Branch").size(HeadlineSize::Small))
1114 .child(
1115 Label::new(format!("Create {} on remote", branch.name()))
1116 .color(Color::Muted),
1117 ),
1118 )
1119 .child(
1120 div().child(render_publish_button(self.focus_handle, "publish".into())),
1121 )
1122 } else {
1123 this.child(Label::new("Remote status unknown").color(Color::Muted))
1124 }
1125 })
1126 };
1127
1128 v_flex().size_full().items_center().justify_center().child(
1129 v_flex()
1130 .gap_1()
1131 .when(self.no_repo, |this| {
1132 // TODO: add git init
1133 this.text_center()
1134 .child(Label::new("No Repository").color(Color::Muted))
1135 })
1136 .map(|this| {
1137 if not_ahead_or_behind && self.current_branch.is_some() {
1138 this.text_center()
1139 .child(Label::new("No Changes").color(Color::Muted))
1140 } else {
1141 this.when_some(self.current_branch.as_ref(), |this, branch| {
1142 this.child(has_branch_container(&branch))
1143 })
1144 }
1145 }),
1146 )
1147 }
1148}
1149
1150mod preview {
1151 use git::repository::{
1152 Branch, CommitSummary, Upstream, UpstreamTracking, UpstreamTrackingStatus,
1153 };
1154 use ui::prelude::*;
1155
1156 use super::ProjectDiffEmptyState;
1157
1158 // View this component preview using `workspace: open component-preview`
1159 impl Component for ProjectDiffEmptyState {
1160 fn scope() -> ComponentScope {
1161 ComponentScope::VersionControl
1162 }
1163
1164 fn preview(_window: &mut Window, _cx: &mut App) -> Option<AnyElement> {
1165 let unknown_upstream: Option<UpstreamTracking> = None;
1166 let ahead_of_upstream: Option<UpstreamTracking> = Some(
1167 UpstreamTrackingStatus {
1168 ahead: 2,
1169 behind: 0,
1170 }
1171 .into(),
1172 );
1173
1174 let not_ahead_or_behind_upstream: Option<UpstreamTracking> = Some(
1175 UpstreamTrackingStatus {
1176 ahead: 0,
1177 behind: 0,
1178 }
1179 .into(),
1180 );
1181
1182 fn branch(upstream: Option<UpstreamTracking>) -> Branch {
1183 Branch {
1184 is_head: true,
1185 ref_name: "some-branch".into(),
1186 upstream: upstream.map(|tracking| Upstream {
1187 ref_name: "origin/some-branch".into(),
1188 tracking,
1189 }),
1190 most_recent_commit: Some(CommitSummary {
1191 sha: "abc123".into(),
1192 subject: "Modify stuff".into(),
1193 commit_timestamp: 1710932954,
1194 has_parent: true,
1195 }),
1196 }
1197 }
1198
1199 let no_repo_state = ProjectDiffEmptyState {
1200 no_repo: true,
1201 can_push_and_pull: false,
1202 focus_handle: None,
1203 current_branch: None,
1204 };
1205
1206 let no_changes_state = ProjectDiffEmptyState {
1207 no_repo: false,
1208 can_push_and_pull: true,
1209 focus_handle: None,
1210 current_branch: Some(branch(not_ahead_or_behind_upstream)),
1211 };
1212
1213 let ahead_of_upstream_state = ProjectDiffEmptyState {
1214 no_repo: false,
1215 can_push_and_pull: true,
1216 focus_handle: None,
1217 current_branch: Some(branch(ahead_of_upstream)),
1218 };
1219
1220 let unknown_upstream_state = ProjectDiffEmptyState {
1221 no_repo: false,
1222 can_push_and_pull: true,
1223 focus_handle: None,
1224 current_branch: Some(branch(unknown_upstream)),
1225 };
1226
1227 let (width, height) = (px(480.), px(320.));
1228
1229 Some(
1230 v_flex()
1231 .gap_6()
1232 .children(vec![
1233 example_group(vec![
1234 single_example(
1235 "No Repo",
1236 div()
1237 .w(width)
1238 .h(height)
1239 .child(no_repo_state)
1240 .into_any_element(),
1241 ),
1242 single_example(
1243 "No Changes",
1244 div()
1245 .w(width)
1246 .h(height)
1247 .child(no_changes_state)
1248 .into_any_element(),
1249 ),
1250 single_example(
1251 "Unknown Upstream",
1252 div()
1253 .w(width)
1254 .h(height)
1255 .child(unknown_upstream_state)
1256 .into_any_element(),
1257 ),
1258 single_example(
1259 "Ahead of Remote",
1260 div()
1261 .w(width)
1262 .h(height)
1263 .child(ahead_of_upstream_state)
1264 .into_any_element(),
1265 ),
1266 ])
1267 .vertical(),
1268 ])
1269 .into_any_element(),
1270 )
1271 }
1272 }
1273}
1274
1275fn merge_anchor_ranges<'a>(
1276 left: impl 'a + Iterator<Item = Range<Anchor>>,
1277 right: impl 'a + Iterator<Item = Range<Anchor>>,
1278 snapshot: &'a language::BufferSnapshot,
1279) -> impl 'a + Iterator<Item = Range<Anchor>> {
1280 let mut left = left.fuse().peekable();
1281 let mut right = right.fuse().peekable();
1282
1283 std::iter::from_fn(move || {
1284 let Some(left_range) = left.peek() else {
1285 return right.next();
1286 };
1287 let Some(right_range) = right.peek() else {
1288 return left.next();
1289 };
1290
1291 let mut next_range = if left_range.start.cmp(&right_range.start, snapshot).is_lt() {
1292 left.next().unwrap()
1293 } else {
1294 right.next().unwrap()
1295 };
1296
1297 // Extend the basic range while there's overlap with a range from either stream.
1298 loop {
1299 if let Some(left_range) = left
1300 .peek()
1301 .filter(|range| range.start.cmp(&next_range.end, &snapshot).is_le())
1302 .cloned()
1303 {
1304 left.next();
1305 next_range.end = left_range.end;
1306 } else if let Some(right_range) = right
1307 .peek()
1308 .filter(|range| range.start.cmp(&next_range.end, &snapshot).is_le())
1309 .cloned()
1310 {
1311 right.next();
1312 next_range.end = right_range.end;
1313 } else {
1314 break;
1315 }
1316 }
1317
1318 Some(next_range)
1319 })
1320}
1321
1322#[cfg(not(target_os = "windows"))]
1323#[cfg(test)]
1324mod tests {
1325 use db::indoc;
1326 use editor::test::editor_test_context::{EditorTestContext, assert_state_with_diff};
1327 use gpui::TestAppContext;
1328 use project::FakeFs;
1329 use serde_json::json;
1330 use settings::SettingsStore;
1331 use std::path::Path;
1332 use unindent::Unindent as _;
1333 use util::path;
1334
1335 use super::*;
1336
1337 #[ctor::ctor]
1338 fn init_logger() {
1339 env_logger::init();
1340 }
1341
1342 fn init_test(cx: &mut TestAppContext) {
1343 cx.update(|cx| {
1344 let store = SettingsStore::test(cx);
1345 cx.set_global(store);
1346 theme::init(theme::LoadThemes::JustBase, cx);
1347 language::init(cx);
1348 Project::init_settings(cx);
1349 workspace::init_settings(cx);
1350 editor::init(cx);
1351 crate::init(cx);
1352 });
1353 }
1354
1355 #[gpui::test]
1356 async fn test_save_after_restore(cx: &mut TestAppContext) {
1357 init_test(cx);
1358
1359 let fs = FakeFs::new(cx.executor());
1360 fs.insert_tree(
1361 path!("/project"),
1362 json!({
1363 ".git": {},
1364 "foo.txt": "FOO\n",
1365 }),
1366 )
1367 .await;
1368 let project = Project::test(fs.clone(), [path!("/project").as_ref()], cx).await;
1369 let (workspace, cx) =
1370 cx.add_window_view(|window, cx| Workspace::test_new(project.clone(), window, cx));
1371 let diff = cx.new_window_entity(|window, cx| {
1372 ProjectDiff::new(project.clone(), workspace, window, cx)
1373 });
1374 cx.run_until_parked();
1375
1376 fs.set_head_for_repo(
1377 path!("/project/.git").as_ref(),
1378 &[("foo.txt".into(), "foo\n".into())],
1379 );
1380 fs.set_index_for_repo(
1381 path!("/project/.git").as_ref(),
1382 &[("foo.txt".into(), "foo\n".into())],
1383 );
1384 cx.run_until_parked();
1385
1386 let editor = diff.update(cx, |diff, _| diff.editor.clone());
1387 assert_state_with_diff(
1388 &editor,
1389 cx,
1390 &"
1391 - foo
1392 + ˇFOO
1393 "
1394 .unindent(),
1395 );
1396
1397 editor.update_in(cx, |editor, window, cx| {
1398 editor.git_restore(&Default::default(), window, cx);
1399 });
1400 cx.run_until_parked();
1401
1402 assert_state_with_diff(&editor, cx, &"ˇ".unindent());
1403
1404 let text = String::from_utf8(fs.read_file_sync("/project/foo.txt").unwrap()).unwrap();
1405 assert_eq!(text, "foo\n");
1406 }
1407
1408 #[gpui::test]
1409 async fn test_scroll_to_beginning_with_deletion(cx: &mut TestAppContext) {
1410 init_test(cx);
1411
1412 let fs = FakeFs::new(cx.executor());
1413 fs.insert_tree(
1414 path!("/project"),
1415 json!({
1416 ".git": {},
1417 "bar": "BAR\n",
1418 "foo": "FOO\n",
1419 }),
1420 )
1421 .await;
1422 let project = Project::test(fs.clone(), [path!("/project").as_ref()], cx).await;
1423 let (workspace, cx) =
1424 cx.add_window_view(|window, cx| Workspace::test_new(project.clone(), window, cx));
1425 let diff = cx.new_window_entity(|window, cx| {
1426 ProjectDiff::new(project.clone(), workspace, window, cx)
1427 });
1428 cx.run_until_parked();
1429
1430 fs.set_head_and_index_for_repo(
1431 path!("/project/.git").as_ref(),
1432 &[
1433 ("bar".into(), "bar\n".into()),
1434 ("foo".into(), "foo\n".into()),
1435 ],
1436 );
1437 cx.run_until_parked();
1438
1439 let editor = cx.update_window_entity(&diff, |diff, window, cx| {
1440 diff.move_to_path(
1441 PathKey::namespaced(TRACKED_NAMESPACE, Path::new("foo").into()),
1442 window,
1443 cx,
1444 );
1445 diff.editor.clone()
1446 });
1447 assert_state_with_diff(
1448 &editor,
1449 cx,
1450 &"
1451 - bar
1452 + BAR
1453
1454 - ˇfoo
1455 + FOO
1456 "
1457 .unindent(),
1458 );
1459
1460 let editor = cx.update_window_entity(&diff, |diff, window, cx| {
1461 diff.move_to_path(
1462 PathKey::namespaced(TRACKED_NAMESPACE, Path::new("bar").into()),
1463 window,
1464 cx,
1465 );
1466 diff.editor.clone()
1467 });
1468 assert_state_with_diff(
1469 &editor,
1470 cx,
1471 &"
1472 - ˇbar
1473 + BAR
1474
1475 - foo
1476 + FOO
1477 "
1478 .unindent(),
1479 );
1480 }
1481
1482 #[gpui::test]
1483 async fn test_hunks_after_restore_then_modify(cx: &mut TestAppContext) {
1484 init_test(cx);
1485
1486 let fs = FakeFs::new(cx.executor());
1487 fs.insert_tree(
1488 path!("/project"),
1489 json!({
1490 ".git": {},
1491 "foo": "modified\n",
1492 }),
1493 )
1494 .await;
1495 let project = Project::test(fs.clone(), [path!("/project").as_ref()], cx).await;
1496 let (workspace, cx) =
1497 cx.add_window_view(|window, cx| Workspace::test_new(project.clone(), window, cx));
1498 let buffer = project
1499 .update(cx, |project, cx| {
1500 project.open_local_buffer(path!("/project/foo"), cx)
1501 })
1502 .await
1503 .unwrap();
1504 let buffer_editor = cx.new_window_entity(|window, cx| {
1505 Editor::for_buffer(buffer, Some(project.clone()), window, cx)
1506 });
1507 let diff = cx.new_window_entity(|window, cx| {
1508 ProjectDiff::new(project.clone(), workspace, window, cx)
1509 });
1510 cx.run_until_parked();
1511
1512 fs.set_head_for_repo(
1513 path!("/project/.git").as_ref(),
1514 &[("foo".into(), "original\n".into())],
1515 );
1516 cx.run_until_parked();
1517
1518 let diff_editor = diff.update(cx, |diff, _| diff.editor.clone());
1519
1520 assert_state_with_diff(
1521 &diff_editor,
1522 cx,
1523 &"
1524 - original
1525 + ˇmodified
1526 "
1527 .unindent(),
1528 );
1529
1530 let prev_buffer_hunks =
1531 cx.update_window_entity(&buffer_editor, |buffer_editor, window, cx| {
1532 let snapshot = buffer_editor.snapshot(window, cx);
1533 let snapshot = &snapshot.buffer_snapshot;
1534 let prev_buffer_hunks = buffer_editor
1535 .diff_hunks_in_ranges(&[editor::Anchor::min()..editor::Anchor::max()], snapshot)
1536 .collect::<Vec<_>>();
1537 buffer_editor.git_restore(&Default::default(), window, cx);
1538 prev_buffer_hunks
1539 });
1540 assert_eq!(prev_buffer_hunks.len(), 1);
1541 cx.run_until_parked();
1542
1543 let new_buffer_hunks =
1544 cx.update_window_entity(&buffer_editor, |buffer_editor, window, cx| {
1545 let snapshot = buffer_editor.snapshot(window, cx);
1546 let snapshot = &snapshot.buffer_snapshot;
1547 buffer_editor
1548 .diff_hunks_in_ranges(&[editor::Anchor::min()..editor::Anchor::max()], snapshot)
1549 .collect::<Vec<_>>()
1550 });
1551 assert_eq!(new_buffer_hunks.as_slice(), &[]);
1552
1553 cx.update_window_entity(&buffer_editor, |buffer_editor, window, cx| {
1554 buffer_editor.set_text("different\n", window, cx);
1555 buffer_editor.save(false, project.clone(), window, cx)
1556 })
1557 .await
1558 .unwrap();
1559
1560 cx.run_until_parked();
1561
1562 cx.update_window_entity(&buffer_editor, |buffer_editor, window, cx| {
1563 buffer_editor.expand_all_diff_hunks(&Default::default(), window, cx);
1564 });
1565
1566 assert_state_with_diff(
1567 &buffer_editor,
1568 cx,
1569 &"
1570 - original
1571 + different
1572 ˇ"
1573 .unindent(),
1574 );
1575
1576 assert_state_with_diff(
1577 &diff_editor,
1578 cx,
1579 &"
1580 - original
1581 + different
1582 ˇ"
1583 .unindent(),
1584 );
1585 }
1586
1587 use crate::project_diff::{self, ProjectDiff};
1588
1589 #[gpui::test]
1590 async fn test_go_to_prev_hunk_multibuffer(cx: &mut TestAppContext) {
1591 init_test(cx);
1592
1593 let fs = FakeFs::new(cx.executor());
1594 fs.insert_tree(
1595 "/a",
1596 json!({
1597 ".git": {},
1598 "a.txt": "created\n",
1599 "b.txt": "really changed\n",
1600 "c.txt": "unchanged\n"
1601 }),
1602 )
1603 .await;
1604
1605 fs.set_git_content_for_repo(
1606 Path::new("/a/.git"),
1607 &[
1608 ("b.txt".into(), "before\n".to_string(), None),
1609 ("c.txt".into(), "unchanged\n".to_string(), None),
1610 ("d.txt".into(), "deleted\n".to_string(), None),
1611 ],
1612 );
1613
1614 let project = Project::test(fs, [Path::new("/a")], cx).await;
1615 let (workspace, cx) =
1616 cx.add_window_view(|window, cx| Workspace::test_new(project, window, cx));
1617
1618 cx.run_until_parked();
1619
1620 cx.focus(&workspace);
1621 cx.update(|window, cx| {
1622 window.dispatch_action(project_diff::Diff.boxed_clone(), cx);
1623 });
1624
1625 cx.run_until_parked();
1626
1627 let item = workspace.update(cx, |workspace, cx| {
1628 workspace.active_item_as::<ProjectDiff>(cx).unwrap()
1629 });
1630 cx.focus(&item);
1631 let editor = item.update(cx, |item, _| item.editor.clone());
1632
1633 let mut cx = EditorTestContext::for_editor_in(editor, cx).await;
1634
1635 cx.assert_excerpts_with_selections(indoc!(
1636 "
1637 [EXCERPT]
1638 before
1639 really changed
1640 [EXCERPT]
1641 [FOLDED]
1642 [EXCERPT]
1643 ˇcreated
1644 "
1645 ));
1646
1647 cx.dispatch_action(editor::actions::GoToPreviousHunk);
1648
1649 cx.assert_excerpts_with_selections(indoc!(
1650 "
1651 [EXCERPT]
1652 before
1653 really changed
1654 [EXCERPT]
1655 ˇ[FOLDED]
1656 [EXCERPT]
1657 created
1658 "
1659 ));
1660
1661 cx.dispatch_action(editor::actions::GoToPreviousHunk);
1662
1663 cx.assert_excerpts_with_selections(indoc!(
1664 "
1665 [EXCERPT]
1666 ˇbefore
1667 really changed
1668 [EXCERPT]
1669 [FOLDED]
1670 [EXCERPT]
1671 created
1672 "
1673 ));
1674 }
1675
1676 #[gpui::test]
1677 async fn test_excerpts_splitting_after_restoring_the_middle_excerpt(cx: &mut TestAppContext) {
1678 init_test(cx);
1679
1680 let git_contents = indoc! {r#"
1681 #[rustfmt::skip]
1682 fn main() {
1683 let x = 0.0; // this line will be removed
1684 // 1
1685 // 2
1686 // 3
1687 let y = 0.0; // this line will be removed
1688 // 1
1689 // 2
1690 // 3
1691 let arr = [
1692 0.0, // this line will be removed
1693 0.0, // this line will be removed
1694 0.0, // this line will be removed
1695 0.0, // this line will be removed
1696 ];
1697 }
1698 "#};
1699 let buffer_contents = indoc! {"
1700 #[rustfmt::skip]
1701 fn main() {
1702 // 1
1703 // 2
1704 // 3
1705 // 1
1706 // 2
1707 // 3
1708 let arr = [
1709 ];
1710 }
1711 "};
1712
1713 let fs = FakeFs::new(cx.executor());
1714 fs.insert_tree(
1715 "/a",
1716 json!({
1717 ".git": {},
1718 "main.rs": buffer_contents,
1719 }),
1720 )
1721 .await;
1722
1723 fs.set_git_content_for_repo(
1724 Path::new("/a/.git"),
1725 &[("main.rs".into(), git_contents.to_owned(), None)],
1726 );
1727
1728 let project = Project::test(fs, [Path::new("/a")], cx).await;
1729 let (workspace, cx) =
1730 cx.add_window_view(|window, cx| Workspace::test_new(project, window, cx));
1731
1732 cx.run_until_parked();
1733
1734 cx.focus(&workspace);
1735 cx.update(|window, cx| {
1736 window.dispatch_action(project_diff::Diff.boxed_clone(), cx);
1737 });
1738
1739 cx.run_until_parked();
1740
1741 let item = workspace.update(cx, |workspace, cx| {
1742 workspace.active_item_as::<ProjectDiff>(cx).unwrap()
1743 });
1744 cx.focus(&item);
1745 let editor = item.update(cx, |item, _| item.editor.clone());
1746
1747 let mut cx = EditorTestContext::for_editor_in(editor, cx).await;
1748
1749 cx.assert_excerpts_with_selections(&format!("[EXCERPT]\nˇ{git_contents}"));
1750
1751 cx.dispatch_action(editor::actions::GoToHunk);
1752 cx.dispatch_action(editor::actions::GoToHunk);
1753 cx.dispatch_action(git::Restore);
1754 cx.dispatch_action(editor::actions::MoveToBeginning);
1755
1756 cx.assert_excerpts_with_selections(&format!("[EXCERPT]\nˇ{git_contents}"));
1757 }
1758}