1use crate::{
2 git_status_icon, settings::GitPanelSettings, CommitAllChanges, CommitStagedChanges, GitState,
3 RevertAll, StageAll, UnstageAll,
4};
5use anyhow::{Context as _, Result};
6use db::kvp::KEY_VALUE_STORE;
7use editor::Editor;
8use git::{
9 diff::DiffHunk,
10 repository::{GitFileStatus, RepoPath},
11};
12use gpui::*;
13use language::Buffer;
14use menu::{SelectNext, SelectPrev};
15use project::{EntryKind, Fs, Project, ProjectEntryId, WorktreeId};
16use serde::{Deserialize, Serialize};
17use settings::Settings as _;
18use std::{
19 cell::OnceCell,
20 collections::HashSet,
21 ffi::OsStr,
22 ops::{Deref, Range},
23 path::PathBuf,
24 rc::Rc,
25 sync::Arc,
26 time::Duration,
27 usize,
28};
29use theme::ThemeSettings;
30use ui::{
31 prelude::*, Checkbox, Divider, DividerColor, ElevationIndex, Scrollbar, ScrollbarState, Tooltip,
32};
33use util::{ResultExt, TryFutureExt};
34use workspace::{
35 dock::{DockPosition, Panel, PanelEvent},
36 Workspace,
37};
38use worktree::StatusEntry;
39
40actions!(git_panel, [ToggleFocus, OpenEntryMenu]);
41
42const GIT_PANEL_KEY: &str = "GitPanel";
43
44const UPDATE_DEBOUNCE: Duration = Duration::from_millis(50);
45
46pub fn init(cx: &mut AppContext) {
47 cx.observe_new_views(
48 |workspace: &mut Workspace, _cx: &mut ViewContext<Workspace>| {
49 workspace.register_action(|workspace, _: &ToggleFocus, cx| {
50 workspace.toggle_panel_focus::<GitPanel>(cx);
51 });
52 },
53 )
54 .detach();
55}
56
57#[derive(Debug)]
58pub enum Event {
59 Focus,
60}
61
62#[derive(Default, Debug, PartialEq, Eq, Clone)]
63pub enum ViewMode {
64 #[default]
65 List,
66 Tree,
67}
68
69pub struct GitStatusEntry {}
70
71#[derive(Debug, PartialEq, Eq, Clone)]
72struct EntryDetails {
73 filename: String,
74 display_name: String,
75 path: RepoPath,
76 kind: EntryKind,
77 depth: usize,
78 is_expanded: bool,
79 status: Option<GitFileStatus>,
80 hunks: Rc<OnceCell<Vec<DiffHunk>>>,
81 index: usize,
82}
83
84#[derive(Serialize, Deserialize)]
85struct SerializedGitPanel {
86 width: Option<Pixels>,
87}
88
89pub struct GitPanel {
90 // workspace: WeakView<Workspace>,
91 current_modifiers: Modifiers,
92 focus_handle: FocusHandle,
93 fs: Arc<dyn Fs>,
94 hide_scrollbar_task: Option<Task<()>>,
95 pending_serialization: Task<Option<()>>,
96 project: Model<Project>,
97 scroll_handle: UniformListScrollHandle,
98 scrollbar_state: ScrollbarState,
99 selected_item: Option<usize>,
100 view_mode: ViewMode,
101 show_scrollbar: bool,
102 // TODO Reintroduce expanded directories, once we're deriving directories from paths
103 // expanded_dir_ids: HashMap<WorktreeId, Vec<ProjectEntryId>>,
104 git_state: Model<GitState>,
105 commit_editor: View<Editor>,
106 // The entries that are currently shown in the panel, aka
107 // not hidden by folding or such
108 visible_entries: Vec<WorktreeEntries>,
109 width: Option<Pixels>,
110 // git_diff_editor: Option<View<Editor>>,
111 // git_diff_editor_updates: Task<()>,
112 reveal_in_editor: Task<()>,
113}
114
115#[derive(Debug, Clone)]
116struct WorktreeEntries {
117 worktree_id: WorktreeId,
118 // TODO support multiple repositories per worktree
119 // work_directory: worktree::WorkDirectory,
120 visible_entries: Vec<GitPanelEntry>,
121 paths: Rc<OnceCell<HashSet<RepoPath>>>,
122}
123
124#[derive(Debug, Clone)]
125struct GitPanelEntry {
126 entry: worktree::StatusEntry,
127 hunks: Rc<OnceCell<Vec<DiffHunk>>>,
128}
129
130impl Deref for GitPanelEntry {
131 type Target = worktree::StatusEntry;
132
133 fn deref(&self) -> &Self::Target {
134 &self.entry
135 }
136}
137
138impl WorktreeEntries {
139 fn paths(&self) -> &HashSet<RepoPath> {
140 self.paths.get_or_init(|| {
141 self.visible_entries
142 .iter()
143 .map(|e| (e.entry.repo_path.clone()))
144 .collect()
145 })
146 }
147}
148
149impl GitPanel {
150 pub fn load(
151 workspace: WeakView<Workspace>,
152 cx: AsyncWindowContext,
153 ) -> Task<Result<View<Self>>> {
154 cx.spawn(|mut cx| async move { workspace.update(&mut cx, Self::new) })
155 }
156
157 pub fn new(workspace: &mut Workspace, cx: &mut ViewContext<Workspace>) -> View<Self> {
158 let git_state = GitState::get_global(cx);
159
160 let fs = workspace.app_state().fs.clone();
161 // let weak_workspace = workspace.weak_handle();
162 let project = workspace.project().clone();
163 let language_registry = workspace.app_state().languages.clone();
164
165 let git_panel = cx.new_view(|cx: &mut ViewContext<Self>| {
166 let focus_handle = cx.focus_handle();
167 cx.on_focus(&focus_handle, Self::focus_in).detach();
168 cx.on_focus_out(&focus_handle, |this, _, cx| {
169 this.hide_scrollbar(cx);
170 })
171 .detach();
172 cx.subscribe(&project, |this, _, event, cx| match event {
173 project::Event::WorktreeRemoved(_id) => {
174 // this.expanded_dir_ids.remove(id);
175 this.update_visible_entries(None, None, cx);
176 cx.notify();
177 }
178 project::Event::WorktreeOrderChanged => {
179 this.update_visible_entries(None, None, cx);
180 cx.notify();
181 }
182 project::Event::WorktreeUpdatedEntries(id, _)
183 | project::Event::WorktreeAdded(id)
184 | project::Event::WorktreeUpdatedGitRepositories(id) => {
185 this.update_visible_entries(Some(*id), None, cx);
186 cx.notify();
187 }
188 project::Event::Closed => {
189 // this.git_diff_editor_updates = Task::ready(());
190 this.reveal_in_editor = Task::ready(());
191 // this.expanded_dir_ids.clear();
192 this.visible_entries.clear();
193 // this.git_diff_editor = None;
194 }
195 _ => {}
196 })
197 .detach();
198
199 let state = git_state.read(cx);
200 let current_commit_message = state.commit_message.clone();
201
202 let commit_editor = cx.new_view(|cx| {
203 let theme = ThemeSettings::get_global(cx);
204
205 let mut text_style = cx.text_style();
206 let refinement = TextStyleRefinement {
207 font_family: Some(theme.buffer_font.family.clone()),
208 font_features: Some(FontFeatures::disable_ligatures()),
209 font_size: Some(px(12.).into()),
210 color: Some(cx.theme().colors().editor_foreground),
211 background_color: Some(gpui::transparent_black()),
212 ..Default::default()
213 };
214
215 text_style.refine(&refinement);
216
217 let mut commit_editor = Editor::auto_height(10, cx);
218 if let Some(message) = current_commit_message {
219 commit_editor.set_text(message, cx);
220 } else {
221 commit_editor.set_text("", cx);
222 }
223 // commit_editor.set_soft_wrap_mode(SoftWrap::EditorWidth, cx);
224 commit_editor.set_use_autoclose(false);
225 commit_editor.set_show_gutter(false, cx);
226 commit_editor.set_show_wrap_guides(false, cx);
227 commit_editor.set_show_indent_guides(false, cx);
228 commit_editor.set_text_style_refinement(refinement);
229 commit_editor.set_placeholder_text("Enter commit message", cx);
230 commit_editor
231 });
232
233 let buffer = commit_editor
234 .read(cx)
235 .buffer()
236 .read(cx)
237 .as_singleton()
238 .expect("commit editor must be singleton");
239
240 cx.subscribe(&buffer, Self::on_buffer_event).detach();
241
242 let markdown = language_registry.language_for_name("Markdown");
243 cx.spawn(|_, mut cx| async move {
244 let markdown = markdown.await.context("failed to load Markdown language")?;
245 buffer.update(&mut cx, |buffer, cx| {
246 buffer.set_language(Some(markdown), cx)
247 })
248 })
249 .detach_and_log_err(cx);
250
251 let scroll_handle = UniformListScrollHandle::new();
252
253 let mut git_panel = Self {
254 // workspace: weak_workspace,
255 focus_handle: cx.focus_handle(),
256 fs,
257 pending_serialization: Task::ready(None),
258 visible_entries: Vec::new(),
259 current_modifiers: cx.modifiers(),
260 // expanded_dir_ids: Default::default(),
261 width: Some(px(360.)),
262 scrollbar_state: ScrollbarState::new(scroll_handle.clone()).parent_view(cx.view()),
263 scroll_handle,
264 selected_item: None,
265 view_mode: ViewMode::default(),
266 show_scrollbar: !Self::should_autohide_scrollbar(cx),
267 hide_scrollbar_task: None,
268 // git_diff_editor: Some(diff_display_editor(cx)),
269 // git_diff_editor_updates: Task::ready(()),
270 commit_editor,
271 git_state,
272 reveal_in_editor: Task::ready(()),
273 project,
274 };
275 git_panel.update_visible_entries(None, None, cx);
276 git_panel
277 });
278
279 git_panel
280 }
281
282 fn serialize(&mut self, cx: &mut ViewContext<Self>) {
283 let width = self.width;
284 self.pending_serialization = cx.background_executor().spawn(
285 async move {
286 KEY_VALUE_STORE
287 .write_kvp(
288 GIT_PANEL_KEY.into(),
289 serde_json::to_string(&SerializedGitPanel { width })?,
290 )
291 .await?;
292 anyhow::Ok(())
293 }
294 .log_err(),
295 );
296 }
297
298 fn dispatch_context(&self) -> KeyContext {
299 let mut dispatch_context = KeyContext::new_with_defaults();
300 dispatch_context.add("GitPanel");
301 dispatch_context.add("menu");
302
303 dispatch_context
304 }
305
306 fn focus_in(&mut self, cx: &mut ViewContext<Self>) {
307 if !self.focus_handle.contains_focused(cx) {
308 cx.emit(Event::Focus);
309 }
310 }
311
312 fn should_show_scrollbar(_cx: &AppContext) -> bool {
313 // TODO: plug into settings
314 true
315 }
316
317 fn should_autohide_scrollbar(_cx: &AppContext) -> bool {
318 // TODO: plug into settings
319 true
320 }
321
322 fn hide_scrollbar(&mut self, cx: &mut ViewContext<Self>) {
323 const SCROLLBAR_SHOW_INTERVAL: Duration = Duration::from_secs(1);
324 if !Self::should_autohide_scrollbar(cx) {
325 return;
326 }
327 self.hide_scrollbar_task = Some(cx.spawn(|panel, mut cx| async move {
328 cx.background_executor()
329 .timer(SCROLLBAR_SHOW_INTERVAL)
330 .await;
331 panel
332 .update(&mut cx, |panel, cx| {
333 panel.show_scrollbar = false;
334 cx.notify();
335 })
336 .log_err();
337 }))
338 }
339
340 fn handle_modifiers_changed(
341 &mut self,
342 event: &ModifiersChangedEvent,
343 cx: &mut ViewContext<Self>,
344 ) {
345 self.current_modifiers = event.modifiers;
346 cx.notify();
347 }
348
349 fn calculate_depth_and_difference(
350 entry: &StatusEntry,
351 visible_worktree_entries: &HashSet<RepoPath>,
352 ) -> (usize, usize) {
353 let (depth, difference) = entry
354 .repo_path
355 .ancestors()
356 .skip(1) // Skip the entry itself
357 .find_map(|ancestor| {
358 if let Some(parent_entry) = visible_worktree_entries.get(ancestor) {
359 let entry_path_components_count = entry.repo_path.components().count();
360 let parent_path_components_count = parent_entry.components().count();
361 let difference = entry_path_components_count - parent_path_components_count;
362 let depth = parent_entry
363 .ancestors()
364 .skip(1)
365 .filter(|ancestor| visible_worktree_entries.contains(*ancestor))
366 .count();
367 Some((depth + 1, difference))
368 } else {
369 None
370 }
371 })
372 .unwrap_or((0, 0));
373
374 (depth, difference)
375 }
376
377 fn select_next(&mut self, _: &SelectNext, cx: &mut ViewContext<Self>) {
378 let item_count = self
379 .visible_entries
380 .iter()
381 .map(|worktree_entries| worktree_entries.visible_entries.len())
382 .sum::<usize>();
383 if item_count == 0 {
384 return;
385 }
386 let selection = match self.selected_item {
387 Some(i) => {
388 if i < item_count - 1 {
389 self.selected_item = Some(i + 1);
390 i + 1
391 } else {
392 self.selected_item = Some(0);
393 0
394 }
395 }
396 None => {
397 self.selected_item = Some(0);
398 0
399 }
400 };
401 self.scroll_handle
402 .scroll_to_item(selection, ScrollStrategy::Center);
403
404 let mut hunks = None;
405 self.for_each_visible_entry(selection..selection + 1, cx, |_, entry, _| {
406 hunks = Some(entry.hunks.clone());
407 });
408 if let Some(hunks) = hunks {
409 self.reveal_entry_in_git_editor(hunks, false, Some(UPDATE_DEBOUNCE), cx);
410 }
411
412 cx.notify();
413 }
414
415 fn select_prev(&mut self, _: &SelectPrev, cx: &mut ViewContext<Self>) {
416 let item_count = self
417 .visible_entries
418 .iter()
419 .map(|worktree_entries| worktree_entries.visible_entries.len())
420 .sum::<usize>();
421 if item_count == 0 {
422 return;
423 }
424 let selection = match self.selected_item {
425 Some(i) => {
426 if i > 0 {
427 self.selected_item = Some(i - 1);
428 i - 1
429 } else {
430 self.selected_item = Some(item_count - 1);
431 item_count - 1
432 }
433 }
434 None => {
435 self.selected_item = Some(0);
436 0
437 }
438 };
439 self.scroll_handle
440 .scroll_to_item(selection, ScrollStrategy::Center);
441
442 let mut hunks = None;
443 self.for_each_visible_entry(selection..selection + 1, cx, |_, entry, _| {
444 hunks = Some(entry.hunks.clone());
445 });
446 if let Some(hunks) = hunks {
447 self.reveal_entry_in_git_editor(hunks, false, Some(UPDATE_DEBOUNCE), cx);
448 }
449
450 cx.notify();
451 }
452}
453
454impl GitPanel {
455 fn stage_all(&mut self, _: &StageAll, _cx: &mut ViewContext<Self>) {
456 // TODO: Implement stage all
457 println!("Stage all triggered");
458 }
459
460 fn unstage_all(&mut self, _: &UnstageAll, _cx: &mut ViewContext<Self>) {
461 // TODO: Implement unstage all
462 println!("Unstage all triggered");
463 }
464
465 fn discard_all(&mut self, _: &RevertAll, _cx: &mut ViewContext<Self>) {
466 // TODO: Implement discard all
467 println!("Discard all triggered");
468 }
469
470 fn clear_message(&mut self, cx: &mut ViewContext<Self>) {
471 let git_state = self.git_state.clone();
472 git_state.update(cx, |state, _cx| state.clear_message());
473 self.commit_editor
474 .update(cx, |editor, cx| editor.set_text("", cx));
475 }
476
477 /// Commit all staged changes
478 fn commit_staged_changes(&mut self, _: &CommitStagedChanges, cx: &mut ViewContext<Self>) {
479 self.clear_message(cx);
480
481 // TODO: Implement commit all staged
482 println!("Commit staged changes triggered");
483 }
484
485 /// Commit all changes, regardless of whether they are staged or not
486 fn commit_all_changes(&mut self, _: &CommitAllChanges, cx: &mut ViewContext<Self>) {
487 self.clear_message(cx);
488
489 // TODO: Implement commit all changes
490 println!("Commit all changes triggered");
491 }
492
493 fn all_staged(&self) -> bool {
494 // TODO: Implement all_staged
495 true
496 }
497
498 fn no_entries(&self) -> bool {
499 self.visible_entries.is_empty()
500 }
501
502 fn entry_count(&self) -> usize {
503 self.visible_entries
504 .iter()
505 .map(|worktree_entries| worktree_entries.visible_entries.len())
506 .sum()
507 }
508
509 fn for_each_visible_entry(
510 &self,
511 range: Range<usize>,
512 cx: &mut ViewContext<Self>,
513 mut callback: impl FnMut(usize, EntryDetails, &mut ViewContext<Self>),
514 ) {
515 let mut ix = 0;
516 for worktree_entries in &self.visible_entries {
517 if ix >= range.end {
518 return;
519 }
520
521 if ix + worktree_entries.visible_entries.len() <= range.start {
522 ix += worktree_entries.visible_entries.len();
523 continue;
524 }
525
526 let end_ix = range.end.min(ix + worktree_entries.visible_entries.len());
527 // let entry_range = range.start.saturating_sub(ix)..end_ix - ix;
528 if let Some(worktree) = self
529 .project
530 .read(cx)
531 .worktree_for_id(worktree_entries.worktree_id, cx)
532 {
533 let snapshot = worktree.read(cx).snapshot();
534 let root_name = OsStr::new(snapshot.root_name());
535 // let expanded_entry_ids = self
536 // .expanded_dir_ids
537 // .get(&snapshot.id())
538 // .map(Vec::as_slice)
539 // .unwrap_or(&[]);
540
541 let entry_range = range.start.saturating_sub(ix)..end_ix - ix;
542 let entries = worktree_entries.paths();
543
544 let index_start = entry_range.start;
545 for (i, entry) in worktree_entries.visible_entries[entry_range]
546 .iter()
547 .enumerate()
548 {
549 let index = index_start + i;
550 let status = entry.status;
551 let is_expanded = true; //expanded_entry_ids.binary_search(&entry.id).is_ok();
552
553 let (depth, difference) = Self::calculate_depth_and_difference(entry, entries);
554
555 let filename = match difference {
556 diff if diff > 1 => entry
557 .repo_path
558 .iter()
559 .skip(entry.repo_path.components().count() - diff)
560 .collect::<PathBuf>()
561 .to_str()
562 .unwrap_or_default()
563 .to_string(),
564 _ => entry
565 .repo_path
566 .file_name()
567 .map(|name| name.to_string_lossy().into_owned())
568 .unwrap_or_else(|| root_name.to_string_lossy().to_string()),
569 };
570
571 let details = EntryDetails {
572 filename,
573 display_name: entry.repo_path.to_string_lossy().into_owned(),
574 // TODO get it from StatusEntry?
575 kind: EntryKind::File,
576 is_expanded,
577 path: entry.repo_path.clone(),
578 status: Some(status),
579 hunks: entry.hunks.clone(),
580 depth,
581 index,
582 };
583 callback(ix, details, cx);
584 }
585 }
586 ix = end_ix;
587 }
588 }
589
590 // TODO: Update expanded directory state
591 // TODO: Updates happen in the main loop, could be long for large workspaces
592 #[track_caller]
593 fn update_visible_entries(
594 &mut self,
595 for_worktree: Option<WorktreeId>,
596 _new_selected_entry: Option<(WorktreeId, ProjectEntryId)>,
597 cx: &mut ViewContext<Self>,
598 ) {
599 let project = self.project.read(cx);
600 let mut old_entries_removed = false;
601 let mut after_update = Vec::new();
602 self.visible_entries
603 .retain(|worktree_entries| match for_worktree {
604 Some(for_worktree) => {
605 if worktree_entries.worktree_id == for_worktree {
606 old_entries_removed = true;
607 false
608 } else if old_entries_removed {
609 after_update.push(worktree_entries.clone());
610 false
611 } else {
612 true
613 }
614 }
615 None => false,
616 });
617 for worktree in project.visible_worktrees(cx) {
618 let snapshot = worktree.read(cx).snapshot();
619 let worktree_id = snapshot.id();
620
621 if for_worktree.is_some() && for_worktree != Some(worktree_id) {
622 continue;
623 }
624
625 let mut visible_worktree_entries = Vec::new();
626 // Only use the first repository for now
627 let repositories = snapshot.repositories().take(1);
628 // let mut work_directory = None;
629 for repository in repositories {
630 visible_worktree_entries.extend(repository.status());
631 // work_directory = Some(worktree::WorkDirectory::clone(repository));
632 }
633
634 // TODO use the GitTraversal
635 // let mut visible_worktree_entries = snapshot
636 // .entries(false, 0)
637 // .filter(|entry| !entry.is_external)
638 // .filter(|entry| entry.git_status.is_some())
639 // .cloned()
640 // .collect::<Vec<_>>();
641 // snapshot.propagate_git_statuses(&mut visible_worktree_entries);
642 // project::sort_worktree_entries(&mut visible_worktree_entries);
643
644 if !visible_worktree_entries.is_empty() {
645 self.visible_entries.push(WorktreeEntries {
646 worktree_id,
647 // work_directory: work_directory.unwrap(),
648 visible_entries: visible_worktree_entries
649 .into_iter()
650 .map(|entry| GitPanelEntry {
651 entry,
652 hunks: Rc::default(),
653 })
654 .collect(),
655 paths: Rc::default(),
656 });
657 }
658 }
659 self.visible_entries.extend(after_update);
660
661 // TODO re-implement this
662 // if let Some((worktree_id, entry_id)) = new_selected_entry {
663 // self.selected_item = self.visible_entries.iter().enumerate().find_map(
664 // |(worktree_index, worktree_entries)| {
665 // if worktree_entries.worktree_id == worktree_id {
666 // worktree_entries
667 // .visible_entries
668 // .iter()
669 // .position(|entry| entry.id == entry_id)
670 // .map(|entry_index| {
671 // worktree_index * worktree_entries.visible_entries.len()
672 // + entry_index
673 // })
674 // } else {
675 // None
676 // }
677 // },
678 // );
679 // }
680
681 // let project = self.project.downgrade();
682 // self.git_diff_editor_updates = cx.spawn(|git_panel, mut cx| async move {
683 // cx.background_executor()
684 // .timer(UPDATE_DEBOUNCE)
685 // .await;
686 // let Some(project_buffers) = git_panel
687 // .update(&mut cx, |git_panel, cx| {
688 // futures::future::join_all(git_panel.visible_entries.iter_mut().flat_map(
689 // |worktree_entries| {
690 // worktree_entries
691 // .visible_entries
692 // .iter()
693 // .filter_map(|entry| {
694 // let git_status = entry.status;
695 // let entry_hunks = entry.hunks.clone();
696 // let (entry_path, unstaged_changes_task) =
697 // project.update(cx, |project, cx| {
698 // let entry_path = ProjectPath {
699 // worktree_id: worktree_entries.worktree_id,
700 // path: worktree_entries.work_directory.unrelativize(&entry.repo_path)?,
701 // };
702 // let open_task =
703 // project.open_path(entry_path.clone(), cx);
704 // let unstaged_changes_task =
705 // cx.spawn(|project, mut cx| async move {
706 // let (_, opened_model) = open_task
707 // .await
708 // .context("opening buffer")?;
709 // let buffer = opened_model
710 // .downcast::<Buffer>()
711 // .map_err(|_| {
712 // anyhow::anyhow!(
713 // "accessing buffer for entry"
714 // )
715 // })?;
716 // // TODO added files have noop changes and those are not expanded properly in the multi buffer
717 // let unstaged_changes = project
718 // .update(&mut cx, |project, cx| {
719 // project.open_unstaged_changes(
720 // buffer.clone(),
721 // cx,
722 // )
723 // })?
724 // .await
725 // .context("opening unstaged changes")?;
726
727 // let hunks = cx.update(|cx| {
728 // entry_hunks
729 // .get_or_init(|| {
730 // match git_status {
731 // GitFileStatus::Added => {
732 // let buffer_snapshot = buffer.read(cx).snapshot();
733 // let entire_buffer_range =
734 // buffer_snapshot.anchor_after(0)
735 // ..buffer_snapshot
736 // .anchor_before(
737 // buffer_snapshot.len(),
738 // );
739 // let entire_buffer_point_range =
740 // entire_buffer_range
741 // .clone()
742 // .to_point(&buffer_snapshot);
743
744 // vec![DiffHunk {
745 // row_range: entire_buffer_point_range
746 // .start
747 // .row
748 // ..entire_buffer_point_range
749 // .end
750 // .row,
751 // buffer_range: entire_buffer_range,
752 // diff_base_byte_range: 0..0,
753 // }]
754 // }
755 // GitFileStatus::Modified => {
756 // let buffer_snapshot =
757 // buffer.read(cx).snapshot();
758 // unstaged_changes.read(cx)
759 // .diff_to_buffer
760 // .hunks_in_row_range(
761 // 0..BufferRow::MAX,
762 // &buffer_snapshot,
763 // )
764 // .collect()
765 // }
766 // // TODO support these
767 // GitFileStatus::Conflict | GitFileStatus::Deleted | GitFileStatus::Untracked => Vec::new(),
768 // }
769 // }).clone()
770 // })?;
771
772 // anyhow::Ok((buffer, unstaged_changes, hunks))
773 // });
774 // Some((entry_path, unstaged_changes_task))
775 // }).ok()??;
776 // Some((entry_path, unstaged_changes_task))
777 // })
778 // .map(|(entry_path, open_task)| async move {
779 // (entry_path, open_task.await)
780 // })
781 // .collect::<Vec<_>>()
782 // },
783 // ))
784 // })
785 // .ok()
786 // else {
787 // return;
788 // };
789
790 // let project_buffers = project_buffers.await;
791 // if project_buffers.is_empty() {
792 // return;
793 // }
794 // let mut change_sets = Vec::with_capacity(project_buffers.len());
795 // if let Some(buffer_update_task) = git_panel
796 // .update(&mut cx, |git_panel, cx| {
797 // let editor = git_panel.git_diff_editor.clone()?;
798 // let multi_buffer = editor.read(cx).buffer().clone();
799 // let mut buffers_with_ranges = Vec::with_capacity(project_buffers.len());
800 // for (buffer_path, open_result) in project_buffers {
801 // if let Some((buffer, unstaged_changes, diff_hunks)) = open_result
802 // .with_context(|| format!("opening buffer {buffer_path:?}"))
803 // .log_err()
804 // {
805 // change_sets.push(unstaged_changes);
806 // buffers_with_ranges.push((
807 // buffer,
808 // diff_hunks
809 // .into_iter()
810 // .map(|hunk| hunk.buffer_range)
811 // .collect(),
812 // ));
813 // }
814 // }
815
816 // Some(multi_buffer.update(cx, |multi_buffer, cx| {
817 // multi_buffer.clear(cx);
818 // multi_buffer.push_multiple_excerpts_with_context_lines(
819 // buffers_with_ranges,
820 // DEFAULT_MULTIBUFFER_CONTEXT,
821 // cx,
822 // )
823 // }))
824 // })
825 // .ok().flatten()
826 // {
827 // buffer_update_task.await;
828 // git_panel
829 // .update(&mut cx, |git_panel, cx| {
830 // if let Some(diff_editor) = git_panel.git_diff_editor.as_ref() {
831 // diff_editor.update(cx, |editor, cx| {
832 // for change_set in change_sets {
833 // editor.add_change_set(change_set, cx);
834 // }
835 // });
836 // }
837 // })
838 // .ok();
839 // }
840 // });
841
842 cx.notify();
843 }
844
845 fn on_buffer_event(
846 &mut self,
847 _buffer: Model<Buffer>,
848 event: &language::BufferEvent,
849 cx: &mut ViewContext<Self>,
850 ) {
851 if let language::BufferEvent::Reparsed | language::BufferEvent::Edited = event {
852 let commit_message = self.commit_editor.update(cx, |editor, cx| editor.text(cx));
853
854 self.git_state.update(cx, |state, _cx| {
855 state.commit_message = Some(commit_message.into());
856 });
857
858 cx.notify();
859 }
860 }
861}
862
863impl GitPanel {
864 pub fn panel_button(
865 &self,
866 id: impl Into<SharedString>,
867 label: impl Into<SharedString>,
868 ) -> Button {
869 let id = id.into().clone();
870 let label = label.into().clone();
871
872 Button::new(id, label)
873 .label_size(LabelSize::Small)
874 .layer(ElevationIndex::ElevatedSurface)
875 .size(ButtonSize::Compact)
876 .style(ButtonStyle::Filled)
877 }
878
879 pub fn render_divider(&self, _cx: &mut ViewContext<Self>) -> impl IntoElement {
880 h_flex()
881 .items_center()
882 .h(px(8.))
883 .child(Divider::horizontal_dashed().color(DividerColor::Border))
884 }
885
886 pub fn render_panel_header(&self, cx: &mut ViewContext<Self>) -> impl IntoElement {
887 let focus_handle = self.focus_handle(cx).clone();
888
889 let changes_string = match self.entry_count() {
890 0 => "No changes".to_string(),
891 1 => "1 change".to_string(),
892 n => format!("{} changes", n),
893 };
894
895 h_flex()
896 .h(px(32.))
897 .items_center()
898 .px_3()
899 .bg(ElevationIndex::Surface.bg(cx))
900 .child(
901 h_flex()
902 .gap_2()
903 .child(Checkbox::new("all-changes", true.into()).disabled(true))
904 .child(div().text_buffer(cx).text_ui_sm(cx).child(changes_string)),
905 )
906 .child(div().flex_grow())
907 .child(
908 h_flex()
909 .gap_2()
910 .child(
911 IconButton::new("discard-changes", IconName::Undo)
912 .tooltip(move |cx| {
913 let focus_handle = focus_handle.clone();
914
915 Tooltip::for_action_in(
916 "Discard all changes",
917 &RevertAll,
918 &focus_handle,
919 cx,
920 )
921 })
922 .icon_size(IconSize::Small)
923 .disabled(true),
924 )
925 .child(if self.all_staged() {
926 self.panel_button("unstage-all", "Unstage All").on_click(
927 cx.listener(move |_, _, cx| cx.dispatch_action(Box::new(RevertAll))),
928 )
929 } else {
930 self.panel_button("stage-all", "Stage All").on_click(
931 cx.listener(move |_, _, cx| cx.dispatch_action(Box::new(StageAll))),
932 )
933 }),
934 )
935 }
936
937 pub fn render_commit_editor(&self, cx: &ViewContext<Self>) -> impl IntoElement {
938 let editor = self.commit_editor.clone();
939 let editor_focus_handle = editor.read(cx).focus_handle(cx).clone();
940
941 let focus_handle_1 = self.focus_handle(cx).clone();
942 let focus_handle_2 = self.focus_handle(cx).clone();
943
944 let commit_staged_button = self
945 .panel_button("commit-staged-changes", "Commit")
946 .tooltip(move |cx| {
947 let focus_handle = focus_handle_1.clone();
948 Tooltip::for_action_in(
949 "Commit all staged changes",
950 &CommitStagedChanges,
951 &focus_handle,
952 cx,
953 )
954 })
955 .on_click(cx.listener(|this, _: &ClickEvent, cx| {
956 this.commit_staged_changes(&CommitStagedChanges, cx)
957 }));
958
959 let commit_all_button = self
960 .panel_button("commit-all-changes", "Commit All")
961 .tooltip(move |cx| {
962 let focus_handle = focus_handle_2.clone();
963 Tooltip::for_action_in(
964 "Commit all changes, including unstaged changes",
965 &CommitAllChanges,
966 &focus_handle,
967 cx,
968 )
969 })
970 .on_click(cx.listener(|this, _: &ClickEvent, cx| {
971 this.commit_all_changes(&CommitAllChanges, cx)
972 }));
973
974 div().w_full().h(px(140.)).px_2().pt_1().pb_2().child(
975 v_flex()
976 .id("commit-editor-container")
977 .relative()
978 .h_full()
979 .py_2p5()
980 .px_3()
981 .bg(cx.theme().colors().editor_background)
982 .on_click(cx.listener(move |_, _: &ClickEvent, cx| cx.focus(&editor_focus_handle)))
983 .child(self.commit_editor.clone())
984 .child(
985 h_flex()
986 .absolute()
987 .bottom_2p5()
988 .right_3()
989 .child(div().gap_1().flex_grow())
990 .child(if self.current_modifiers.alt {
991 commit_all_button
992 } else {
993 commit_staged_button
994 }),
995 ),
996 )
997 }
998
999 fn render_empty_state(&self, cx: &ViewContext<Self>) -> impl IntoElement {
1000 h_flex()
1001 .h_full()
1002 .flex_1()
1003 .justify_center()
1004 .items_center()
1005 .child(
1006 v_flex()
1007 .gap_3()
1008 .child("No changes to commit")
1009 .text_ui_sm(cx)
1010 .mx_auto()
1011 .text_color(Color::Placeholder.color(cx)),
1012 )
1013 }
1014
1015 fn render_scrollbar(&self, cx: &mut ViewContext<Self>) -> Option<Stateful<Div>> {
1016 if !Self::should_show_scrollbar(cx)
1017 || !(self.show_scrollbar || self.scrollbar_state.is_dragging())
1018 {
1019 return None;
1020 }
1021 Some(
1022 div()
1023 .occlude()
1024 .id("project-panel-vertical-scroll")
1025 .on_mouse_move(cx.listener(|_, _, cx| {
1026 cx.notify();
1027 cx.stop_propagation()
1028 }))
1029 .on_hover(|_, cx| {
1030 cx.stop_propagation();
1031 })
1032 .on_any_mouse_down(|_, cx| {
1033 cx.stop_propagation();
1034 })
1035 .on_mouse_up(
1036 MouseButton::Left,
1037 cx.listener(|this, _, cx| {
1038 if !this.scrollbar_state.is_dragging()
1039 && !this.focus_handle.contains_focused(cx)
1040 {
1041 this.hide_scrollbar(cx);
1042 cx.notify();
1043 }
1044
1045 cx.stop_propagation();
1046 }),
1047 )
1048 .on_scroll_wheel(cx.listener(|_, _, cx| {
1049 cx.notify();
1050 }))
1051 .h_full()
1052 .absolute()
1053 .right_1()
1054 .top_1()
1055 .bottom_1()
1056 .w(px(12.))
1057 .cursor_default()
1058 .children(Scrollbar::vertical(
1059 // percentage as f32..end_offset as f32,
1060 self.scrollbar_state.clone(),
1061 )),
1062 )
1063 }
1064
1065 fn render_entries(&self, cx: &mut ViewContext<Self>) -> impl IntoElement {
1066 let item_count = self
1067 .visible_entries
1068 .iter()
1069 .map(|worktree_entries| worktree_entries.visible_entries.len())
1070 .sum();
1071 let selected_entry = self.selected_item;
1072 h_flex()
1073 .size_full()
1074 .overflow_hidden()
1075 .child(
1076 uniform_list(cx.view().clone(), "entries", item_count, {
1077 move |git_panel, range, cx| {
1078 let mut items = Vec::with_capacity(range.end - range.start);
1079 git_panel.for_each_visible_entry(range, cx, |id, details, cx| {
1080 items.push(git_panel.render_entry(
1081 id,
1082 Some(details.index) == selected_entry,
1083 details,
1084 cx,
1085 ));
1086 });
1087 items
1088 }
1089 })
1090 .size_full()
1091 .with_sizing_behavior(ListSizingBehavior::Infer)
1092 .with_horizontal_sizing_behavior(ListHorizontalSizingBehavior::Unconstrained)
1093 // .with_width_from_item(self.max_width_item_index)
1094 .track_scroll(self.scroll_handle.clone()),
1095 )
1096 .children(self.render_scrollbar(cx))
1097 }
1098
1099 fn render_entry(
1100 &self,
1101 ix: usize,
1102 selected: bool,
1103 details: EntryDetails,
1104 cx: &ViewContext<Self>,
1105 ) -> impl IntoElement {
1106 let view_mode = self.view_mode.clone();
1107 let checkbox_id = ElementId::Name(format!("checkbox_{}", ix).into());
1108 let is_staged = ToggleState::Selected;
1109 let handle = cx.view().downgrade();
1110
1111 // TODO: At this point, an entry should really have a status.
1112 // Is this fixed with the new git status stuff?
1113 let status = details.status.unwrap_or(GitFileStatus::Untracked);
1114
1115 let end_slot = h_flex()
1116 .invisible()
1117 .when(selected, |this| this.visible())
1118 .when(!selected, |this| {
1119 this.group_hover("git-panel-entry", |this| this.visible())
1120 })
1121 .gap_1()
1122 .items_center()
1123 .child(
1124 IconButton::new("more", IconName::EllipsisVertical)
1125 .icon_color(Color::Placeholder)
1126 .icon_size(IconSize::Small),
1127 );
1128
1129 let mut entry = h_flex()
1130 .id(("git-panel-entry", ix))
1131 .group("git-panel-entry")
1132 .h(px(28.))
1133 .w_full()
1134 .pr(px(4.))
1135 .items_center()
1136 .gap_2()
1137 .font_buffer(cx)
1138 .text_ui_sm(cx)
1139 .when(!selected, |this| {
1140 this.hover(|this| this.bg(cx.theme().colors().ghost_element_hover))
1141 });
1142
1143 if view_mode == ViewMode::Tree {
1144 entry = entry.pl(px(12. + 12. * details.depth as f32))
1145 } else {
1146 entry = entry.pl(px(12.))
1147 }
1148
1149 if selected {
1150 entry = entry.bg(cx.theme().status().info_background);
1151 }
1152
1153 entry = entry
1154 .child(Checkbox::new(checkbox_id, is_staged))
1155 .child(git_status_icon(status))
1156 .child(
1157 h_flex()
1158 .gap_1p5()
1159 .when(status == GitFileStatus::Deleted, |this| {
1160 this.text_color(cx.theme().colors().text_disabled)
1161 .line_through()
1162 })
1163 .child(details.display_name.clone()),
1164 )
1165 .child(div().flex_1())
1166 .child(end_slot)
1167 // TODO: Only fire this if the entry is not currently revealed, otherwise the ui flashes
1168 .on_click(move |e, cx| {
1169 handle
1170 .update(cx, |git_panel, cx| {
1171 git_panel.selected_item = Some(details.index);
1172 let change_focus = e.down.click_count > 1;
1173 git_panel.reveal_entry_in_git_editor(
1174 details.hunks.clone(),
1175 change_focus,
1176 None,
1177 cx,
1178 );
1179 })
1180 .ok();
1181 });
1182
1183 entry
1184 }
1185
1186 fn reveal_entry_in_git_editor(
1187 &mut self,
1188 _hunks: Rc<OnceCell<Vec<DiffHunk>>>,
1189 _change_focus: bool,
1190 _debounce: Option<Duration>,
1191 _cx: &mut ViewContext<Self>,
1192 ) {
1193 // let workspace = self.workspace.clone();
1194 // let Some(diff_editor) = self.git_diff_editor.clone() else {
1195 // return;
1196 // };
1197 // self.reveal_in_editor = cx.spawn(|_, mut cx| async move {
1198 // if let Some(debounce) = debounce {
1199 // cx.background_executor().timer(debounce).await;
1200 // }
1201
1202 // let Some(editor) = workspace
1203 // .update(&mut cx, |workspace, cx| {
1204 // let git_diff_editor = workspace
1205 // .items_of_type::<Editor>(cx)
1206 // .find(|editor| &diff_editor == editor);
1207 // match git_diff_editor {
1208 // Some(existing_editor) => {
1209 // workspace.activate_item(&existing_editor, true, change_focus, cx);
1210 // existing_editor
1211 // }
1212 // None => {
1213 // workspace.active_pane().update(cx, |pane, cx| {
1214 // pane.add_item(
1215 // ` diff_editor.boxed_clone(),
1216 // true,
1217 // change_focus,
1218 // None,
1219 // cx,
1220 // )
1221 // });
1222 // diff_editor.clone()
1223 // }
1224 // }
1225 // })
1226 // .ok()
1227 // else {
1228 // return;
1229 // };
1230
1231 // if let Some(first_hunk) = hunks.get().and_then(|hunks| hunks.first()) {
1232 // let hunk_buffer_range = &first_hunk.buffer_range;
1233 // if let Some(buffer_id) = hunk_buffer_range
1234 // .start
1235 // .buffer_id
1236 // .or_else(|| first_hunk.buffer_range.end.buffer_id)
1237 // {
1238 // editor
1239 // .update(&mut cx, |editor, cx| {
1240 // let multi_buffer = editor.buffer().read(cx);
1241 // let buffer = multi_buffer.buffer(buffer_id)?;
1242 // let buffer_snapshot = buffer.read(cx).snapshot();
1243 // let (excerpt_id, _) = multi_buffer
1244 // .excerpts_for_buffer(&buffer, cx)
1245 // .into_iter()
1246 // .find(|(_, excerpt)| {
1247 // hunk_buffer_range
1248 // .start
1249 // .cmp(&excerpt.context.start, &buffer_snapshot)
1250 // .is_ge()
1251 // && hunk_buffer_range
1252 // .end
1253 // .cmp(&excerpt.context.end, &buffer_snapshot)
1254 // .is_le()
1255 // })?;
1256 // let multi_buffer_hunk_start = multi_buffer
1257 // .snapshot(cx)
1258 // .anchor_in_excerpt(excerpt_id, hunk_buffer_range.start)?;
1259 // editor.change_selections(
1260 // Some(Autoscroll::Strategy(AutoscrollStrategy::Center)),
1261 // cx,
1262 // |s| {
1263 // s.select_ranges(Some(
1264 // multi_buffer_hunk_start..multi_buffer_hunk_start,
1265 // ))
1266 // },
1267 // );
1268 // cx.notify();
1269 // Some(())
1270 // })
1271 // .ok()
1272 // .flatten();
1273 // }
1274 // }
1275 // });
1276 }
1277}
1278
1279impl Render for GitPanel {
1280 fn render(&mut self, cx: &mut ViewContext<Self>) -> impl IntoElement {
1281 let project = self.project.read(cx);
1282
1283 v_flex()
1284 .id("git_panel")
1285 .key_context(self.dispatch_context())
1286 .track_focus(&self.focus_handle)
1287 .on_modifiers_changed(cx.listener(Self::handle_modifiers_changed))
1288 .when(!project.is_read_only(cx), |this| {
1289 this.on_action(cx.listener(|this, &StageAll, cx| this.stage_all(&StageAll, cx)))
1290 .on_action(
1291 cx.listener(|this, &UnstageAll, cx| this.unstage_all(&UnstageAll, cx)),
1292 )
1293 .on_action(cx.listener(|this, &RevertAll, cx| this.discard_all(&RevertAll, cx)))
1294 .on_action(cx.listener(|this, &CommitStagedChanges, cx| {
1295 this.commit_staged_changes(&CommitStagedChanges, cx)
1296 }))
1297 .on_action(cx.listener(|this, &CommitAllChanges, cx| {
1298 this.commit_all_changes(&CommitAllChanges, cx)
1299 }))
1300 })
1301 .on_action(cx.listener(Self::select_next))
1302 .on_action(cx.listener(Self::select_prev))
1303 .on_hover(cx.listener(|this, hovered, cx| {
1304 if *hovered {
1305 this.show_scrollbar = true;
1306 this.hide_scrollbar_task.take();
1307 cx.notify();
1308 } else if !this.focus_handle.contains_focused(cx) {
1309 this.hide_scrollbar(cx);
1310 }
1311 }))
1312 .size_full()
1313 .overflow_hidden()
1314 .font_buffer(cx)
1315 .py_1()
1316 .bg(ElevationIndex::Surface.bg(cx))
1317 .child(self.render_panel_header(cx))
1318 .child(self.render_divider(cx))
1319 .child(if !self.no_entries() {
1320 self.render_entries(cx).into_any_element()
1321 } else {
1322 self.render_empty_state(cx).into_any_element()
1323 })
1324 .child(self.render_divider(cx))
1325 .child(self.render_commit_editor(cx))
1326 }
1327}
1328
1329impl FocusableView for GitPanel {
1330 fn focus_handle(&self, _: &AppContext) -> gpui::FocusHandle {
1331 self.focus_handle.clone()
1332 }
1333}
1334
1335impl EventEmitter<Event> for GitPanel {}
1336
1337impl EventEmitter<PanelEvent> for GitPanel {}
1338
1339impl Panel for GitPanel {
1340 fn persistent_name() -> &'static str {
1341 "GitPanel"
1342 }
1343
1344 fn position(&self, cx: &WindowContext) -> DockPosition {
1345 GitPanelSettings::get_global(cx).dock
1346 }
1347
1348 fn position_is_valid(&self, position: DockPosition) -> bool {
1349 matches!(position, DockPosition::Left | DockPosition::Right)
1350 }
1351
1352 fn set_position(&mut self, position: DockPosition, cx: &mut ViewContext<Self>) {
1353 settings::update_settings_file::<GitPanelSettings>(
1354 self.fs.clone(),
1355 cx,
1356 move |settings, _| settings.dock = Some(position),
1357 );
1358 }
1359
1360 fn size(&self, cx: &WindowContext) -> Pixels {
1361 self.width
1362 .unwrap_or_else(|| GitPanelSettings::get_global(cx).default_width)
1363 }
1364
1365 fn set_size(&mut self, size: Option<Pixels>, cx: &mut ViewContext<Self>) {
1366 self.width = size;
1367 self.serialize(cx);
1368 cx.notify();
1369 }
1370
1371 fn icon(&self, cx: &WindowContext) -> Option<ui::IconName> {
1372 Some(ui::IconName::GitBranch).filter(|_| GitPanelSettings::get_global(cx).button)
1373 }
1374
1375 fn icon_tooltip(&self, _cx: &WindowContext) -> Option<&'static str> {
1376 Some("Git Panel")
1377 }
1378
1379 fn toggle_action(&self) -> Box<dyn Action> {
1380 Box::new(ToggleFocus)
1381 }
1382
1383 fn activation_priority(&self) -> u32 {
1384 2
1385 }
1386}
1387
1388// fn diff_display_editor(cx: &mut WindowContext) -> View<Editor> {
1389// cx.new_view(|cx| {
1390// let multi_buffer = cx.new_model(|_| {
1391// MultiBuffer::new(language::Capability::ReadWrite).with_title("Project diff".to_string())
1392// });
1393// let mut editor = Editor::for_multibuffer(multi_buffer, None, true, cx);
1394// editor.set_expand_all_diff_hunks();
1395// editor
1396// })
1397// }