1mod project_panel_settings;
2mod utils;
3
4use anyhow::{Context as _, Result};
5use client::{ErrorCode, ErrorExt};
6use collections::{BTreeSet, HashMap, hash_map};
7use command_palette_hooks::CommandPaletteFilter;
8use db::kvp::KEY_VALUE_STORE;
9use editor::{
10 Editor, EditorEvent, EditorSettings, ShowScrollbar,
11 items::{
12 entry_diagnostic_aware_icon_decoration_and_color,
13 entry_diagnostic_aware_icon_name_and_color, entry_git_aware_label_color,
14 },
15 scroll::ScrollbarAutoHide,
16};
17use file_icons::FileIcons;
18use git::status::GitSummary;
19use gpui::{
20 Action, AnyElement, App, ArcCow, AsyncWindowContext, Bounds, ClipboardItem, Context,
21 CursorStyle, DismissEvent, Div, DragMoveEvent, Entity, EventEmitter, ExternalPaths,
22 FocusHandle, Focusable, Hsla, InteractiveElement, KeyContext, ListHorizontalSizingBehavior,
23 ListSizingBehavior, Modifiers, ModifiersChangedEvent, MouseButton, MouseDownEvent,
24 ParentElement, Pixels, Point, PromptLevel, Render, ScrollStrategy, Stateful, Styled,
25 Subscription, Task, UniformListScrollHandle, WeakEntity, Window, actions, anchored, deferred,
26 div, hsla, linear_color_stop, linear_gradient, point, px, size, transparent_white,
27 uniform_list,
28};
29use indexmap::IndexMap;
30use language::DiagnosticSeverity;
31use menu::{Confirm, SelectFirst, SelectLast, SelectNext, SelectPrevious};
32use project::{
33 Entry, EntryKind, Fs, GitEntry, GitEntryRef, GitTraversal, Project, ProjectEntryId,
34 ProjectPath, Worktree, WorktreeId,
35 git_store::{GitStoreEvent, git_traversal::ChildEntriesGitIter},
36 relativize_path,
37};
38use project_panel_settings::{
39 ProjectPanelDockPosition, ProjectPanelSettings, ShowDiagnostics, ShowIndentGuides,
40};
41use schemars::JsonSchema;
42use serde::{Deserialize, Serialize};
43use settings::{Settings, SettingsStore, update_settings_file};
44use smallvec::SmallVec;
45use std::any::TypeId;
46use std::{
47 cell::OnceCell,
48 cmp,
49 collections::HashSet,
50 ffi::OsStr,
51 ops::Range,
52 path::{Path, PathBuf},
53 sync::Arc,
54 time::Duration,
55};
56use theme::ThemeSettings;
57use ui::{
58 Color, ContextMenu, DecoratedIcon, Icon, IconDecoration, IconDecorationKind, IndentGuideColors,
59 IndentGuideLayout, KeyBinding, Label, LabelSize, ListItem, ListItemSpacing, ScrollableHandle,
60 Scrollbar, ScrollbarState, StickyCandidate, Tooltip, prelude::*, v_flex,
61};
62use util::{ResultExt, TakeUntilExt, TryFutureExt, maybe, paths::compare_paths};
63use workspace::{
64 DraggedSelection, OpenInTerminal, OpenOptions, OpenVisible, PreviewTabsSettings, SelectedEntry,
65 Workspace,
66 dock::{DockPosition, Panel, PanelEvent},
67 notifications::{DetachAndPromptErr, NotifyTaskExt},
68};
69use worktree::CreatedEntry;
70use zed_actions::OpenRecent;
71
72const PROJECT_PANEL_KEY: &str = "ProjectPanel";
73const NEW_ENTRY_ID: ProjectEntryId = ProjectEntryId::MAX;
74
75pub struct ProjectPanel {
76 project: Entity<Project>,
77 fs: Arc<dyn Fs>,
78 focus_handle: FocusHandle,
79 scroll_handle: UniformListScrollHandle,
80 // An update loop that keeps incrementing/decrementing scroll offset while there is a dragged entry that's
81 // hovered over the start/end of a list.
82 hover_scroll_task: Option<Task<()>>,
83 visible_entries: Vec<(WorktreeId, Vec<GitEntry>, OnceCell<HashSet<Arc<Path>>>)>,
84 /// Maps from leaf project entry ID to the currently selected ancestor.
85 /// Relevant only for auto-fold dirs, where a single project panel entry may actually consist of several
86 /// project entries (and all non-leaf nodes are guaranteed to be directories).
87 ancestors: HashMap<ProjectEntryId, FoldedAncestors>,
88 folded_directory_drag_target: Option<FoldedDirectoryDragTarget>,
89 last_worktree_root_id: Option<ProjectEntryId>,
90 drag_target_entry: Option<DragTargetEntry>,
91 expanded_dir_ids: HashMap<WorktreeId, Vec<ProjectEntryId>>,
92 unfolded_dir_ids: HashSet<ProjectEntryId>,
93 // Currently selected leaf entry (see auto-folding for a definition of that) in a file tree
94 selection: Option<SelectedEntry>,
95 marked_entries: BTreeSet<SelectedEntry>,
96 context_menu: Option<(Entity<ContextMenu>, Point<Pixels>, Subscription)>,
97 edit_state: Option<EditState>,
98 filename_editor: Entity<Editor>,
99 clipboard: Option<ClipboardEntry>,
100 _dragged_entry_destination: Option<Arc<Path>>,
101 workspace: WeakEntity<Workspace>,
102 width: Option<Pixels>,
103 pending_serialization: Task<Option<()>>,
104 show_scrollbar: bool,
105 vertical_scrollbar_state: ScrollbarState,
106 horizontal_scrollbar_state: ScrollbarState,
107 hide_scrollbar_task: Option<Task<()>>,
108 diagnostics: HashMap<(WorktreeId, PathBuf), DiagnosticSeverity>,
109 max_width_item_index: Option<usize>,
110 // We keep track of the mouse down state on entries so we don't flash the UI
111 // in case a user clicks to open a file.
112 mouse_down: bool,
113 hover_expand_task: Option<Task<()>>,
114 previous_drag_position: Option<Point<Pixels>>,
115}
116
117struct DragTargetEntry {
118 /// The entry currently under the mouse cursor during a drag operation
119 entry_id: ProjectEntryId,
120 /// Highlight this entry along with all of its children
121 highlight_entry_id: Option<ProjectEntryId>,
122}
123
124#[derive(Copy, Clone, Debug)]
125struct FoldedDirectoryDragTarget {
126 entry_id: ProjectEntryId,
127 index: usize,
128 /// Whether we are dragging over the delimiter rather than the component itself.
129 is_delimiter_target: bool,
130}
131
132#[derive(Clone, Debug)]
133enum ValidationState {
134 None,
135 Warning(String),
136 Error(String),
137}
138
139#[derive(Clone, Debug)]
140struct EditState {
141 worktree_id: WorktreeId,
142 entry_id: ProjectEntryId,
143 leaf_entry_id: Option<ProjectEntryId>,
144 is_dir: bool,
145 depth: usize,
146 processing_filename: Option<String>,
147 previously_focused: Option<SelectedEntry>,
148 validation_state: ValidationState,
149}
150
151impl EditState {
152 fn is_new_entry(&self) -> bool {
153 self.leaf_entry_id.is_none()
154 }
155}
156
157#[derive(Clone, Debug)]
158enum ClipboardEntry {
159 Copied(BTreeSet<SelectedEntry>),
160 Cut(BTreeSet<SelectedEntry>),
161}
162
163#[derive(Debug, PartialEq, Eq, Clone)]
164struct EntryDetails {
165 filename: String,
166 icon: Option<SharedString>,
167 path: Arc<Path>,
168 depth: usize,
169 kind: EntryKind,
170 is_ignored: bool,
171 is_expanded: bool,
172 is_selected: bool,
173 is_marked: bool,
174 is_editing: bool,
175 is_processing: bool,
176 is_cut: bool,
177 sticky: Option<StickyDetails>,
178 filename_text_color: Color,
179 diagnostic_severity: Option<DiagnosticSeverity>,
180 git_status: GitSummary,
181 is_private: bool,
182 worktree_id: WorktreeId,
183 canonical_path: Option<Arc<Path>>,
184}
185
186#[derive(Debug, PartialEq, Eq, Clone)]
187struct StickyDetails {
188 sticky_index: usize,
189 is_last: bool,
190}
191
192/// Permanently deletes the selected file or directory.
193#[derive(PartialEq, Clone, Default, Debug, Deserialize, JsonSchema, Action)]
194#[action(namespace = project_panel)]
195#[serde(deny_unknown_fields)]
196struct Delete {
197 #[serde(default)]
198 pub skip_prompt: bool,
199}
200
201/// Moves the selected file or directory to the system trash.
202#[derive(PartialEq, Clone, Default, Debug, Deserialize, JsonSchema, Action)]
203#[action(namespace = project_panel)]
204#[serde(deny_unknown_fields)]
205struct Trash {
206 #[serde(default)]
207 pub skip_prompt: bool,
208}
209
210actions!(
211 project_panel,
212 [
213 /// Expands the selected entry in the project tree.
214 ExpandSelectedEntry,
215 /// Collapses the selected entry in the project tree.
216 CollapseSelectedEntry,
217 /// Collapses all entries in the project tree.
218 CollapseAllEntries,
219 /// Creates a new directory.
220 NewDirectory,
221 /// Creates a new file.
222 NewFile,
223 /// Copies the selected file or directory.
224 Copy,
225 /// Duplicates the selected file or directory.
226 Duplicate,
227 /// Reveals the selected item in the system file manager.
228 RevealInFileManager,
229 /// Removes the selected folder from the project.
230 RemoveFromProject,
231 /// Opens the selected file with the system's default application.
232 OpenWithSystem,
233 /// Cuts the selected file or directory.
234 Cut,
235 /// Pastes the previously cut or copied item.
236 Paste,
237 /// Renames the selected file or directory.
238 Rename,
239 /// Opens the selected file in the editor.
240 Open,
241 /// Opens the selected file in a permanent tab.
242 OpenPermanent,
243 /// Toggles focus on the project panel.
244 ToggleFocus,
245 /// Toggles visibility of git-ignored files.
246 ToggleHideGitIgnore,
247 /// Starts a new search in the selected directory.
248 NewSearchInDirectory,
249 /// Unfolds the selected directory.
250 UnfoldDirectory,
251 /// Folds the selected directory.
252 FoldDirectory,
253 /// Selects the parent directory.
254 SelectParent,
255 /// Selects the next entry with git changes.
256 SelectNextGitEntry,
257 /// Selects the previous entry with git changes.
258 SelectPrevGitEntry,
259 /// Selects the next entry with diagnostics.
260 SelectNextDiagnostic,
261 /// Selects the previous entry with diagnostics.
262 SelectPrevDiagnostic,
263 /// Selects the next directory.
264 SelectNextDirectory,
265 /// Selects the previous directory.
266 SelectPrevDirectory,
267 ]
268);
269
270#[derive(Debug, Default)]
271struct FoldedAncestors {
272 current_ancestor_depth: usize,
273 ancestors: Vec<ProjectEntryId>,
274}
275
276impl FoldedAncestors {
277 fn max_ancestor_depth(&self) -> usize {
278 self.ancestors.len()
279 }
280}
281
282pub fn init_settings(cx: &mut App) {
283 ProjectPanelSettings::register(cx);
284}
285
286pub fn init(cx: &mut App) {
287 init_settings(cx);
288
289 cx.observe_new(|workspace: &mut Workspace, _, _| {
290 workspace.register_action(|workspace, _: &ToggleFocus, window, cx| {
291 workspace.toggle_panel_focus::<ProjectPanel>(window, cx);
292 });
293
294 workspace.register_action(|workspace, _: &ToggleHideGitIgnore, _, cx| {
295 let fs = workspace.app_state().fs.clone();
296 update_settings_file::<ProjectPanelSettings>(fs, cx, move |setting, _| {
297 setting.hide_gitignore = Some(!setting.hide_gitignore.unwrap_or(false));
298 })
299 });
300
301 workspace.register_action(|workspace, action: &CollapseAllEntries, window, cx| {
302 if let Some(panel) = workspace.panel::<ProjectPanel>(cx) {
303 panel.update(cx, |panel, cx| {
304 panel.collapse_all_entries(action, window, cx);
305 });
306 }
307 });
308 })
309 .detach();
310}
311
312#[derive(Debug)]
313pub enum Event {
314 OpenedEntry {
315 entry_id: ProjectEntryId,
316 focus_opened_item: bool,
317 allow_preview: bool,
318 },
319 SplitEntry {
320 entry_id: ProjectEntryId,
321 },
322 Focus,
323}
324
325#[derive(Serialize, Deserialize)]
326struct SerializedProjectPanel {
327 width: Option<Pixels>,
328}
329
330struct DraggedProjectEntryView {
331 selection: SelectedEntry,
332 details: EntryDetails,
333 click_offset: Point<Pixels>,
334 selections: Arc<BTreeSet<SelectedEntry>>,
335}
336
337struct ItemColors {
338 default: Hsla,
339 hover: Hsla,
340 drag_over: Hsla,
341 marked: Hsla,
342 focused: Hsla,
343}
344
345fn get_item_color(cx: &App) -> ItemColors {
346 let colors = cx.theme().colors();
347
348 ItemColors {
349 default: colors.panel_background,
350 hover: colors.element_hover,
351 marked: colors.element_selected,
352 focused: colors.panel_focused_border,
353 drag_over: colors.drop_target_background,
354 }
355}
356
357impl ProjectPanel {
358 fn new(
359 workspace: &mut Workspace,
360 window: &mut Window,
361 cx: &mut Context<Workspace>,
362 ) -> Entity<Self> {
363 let project = workspace.project().clone();
364 let git_store = project.read(cx).git_store().clone();
365 let project_panel = cx.new(|cx| {
366 let focus_handle = cx.focus_handle();
367 cx.on_focus(&focus_handle, window, Self::focus_in).detach();
368 cx.on_focus_out(&focus_handle, window, |this, _, window, cx| {
369 this.focus_out(window, cx);
370 this.hide_scrollbar(window, cx);
371 })
372 .detach();
373
374 cx.subscribe(&git_store, |this, _, event, cx| match event {
375 GitStoreEvent::RepositoryUpdated(_, _, _)
376 | GitStoreEvent::RepositoryAdded(_)
377 | GitStoreEvent::RepositoryRemoved(_) => {
378 this.update_visible_entries(None, cx);
379 cx.notify();
380 }
381 _ => {}
382 })
383 .detach();
384
385 cx.subscribe(&project, |this, project, event, cx| match event {
386 project::Event::ActiveEntryChanged(Some(entry_id)) => {
387 if ProjectPanelSettings::get_global(cx).auto_reveal_entries {
388 this.reveal_entry(project.clone(), *entry_id, true, cx).ok();
389 }
390 }
391 project::Event::ActiveEntryChanged(None) => {
392 this.marked_entries.clear();
393 }
394 project::Event::RevealInProjectPanel(entry_id) => {
395 if let Some(()) = this
396 .reveal_entry(project.clone(), *entry_id, false, cx)
397 .log_err()
398 {
399 cx.emit(PanelEvent::Activate);
400 }
401 }
402 project::Event::ActivateProjectPanel => {
403 cx.emit(PanelEvent::Activate);
404 }
405 project::Event::DiskBasedDiagnosticsFinished { .. }
406 | project::Event::DiagnosticsUpdated { .. } => {
407 if ProjectPanelSettings::get_global(cx).show_diagnostics != ShowDiagnostics::Off
408 {
409 this.update_diagnostics(cx);
410 cx.notify();
411 }
412 }
413 project::Event::WorktreeRemoved(id) => {
414 this.expanded_dir_ids.remove(id);
415 this.update_visible_entries(None, cx);
416 cx.notify();
417 }
418 project::Event::WorktreeUpdatedEntries(_, _)
419 | project::Event::WorktreeAdded(_)
420 | project::Event::WorktreeOrderChanged => {
421 this.update_visible_entries(None, cx);
422 cx.notify();
423 }
424 project::Event::ExpandedAllForEntry(worktree_id, entry_id) => {
425 if let Some((worktree, expanded_dir_ids)) = project
426 .read(cx)
427 .worktree_for_id(*worktree_id, cx)
428 .zip(this.expanded_dir_ids.get_mut(&worktree_id))
429 {
430 let worktree = worktree.read(cx);
431
432 let Some(entry) = worktree.entry_for_id(*entry_id) else {
433 return;
434 };
435 let include_ignored_dirs = !entry.is_ignored;
436
437 let mut dirs_to_expand = vec![*entry_id];
438 while let Some(current_id) = dirs_to_expand.pop() {
439 let Some(current_entry) = worktree.entry_for_id(current_id) else {
440 continue;
441 };
442 for child in worktree.child_entries(¤t_entry.path) {
443 if !child.is_dir() || (include_ignored_dirs && child.is_ignored) {
444 continue;
445 }
446
447 dirs_to_expand.push(child.id);
448
449 if let Err(ix) = expanded_dir_ids.binary_search(&child.id) {
450 expanded_dir_ids.insert(ix, child.id);
451 }
452 this.unfolded_dir_ids.insert(child.id);
453 }
454 }
455 this.update_visible_entries(None, cx);
456 cx.notify();
457 }
458 }
459 _ => {}
460 })
461 .detach();
462
463 let trash_action = [TypeId::of::<Trash>()];
464 let is_remote = project.read(cx).is_via_collab();
465
466 if is_remote {
467 CommandPaletteFilter::update_global(cx, |filter, _cx| {
468 filter.hide_action_types(&trash_action);
469 });
470 }
471
472 let filename_editor = cx.new(|cx| Editor::single_line(window, cx));
473
474 cx.subscribe(
475 &filename_editor,
476 |project_panel, _, editor_event, cx| match editor_event {
477 EditorEvent::BufferEdited => {
478 project_panel.populate_validation_error(cx);
479 project_panel.autoscroll(cx);
480 }
481 EditorEvent::SelectionsChanged { .. } => {
482 project_panel.autoscroll(cx);
483 }
484 EditorEvent::Blurred => {
485 if project_panel
486 .edit_state
487 .as_ref()
488 .map_or(false, |state| state.processing_filename.is_none())
489 {
490 project_panel.edit_state = None;
491 project_panel.update_visible_entries(None, cx);
492 cx.notify();
493 }
494 }
495 _ => {}
496 },
497 )
498 .detach();
499
500 cx.observe_global::<FileIcons>(|_, cx| {
501 cx.notify();
502 })
503 .detach();
504
505 let mut project_panel_settings = *ProjectPanelSettings::get_global(cx);
506 cx.observe_global::<SettingsStore>(move |this, cx| {
507 let new_settings = *ProjectPanelSettings::get_global(cx);
508 if project_panel_settings != new_settings {
509 if project_panel_settings.hide_gitignore != new_settings.hide_gitignore {
510 this.update_visible_entries(None, cx);
511 }
512 if project_panel_settings.hide_root != new_settings.hide_root {
513 this.update_visible_entries(None, cx);
514 }
515 project_panel_settings = new_settings;
516 this.update_diagnostics(cx);
517 cx.notify();
518 }
519 })
520 .detach();
521
522 let scroll_handle = UniformListScrollHandle::new();
523 let mut this = Self {
524 project: project.clone(),
525 hover_scroll_task: None,
526 fs: workspace.app_state().fs.clone(),
527 focus_handle,
528 visible_entries: Default::default(),
529 ancestors: Default::default(),
530 folded_directory_drag_target: None,
531 drag_target_entry: None,
532 last_worktree_root_id: Default::default(),
533 expanded_dir_ids: Default::default(),
534 unfolded_dir_ids: Default::default(),
535 selection: None,
536 marked_entries: Default::default(),
537 edit_state: None,
538 context_menu: None,
539 filename_editor,
540 clipboard: None,
541 _dragged_entry_destination: None,
542 workspace: workspace.weak_handle(),
543 width: None,
544 pending_serialization: Task::ready(None),
545 show_scrollbar: !Self::should_autohide_scrollbar(cx),
546 hide_scrollbar_task: None,
547 vertical_scrollbar_state: ScrollbarState::new(scroll_handle.clone())
548 .parent_entity(&cx.entity()),
549 horizontal_scrollbar_state: ScrollbarState::new(scroll_handle.clone())
550 .parent_entity(&cx.entity()),
551 max_width_item_index: None,
552 diagnostics: Default::default(),
553 scroll_handle,
554 mouse_down: false,
555 hover_expand_task: None,
556 previous_drag_position: None,
557 };
558 this.update_visible_entries(None, cx);
559
560 this
561 });
562
563 cx.subscribe_in(&project_panel, window, {
564 let project_panel = project_panel.downgrade();
565 move |workspace, _, event, window, cx| match event {
566 &Event::OpenedEntry {
567 entry_id,
568 focus_opened_item,
569 allow_preview,
570 } => {
571 if let Some(worktree) = project.read(cx).worktree_for_entry(entry_id, cx) {
572 if let Some(entry) = worktree.read(cx).entry_for_id(entry_id) {
573 let file_path = entry.path.clone();
574 let worktree_id = worktree.read(cx).id();
575 let entry_id = entry.id;
576 let is_via_ssh = project.read(cx).is_via_ssh();
577
578 workspace
579 .open_path_preview(
580 ProjectPath {
581 worktree_id,
582 path: file_path.clone(),
583 },
584 None,
585 focus_opened_item,
586 allow_preview,
587 true,
588 window, cx,
589 )
590 .detach_and_prompt_err("Failed to open file", window, cx, move |e, _, _| {
591 match e.error_code() {
592 ErrorCode::Disconnected => if is_via_ssh {
593 Some("Disconnected from SSH host".to_string())
594 } else {
595 Some("Disconnected from remote project".to_string())
596 },
597 ErrorCode::UnsharedItem => Some(format!(
598 "{} is not shared by the host. This could be because it has been marked as `private`",
599 file_path.display()
600 )),
601 // See note in worktree.rs where this error originates. Returning Some in this case prevents
602 // the error popup from saying "Try Again", which is a red herring in this case
603 ErrorCode::Internal if e.to_string().contains("File is too large to load") => Some(e.to_string()),
604 _ => None,
605 }
606 });
607
608 if let Some(project_panel) = project_panel.upgrade() {
609 // Always select and mark the entry, regardless of whether it is opened or not.
610 project_panel.update(cx, |project_panel, _| {
611 let entry = SelectedEntry { worktree_id, entry_id };
612 project_panel.marked_entries.clear();
613 project_panel.marked_entries.insert(entry);
614 project_panel.selection = Some(entry);
615 });
616 if !focus_opened_item {
617 let focus_handle = project_panel.read(cx).focus_handle.clone();
618 window.focus(&focus_handle);
619 }
620 }
621 }
622 }
623 }
624 &Event::SplitEntry { entry_id } => {
625 if let Some(worktree) = project.read(cx).worktree_for_entry(entry_id, cx) {
626 if let Some(entry) = worktree.read(cx).entry_for_id(entry_id) {
627 workspace
628 .split_path(
629 ProjectPath {
630 worktree_id: worktree.read(cx).id(),
631 path: entry.path.clone(),
632 },
633 window, cx,
634 )
635 .detach_and_log_err(cx);
636 }
637 }
638 }
639
640 _ => {}
641 }
642 })
643 .detach();
644
645 project_panel
646 }
647
648 pub async fn load(
649 workspace: WeakEntity<Workspace>,
650 mut cx: AsyncWindowContext,
651 ) -> Result<Entity<Self>> {
652 let serialized_panel = match workspace
653 .read_with(&cx, |workspace, _| {
654 ProjectPanel::serialization_key(workspace)
655 })
656 .ok()
657 .flatten()
658 {
659 Some(serialization_key) => cx
660 .background_spawn(async move { KEY_VALUE_STORE.read_kvp(&serialization_key) })
661 .await
662 .context("loading project panel")
663 .log_err()
664 .flatten()
665 .map(|panel| serde_json::from_str::<SerializedProjectPanel>(&panel))
666 .transpose()
667 .log_err()
668 .flatten(),
669 None => None,
670 };
671
672 workspace.update_in(&mut cx, |workspace, window, cx| {
673 let panel = ProjectPanel::new(workspace, window, cx);
674 if let Some(serialized_panel) = serialized_panel {
675 panel.update(cx, |panel, cx| {
676 panel.width = serialized_panel.width.map(|px| px.round());
677 cx.notify();
678 });
679 }
680 panel
681 })
682 }
683
684 fn update_diagnostics(&mut self, cx: &mut Context<Self>) {
685 let mut diagnostics: HashMap<(WorktreeId, PathBuf), DiagnosticSeverity> =
686 Default::default();
687 let show_diagnostics_setting = ProjectPanelSettings::get_global(cx).show_diagnostics;
688
689 if show_diagnostics_setting != ShowDiagnostics::Off {
690 self.project
691 .read(cx)
692 .diagnostic_summaries(false, cx)
693 .filter_map(|(path, _, diagnostic_summary)| {
694 if diagnostic_summary.error_count > 0 {
695 Some((path, DiagnosticSeverity::ERROR))
696 } else if show_diagnostics_setting == ShowDiagnostics::All
697 && diagnostic_summary.warning_count > 0
698 {
699 Some((path, DiagnosticSeverity::WARNING))
700 } else {
701 None
702 }
703 })
704 .for_each(|(project_path, diagnostic_severity)| {
705 let mut path_buffer = PathBuf::new();
706 Self::update_strongest_diagnostic_severity(
707 &mut diagnostics,
708 &project_path,
709 path_buffer.clone(),
710 diagnostic_severity,
711 );
712
713 for component in project_path.path.components() {
714 path_buffer.push(component);
715 Self::update_strongest_diagnostic_severity(
716 &mut diagnostics,
717 &project_path,
718 path_buffer.clone(),
719 diagnostic_severity,
720 );
721 }
722 });
723 }
724 self.diagnostics = diagnostics;
725 }
726
727 fn update_strongest_diagnostic_severity(
728 diagnostics: &mut HashMap<(WorktreeId, PathBuf), DiagnosticSeverity>,
729 project_path: &ProjectPath,
730 path_buffer: PathBuf,
731 diagnostic_severity: DiagnosticSeverity,
732 ) {
733 diagnostics
734 .entry((project_path.worktree_id, path_buffer.clone()))
735 .and_modify(|strongest_diagnostic_severity| {
736 *strongest_diagnostic_severity =
737 cmp::min(*strongest_diagnostic_severity, diagnostic_severity);
738 })
739 .or_insert(diagnostic_severity);
740 }
741
742 fn serialization_key(workspace: &Workspace) -> Option<String> {
743 workspace
744 .database_id()
745 .map(|id| i64::from(id).to_string())
746 .or(workspace.session_id())
747 .map(|id| format!("{}-{:?}", PROJECT_PANEL_KEY, id))
748 }
749
750 fn serialize(&mut self, cx: &mut Context<Self>) {
751 let Some(serialization_key) = self
752 .workspace
753 .read_with(cx, |workspace, _| {
754 ProjectPanel::serialization_key(workspace)
755 })
756 .ok()
757 .flatten()
758 else {
759 return;
760 };
761 let width = self.width;
762 self.pending_serialization = cx.background_spawn(
763 async move {
764 KEY_VALUE_STORE
765 .write_kvp(
766 serialization_key,
767 serde_json::to_string(&SerializedProjectPanel { width })?,
768 )
769 .await?;
770 anyhow::Ok(())
771 }
772 .log_err(),
773 );
774 }
775
776 fn focus_in(&mut self, window: &mut Window, cx: &mut Context<Self>) {
777 if !self.focus_handle.contains_focused(window, cx) {
778 cx.emit(Event::Focus);
779 }
780 }
781
782 fn focus_out(&mut self, window: &mut Window, cx: &mut Context<Self>) {
783 if !self.focus_handle.is_focused(window) {
784 self.confirm(&Confirm, window, cx);
785 }
786 }
787
788 fn deploy_context_menu(
789 &mut self,
790 position: Point<Pixels>,
791 entry_id: ProjectEntryId,
792 window: &mut Window,
793 cx: &mut Context<Self>,
794 ) {
795 let project = self.project.read(cx);
796
797 let worktree_id = if let Some(id) = project.worktree_id_for_entry(entry_id, cx) {
798 id
799 } else {
800 return;
801 };
802
803 self.selection = Some(SelectedEntry {
804 worktree_id,
805 entry_id,
806 });
807
808 if let Some((worktree, entry)) = self.selected_sub_entry(cx) {
809 let auto_fold_dirs = ProjectPanelSettings::get_global(cx).auto_fold_dirs;
810 let worktree = worktree.read(cx);
811 let is_root = Some(entry) == worktree.root_entry();
812 let is_dir = entry.is_dir();
813 let is_foldable = auto_fold_dirs && self.is_foldable(entry, worktree);
814 let is_unfoldable = auto_fold_dirs && self.is_unfoldable(entry, worktree);
815 let is_read_only = project.is_read_only(cx);
816 let is_remote = project.is_via_collab();
817 let is_local = project.is_local();
818
819 let settings = ProjectPanelSettings::get_global(cx);
820 let visible_worktrees_count = project.visible_worktrees(cx).count();
821 let should_hide_rename = is_root
822 && (cfg!(target_os = "windows")
823 || (settings.hide_root && visible_worktrees_count == 1));
824
825 let context_menu = ContextMenu::build(window, cx, |menu, _, _| {
826 menu.context(self.focus_handle.clone()).map(|menu| {
827 if is_read_only {
828 menu.when(is_dir, |menu| {
829 menu.action("Search Inside", Box::new(NewSearchInDirectory))
830 })
831 } else {
832 menu.action("New File", Box::new(NewFile))
833 .action("New Folder", Box::new(NewDirectory))
834 .separator()
835 .when(is_local && cfg!(target_os = "macos"), |menu| {
836 menu.action("Reveal in Finder", Box::new(RevealInFileManager))
837 })
838 .when(is_local && cfg!(not(target_os = "macos")), |menu| {
839 menu.action("Reveal in File Manager", Box::new(RevealInFileManager))
840 })
841 .when(is_local, |menu| {
842 menu.action("Open in Default App", Box::new(OpenWithSystem))
843 })
844 .action("Open in Terminal", Box::new(OpenInTerminal))
845 .when(is_dir, |menu| {
846 menu.separator()
847 .action("Find in Folder…", Box::new(NewSearchInDirectory))
848 })
849 .when(is_unfoldable, |menu| {
850 menu.action("Unfold Directory", Box::new(UnfoldDirectory))
851 })
852 .when(is_foldable, |menu| {
853 menu.action("Fold Directory", Box::new(FoldDirectory))
854 })
855 .separator()
856 .action("Cut", Box::new(Cut))
857 .action("Copy", Box::new(Copy))
858 .action("Duplicate", Box::new(Duplicate))
859 // TODO: Paste should always be visible, cbut disabled when clipboard is empty
860 .action_disabled_when(
861 self.clipboard.as_ref().is_none(),
862 "Paste",
863 Box::new(Paste),
864 )
865 .separator()
866 .action("Copy Path", Box::new(zed_actions::workspace::CopyPath))
867 .action(
868 "Copy Relative Path",
869 Box::new(zed_actions::workspace::CopyRelativePath),
870 )
871 .separator()
872 .when(!should_hide_rename, |menu| {
873 menu.action("Rename", Box::new(Rename))
874 })
875 .when(!is_root & !is_remote, |menu| {
876 menu.action("Trash", Box::new(Trash { skip_prompt: false }))
877 })
878 .when(!is_root, |menu| {
879 menu.action("Delete", Box::new(Delete { skip_prompt: false }))
880 })
881 .when(!is_remote & is_root, |menu| {
882 menu.separator()
883 .action(
884 "Add Folder to Project…",
885 Box::new(workspace::AddFolderToProject),
886 )
887 .action("Remove from Project", Box::new(RemoveFromProject))
888 })
889 .when(is_root, |menu| {
890 menu.separator()
891 .action("Collapse All", Box::new(CollapseAllEntries))
892 })
893 }
894 })
895 });
896
897 window.focus(&context_menu.focus_handle(cx));
898 let subscription = cx.subscribe(&context_menu, |this, _, _: &DismissEvent, cx| {
899 this.context_menu.take();
900 cx.notify();
901 });
902 self.context_menu = Some((context_menu, position, subscription));
903 }
904
905 cx.notify();
906 }
907
908 fn is_unfoldable(&self, entry: &Entry, worktree: &Worktree) -> bool {
909 if !entry.is_dir() || self.unfolded_dir_ids.contains(&entry.id) {
910 return false;
911 }
912
913 if let Some(parent_path) = entry.path.parent() {
914 let snapshot = worktree.snapshot();
915 let mut child_entries = snapshot.child_entries(parent_path);
916 if let Some(child) = child_entries.next() {
917 if child_entries.next().is_none() {
918 return child.kind.is_dir();
919 }
920 }
921 };
922 false
923 }
924
925 fn is_foldable(&self, entry: &Entry, worktree: &Worktree) -> bool {
926 if entry.is_dir() {
927 let snapshot = worktree.snapshot();
928
929 let mut child_entries = snapshot.child_entries(&entry.path);
930 if let Some(child) = child_entries.next() {
931 if child_entries.next().is_none() {
932 return child.kind.is_dir();
933 }
934 }
935 }
936 false
937 }
938
939 fn expand_selected_entry(
940 &mut self,
941 _: &ExpandSelectedEntry,
942 window: &mut Window,
943 cx: &mut Context<Self>,
944 ) {
945 if let Some((worktree, entry)) = self.selected_entry(cx) {
946 if let Some(folded_ancestors) = self.ancestors.get_mut(&entry.id) {
947 if folded_ancestors.current_ancestor_depth > 0 {
948 folded_ancestors.current_ancestor_depth -= 1;
949 cx.notify();
950 return;
951 }
952 }
953 if entry.is_dir() {
954 let worktree_id = worktree.id();
955 let entry_id = entry.id;
956 let expanded_dir_ids =
957 if let Some(expanded_dir_ids) = self.expanded_dir_ids.get_mut(&worktree_id) {
958 expanded_dir_ids
959 } else {
960 return;
961 };
962
963 match expanded_dir_ids.binary_search(&entry_id) {
964 Ok(_) => self.select_next(&SelectNext, window, cx),
965 Err(ix) => {
966 self.project.update(cx, |project, cx| {
967 project.expand_entry(worktree_id, entry_id, cx);
968 });
969
970 expanded_dir_ids.insert(ix, entry_id);
971 self.update_visible_entries(None, cx);
972 cx.notify();
973 }
974 }
975 }
976 }
977 }
978
979 fn collapse_selected_entry(
980 &mut self,
981 _: &CollapseSelectedEntry,
982 _: &mut Window,
983 cx: &mut Context<Self>,
984 ) {
985 let Some((worktree, entry)) = self.selected_entry_handle(cx) else {
986 return;
987 };
988 self.collapse_entry(entry.clone(), worktree, cx)
989 }
990
991 fn collapse_entry(&mut self, entry: Entry, worktree: Entity<Worktree>, cx: &mut Context<Self>) {
992 let worktree = worktree.read(cx);
993 if let Some(folded_ancestors) = self.ancestors.get_mut(&entry.id) {
994 if folded_ancestors.current_ancestor_depth + 1 < folded_ancestors.max_ancestor_depth() {
995 folded_ancestors.current_ancestor_depth += 1;
996 cx.notify();
997 return;
998 }
999 }
1000 let worktree_id = worktree.id();
1001 let expanded_dir_ids =
1002 if let Some(expanded_dir_ids) = self.expanded_dir_ids.get_mut(&worktree_id) {
1003 expanded_dir_ids
1004 } else {
1005 return;
1006 };
1007
1008 let mut entry = &entry;
1009 loop {
1010 let entry_id = entry.id;
1011 match expanded_dir_ids.binary_search(&entry_id) {
1012 Ok(ix) => {
1013 expanded_dir_ids.remove(ix);
1014 self.update_visible_entries(Some((worktree_id, entry_id)), cx);
1015 cx.notify();
1016 break;
1017 }
1018 Err(_) => {
1019 if let Some(parent_entry) =
1020 entry.path.parent().and_then(|p| worktree.entry_for_path(p))
1021 {
1022 entry = parent_entry;
1023 } else {
1024 break;
1025 }
1026 }
1027 }
1028 }
1029 }
1030
1031 pub fn collapse_all_entries(
1032 &mut self,
1033 _: &CollapseAllEntries,
1034 _: &mut Window,
1035 cx: &mut Context<Self>,
1036 ) {
1037 // By keeping entries for fully collapsed worktrees, we avoid expanding them within update_visible_entries
1038 // (which is it's default behavior when there's no entry for a worktree in expanded_dir_ids).
1039 self.expanded_dir_ids
1040 .retain(|_, expanded_entries| expanded_entries.is_empty());
1041 self.update_visible_entries(None, cx);
1042 cx.notify();
1043 }
1044
1045 fn toggle_expanded(
1046 &mut self,
1047 entry_id: ProjectEntryId,
1048 window: &mut Window,
1049 cx: &mut Context<Self>,
1050 ) {
1051 if let Some(worktree_id) = self.project.read(cx).worktree_id_for_entry(entry_id, cx) {
1052 if let Some(expanded_dir_ids) = self.expanded_dir_ids.get_mut(&worktree_id) {
1053 self.project.update(cx, |project, cx| {
1054 match expanded_dir_ids.binary_search(&entry_id) {
1055 Ok(ix) => {
1056 expanded_dir_ids.remove(ix);
1057 }
1058 Err(ix) => {
1059 project.expand_entry(worktree_id, entry_id, cx);
1060 expanded_dir_ids.insert(ix, entry_id);
1061 }
1062 }
1063 });
1064 self.update_visible_entries(Some((worktree_id, entry_id)), cx);
1065 window.focus(&self.focus_handle);
1066 cx.notify();
1067 }
1068 }
1069 }
1070
1071 fn toggle_expand_all(
1072 &mut self,
1073 entry_id: ProjectEntryId,
1074 window: &mut Window,
1075 cx: &mut Context<Self>,
1076 ) {
1077 if let Some(worktree_id) = self.project.read(cx).worktree_id_for_entry(entry_id, cx) {
1078 if let Some(expanded_dir_ids) = self.expanded_dir_ids.get_mut(&worktree_id) {
1079 match expanded_dir_ids.binary_search(&entry_id) {
1080 Ok(_ix) => {
1081 self.collapse_all_for_entry(worktree_id, entry_id, cx);
1082 }
1083 Err(_ix) => {
1084 self.expand_all_for_entry(worktree_id, entry_id, cx);
1085 }
1086 }
1087 self.update_visible_entries(Some((worktree_id, entry_id)), cx);
1088 window.focus(&self.focus_handle);
1089 cx.notify();
1090 }
1091 }
1092 }
1093
1094 fn expand_all_for_entry(
1095 &mut self,
1096 worktree_id: WorktreeId,
1097 entry_id: ProjectEntryId,
1098 cx: &mut Context<Self>,
1099 ) {
1100 self.project.update(cx, |project, cx| {
1101 if let Some((worktree, expanded_dir_ids)) = project
1102 .worktree_for_id(worktree_id, cx)
1103 .zip(self.expanded_dir_ids.get_mut(&worktree_id))
1104 {
1105 if let Some(task) = project.expand_all_for_entry(worktree_id, entry_id, cx) {
1106 task.detach();
1107 }
1108
1109 let worktree = worktree.read(cx);
1110
1111 if let Some(mut entry) = worktree.entry_for_id(entry_id) {
1112 loop {
1113 if let Err(ix) = expanded_dir_ids.binary_search(&entry.id) {
1114 expanded_dir_ids.insert(ix, entry.id);
1115 }
1116
1117 if let Some(parent_entry) =
1118 entry.path.parent().and_then(|p| worktree.entry_for_path(p))
1119 {
1120 entry = parent_entry;
1121 } else {
1122 break;
1123 }
1124 }
1125 }
1126 }
1127 });
1128 }
1129
1130 fn collapse_all_for_entry(
1131 &mut self,
1132 worktree_id: WorktreeId,
1133 entry_id: ProjectEntryId,
1134 cx: &mut Context<Self>,
1135 ) {
1136 self.project.update(cx, |project, cx| {
1137 if let Some((worktree, expanded_dir_ids)) = project
1138 .worktree_for_id(worktree_id, cx)
1139 .zip(self.expanded_dir_ids.get_mut(&worktree_id))
1140 {
1141 let worktree = worktree.read(cx);
1142 let mut dirs_to_collapse = vec![entry_id];
1143 let auto_fold_enabled = ProjectPanelSettings::get_global(cx).auto_fold_dirs;
1144 while let Some(current_id) = dirs_to_collapse.pop() {
1145 let Some(current_entry) = worktree.entry_for_id(current_id) else {
1146 continue;
1147 };
1148 if let Ok(ix) = expanded_dir_ids.binary_search(¤t_id) {
1149 expanded_dir_ids.remove(ix);
1150 }
1151 if auto_fold_enabled {
1152 self.unfolded_dir_ids.remove(¤t_id);
1153 }
1154 for child in worktree.child_entries(¤t_entry.path) {
1155 if child.is_dir() {
1156 dirs_to_collapse.push(child.id);
1157 }
1158 }
1159 }
1160 }
1161 });
1162 }
1163
1164 fn select_previous(&mut self, _: &SelectPrevious, window: &mut Window, cx: &mut Context<Self>) {
1165 if let Some(edit_state) = &self.edit_state {
1166 if edit_state.processing_filename.is_none() {
1167 self.filename_editor.update(cx, |editor, cx| {
1168 editor.move_to_beginning_of_line(
1169 &editor::actions::MoveToBeginningOfLine {
1170 stop_at_soft_wraps: false,
1171 stop_at_indent: false,
1172 },
1173 window,
1174 cx,
1175 );
1176 });
1177 return;
1178 }
1179 }
1180 if let Some(selection) = self.selection {
1181 let (mut worktree_ix, mut entry_ix, _) =
1182 self.index_for_selection(selection).unwrap_or_default();
1183 if entry_ix > 0 {
1184 entry_ix -= 1;
1185 } else if worktree_ix > 0 {
1186 worktree_ix -= 1;
1187 entry_ix = self.visible_entries[worktree_ix].1.len() - 1;
1188 } else {
1189 return;
1190 }
1191
1192 let (worktree_id, worktree_entries, _) = &self.visible_entries[worktree_ix];
1193 let selection = SelectedEntry {
1194 worktree_id: *worktree_id,
1195 entry_id: worktree_entries[entry_ix].id,
1196 };
1197 self.selection = Some(selection);
1198 if window.modifiers().shift {
1199 self.marked_entries.insert(selection);
1200 }
1201 self.autoscroll(cx);
1202 cx.notify();
1203 } else {
1204 self.select_first(&SelectFirst {}, window, cx);
1205 }
1206 }
1207
1208 fn confirm(&mut self, _: &Confirm, window: &mut Window, cx: &mut Context<Self>) {
1209 if let Some(task) = self.confirm_edit(window, cx) {
1210 task.detach_and_notify_err(window, cx);
1211 }
1212 }
1213
1214 fn open(&mut self, _: &Open, window: &mut Window, cx: &mut Context<Self>) {
1215 let preview_tabs_enabled = PreviewTabsSettings::get_global(cx).enabled;
1216 self.open_internal(true, !preview_tabs_enabled, window, cx);
1217 }
1218
1219 fn open_permanent(&mut self, _: &OpenPermanent, window: &mut Window, cx: &mut Context<Self>) {
1220 self.open_internal(false, true, window, cx);
1221 }
1222
1223 fn open_internal(
1224 &mut self,
1225 allow_preview: bool,
1226 focus_opened_item: bool,
1227 window: &mut Window,
1228 cx: &mut Context<Self>,
1229 ) {
1230 if let Some((_, entry)) = self.selected_entry(cx) {
1231 if entry.is_file() {
1232 self.open_entry(entry.id, focus_opened_item, allow_preview, cx);
1233 cx.notify();
1234 } else {
1235 self.toggle_expanded(entry.id, window, cx);
1236 }
1237 }
1238 }
1239
1240 fn populate_validation_error(&mut self, cx: &mut Context<Self>) {
1241 let edit_state = match self.edit_state.as_mut() {
1242 Some(state) => state,
1243 None => return,
1244 };
1245 let filename = self.filename_editor.read(cx).text(cx);
1246 if !filename.is_empty() {
1247 if let Some(worktree) = self
1248 .project
1249 .read(cx)
1250 .worktree_for_id(edit_state.worktree_id, cx)
1251 {
1252 if let Some(entry) = worktree.read(cx).entry_for_id(edit_state.entry_id) {
1253 let mut already_exists = false;
1254 if edit_state.is_new_entry() {
1255 let new_path = entry.path.join(filename.trim_start_matches('/'));
1256 if worktree
1257 .read(cx)
1258 .entry_for_path(new_path.as_path())
1259 .is_some()
1260 {
1261 already_exists = true;
1262 }
1263 } else {
1264 let new_path = if let Some(parent) = entry.path.clone().parent() {
1265 parent.join(&filename)
1266 } else {
1267 filename.clone().into()
1268 };
1269 if let Some(existing) = worktree.read(cx).entry_for_path(new_path.as_path())
1270 {
1271 if existing.id != entry.id {
1272 already_exists = true;
1273 }
1274 }
1275 };
1276 if already_exists {
1277 edit_state.validation_state = ValidationState::Error(format!(
1278 "File or directory '{}' already exists at location. Please choose a different name.",
1279 filename
1280 ));
1281 cx.notify();
1282 return;
1283 }
1284 }
1285 }
1286 let trimmed_filename = filename.trim();
1287 if trimmed_filename.is_empty() {
1288 edit_state.validation_state =
1289 ValidationState::Error("File or directory name cannot be empty.".to_string());
1290 cx.notify();
1291 return;
1292 }
1293 if trimmed_filename != filename {
1294 edit_state.validation_state = ValidationState::Warning(
1295 "File or directory name contains leading or trailing whitespace.".to_string(),
1296 );
1297 cx.notify();
1298 return;
1299 }
1300 }
1301 edit_state.validation_state = ValidationState::None;
1302 cx.notify();
1303 }
1304
1305 fn confirm_edit(
1306 &mut self,
1307 window: &mut Window,
1308 cx: &mut Context<Self>,
1309 ) -> Option<Task<Result<()>>> {
1310 let edit_state = self.edit_state.as_mut()?;
1311 let worktree_id = edit_state.worktree_id;
1312 let is_new_entry = edit_state.is_new_entry();
1313 let filename = self.filename_editor.read(cx).text(cx);
1314 if filename.trim().is_empty() {
1315 return None;
1316 }
1317 #[cfg(not(target_os = "windows"))]
1318 let filename_indicates_dir = filename.ends_with("/");
1319 // On Windows, path separator could be either `/` or `\`.
1320 #[cfg(target_os = "windows")]
1321 let filename_indicates_dir = filename.ends_with("/") || filename.ends_with("\\");
1322 edit_state.is_dir =
1323 edit_state.is_dir || (edit_state.is_new_entry() && filename_indicates_dir);
1324 let is_dir = edit_state.is_dir;
1325 let worktree = self.project.read(cx).worktree_for_id(worktree_id, cx)?;
1326 let entry = worktree.read(cx).entry_for_id(edit_state.entry_id)?.clone();
1327
1328 let edit_task;
1329 let edited_entry_id;
1330 if is_new_entry {
1331 self.selection = Some(SelectedEntry {
1332 worktree_id,
1333 entry_id: NEW_ENTRY_ID,
1334 });
1335 let new_path = entry.path.join(filename.trim_start_matches('/'));
1336 if worktree
1337 .read(cx)
1338 .entry_for_path(new_path.as_path())
1339 .is_some()
1340 {
1341 return None;
1342 }
1343
1344 edited_entry_id = NEW_ENTRY_ID;
1345 edit_task = self.project.update(cx, |project, cx| {
1346 project.create_entry((worktree_id, &new_path), is_dir, cx)
1347 });
1348 } else {
1349 let new_path = if let Some(parent) = entry.path.clone().parent() {
1350 parent.join(&filename)
1351 } else {
1352 filename.clone().into()
1353 };
1354 if let Some(existing) = worktree.read(cx).entry_for_path(new_path.as_path()) {
1355 if existing.id == entry.id {
1356 window.focus(&self.focus_handle);
1357 }
1358 return None;
1359 }
1360 edited_entry_id = entry.id;
1361 edit_task = self.project.update(cx, |project, cx| {
1362 project.rename_entry(entry.id, new_path.as_path(), cx)
1363 });
1364 };
1365
1366 window.focus(&self.focus_handle);
1367 edit_state.processing_filename = Some(filename);
1368 cx.notify();
1369
1370 Some(cx.spawn_in(window, async move |project_panel, cx| {
1371 let new_entry = edit_task.await;
1372 project_panel.update(cx, |project_panel, cx| {
1373 project_panel.edit_state = None;
1374 cx.notify();
1375 })?;
1376
1377 match new_entry {
1378 Err(e) => {
1379 project_panel.update( cx, |project_panel, cx| {
1380 project_panel.marked_entries.clear();
1381 project_panel.update_visible_entries(None, cx);
1382 }).ok();
1383 Err(e)?;
1384 }
1385 Ok(CreatedEntry::Included(new_entry)) => {
1386 project_panel.update( cx, |project_panel, cx| {
1387 if let Some(selection) = &mut project_panel.selection {
1388 if selection.entry_id == edited_entry_id {
1389 selection.worktree_id = worktree_id;
1390 selection.entry_id = new_entry.id;
1391 project_panel.marked_entries.clear();
1392 project_panel.expand_to_selection(cx);
1393 }
1394 }
1395 project_panel.update_visible_entries(None, cx);
1396 if is_new_entry && !is_dir {
1397 project_panel.open_entry(new_entry.id, true, false, cx);
1398 }
1399 cx.notify();
1400 })?;
1401 }
1402 Ok(CreatedEntry::Excluded { abs_path }) => {
1403 if let Some(open_task) = project_panel
1404 .update_in( cx, |project_panel, window, cx| {
1405 project_panel.marked_entries.clear();
1406 project_panel.update_visible_entries(None, cx);
1407
1408 if is_dir {
1409 project_panel.project.update(cx, |_, cx| {
1410 cx.emit(project::Event::Toast {
1411 notification_id: "excluded-directory".into(),
1412 message: format!("Created an excluded directory at {abs_path:?}.\nAlter `file_scan_exclusions` in the settings to show it in the panel")
1413 })
1414 });
1415 None
1416 } else {
1417 project_panel
1418 .workspace
1419 .update(cx, |workspace, cx| {
1420 workspace.open_abs_path(abs_path, OpenOptions { visible: Some(OpenVisible::All), ..Default::default() }, window, cx)
1421 })
1422 .ok()
1423 }
1424 })
1425 .ok()
1426 .flatten()
1427 {
1428 let _ = open_task.await?;
1429 }
1430 }
1431 }
1432 Ok(())
1433 }))
1434 }
1435
1436 fn cancel(&mut self, _: &menu::Cancel, window: &mut Window, cx: &mut Context<Self>) {
1437 if cx.stop_active_drag(window) {
1438 self.drag_target_entry.take();
1439 self.hover_expand_task.take();
1440 return;
1441 }
1442
1443 let previous_edit_state = self.edit_state.take();
1444 self.update_visible_entries(None, cx);
1445 self.marked_entries.clear();
1446
1447 if let Some(previously_focused) =
1448 previous_edit_state.and_then(|edit_state| edit_state.previously_focused)
1449 {
1450 self.selection = Some(previously_focused);
1451 self.autoscroll(cx);
1452 }
1453
1454 window.focus(&self.focus_handle);
1455 cx.notify();
1456 }
1457
1458 fn open_entry(
1459 &mut self,
1460 entry_id: ProjectEntryId,
1461 focus_opened_item: bool,
1462 allow_preview: bool,
1463
1464 cx: &mut Context<Self>,
1465 ) {
1466 cx.emit(Event::OpenedEntry {
1467 entry_id,
1468 focus_opened_item,
1469 allow_preview,
1470 });
1471 }
1472
1473 fn split_entry(&mut self, entry_id: ProjectEntryId, cx: &mut Context<Self>) {
1474 cx.emit(Event::SplitEntry { entry_id });
1475 }
1476
1477 fn new_file(&mut self, _: &NewFile, window: &mut Window, cx: &mut Context<Self>) {
1478 self.add_entry(false, window, cx)
1479 }
1480
1481 fn new_directory(&mut self, _: &NewDirectory, window: &mut Window, cx: &mut Context<Self>) {
1482 self.add_entry(true, window, cx)
1483 }
1484
1485 fn add_entry(&mut self, is_dir: bool, window: &mut Window, cx: &mut Context<Self>) {
1486 let Some((worktree_id, entry_id)) = self
1487 .selection
1488 .map(|entry| (entry.worktree_id, entry.entry_id))
1489 .or_else(|| {
1490 let entry_id = self.last_worktree_root_id?;
1491 let worktree_id = self
1492 .project
1493 .read(cx)
1494 .worktree_for_entry(entry_id, cx)?
1495 .read(cx)
1496 .id();
1497
1498 self.selection = Some(SelectedEntry {
1499 worktree_id,
1500 entry_id,
1501 });
1502
1503 Some((worktree_id, entry_id))
1504 })
1505 else {
1506 return;
1507 };
1508
1509 let directory_id;
1510 let new_entry_id = self.resolve_entry(entry_id);
1511 if let Some((worktree, expanded_dir_ids)) = self
1512 .project
1513 .read(cx)
1514 .worktree_for_id(worktree_id, cx)
1515 .zip(self.expanded_dir_ids.get_mut(&worktree_id))
1516 {
1517 let worktree = worktree.read(cx);
1518 if let Some(mut entry) = worktree.entry_for_id(new_entry_id) {
1519 loop {
1520 if entry.is_dir() {
1521 if let Err(ix) = expanded_dir_ids.binary_search(&entry.id) {
1522 expanded_dir_ids.insert(ix, entry.id);
1523 }
1524 directory_id = entry.id;
1525 break;
1526 } else {
1527 if let Some(parent_path) = entry.path.parent() {
1528 if let Some(parent_entry) = worktree.entry_for_path(parent_path) {
1529 entry = parent_entry;
1530 continue;
1531 }
1532 }
1533 return;
1534 }
1535 }
1536 } else {
1537 return;
1538 };
1539 } else {
1540 return;
1541 };
1542
1543 self.marked_entries.clear();
1544 self.edit_state = Some(EditState {
1545 worktree_id,
1546 entry_id: directory_id,
1547 leaf_entry_id: None,
1548 is_dir,
1549 processing_filename: None,
1550 previously_focused: self.selection,
1551 depth: 0,
1552 validation_state: ValidationState::None,
1553 });
1554 self.filename_editor.update(cx, |editor, cx| {
1555 editor.clear(window, cx);
1556 window.focus(&editor.focus_handle(cx));
1557 });
1558 self.update_visible_entries(Some((worktree_id, NEW_ENTRY_ID)), cx);
1559 self.autoscroll(cx);
1560 cx.notify();
1561 }
1562
1563 fn unflatten_entry_id(&self, leaf_entry_id: ProjectEntryId) -> ProjectEntryId {
1564 if let Some(ancestors) = self.ancestors.get(&leaf_entry_id) {
1565 ancestors
1566 .ancestors
1567 .get(ancestors.current_ancestor_depth)
1568 .copied()
1569 .unwrap_or(leaf_entry_id)
1570 } else {
1571 leaf_entry_id
1572 }
1573 }
1574
1575 fn rename_impl(
1576 &mut self,
1577 selection: Option<Range<usize>>,
1578 window: &mut Window,
1579 cx: &mut Context<Self>,
1580 ) {
1581 if let Some(SelectedEntry {
1582 worktree_id,
1583 entry_id,
1584 }) = self.selection
1585 {
1586 if let Some(worktree) = self.project.read(cx).worktree_for_id(worktree_id, cx) {
1587 let sub_entry_id = self.unflatten_entry_id(entry_id);
1588 if let Some(entry) = worktree.read(cx).entry_for_id(sub_entry_id) {
1589 #[cfg(target_os = "windows")]
1590 if Some(entry) == worktree.read(cx).root_entry() {
1591 return;
1592 }
1593
1594 if Some(entry) == worktree.read(cx).root_entry() {
1595 let settings = ProjectPanelSettings::get_global(cx);
1596 let visible_worktrees_count =
1597 self.project.read(cx).visible_worktrees(cx).count();
1598 if settings.hide_root && visible_worktrees_count == 1 {
1599 return;
1600 }
1601 }
1602
1603 self.edit_state = Some(EditState {
1604 worktree_id,
1605 entry_id: sub_entry_id,
1606 leaf_entry_id: Some(entry_id),
1607 is_dir: entry.is_dir(),
1608 processing_filename: None,
1609 previously_focused: None,
1610 depth: 0,
1611 validation_state: ValidationState::None,
1612 });
1613 let file_name = entry
1614 .path
1615 .file_name()
1616 .map(|s| s.to_string_lossy())
1617 .unwrap_or_default()
1618 .to_string();
1619 let selection = selection.unwrap_or_else(|| {
1620 let file_stem = entry.path.file_stem().map(|s| s.to_string_lossy());
1621 let selection_end =
1622 file_stem.map_or(file_name.len(), |file_stem| file_stem.len());
1623 0..selection_end
1624 });
1625 self.filename_editor.update(cx, |editor, cx| {
1626 editor.set_text(file_name, window, cx);
1627 editor.change_selections(Default::default(), window, cx, |s| {
1628 s.select_ranges([selection])
1629 });
1630 window.focus(&editor.focus_handle(cx));
1631 });
1632 self.update_visible_entries(None, cx);
1633 self.autoscroll(cx);
1634 cx.notify();
1635 }
1636 }
1637 }
1638 }
1639
1640 fn rename(&mut self, _: &Rename, window: &mut Window, cx: &mut Context<Self>) {
1641 self.rename_impl(None, window, cx);
1642 }
1643
1644 fn trash(&mut self, action: &Trash, window: &mut Window, cx: &mut Context<Self>) {
1645 self.remove(true, action.skip_prompt, window, cx);
1646 }
1647
1648 fn delete(&mut self, action: &Delete, window: &mut Window, cx: &mut Context<Self>) {
1649 self.remove(false, action.skip_prompt, window, cx);
1650 }
1651
1652 fn remove(
1653 &mut self,
1654 trash: bool,
1655 skip_prompt: bool,
1656 window: &mut Window,
1657 cx: &mut Context<ProjectPanel>,
1658 ) {
1659 maybe!({
1660 let items_to_delete = self.disjoint_entries(cx);
1661 if items_to_delete.is_empty() {
1662 return None;
1663 }
1664 let project = self.project.read(cx);
1665
1666 let mut dirty_buffers = 0;
1667 let file_paths = items_to_delete
1668 .iter()
1669 .filter_map(|selection| {
1670 let project_path = project.path_for_entry(selection.entry_id, cx)?;
1671 dirty_buffers +=
1672 project.dirty_buffers(cx).any(|path| path == project_path) as usize;
1673 Some((
1674 selection.entry_id,
1675 project_path
1676 .path
1677 .file_name()?
1678 .to_string_lossy()
1679 .into_owned(),
1680 ))
1681 })
1682 .collect::<Vec<_>>();
1683 if file_paths.is_empty() {
1684 return None;
1685 }
1686 let answer = if !skip_prompt {
1687 let operation = if trash { "Trash" } else { "Delete" };
1688 let prompt = match file_paths.first() {
1689 Some((_, path)) if file_paths.len() == 1 => {
1690 let unsaved_warning = if dirty_buffers > 0 {
1691 "\n\nIt has unsaved changes, which will be lost."
1692 } else {
1693 ""
1694 };
1695
1696 format!("{operation} {path}?{unsaved_warning}")
1697 }
1698 _ => {
1699 const CUTOFF_POINT: usize = 10;
1700 let names = if file_paths.len() > CUTOFF_POINT {
1701 let truncated_path_counts = file_paths.len() - CUTOFF_POINT;
1702 let mut paths = file_paths
1703 .iter()
1704 .map(|(_, path)| path.clone())
1705 .take(CUTOFF_POINT)
1706 .collect::<Vec<_>>();
1707 paths.truncate(CUTOFF_POINT);
1708 if truncated_path_counts == 1 {
1709 paths.push(".. 1 file not shown".into());
1710 } else {
1711 paths.push(format!(".. {} files not shown", truncated_path_counts));
1712 }
1713 paths
1714 } else {
1715 file_paths.iter().map(|(_, path)| path.clone()).collect()
1716 };
1717 let unsaved_warning = if dirty_buffers == 0 {
1718 String::new()
1719 } else if dirty_buffers == 1 {
1720 "\n\n1 of these has unsaved changes, which will be lost.".to_string()
1721 } else {
1722 format!(
1723 "\n\n{dirty_buffers} of these have unsaved changes, which will be lost."
1724 )
1725 };
1726
1727 format!(
1728 "Do you want to {} the following {} files?\n{}{unsaved_warning}",
1729 operation.to_lowercase(),
1730 file_paths.len(),
1731 names.join("\n")
1732 )
1733 }
1734 };
1735 Some(window.prompt(PromptLevel::Info, &prompt, None, &[operation, "Cancel"], cx))
1736 } else {
1737 None
1738 };
1739 let next_selection = self.find_next_selection_after_deletion(items_to_delete, cx);
1740 cx.spawn_in(window, async move |panel, cx| {
1741 if let Some(answer) = answer {
1742 if answer.await != Ok(0) {
1743 return anyhow::Ok(());
1744 }
1745 }
1746 for (entry_id, _) in file_paths {
1747 panel
1748 .update(cx, |panel, cx| {
1749 panel
1750 .project
1751 .update(cx, |project, cx| project.delete_entry(entry_id, trash, cx))
1752 .context("no such entry")
1753 })??
1754 .await?;
1755 }
1756 panel.update_in(cx, |panel, window, cx| {
1757 if let Some(next_selection) = next_selection {
1758 panel.selection = Some(next_selection);
1759 panel.autoscroll(cx);
1760 } else {
1761 panel.select_last(&SelectLast {}, window, cx);
1762 }
1763 })?;
1764 Ok(())
1765 })
1766 .detach_and_log_err(cx);
1767 Some(())
1768 });
1769 }
1770
1771 fn find_next_selection_after_deletion(
1772 &self,
1773 sanitized_entries: BTreeSet<SelectedEntry>,
1774 cx: &mut Context<Self>,
1775 ) -> Option<SelectedEntry> {
1776 if sanitized_entries.is_empty() {
1777 return None;
1778 }
1779 let project = self.project.read(cx);
1780 let (worktree_id, worktree) = sanitized_entries
1781 .iter()
1782 .map(|entry| entry.worktree_id)
1783 .filter_map(|id| project.worktree_for_id(id, cx).map(|w| (id, w.read(cx))))
1784 .max_by(|(_, a), (_, b)| a.root_name().cmp(b.root_name()))?;
1785 let git_store = project.git_store().read(cx);
1786
1787 let marked_entries_in_worktree = sanitized_entries
1788 .iter()
1789 .filter(|e| e.worktree_id == worktree_id)
1790 .collect::<HashSet<_>>();
1791 let latest_entry = marked_entries_in_worktree
1792 .iter()
1793 .max_by(|a, b| {
1794 match (
1795 worktree.entry_for_id(a.entry_id),
1796 worktree.entry_for_id(b.entry_id),
1797 ) {
1798 (Some(a), Some(b)) => {
1799 compare_paths((&a.path, a.is_file()), (&b.path, b.is_file()))
1800 }
1801 _ => cmp::Ordering::Equal,
1802 }
1803 })
1804 .and_then(|e| worktree.entry_for_id(e.entry_id))?;
1805
1806 let parent_path = latest_entry.path.parent()?;
1807 let parent_entry = worktree.entry_for_path(parent_path)?;
1808
1809 // Remove all siblings that are being deleted except the last marked entry
1810 let repo_snapshots = git_store.repo_snapshots(cx);
1811 let worktree_snapshot = worktree.snapshot();
1812 let hide_gitignore = ProjectPanelSettings::get_global(cx).hide_gitignore;
1813 let mut siblings: Vec<_> =
1814 ChildEntriesGitIter::new(&repo_snapshots, &worktree_snapshot, parent_path)
1815 .filter(|sibling| {
1816 (sibling.id == latest_entry.id)
1817 || (!marked_entries_in_worktree.contains(&&SelectedEntry {
1818 worktree_id,
1819 entry_id: sibling.id,
1820 }) && (!hide_gitignore || !sibling.is_ignored))
1821 })
1822 .map(|entry| entry.to_owned())
1823 .collect();
1824
1825 project::sort_worktree_entries(&mut siblings);
1826 let sibling_entry_index = siblings
1827 .iter()
1828 .position(|sibling| sibling.id == latest_entry.id)?;
1829
1830 if let Some(next_sibling) = sibling_entry_index
1831 .checked_add(1)
1832 .and_then(|i| siblings.get(i))
1833 {
1834 return Some(SelectedEntry {
1835 worktree_id,
1836 entry_id: next_sibling.id,
1837 });
1838 }
1839 if let Some(prev_sibling) = sibling_entry_index
1840 .checked_sub(1)
1841 .and_then(|i| siblings.get(i))
1842 {
1843 return Some(SelectedEntry {
1844 worktree_id,
1845 entry_id: prev_sibling.id,
1846 });
1847 }
1848 // No neighbour sibling found, fall back to parent
1849 Some(SelectedEntry {
1850 worktree_id,
1851 entry_id: parent_entry.id,
1852 })
1853 }
1854
1855 fn unfold_directory(&mut self, _: &UnfoldDirectory, _: &mut Window, cx: &mut Context<Self>) {
1856 if let Some((worktree, entry)) = self.selected_entry(cx) {
1857 self.unfolded_dir_ids.insert(entry.id);
1858
1859 let snapshot = worktree.snapshot();
1860 let mut parent_path = entry.path.parent();
1861 while let Some(path) = parent_path {
1862 if let Some(parent_entry) = worktree.entry_for_path(path) {
1863 let mut children_iter = snapshot.child_entries(path);
1864
1865 if children_iter.by_ref().take(2).count() > 1 {
1866 break;
1867 }
1868
1869 self.unfolded_dir_ids.insert(parent_entry.id);
1870 parent_path = path.parent();
1871 } else {
1872 break;
1873 }
1874 }
1875
1876 self.update_visible_entries(None, cx);
1877 self.autoscroll(cx);
1878 cx.notify();
1879 }
1880 }
1881
1882 fn fold_directory(&mut self, _: &FoldDirectory, _: &mut Window, cx: &mut Context<Self>) {
1883 if let Some((worktree, entry)) = self.selected_entry(cx) {
1884 self.unfolded_dir_ids.remove(&entry.id);
1885
1886 let snapshot = worktree.snapshot();
1887 let mut path = &*entry.path;
1888 loop {
1889 let mut child_entries_iter = snapshot.child_entries(path);
1890 if let Some(child) = child_entries_iter.next() {
1891 if child_entries_iter.next().is_none() && child.is_dir() {
1892 self.unfolded_dir_ids.remove(&child.id);
1893 path = &*child.path;
1894 } else {
1895 break;
1896 }
1897 } else {
1898 break;
1899 }
1900 }
1901
1902 self.update_visible_entries(None, cx);
1903 self.autoscroll(cx);
1904 cx.notify();
1905 }
1906 }
1907
1908 fn select_next(&mut self, _: &SelectNext, window: &mut Window, cx: &mut Context<Self>) {
1909 if let Some(edit_state) = &self.edit_state {
1910 if edit_state.processing_filename.is_none() {
1911 self.filename_editor.update(cx, |editor, cx| {
1912 editor.move_to_end_of_line(
1913 &editor::actions::MoveToEndOfLine {
1914 stop_at_soft_wraps: false,
1915 },
1916 window,
1917 cx,
1918 );
1919 });
1920 return;
1921 }
1922 }
1923 if let Some(selection) = self.selection {
1924 let (mut worktree_ix, mut entry_ix, _) =
1925 self.index_for_selection(selection).unwrap_or_default();
1926 if let Some((_, worktree_entries, _)) = self.visible_entries.get(worktree_ix) {
1927 if entry_ix + 1 < worktree_entries.len() {
1928 entry_ix += 1;
1929 } else {
1930 worktree_ix += 1;
1931 entry_ix = 0;
1932 }
1933 }
1934
1935 if let Some((worktree_id, worktree_entries, _)) = self.visible_entries.get(worktree_ix)
1936 {
1937 if let Some(entry) = worktree_entries.get(entry_ix) {
1938 let selection = SelectedEntry {
1939 worktree_id: *worktree_id,
1940 entry_id: entry.id,
1941 };
1942 self.selection = Some(selection);
1943 if window.modifiers().shift {
1944 self.marked_entries.insert(selection);
1945 }
1946
1947 self.autoscroll(cx);
1948 cx.notify();
1949 }
1950 }
1951 } else {
1952 self.select_first(&SelectFirst {}, window, cx);
1953 }
1954 }
1955
1956 fn select_prev_diagnostic(
1957 &mut self,
1958 _: &SelectPrevDiagnostic,
1959 _: &mut Window,
1960 cx: &mut Context<Self>,
1961 ) {
1962 let selection = self.find_entry(
1963 self.selection.as_ref(),
1964 true,
1965 |entry, worktree_id| {
1966 (self.selection.is_none()
1967 || self.selection.is_some_and(|selection| {
1968 if selection.worktree_id == worktree_id {
1969 selection.entry_id != entry.id
1970 } else {
1971 true
1972 }
1973 }))
1974 && entry.is_file()
1975 && self
1976 .diagnostics
1977 .contains_key(&(worktree_id, entry.path.to_path_buf()))
1978 },
1979 cx,
1980 );
1981
1982 if let Some(selection) = selection {
1983 self.selection = Some(selection);
1984 self.expand_entry(selection.worktree_id, selection.entry_id, cx);
1985 self.update_visible_entries(Some((selection.worktree_id, selection.entry_id)), cx);
1986 self.autoscroll(cx);
1987 cx.notify();
1988 }
1989 }
1990
1991 fn select_next_diagnostic(
1992 &mut self,
1993 _: &SelectNextDiagnostic,
1994 _: &mut Window,
1995 cx: &mut Context<Self>,
1996 ) {
1997 let selection = self.find_entry(
1998 self.selection.as_ref(),
1999 false,
2000 |entry, worktree_id| {
2001 (self.selection.is_none()
2002 || self.selection.is_some_and(|selection| {
2003 if selection.worktree_id == worktree_id {
2004 selection.entry_id != entry.id
2005 } else {
2006 true
2007 }
2008 }))
2009 && entry.is_file()
2010 && self
2011 .diagnostics
2012 .contains_key(&(worktree_id, entry.path.to_path_buf()))
2013 },
2014 cx,
2015 );
2016
2017 if let Some(selection) = selection {
2018 self.selection = Some(selection);
2019 self.expand_entry(selection.worktree_id, selection.entry_id, cx);
2020 self.update_visible_entries(Some((selection.worktree_id, selection.entry_id)), cx);
2021 self.autoscroll(cx);
2022 cx.notify();
2023 }
2024 }
2025
2026 fn select_prev_git_entry(
2027 &mut self,
2028 _: &SelectPrevGitEntry,
2029 _: &mut Window,
2030 cx: &mut Context<Self>,
2031 ) {
2032 let selection = self.find_entry(
2033 self.selection.as_ref(),
2034 true,
2035 |entry, worktree_id| {
2036 (self.selection.is_none()
2037 || self.selection.is_some_and(|selection| {
2038 if selection.worktree_id == worktree_id {
2039 selection.entry_id != entry.id
2040 } else {
2041 true
2042 }
2043 }))
2044 && entry.is_file()
2045 && entry.git_summary.index.modified + entry.git_summary.worktree.modified > 0
2046 },
2047 cx,
2048 );
2049
2050 if let Some(selection) = selection {
2051 self.selection = Some(selection);
2052 self.expand_entry(selection.worktree_id, selection.entry_id, cx);
2053 self.update_visible_entries(Some((selection.worktree_id, selection.entry_id)), cx);
2054 self.autoscroll(cx);
2055 cx.notify();
2056 }
2057 }
2058
2059 fn select_prev_directory(
2060 &mut self,
2061 _: &SelectPrevDirectory,
2062 _: &mut Window,
2063 cx: &mut Context<Self>,
2064 ) {
2065 let selection = self.find_visible_entry(
2066 self.selection.as_ref(),
2067 true,
2068 |entry, worktree_id| {
2069 (self.selection.is_none()
2070 || self.selection.is_some_and(|selection| {
2071 if selection.worktree_id == worktree_id {
2072 selection.entry_id != entry.id
2073 } else {
2074 true
2075 }
2076 }))
2077 && entry.is_dir()
2078 },
2079 cx,
2080 );
2081
2082 if let Some(selection) = selection {
2083 self.selection = Some(selection);
2084 self.autoscroll(cx);
2085 cx.notify();
2086 }
2087 }
2088
2089 fn select_next_directory(
2090 &mut self,
2091 _: &SelectNextDirectory,
2092 _: &mut Window,
2093 cx: &mut Context<Self>,
2094 ) {
2095 let selection = self.find_visible_entry(
2096 self.selection.as_ref(),
2097 false,
2098 |entry, worktree_id| {
2099 (self.selection.is_none()
2100 || self.selection.is_some_and(|selection| {
2101 if selection.worktree_id == worktree_id {
2102 selection.entry_id != entry.id
2103 } else {
2104 true
2105 }
2106 }))
2107 && entry.is_dir()
2108 },
2109 cx,
2110 );
2111
2112 if let Some(selection) = selection {
2113 self.selection = Some(selection);
2114 self.autoscroll(cx);
2115 cx.notify();
2116 }
2117 }
2118
2119 fn select_next_git_entry(
2120 &mut self,
2121 _: &SelectNextGitEntry,
2122 _: &mut Window,
2123 cx: &mut Context<Self>,
2124 ) {
2125 let selection = self.find_entry(
2126 self.selection.as_ref(),
2127 false,
2128 |entry, worktree_id| {
2129 (self.selection.is_none()
2130 || self.selection.is_some_and(|selection| {
2131 if selection.worktree_id == worktree_id {
2132 selection.entry_id != entry.id
2133 } else {
2134 true
2135 }
2136 }))
2137 && entry.is_file()
2138 && entry.git_summary.index.modified + entry.git_summary.worktree.modified > 0
2139 },
2140 cx,
2141 );
2142
2143 if let Some(selection) = selection {
2144 self.selection = Some(selection);
2145 self.expand_entry(selection.worktree_id, selection.entry_id, cx);
2146 self.update_visible_entries(Some((selection.worktree_id, selection.entry_id)), cx);
2147 self.autoscroll(cx);
2148 cx.notify();
2149 }
2150 }
2151
2152 fn select_parent(&mut self, _: &SelectParent, window: &mut Window, cx: &mut Context<Self>) {
2153 if let Some((worktree, entry)) = self.selected_sub_entry(cx) {
2154 if let Some(parent) = entry.path.parent() {
2155 let worktree = worktree.read(cx);
2156 if let Some(parent_entry) = worktree.entry_for_path(parent) {
2157 self.selection = Some(SelectedEntry {
2158 worktree_id: worktree.id(),
2159 entry_id: parent_entry.id,
2160 });
2161 self.autoscroll(cx);
2162 cx.notify();
2163 }
2164 }
2165 } else {
2166 self.select_first(&SelectFirst {}, window, cx);
2167 }
2168 }
2169
2170 fn select_first(&mut self, _: &SelectFirst, window: &mut Window, cx: &mut Context<Self>) {
2171 if let Some((worktree_id, visible_worktree_entries, _)) = self.visible_entries.first() {
2172 if let Some(entry) = visible_worktree_entries.first() {
2173 let selection = SelectedEntry {
2174 worktree_id: *worktree_id,
2175 entry_id: entry.id,
2176 };
2177 self.selection = Some(selection);
2178 if window.modifiers().shift {
2179 self.marked_entries.insert(selection);
2180 }
2181 self.autoscroll(cx);
2182 cx.notify();
2183 }
2184 }
2185 }
2186
2187 fn select_last(&mut self, _: &SelectLast, _: &mut Window, cx: &mut Context<Self>) {
2188 if let Some((worktree_id, visible_worktree_entries, _)) = self.visible_entries.last() {
2189 let worktree = self.project.read(cx).worktree_for_id(*worktree_id, cx);
2190 if let (Some(worktree), Some(entry)) = (worktree, visible_worktree_entries.last()) {
2191 let worktree = worktree.read(cx);
2192 if let Some(entry) = worktree.entry_for_id(entry.id) {
2193 let selection = SelectedEntry {
2194 worktree_id: *worktree_id,
2195 entry_id: entry.id,
2196 };
2197 self.selection = Some(selection);
2198 self.autoscroll(cx);
2199 cx.notify();
2200 }
2201 }
2202 }
2203 }
2204
2205 fn autoscroll(&mut self, cx: &mut Context<Self>) {
2206 if let Some((_, _, index)) = self.selection.and_then(|s| self.index_for_selection(s)) {
2207 self.scroll_handle
2208 .scroll_to_item(index, ScrollStrategy::Center);
2209 cx.notify();
2210 }
2211 }
2212
2213 fn cut(&mut self, _: &Cut, _: &mut Window, cx: &mut Context<Self>) {
2214 let entries = self.disjoint_entries(cx);
2215 if !entries.is_empty() {
2216 self.clipboard = Some(ClipboardEntry::Cut(entries));
2217 cx.notify();
2218 }
2219 }
2220
2221 fn copy(&mut self, _: &Copy, _: &mut Window, cx: &mut Context<Self>) {
2222 let entries = self.disjoint_entries(cx);
2223 if !entries.is_empty() {
2224 self.clipboard = Some(ClipboardEntry::Copied(entries));
2225 cx.notify();
2226 }
2227 }
2228
2229 fn create_paste_path(
2230 &self,
2231 source: &SelectedEntry,
2232 (worktree, target_entry): (Entity<Worktree>, &Entry),
2233 cx: &App,
2234 ) -> Option<(PathBuf, Option<Range<usize>>)> {
2235 let mut new_path = target_entry.path.to_path_buf();
2236 // If we're pasting into a file, or a directory into itself, go up one level.
2237 if target_entry.is_file() || (target_entry.is_dir() && target_entry.id == source.entry_id) {
2238 new_path.pop();
2239 }
2240 let clipboard_entry_file_name = self
2241 .project
2242 .read(cx)
2243 .path_for_entry(source.entry_id, cx)?
2244 .path
2245 .file_name()?
2246 .to_os_string();
2247 new_path.push(&clipboard_entry_file_name);
2248 let extension = new_path.extension().map(|e| e.to_os_string());
2249 let file_name_without_extension = Path::new(&clipboard_entry_file_name).file_stem()?;
2250 let file_name_len = file_name_without_extension.to_string_lossy().len();
2251 let mut disambiguation_range = None;
2252 let mut ix = 0;
2253 {
2254 let worktree = worktree.read(cx);
2255 while worktree.entry_for_path(&new_path).is_some() {
2256 new_path.pop();
2257
2258 let mut new_file_name = file_name_without_extension.to_os_string();
2259
2260 let disambiguation = " copy";
2261 let mut disambiguation_len = disambiguation.len();
2262
2263 new_file_name.push(disambiguation);
2264
2265 if ix > 0 {
2266 let extra_disambiguation = format!(" {}", ix);
2267 disambiguation_len += extra_disambiguation.len();
2268
2269 new_file_name.push(extra_disambiguation);
2270 }
2271 if let Some(extension) = extension.as_ref() {
2272 new_file_name.push(".");
2273 new_file_name.push(extension);
2274 }
2275
2276 new_path.push(new_file_name);
2277 disambiguation_range = Some(file_name_len..(file_name_len + disambiguation_len));
2278 ix += 1;
2279 }
2280 }
2281 Some((new_path, disambiguation_range))
2282 }
2283
2284 fn paste(&mut self, _: &Paste, window: &mut Window, cx: &mut Context<Self>) {
2285 maybe!({
2286 let (worktree, entry) = self.selected_entry_handle(cx)?;
2287 let entry = entry.clone();
2288 let worktree_id = worktree.read(cx).id();
2289 let clipboard_entries = self
2290 .clipboard
2291 .as_ref()
2292 .filter(|clipboard| !clipboard.items().is_empty())?;
2293 enum PasteTask {
2294 Rename(Task<Result<CreatedEntry>>),
2295 Copy(Task<Result<Option<Entry>>>),
2296 }
2297 let mut paste_entry_tasks: IndexMap<(ProjectEntryId, bool), PasteTask> =
2298 IndexMap::default();
2299 let mut disambiguation_range = None;
2300 let clip_is_cut = clipboard_entries.is_cut();
2301 for clipboard_entry in clipboard_entries.items() {
2302 let (new_path, new_disambiguation_range) =
2303 self.create_paste_path(clipboard_entry, self.selected_sub_entry(cx)?, cx)?;
2304 let clip_entry_id = clipboard_entry.entry_id;
2305 let is_same_worktree = clipboard_entry.worktree_id == worktree_id;
2306 let relative_worktree_source_path = if !is_same_worktree {
2307 let target_base_path = worktree.read(cx).abs_path();
2308 let clipboard_project_path =
2309 self.project.read(cx).path_for_entry(clip_entry_id, cx)?;
2310 let clipboard_abs_path = self
2311 .project
2312 .read(cx)
2313 .absolute_path(&clipboard_project_path, cx)?;
2314 Some(relativize_path(
2315 &target_base_path,
2316 clipboard_abs_path.as_path(),
2317 ))
2318 } else {
2319 None
2320 };
2321 let task = if clip_is_cut && is_same_worktree {
2322 let task = self.project.update(cx, |project, cx| {
2323 project.rename_entry(clip_entry_id, new_path, cx)
2324 });
2325 PasteTask::Rename(task)
2326 } else {
2327 let entry_id = if is_same_worktree {
2328 clip_entry_id
2329 } else {
2330 entry.id
2331 };
2332 let task = self.project.update(cx, |project, cx| {
2333 project.copy_entry(entry_id, relative_worktree_source_path, new_path, cx)
2334 });
2335 PasteTask::Copy(task)
2336 };
2337 let needs_delete = !is_same_worktree && clip_is_cut;
2338 paste_entry_tasks.insert((clip_entry_id, needs_delete), task);
2339 disambiguation_range = new_disambiguation_range.or(disambiguation_range);
2340 }
2341
2342 let item_count = paste_entry_tasks.len();
2343
2344 cx.spawn_in(window, async move |project_panel, cx| {
2345 let mut last_succeed = None;
2346 let mut need_delete_ids = Vec::new();
2347 for ((entry_id, need_delete), task) in paste_entry_tasks.into_iter() {
2348 match task {
2349 PasteTask::Rename(task) => {
2350 if let Some(CreatedEntry::Included(entry)) = task.await.log_err() {
2351 last_succeed = Some(entry);
2352 }
2353 }
2354 PasteTask::Copy(task) => {
2355 if let Some(Some(entry)) = task.await.log_err() {
2356 last_succeed = Some(entry);
2357 if need_delete {
2358 need_delete_ids.push(entry_id);
2359 }
2360 }
2361 }
2362 }
2363 }
2364 // remove entry for cut in difference worktree
2365 for entry_id in need_delete_ids {
2366 project_panel
2367 .update(cx, |project_panel, cx| {
2368 project_panel
2369 .project
2370 .update(cx, |project, cx| project.delete_entry(entry_id, true, cx))
2371 .context("no such entry")
2372 })??
2373 .await?;
2374 }
2375 // update selection
2376 if let Some(entry) = last_succeed {
2377 project_panel
2378 .update_in(cx, |project_panel, window, cx| {
2379 project_panel.selection = Some(SelectedEntry {
2380 worktree_id,
2381 entry_id: entry.id,
2382 });
2383
2384 if item_count == 1 {
2385 // open entry if not dir, and only focus if rename is not pending
2386 if !entry.is_dir() {
2387 project_panel.open_entry(
2388 entry.id,
2389 disambiguation_range.is_none(),
2390 false,
2391 cx,
2392 );
2393 }
2394
2395 // if only one entry was pasted and it was disambiguated, open the rename editor
2396 if disambiguation_range.is_some() {
2397 cx.defer_in(window, |this, window, cx| {
2398 this.rename_impl(disambiguation_range, window, cx);
2399 });
2400 }
2401 }
2402 })
2403 .ok();
2404 }
2405
2406 anyhow::Ok(())
2407 })
2408 .detach_and_log_err(cx);
2409
2410 if clip_is_cut {
2411 // Convert the clipboard cut entry to a copy entry after the first paste.
2412 self.clipboard = self.clipboard.take().map(ClipboardEntry::to_copy_entry);
2413 }
2414
2415 self.expand_entry(worktree_id, entry.id, cx);
2416 Some(())
2417 });
2418 }
2419
2420 fn duplicate(&mut self, _: &Duplicate, window: &mut Window, cx: &mut Context<Self>) {
2421 self.copy(&Copy {}, window, cx);
2422 self.paste(&Paste {}, window, cx);
2423 }
2424
2425 fn copy_path(
2426 &mut self,
2427 _: &zed_actions::workspace::CopyPath,
2428 _: &mut Window,
2429 cx: &mut Context<Self>,
2430 ) {
2431 let abs_file_paths = {
2432 let project = self.project.read(cx);
2433 self.effective_entries()
2434 .into_iter()
2435 .filter_map(|entry| {
2436 let entry_path = project.path_for_entry(entry.entry_id, cx)?.path;
2437 Some(
2438 project
2439 .worktree_for_id(entry.worktree_id, cx)?
2440 .read(cx)
2441 .abs_path()
2442 .join(entry_path)
2443 .to_string_lossy()
2444 .to_string(),
2445 )
2446 })
2447 .collect::<Vec<_>>()
2448 };
2449 if !abs_file_paths.is_empty() {
2450 cx.write_to_clipboard(ClipboardItem::new_string(abs_file_paths.join("\n")));
2451 }
2452 }
2453
2454 fn copy_relative_path(
2455 &mut self,
2456 _: &zed_actions::workspace::CopyRelativePath,
2457 _: &mut Window,
2458 cx: &mut Context<Self>,
2459 ) {
2460 let file_paths = {
2461 let project = self.project.read(cx);
2462 self.effective_entries()
2463 .into_iter()
2464 .filter_map(|entry| {
2465 Some(
2466 project
2467 .path_for_entry(entry.entry_id, cx)?
2468 .path
2469 .to_string_lossy()
2470 .to_string(),
2471 )
2472 })
2473 .collect::<Vec<_>>()
2474 };
2475 if !file_paths.is_empty() {
2476 cx.write_to_clipboard(ClipboardItem::new_string(file_paths.join("\n")));
2477 }
2478 }
2479
2480 fn reveal_in_finder(
2481 &mut self,
2482 _: &RevealInFileManager,
2483 _: &mut Window,
2484 cx: &mut Context<Self>,
2485 ) {
2486 if let Some((worktree, entry)) = self.selected_sub_entry(cx) {
2487 cx.reveal_path(&worktree.read(cx).abs_path().join(&entry.path));
2488 }
2489 }
2490
2491 fn remove_from_project(
2492 &mut self,
2493 _: &RemoveFromProject,
2494 _window: &mut Window,
2495 cx: &mut Context<Self>,
2496 ) {
2497 for entry in self.effective_entries().iter() {
2498 let worktree_id = entry.worktree_id;
2499 self.project
2500 .update(cx, |project, cx| project.remove_worktree(worktree_id, cx));
2501 }
2502 }
2503
2504 fn open_system(&mut self, _: &OpenWithSystem, _: &mut Window, cx: &mut Context<Self>) {
2505 if let Some((worktree, entry)) = self.selected_entry(cx) {
2506 let abs_path = worktree.abs_path().join(&entry.path);
2507 cx.open_with_system(&abs_path);
2508 }
2509 }
2510
2511 fn open_in_terminal(
2512 &mut self,
2513 _: &OpenInTerminal,
2514 window: &mut Window,
2515 cx: &mut Context<Self>,
2516 ) {
2517 if let Some((worktree, entry)) = self.selected_sub_entry(cx) {
2518 let abs_path = match &entry.canonical_path {
2519 Some(canonical_path) => Some(canonical_path.to_path_buf()),
2520 None => worktree.read(cx).absolutize(&entry.path).ok(),
2521 };
2522
2523 let working_directory = if entry.is_dir() {
2524 abs_path
2525 } else {
2526 abs_path.and_then(|path| Some(path.parent()?.to_path_buf()))
2527 };
2528 if let Some(working_directory) = working_directory {
2529 window.dispatch_action(
2530 workspace::OpenTerminal { working_directory }.boxed_clone(),
2531 cx,
2532 )
2533 }
2534 }
2535 }
2536
2537 pub fn new_search_in_directory(
2538 &mut self,
2539 _: &NewSearchInDirectory,
2540 window: &mut Window,
2541 cx: &mut Context<Self>,
2542 ) {
2543 if let Some((worktree, entry)) = self.selected_sub_entry(cx) {
2544 let dir_path = if entry.is_dir() {
2545 entry.path.clone()
2546 } else {
2547 // entry is a file, use its parent directory
2548 match entry.path.parent() {
2549 Some(parent) => Arc::from(parent),
2550 None => {
2551 // File at root, open search with empty filter
2552 self.workspace
2553 .update(cx, |workspace, cx| {
2554 search::ProjectSearchView::new_search_in_directory(
2555 workspace,
2556 Path::new(""),
2557 window,
2558 cx,
2559 );
2560 })
2561 .ok();
2562 return;
2563 }
2564 }
2565 };
2566
2567 let include_root = self.project.read(cx).visible_worktrees(cx).count() > 1;
2568 let dir_path = if include_root {
2569 let mut full_path = PathBuf::from(worktree.read(cx).root_name());
2570 full_path.push(&dir_path);
2571 Arc::from(full_path)
2572 } else {
2573 dir_path
2574 };
2575
2576 self.workspace
2577 .update(cx, |workspace, cx| {
2578 search::ProjectSearchView::new_search_in_directory(
2579 workspace, &dir_path, window, cx,
2580 );
2581 })
2582 .ok();
2583 }
2584 }
2585
2586 fn move_entry(
2587 &mut self,
2588 entry_to_move: ProjectEntryId,
2589 destination: ProjectEntryId,
2590 destination_is_file: bool,
2591 cx: &mut Context<Self>,
2592 ) {
2593 if self
2594 .project
2595 .read(cx)
2596 .entry_is_worktree_root(entry_to_move, cx)
2597 {
2598 self.move_worktree_root(entry_to_move, destination, cx)
2599 } else {
2600 self.move_worktree_entry(entry_to_move, destination, destination_is_file, cx)
2601 }
2602 }
2603
2604 fn move_worktree_root(
2605 &mut self,
2606 entry_to_move: ProjectEntryId,
2607 destination: ProjectEntryId,
2608 cx: &mut Context<Self>,
2609 ) {
2610 self.project.update(cx, |project, cx| {
2611 let Some(worktree_to_move) = project.worktree_for_entry(entry_to_move, cx) else {
2612 return;
2613 };
2614 let Some(destination_worktree) = project.worktree_for_entry(destination, cx) else {
2615 return;
2616 };
2617
2618 let worktree_id = worktree_to_move.read(cx).id();
2619 let destination_id = destination_worktree.read(cx).id();
2620
2621 project
2622 .move_worktree(worktree_id, destination_id, cx)
2623 .log_err();
2624 });
2625 }
2626
2627 fn move_worktree_entry(
2628 &mut self,
2629 entry_to_move: ProjectEntryId,
2630 destination: ProjectEntryId,
2631 destination_is_file: bool,
2632 cx: &mut Context<Self>,
2633 ) {
2634 if entry_to_move == destination {
2635 return;
2636 }
2637
2638 let destination_worktree = self.project.update(cx, |project, cx| {
2639 let entry_path = project.path_for_entry(entry_to_move, cx)?;
2640 let destination_entry_path = project.path_for_entry(destination, cx)?.path.clone();
2641
2642 let mut destination_path = destination_entry_path.as_ref();
2643 if destination_is_file {
2644 destination_path = destination_path.parent()?;
2645 }
2646
2647 let mut new_path = destination_path.to_path_buf();
2648 new_path.push(entry_path.path.file_name()?);
2649 if new_path != entry_path.path.as_ref() {
2650 let task = project.rename_entry(entry_to_move, new_path, cx);
2651 cx.foreground_executor().spawn(task).detach_and_log_err(cx);
2652 }
2653
2654 project.worktree_id_for_entry(destination, cx)
2655 });
2656
2657 if let Some(destination_worktree) = destination_worktree {
2658 self.expand_entry(destination_worktree, destination, cx);
2659 }
2660 }
2661
2662 fn index_for_selection(&self, selection: SelectedEntry) -> Option<(usize, usize, usize)> {
2663 let mut entry_index = 0;
2664 let mut visible_entries_index = 0;
2665 for (worktree_index, (worktree_id, worktree_entries, _)) in
2666 self.visible_entries.iter().enumerate()
2667 {
2668 if *worktree_id == selection.worktree_id {
2669 for entry in worktree_entries {
2670 if entry.id == selection.entry_id {
2671 return Some((worktree_index, entry_index, visible_entries_index));
2672 } else {
2673 visible_entries_index += 1;
2674 entry_index += 1;
2675 }
2676 }
2677 break;
2678 } else {
2679 visible_entries_index += worktree_entries.len();
2680 }
2681 }
2682 None
2683 }
2684
2685 fn disjoint_entries(&self, cx: &App) -> BTreeSet<SelectedEntry> {
2686 let marked_entries = self.effective_entries();
2687 let mut sanitized_entries = BTreeSet::new();
2688 if marked_entries.is_empty() {
2689 return sanitized_entries;
2690 }
2691
2692 let project = self.project.read(cx);
2693 let marked_entries_by_worktree: HashMap<WorktreeId, Vec<SelectedEntry>> = marked_entries
2694 .into_iter()
2695 .filter(|entry| !project.entry_is_worktree_root(entry.entry_id, cx))
2696 .fold(HashMap::default(), |mut map, entry| {
2697 map.entry(entry.worktree_id).or_default().push(entry);
2698 map
2699 });
2700
2701 for (worktree_id, marked_entries) in marked_entries_by_worktree {
2702 if let Some(worktree) = project.worktree_for_id(worktree_id, cx) {
2703 let worktree = worktree.read(cx);
2704 let marked_dir_paths = marked_entries
2705 .iter()
2706 .filter_map(|entry| {
2707 worktree.entry_for_id(entry.entry_id).and_then(|entry| {
2708 if entry.is_dir() {
2709 Some(entry.path.as_ref())
2710 } else {
2711 None
2712 }
2713 })
2714 })
2715 .collect::<BTreeSet<_>>();
2716
2717 sanitized_entries.extend(marked_entries.into_iter().filter(|entry| {
2718 let Some(entry_info) = worktree.entry_for_id(entry.entry_id) else {
2719 return false;
2720 };
2721 let entry_path = entry_info.path.as_ref();
2722 let inside_marked_dir = marked_dir_paths.iter().any(|&marked_dir_path| {
2723 entry_path != marked_dir_path && entry_path.starts_with(marked_dir_path)
2724 });
2725 !inside_marked_dir
2726 }));
2727 }
2728 }
2729
2730 sanitized_entries
2731 }
2732
2733 fn effective_entries(&self) -> BTreeSet<SelectedEntry> {
2734 if let Some(selection) = self.selection {
2735 let selection = SelectedEntry {
2736 entry_id: self.resolve_entry(selection.entry_id),
2737 worktree_id: selection.worktree_id,
2738 };
2739
2740 // Default to using just the selected item when nothing is marked.
2741 if self.marked_entries.is_empty() {
2742 return BTreeSet::from([selection]);
2743 }
2744
2745 // Allow operating on the selected item even when something else is marked,
2746 // making it easier to perform one-off actions without clearing a mark.
2747 if self.marked_entries.len() == 1 && !self.marked_entries.contains(&selection) {
2748 return BTreeSet::from([selection]);
2749 }
2750 }
2751
2752 // Return only marked entries since we've already handled special cases where
2753 // only selection should take precedence. At this point, marked entries may or
2754 // may not include the current selection, which is intentional.
2755 self.marked_entries
2756 .iter()
2757 .map(|entry| SelectedEntry {
2758 entry_id: self.resolve_entry(entry.entry_id),
2759 worktree_id: entry.worktree_id,
2760 })
2761 .collect::<BTreeSet<_>>()
2762 }
2763
2764 /// Finds the currently selected subentry for a given leaf entry id. If a given entry
2765 /// has no ancestors, the project entry ID that's passed in is returned as-is.
2766 fn resolve_entry(&self, id: ProjectEntryId) -> ProjectEntryId {
2767 self.ancestors
2768 .get(&id)
2769 .and_then(|ancestors| {
2770 if ancestors.current_ancestor_depth == 0 {
2771 return None;
2772 }
2773 ancestors.ancestors.get(ancestors.current_ancestor_depth)
2774 })
2775 .copied()
2776 .unwrap_or(id)
2777 }
2778
2779 pub fn selected_entry<'a>(&self, cx: &'a App) -> Option<(&'a Worktree, &'a project::Entry)> {
2780 let (worktree, entry) = self.selected_entry_handle(cx)?;
2781 Some((worktree.read(cx), entry))
2782 }
2783
2784 /// Compared to selected_entry, this function resolves to the currently
2785 /// selected subentry if dir auto-folding is enabled.
2786 fn selected_sub_entry<'a>(
2787 &self,
2788 cx: &'a App,
2789 ) -> Option<(Entity<Worktree>, &'a project::Entry)> {
2790 let (worktree, mut entry) = self.selected_entry_handle(cx)?;
2791
2792 let resolved_id = self.resolve_entry(entry.id);
2793 if resolved_id != entry.id {
2794 let worktree = worktree.read(cx);
2795 entry = worktree.entry_for_id(resolved_id)?;
2796 }
2797 Some((worktree, entry))
2798 }
2799 fn selected_entry_handle<'a>(
2800 &self,
2801 cx: &'a App,
2802 ) -> Option<(Entity<Worktree>, &'a project::Entry)> {
2803 let selection = self.selection?;
2804 let project = self.project.read(cx);
2805 let worktree = project.worktree_for_id(selection.worktree_id, cx)?;
2806 let entry = worktree.read(cx).entry_for_id(selection.entry_id)?;
2807 Some((worktree, entry))
2808 }
2809
2810 fn expand_to_selection(&mut self, cx: &mut Context<Self>) -> Option<()> {
2811 let (worktree, entry) = self.selected_entry(cx)?;
2812 let expanded_dir_ids = self.expanded_dir_ids.entry(worktree.id()).or_default();
2813
2814 for path in entry.path.ancestors() {
2815 let Some(entry) = worktree.entry_for_path(path) else {
2816 continue;
2817 };
2818 if entry.is_dir() {
2819 if let Err(idx) = expanded_dir_ids.binary_search(&entry.id) {
2820 expanded_dir_ids.insert(idx, entry.id);
2821 }
2822 }
2823 }
2824
2825 Some(())
2826 }
2827
2828 fn create_new_git_entry(
2829 parent_entry: &Entry,
2830 git_summary: GitSummary,
2831 new_entry_kind: EntryKind,
2832 ) -> GitEntry {
2833 GitEntry {
2834 entry: Entry {
2835 id: NEW_ENTRY_ID,
2836 kind: new_entry_kind,
2837 path: parent_entry.path.join("\0").into(),
2838 inode: 0,
2839 mtime: parent_entry.mtime,
2840 size: parent_entry.size,
2841 is_ignored: parent_entry.is_ignored,
2842 is_external: false,
2843 is_private: false,
2844 is_always_included: parent_entry.is_always_included,
2845 canonical_path: parent_entry.canonical_path.clone(),
2846 char_bag: parent_entry.char_bag,
2847 is_fifo: parent_entry.is_fifo,
2848 },
2849 git_summary,
2850 }
2851 }
2852
2853 fn update_visible_entries(
2854 &mut self,
2855 new_selected_entry: Option<(WorktreeId, ProjectEntryId)>,
2856 cx: &mut Context<Self>,
2857 ) {
2858 let settings = ProjectPanelSettings::get_global(cx);
2859 let auto_collapse_dirs = settings.auto_fold_dirs;
2860 let hide_gitignore = settings.hide_gitignore;
2861 let project = self.project.read(cx);
2862 let repo_snapshots = project.git_store().read(cx).repo_snapshots(cx);
2863 self.last_worktree_root_id = project
2864 .visible_worktrees(cx)
2865 .next_back()
2866 .and_then(|worktree| worktree.read(cx).root_entry())
2867 .map(|entry| entry.id);
2868
2869 let old_ancestors = std::mem::take(&mut self.ancestors);
2870 self.visible_entries.clear();
2871 let mut max_width_item = None;
2872
2873 let visible_worktrees: Vec<_> = project.visible_worktrees(cx).collect();
2874 let hide_root = settings.hide_root && visible_worktrees.len() == 1;
2875 for worktree in visible_worktrees {
2876 let worktree_snapshot = worktree.read(cx).snapshot();
2877 let worktree_id = worktree_snapshot.id();
2878
2879 let expanded_dir_ids = match self.expanded_dir_ids.entry(worktree_id) {
2880 hash_map::Entry::Occupied(e) => e.into_mut(),
2881 hash_map::Entry::Vacant(e) => {
2882 // The first time a worktree's root entry becomes available,
2883 // mark that root entry as expanded.
2884 if let Some(entry) = worktree_snapshot.root_entry() {
2885 e.insert(vec![entry.id]).as_slice()
2886 } else {
2887 &[]
2888 }
2889 }
2890 };
2891
2892 let mut new_entry_parent_id = None;
2893 let mut new_entry_kind = EntryKind::Dir;
2894 if let Some(edit_state) = &self.edit_state {
2895 if edit_state.worktree_id == worktree_id && edit_state.is_new_entry() {
2896 new_entry_parent_id = Some(edit_state.entry_id);
2897 new_entry_kind = if edit_state.is_dir {
2898 EntryKind::Dir
2899 } else {
2900 EntryKind::File
2901 };
2902 }
2903 }
2904
2905 let mut visible_worktree_entries = Vec::new();
2906 let mut entry_iter =
2907 GitTraversal::new(&repo_snapshots, worktree_snapshot.entries(true, 0));
2908 let mut auto_folded_ancestors = vec![];
2909 while let Some(entry) = entry_iter.entry() {
2910 if hide_root && Some(entry.entry) == worktree.read(cx).root_entry() {
2911 if new_entry_parent_id == Some(entry.id) {
2912 visible_worktree_entries.push(Self::create_new_git_entry(
2913 &entry.entry,
2914 entry.git_summary,
2915 new_entry_kind,
2916 ));
2917 new_entry_parent_id = None;
2918 }
2919 entry_iter.advance();
2920 continue;
2921 }
2922 if auto_collapse_dirs && entry.kind.is_dir() {
2923 auto_folded_ancestors.push(entry.id);
2924 if !self.unfolded_dir_ids.contains(&entry.id) {
2925 if let Some(root_path) = worktree_snapshot.root_entry() {
2926 let mut child_entries = worktree_snapshot.child_entries(&entry.path);
2927 if let Some(child) = child_entries.next() {
2928 if entry.path != root_path.path
2929 && child_entries.next().is_none()
2930 && child.kind.is_dir()
2931 {
2932 entry_iter.advance();
2933
2934 continue;
2935 }
2936 }
2937 }
2938 }
2939 let depth = old_ancestors
2940 .get(&entry.id)
2941 .map(|ancestor| ancestor.current_ancestor_depth)
2942 .unwrap_or_default()
2943 .min(auto_folded_ancestors.len());
2944 if let Some(edit_state) = &mut self.edit_state {
2945 if edit_state.entry_id == entry.id {
2946 edit_state.depth = depth;
2947 }
2948 }
2949 let mut ancestors = std::mem::take(&mut auto_folded_ancestors);
2950 if ancestors.len() > 1 {
2951 ancestors.reverse();
2952 self.ancestors.insert(
2953 entry.id,
2954 FoldedAncestors {
2955 current_ancestor_depth: depth,
2956 ancestors,
2957 },
2958 );
2959 }
2960 }
2961 auto_folded_ancestors.clear();
2962 if !hide_gitignore || !entry.is_ignored {
2963 visible_worktree_entries.push(entry.to_owned());
2964 }
2965 let precedes_new_entry = if let Some(new_entry_id) = new_entry_parent_id {
2966 entry.id == new_entry_id || {
2967 self.ancestors
2968 .get(&entry.id)
2969 .map_or(false, |entries| entries.ancestors.contains(&new_entry_id))
2970 }
2971 } else {
2972 false
2973 };
2974 if precedes_new_entry && (!hide_gitignore || !entry.is_ignored) {
2975 visible_worktree_entries.push(Self::create_new_git_entry(
2976 &entry.entry,
2977 entry.git_summary,
2978 new_entry_kind,
2979 ));
2980 }
2981 let worktree_abs_path = worktree.read(cx).abs_path();
2982 let (depth, path) = if Some(entry.entry) == worktree.read(cx).root_entry() {
2983 let Some(path_name) = worktree_abs_path.file_name() else {
2984 continue;
2985 };
2986 let path = ArcCow::Borrowed(Path::new(path_name));
2987 let depth = 0;
2988 (depth, path)
2989 } else if entry.is_file() {
2990 let Some(path_name) = entry
2991 .path
2992 .file_name()
2993 .with_context(|| format!("Non-root entry has no file name: {entry:?}"))
2994 .log_err()
2995 else {
2996 continue;
2997 };
2998 let path = ArcCow::Borrowed(Path::new(path_name));
2999 let depth = entry.path.ancestors().count() - 1;
3000 (depth, path)
3001 } else {
3002 let path = self
3003 .ancestors
3004 .get(&entry.id)
3005 .and_then(|ancestors| {
3006 let outermost_ancestor = ancestors.ancestors.last()?;
3007 let root_folded_entry = worktree
3008 .read(cx)
3009 .entry_for_id(*outermost_ancestor)?
3010 .path
3011 .as_ref();
3012 entry
3013 .path
3014 .strip_prefix(root_folded_entry)
3015 .ok()
3016 .and_then(|suffix| {
3017 let full_path = Path::new(root_folded_entry.file_name()?);
3018 Some(ArcCow::Owned(Arc::<Path>::from(full_path.join(suffix))))
3019 })
3020 })
3021 .or_else(|| entry.path.file_name().map(Path::new).map(ArcCow::Borrowed))
3022 .unwrap_or_else(|| ArcCow::Owned(entry.path.clone()));
3023 let depth = path.components().count();
3024 (depth, path)
3025 };
3026 let width_estimate = item_width_estimate(
3027 depth,
3028 path.to_string_lossy().chars().count(),
3029 entry.canonical_path.is_some(),
3030 );
3031
3032 match max_width_item.as_mut() {
3033 Some((id, worktree_id, width)) => {
3034 if *width < width_estimate {
3035 *id = entry.id;
3036 *worktree_id = worktree.read(cx).id();
3037 *width = width_estimate;
3038 }
3039 }
3040 None => {
3041 max_width_item = Some((entry.id, worktree.read(cx).id(), width_estimate))
3042 }
3043 }
3044
3045 if expanded_dir_ids.binary_search(&entry.id).is_err()
3046 && entry_iter.advance_to_sibling()
3047 {
3048 continue;
3049 }
3050 entry_iter.advance();
3051 }
3052
3053 project::sort_worktree_entries(&mut visible_worktree_entries);
3054
3055 self.visible_entries
3056 .push((worktree_id, visible_worktree_entries, OnceCell::new()));
3057 }
3058
3059 if let Some((project_entry_id, worktree_id, _)) = max_width_item {
3060 let mut visited_worktrees_length = 0;
3061 let index = self.visible_entries.iter().find_map(|(id, entries, _)| {
3062 if worktree_id == *id {
3063 entries
3064 .iter()
3065 .position(|entry| entry.id == project_entry_id)
3066 } else {
3067 visited_worktrees_length += entries.len();
3068 None
3069 }
3070 });
3071 if let Some(index) = index {
3072 self.max_width_item_index = Some(visited_worktrees_length + index);
3073 }
3074 }
3075 if let Some((worktree_id, entry_id)) = new_selected_entry {
3076 self.selection = Some(SelectedEntry {
3077 worktree_id,
3078 entry_id,
3079 });
3080 }
3081 }
3082
3083 fn expand_entry(
3084 &mut self,
3085 worktree_id: WorktreeId,
3086 entry_id: ProjectEntryId,
3087 cx: &mut Context<Self>,
3088 ) {
3089 self.project.update(cx, |project, cx| {
3090 if let Some((worktree, expanded_dir_ids)) = project
3091 .worktree_for_id(worktree_id, cx)
3092 .zip(self.expanded_dir_ids.get_mut(&worktree_id))
3093 {
3094 project.expand_entry(worktree_id, entry_id, cx);
3095 let worktree = worktree.read(cx);
3096
3097 if let Some(mut entry) = worktree.entry_for_id(entry_id) {
3098 loop {
3099 if let Err(ix) = expanded_dir_ids.binary_search(&entry.id) {
3100 expanded_dir_ids.insert(ix, entry.id);
3101 }
3102
3103 if let Some(parent_entry) =
3104 entry.path.parent().and_then(|p| worktree.entry_for_path(p))
3105 {
3106 entry = parent_entry;
3107 } else {
3108 break;
3109 }
3110 }
3111 }
3112 }
3113 });
3114 }
3115
3116 fn drop_external_files(
3117 &mut self,
3118 paths: &[PathBuf],
3119 entry_id: ProjectEntryId,
3120 window: &mut Window,
3121 cx: &mut Context<Self>,
3122 ) {
3123 let mut paths: Vec<Arc<Path>> = paths.iter().map(|path| Arc::from(path.clone())).collect();
3124
3125 let open_file_after_drop = paths.len() == 1 && paths[0].is_file();
3126
3127 let Some((target_directory, worktree, fs)) = maybe!({
3128 let project = self.project.read(cx);
3129 let fs = project.fs().clone();
3130 let worktree = project.worktree_for_entry(entry_id, cx)?;
3131 let entry = worktree.read(cx).entry_for_id(entry_id)?;
3132 let path = entry.path.clone();
3133 let target_directory = if entry.is_dir() {
3134 path.to_path_buf()
3135 } else {
3136 path.parent()?.to_path_buf()
3137 };
3138 Some((target_directory, worktree, fs))
3139 }) else {
3140 return;
3141 };
3142
3143 let mut paths_to_replace = Vec::new();
3144 for path in &paths {
3145 if let Some(name) = path.file_name() {
3146 let mut target_path = target_directory.clone();
3147 target_path.push(name);
3148 if target_path.exists() {
3149 paths_to_replace.push((name.to_string_lossy().to_string(), path.clone()));
3150 }
3151 }
3152 }
3153
3154 cx.spawn_in(window, async move |this, cx| {
3155 async move {
3156 for (filename, original_path) in &paths_to_replace {
3157 let answer = cx.update(|window, cx| {
3158 window
3159 .prompt(
3160 PromptLevel::Info,
3161 format!("A file or folder with name {filename} already exists in the destination folder. Do you want to replace it?").as_str(),
3162 None,
3163 &["Replace", "Cancel"],
3164 cx,
3165 )
3166 })?.await?;
3167
3168 if answer == 1 {
3169 if let Some(item_idx) = paths.iter().position(|p| p == original_path) {
3170 paths.remove(item_idx);
3171 }
3172 }
3173 }
3174
3175 if paths.is_empty() {
3176 return Ok(());
3177 }
3178
3179 let task = worktree.update( cx, |worktree, cx| {
3180 worktree.copy_external_entries(target_directory.into(), paths, fs, cx)
3181 })?;
3182
3183 let opened_entries = task.await.with_context(|| "failed to copy external paths")?;
3184 this.update(cx, |this, cx| {
3185 if open_file_after_drop && !opened_entries.is_empty() {
3186 this.open_entry(opened_entries[0], true, false, cx);
3187 }
3188 })
3189 }
3190 .log_err().await
3191 })
3192 .detach();
3193 }
3194
3195 fn refresh_drag_cursor_style(
3196 &self,
3197 modifiers: &Modifiers,
3198 window: &mut Window,
3199 cx: &mut Context<Self>,
3200 ) {
3201 if let Some(existing_cursor) = cx.active_drag_cursor_style() {
3202 let new_cursor = if Self::is_copy_modifier_set(modifiers) {
3203 CursorStyle::DragCopy
3204 } else {
3205 CursorStyle::PointingHand
3206 };
3207 if existing_cursor != new_cursor {
3208 cx.set_active_drag_cursor_style(new_cursor, window);
3209 }
3210 }
3211 }
3212
3213 fn is_copy_modifier_set(modifiers: &Modifiers) -> bool {
3214 cfg!(target_os = "macos") && modifiers.alt
3215 || cfg!(not(target_os = "macos")) && modifiers.control
3216 }
3217
3218 fn drag_onto(
3219 &mut self,
3220 selections: &DraggedSelection,
3221 target_entry_id: ProjectEntryId,
3222 is_file: bool,
3223 window: &mut Window,
3224 cx: &mut Context<Self>,
3225 ) {
3226 if Self::is_copy_modifier_set(&window.modifiers()) {
3227 let _ = maybe!({
3228 let project = self.project.read(cx);
3229 let target_worktree = project.worktree_for_entry(target_entry_id, cx)?;
3230 let worktree_id = target_worktree.read(cx).id();
3231 let target_entry = target_worktree
3232 .read(cx)
3233 .entry_for_id(target_entry_id)?
3234 .clone();
3235
3236 let mut copy_tasks = Vec::new();
3237 let mut disambiguation_range = None;
3238 for selection in selections.items() {
3239 let (new_path, new_disambiguation_range) = self.create_paste_path(
3240 selection,
3241 (target_worktree.clone(), &target_entry),
3242 cx,
3243 )?;
3244
3245 let task = self.project.update(cx, |project, cx| {
3246 project.copy_entry(selection.entry_id, None, new_path, cx)
3247 });
3248 copy_tasks.push(task);
3249 disambiguation_range = new_disambiguation_range.or(disambiguation_range);
3250 }
3251
3252 let item_count = copy_tasks.len();
3253
3254 cx.spawn_in(window, async move |project_panel, cx| {
3255 let mut last_succeed = None;
3256 for task in copy_tasks.into_iter() {
3257 if let Some(Some(entry)) = task.await.log_err() {
3258 last_succeed = Some(entry.id);
3259 }
3260 }
3261 // update selection
3262 if let Some(entry_id) = last_succeed {
3263 project_panel
3264 .update_in(cx, |project_panel, window, cx| {
3265 project_panel.selection = Some(SelectedEntry {
3266 worktree_id,
3267 entry_id,
3268 });
3269
3270 // if only one entry was dragged and it was disambiguated, open the rename editor
3271 if item_count == 1 && disambiguation_range.is_some() {
3272 project_panel.rename_impl(disambiguation_range, window, cx);
3273 }
3274 })
3275 .ok();
3276 }
3277 })
3278 .detach();
3279 Some(())
3280 });
3281 } else {
3282 for selection in selections.items() {
3283 self.move_entry(selection.entry_id, target_entry_id, is_file, cx);
3284 }
3285 }
3286 }
3287
3288 fn index_for_entry(
3289 &self,
3290 entry_id: ProjectEntryId,
3291 worktree_id: WorktreeId,
3292 ) -> Option<(usize, usize, usize)> {
3293 let mut worktree_ix = 0;
3294 let mut total_ix = 0;
3295 for (current_worktree_id, visible_worktree_entries, _) in &self.visible_entries {
3296 if worktree_id != *current_worktree_id {
3297 total_ix += visible_worktree_entries.len();
3298 worktree_ix += 1;
3299 continue;
3300 }
3301
3302 return visible_worktree_entries
3303 .iter()
3304 .enumerate()
3305 .find(|(_, entry)| entry.id == entry_id)
3306 .map(|(ix, _)| (worktree_ix, ix, total_ix + ix));
3307 }
3308 None
3309 }
3310
3311 fn entry_at_index(&self, index: usize) -> Option<(WorktreeId, GitEntryRef<'_>)> {
3312 let mut offset = 0;
3313 for (worktree_id, visible_worktree_entries, _) in &self.visible_entries {
3314 let current_len = visible_worktree_entries.len();
3315 if index < offset + current_len {
3316 return visible_worktree_entries
3317 .get(index - offset)
3318 .map(|entry| (*worktree_id, entry.to_ref()));
3319 }
3320 offset += current_len;
3321 }
3322 None
3323 }
3324
3325 fn iter_visible_entries(
3326 &self,
3327 range: Range<usize>,
3328 window: &mut Window,
3329 cx: &mut Context<ProjectPanel>,
3330 mut callback: impl FnMut(
3331 &Entry,
3332 usize,
3333 &HashSet<Arc<Path>>,
3334 &mut Window,
3335 &mut Context<ProjectPanel>,
3336 ),
3337 ) {
3338 let mut ix = 0;
3339 for (_, visible_worktree_entries, entries_paths) in &self.visible_entries {
3340 if ix >= range.end {
3341 return;
3342 }
3343
3344 if ix + visible_worktree_entries.len() <= range.start {
3345 ix += visible_worktree_entries.len();
3346 continue;
3347 }
3348
3349 let end_ix = range.end.min(ix + visible_worktree_entries.len());
3350 let entry_range = range.start.saturating_sub(ix)..end_ix - ix;
3351 let entries = entries_paths.get_or_init(|| {
3352 visible_worktree_entries
3353 .iter()
3354 .map(|e| (e.path.clone()))
3355 .collect()
3356 });
3357 let base_index = ix + entry_range.start;
3358 for (i, entry) in visible_worktree_entries[entry_range].iter().enumerate() {
3359 let global_index = base_index + i;
3360 callback(&entry, global_index, entries, window, cx);
3361 }
3362 ix = end_ix;
3363 }
3364 }
3365
3366 fn for_each_visible_entry(
3367 &self,
3368 range: Range<usize>,
3369 window: &mut Window,
3370 cx: &mut Context<ProjectPanel>,
3371 mut callback: impl FnMut(ProjectEntryId, EntryDetails, &mut Window, &mut Context<ProjectPanel>),
3372 ) {
3373 let mut ix = 0;
3374 for (worktree_id, visible_worktree_entries, entries_paths) in &self.visible_entries {
3375 if ix >= range.end {
3376 return;
3377 }
3378
3379 if ix + visible_worktree_entries.len() <= range.start {
3380 ix += visible_worktree_entries.len();
3381 continue;
3382 }
3383
3384 let end_ix = range.end.min(ix + visible_worktree_entries.len());
3385 let git_status_setting = {
3386 let settings = ProjectPanelSettings::get_global(cx);
3387 settings.git_status
3388 };
3389 if let Some(worktree) = self.project.read(cx).worktree_for_id(*worktree_id, cx) {
3390 let snapshot = worktree.read(cx).snapshot();
3391 let root_name = OsStr::new(snapshot.root_name());
3392
3393 let entry_range = range.start.saturating_sub(ix)..end_ix - ix;
3394 let entries = entries_paths.get_or_init(|| {
3395 visible_worktree_entries
3396 .iter()
3397 .map(|e| (e.path.clone()))
3398 .collect()
3399 });
3400 for entry in visible_worktree_entries[entry_range].iter() {
3401 let status = git_status_setting
3402 .then_some(entry.git_summary)
3403 .unwrap_or_default();
3404
3405 let mut details = self.details_for_entry(
3406 entry,
3407 *worktree_id,
3408 root_name,
3409 entries,
3410 status,
3411 None,
3412 window,
3413 cx,
3414 );
3415
3416 if let Some(edit_state) = &self.edit_state {
3417 let is_edited_entry = if edit_state.is_new_entry() {
3418 entry.id == NEW_ENTRY_ID
3419 } else {
3420 entry.id == edit_state.entry_id
3421 || self
3422 .ancestors
3423 .get(&entry.id)
3424 .is_some_and(|auto_folded_dirs| {
3425 auto_folded_dirs.ancestors.contains(&edit_state.entry_id)
3426 })
3427 };
3428
3429 if is_edited_entry {
3430 if let Some(processing_filename) = &edit_state.processing_filename {
3431 details.is_processing = true;
3432 if let Some(ancestors) = edit_state
3433 .leaf_entry_id
3434 .and_then(|entry| self.ancestors.get(&entry))
3435 {
3436 let position = ancestors.ancestors.iter().position(|entry_id| *entry_id == edit_state.entry_id).expect("Edited sub-entry should be an ancestor of selected leaf entry") + 1;
3437 let all_components = ancestors.ancestors.len();
3438
3439 let prefix_components = all_components - position;
3440 let suffix_components = position.checked_sub(1);
3441 let mut previous_components =
3442 Path::new(&details.filename).components();
3443 let mut new_path = previous_components
3444 .by_ref()
3445 .take(prefix_components)
3446 .collect::<PathBuf>();
3447 if let Some(last_component) =
3448 Path::new(processing_filename).components().next_back()
3449 {
3450 new_path.push(last_component);
3451 previous_components.next();
3452 }
3453
3454 if let Some(_) = suffix_components {
3455 new_path.push(previous_components);
3456 }
3457 if let Some(str) = new_path.to_str() {
3458 details.filename.clear();
3459 details.filename.push_str(str);
3460 }
3461 } else {
3462 details.filename.clear();
3463 details.filename.push_str(processing_filename);
3464 }
3465 } else {
3466 if edit_state.is_new_entry() {
3467 details.filename.clear();
3468 }
3469 details.is_editing = true;
3470 }
3471 }
3472 }
3473
3474 callback(entry.id, details, window, cx);
3475 }
3476 }
3477 ix = end_ix;
3478 }
3479 }
3480
3481 fn find_entry_in_worktree(
3482 &self,
3483 worktree_id: WorktreeId,
3484 reverse_search: bool,
3485 only_visible_entries: bool,
3486 predicate: impl Fn(GitEntryRef, WorktreeId) -> bool,
3487 cx: &mut Context<Self>,
3488 ) -> Option<GitEntry> {
3489 if only_visible_entries {
3490 let entries = self
3491 .visible_entries
3492 .iter()
3493 .find_map(|(tree_id, entries, _)| {
3494 if worktree_id == *tree_id {
3495 Some(entries)
3496 } else {
3497 None
3498 }
3499 })?
3500 .clone();
3501
3502 return utils::ReversibleIterable::new(entries.iter(), reverse_search)
3503 .find(|ele| predicate(ele.to_ref(), worktree_id))
3504 .cloned();
3505 }
3506
3507 let repo_snapshots = self
3508 .project
3509 .read(cx)
3510 .git_store()
3511 .read(cx)
3512 .repo_snapshots(cx);
3513 let worktree = self.project.read(cx).worktree_for_id(worktree_id, cx)?;
3514 worktree.read_with(cx, |tree, _| {
3515 utils::ReversibleIterable::new(
3516 GitTraversal::new(&repo_snapshots, tree.entries(true, 0usize)),
3517 reverse_search,
3518 )
3519 .find_single_ended(|ele| predicate(*ele, worktree_id))
3520 .map(|ele| ele.to_owned())
3521 })
3522 }
3523
3524 fn find_entry(
3525 &self,
3526 start: Option<&SelectedEntry>,
3527 reverse_search: bool,
3528 predicate: impl Fn(GitEntryRef, WorktreeId) -> bool,
3529 cx: &mut Context<Self>,
3530 ) -> Option<SelectedEntry> {
3531 let mut worktree_ids: Vec<_> = self
3532 .visible_entries
3533 .iter()
3534 .map(|(worktree_id, _, _)| *worktree_id)
3535 .collect();
3536 let repo_snapshots = self
3537 .project
3538 .read(cx)
3539 .git_store()
3540 .read(cx)
3541 .repo_snapshots(cx);
3542
3543 let mut last_found: Option<SelectedEntry> = None;
3544
3545 if let Some(start) = start {
3546 let worktree = self
3547 .project
3548 .read(cx)
3549 .worktree_for_id(start.worktree_id, cx)?
3550 .read(cx);
3551
3552 let search = {
3553 let entry = worktree.entry_for_id(start.entry_id)?;
3554 let root_entry = worktree.root_entry()?;
3555 let tree_id = worktree.id();
3556
3557 let mut first_iter = GitTraversal::new(
3558 &repo_snapshots,
3559 worktree.traverse_from_path(true, true, true, entry.path.as_ref()),
3560 );
3561
3562 if reverse_search {
3563 first_iter.next();
3564 }
3565
3566 let first = first_iter
3567 .enumerate()
3568 .take_until(|(count, entry)| entry.entry == root_entry && *count != 0usize)
3569 .map(|(_, entry)| entry)
3570 .find(|ele| predicate(*ele, tree_id))
3571 .map(|ele| ele.to_owned());
3572
3573 let second_iter =
3574 GitTraversal::new(&repo_snapshots, worktree.entries(true, 0usize));
3575
3576 let second = if reverse_search {
3577 second_iter
3578 .take_until(|ele| ele.id == start.entry_id)
3579 .filter(|ele| predicate(*ele, tree_id))
3580 .last()
3581 .map(|ele| ele.to_owned())
3582 } else {
3583 second_iter
3584 .take_while(|ele| ele.id != start.entry_id)
3585 .filter(|ele| predicate(*ele, tree_id))
3586 .last()
3587 .map(|ele| ele.to_owned())
3588 };
3589
3590 if reverse_search {
3591 Some((second, first))
3592 } else {
3593 Some((first, second))
3594 }
3595 };
3596
3597 if let Some((first, second)) = search {
3598 let first = first.map(|entry| SelectedEntry {
3599 worktree_id: start.worktree_id,
3600 entry_id: entry.id,
3601 });
3602
3603 let second = second.map(|entry| SelectedEntry {
3604 worktree_id: start.worktree_id,
3605 entry_id: entry.id,
3606 });
3607
3608 if first.is_some() {
3609 return first;
3610 }
3611 last_found = second;
3612
3613 let idx = worktree_ids
3614 .iter()
3615 .enumerate()
3616 .find(|(_, ele)| **ele == start.worktree_id)
3617 .map(|(idx, _)| idx);
3618
3619 if let Some(idx) = idx {
3620 worktree_ids.rotate_left(idx + 1usize);
3621 worktree_ids.pop();
3622 }
3623 }
3624 }
3625
3626 for tree_id in worktree_ids.into_iter() {
3627 if let Some(found) =
3628 self.find_entry_in_worktree(tree_id, reverse_search, false, &predicate, cx)
3629 {
3630 return Some(SelectedEntry {
3631 worktree_id: tree_id,
3632 entry_id: found.id,
3633 });
3634 }
3635 }
3636
3637 last_found
3638 }
3639
3640 fn find_visible_entry(
3641 &self,
3642 start: Option<&SelectedEntry>,
3643 reverse_search: bool,
3644 predicate: impl Fn(GitEntryRef, WorktreeId) -> bool,
3645 cx: &mut Context<Self>,
3646 ) -> Option<SelectedEntry> {
3647 let mut worktree_ids: Vec<_> = self
3648 .visible_entries
3649 .iter()
3650 .map(|(worktree_id, _, _)| *worktree_id)
3651 .collect();
3652
3653 let mut last_found: Option<SelectedEntry> = None;
3654
3655 if let Some(start) = start {
3656 let entries = self
3657 .visible_entries
3658 .iter()
3659 .find(|(worktree_id, _, _)| *worktree_id == start.worktree_id)
3660 .map(|(_, entries, _)| entries)?;
3661
3662 let mut start_idx = entries
3663 .iter()
3664 .enumerate()
3665 .find(|(_, ele)| ele.id == start.entry_id)
3666 .map(|(idx, _)| idx)?;
3667
3668 if reverse_search {
3669 start_idx = start_idx.saturating_add(1usize);
3670 }
3671
3672 let (left, right) = entries.split_at_checked(start_idx)?;
3673
3674 let (first_iter, second_iter) = if reverse_search {
3675 (
3676 utils::ReversibleIterable::new(left.iter(), reverse_search),
3677 utils::ReversibleIterable::new(right.iter(), reverse_search),
3678 )
3679 } else {
3680 (
3681 utils::ReversibleIterable::new(right.iter(), reverse_search),
3682 utils::ReversibleIterable::new(left.iter(), reverse_search),
3683 )
3684 };
3685
3686 let first_search = first_iter.find(|ele| predicate(ele.to_ref(), start.worktree_id));
3687 let second_search = second_iter.find(|ele| predicate(ele.to_ref(), start.worktree_id));
3688
3689 if first_search.is_some() {
3690 return first_search.map(|entry| SelectedEntry {
3691 worktree_id: start.worktree_id,
3692 entry_id: entry.id,
3693 });
3694 }
3695
3696 last_found = second_search.map(|entry| SelectedEntry {
3697 worktree_id: start.worktree_id,
3698 entry_id: entry.id,
3699 });
3700
3701 let idx = worktree_ids
3702 .iter()
3703 .enumerate()
3704 .find(|(_, ele)| **ele == start.worktree_id)
3705 .map(|(idx, _)| idx);
3706
3707 if let Some(idx) = idx {
3708 worktree_ids.rotate_left(idx + 1usize);
3709 worktree_ids.pop();
3710 }
3711 }
3712
3713 for tree_id in worktree_ids.into_iter() {
3714 if let Some(found) =
3715 self.find_entry_in_worktree(tree_id, reverse_search, true, &predicate, cx)
3716 {
3717 return Some(SelectedEntry {
3718 worktree_id: tree_id,
3719 entry_id: found.id,
3720 });
3721 }
3722 }
3723
3724 last_found
3725 }
3726
3727 fn calculate_depth_and_difference(
3728 entry: &Entry,
3729 visible_worktree_entries: &HashSet<Arc<Path>>,
3730 ) -> (usize, usize) {
3731 let (depth, difference) = entry
3732 .path
3733 .ancestors()
3734 .skip(1) // Skip the entry itself
3735 .find_map(|ancestor| {
3736 if let Some(parent_entry) = visible_worktree_entries.get(ancestor) {
3737 let entry_path_components_count = entry.path.components().count();
3738 let parent_path_components_count = parent_entry.components().count();
3739 let difference = entry_path_components_count - parent_path_components_count;
3740 let depth = parent_entry
3741 .ancestors()
3742 .skip(1)
3743 .filter(|ancestor| visible_worktree_entries.contains(*ancestor))
3744 .count();
3745 Some((depth + 1, difference))
3746 } else {
3747 None
3748 }
3749 })
3750 .unwrap_or_else(|| (0, entry.path.components().count()));
3751
3752 (depth, difference)
3753 }
3754
3755 fn highlight_entry_for_external_drag(
3756 &self,
3757 target_entry: &Entry,
3758 target_worktree: &Worktree,
3759 ) -> Option<ProjectEntryId> {
3760 // Always highlight directory or parent directory if it's file
3761 if target_entry.is_dir() {
3762 Some(target_entry.id)
3763 } else if let Some(parent_entry) = target_entry
3764 .path
3765 .parent()
3766 .and_then(|parent_path| target_worktree.entry_for_path(parent_path))
3767 {
3768 Some(parent_entry.id)
3769 } else {
3770 None
3771 }
3772 }
3773
3774 fn highlight_entry_for_selection_drag(
3775 &self,
3776 target_entry: &Entry,
3777 target_worktree: &Worktree,
3778 drag_state: &DraggedSelection,
3779 cx: &Context<Self>,
3780 ) -> Option<ProjectEntryId> {
3781 let target_parent_path = target_entry.path.parent();
3782
3783 // In case of single item drag, we do not highlight existing
3784 // directory which item belongs too
3785 if drag_state.items().count() == 1 {
3786 let active_entry_path = self
3787 .project
3788 .read(cx)
3789 .path_for_entry(drag_state.active_selection.entry_id, cx)?;
3790
3791 if let Some(active_parent_path) = active_entry_path.path.parent() {
3792 // Do not highlight active entry parent
3793 if active_parent_path == target_entry.path.as_ref() {
3794 return None;
3795 }
3796
3797 // Do not highlight active entry sibling files
3798 if Some(active_parent_path) == target_parent_path && target_entry.is_file() {
3799 return None;
3800 }
3801 }
3802 }
3803
3804 // Always highlight directory or parent directory if it's file
3805 if target_entry.is_dir() {
3806 Some(target_entry.id)
3807 } else if let Some(parent_entry) =
3808 target_parent_path.and_then(|parent_path| target_worktree.entry_for_path(parent_path))
3809 {
3810 Some(parent_entry.id)
3811 } else {
3812 None
3813 }
3814 }
3815
3816 fn render_entry(
3817 &self,
3818 entry_id: ProjectEntryId,
3819 details: EntryDetails,
3820 window: &mut Window,
3821 cx: &mut Context<Self>,
3822 ) -> Stateful<Div> {
3823 const GROUP_NAME: &str = "project_entry";
3824
3825 let kind = details.kind;
3826 let is_sticky = details.sticky.is_some();
3827 let sticky_index = details.sticky.as_ref().map(|this| this.sticky_index);
3828 let settings = ProjectPanelSettings::get_global(cx);
3829 let show_editor = details.is_editing && !details.is_processing;
3830
3831 let selection = SelectedEntry {
3832 worktree_id: details.worktree_id,
3833 entry_id,
3834 };
3835
3836 let is_marked = self.marked_entries.contains(&selection);
3837 let is_active = self
3838 .selection
3839 .map_or(false, |selection| selection.entry_id == entry_id);
3840
3841 let file_name = details.filename.clone();
3842
3843 let mut icon = details.icon.clone();
3844 if settings.file_icons && show_editor && details.kind.is_file() {
3845 let filename = self.filename_editor.read(cx).text(cx);
3846 if filename.len() > 2 {
3847 icon = FileIcons::get_icon(Path::new(&filename), cx);
3848 }
3849 }
3850
3851 let filename_text_color = details.filename_text_color;
3852 let diagnostic_severity = details.diagnostic_severity;
3853 let item_colors = get_item_color(cx);
3854
3855 let canonical_path = details
3856 .canonical_path
3857 .as_ref()
3858 .map(|f| f.to_string_lossy().to_string());
3859 let path = details.path.clone();
3860 let path_for_external_paths = path.clone();
3861 let path_for_dragged_selection = path.clone();
3862
3863 let depth = details.depth;
3864 let worktree_id = details.worktree_id;
3865 let selections = Arc::new(self.marked_entries.clone());
3866
3867 let dragged_selection = DraggedSelection {
3868 active_selection: selection,
3869 marked_selections: selections,
3870 };
3871
3872 let bg_color = if is_marked {
3873 item_colors.marked
3874 } else {
3875 item_colors.default
3876 };
3877
3878 let bg_hover_color = if is_marked {
3879 item_colors.marked
3880 } else {
3881 item_colors.hover
3882 };
3883
3884 let validation_color_and_message = if show_editor {
3885 match self
3886 .edit_state
3887 .as_ref()
3888 .map_or(ValidationState::None, |e| e.validation_state.clone())
3889 {
3890 ValidationState::Error(msg) => Some((Color::Error.color(cx), msg.clone())),
3891 ValidationState::Warning(msg) => Some((Color::Warning.color(cx), msg.clone())),
3892 ValidationState::None => None,
3893 }
3894 } else {
3895 None
3896 };
3897
3898 let border_color =
3899 if !self.mouse_down && is_active && self.focus_handle.contains_focused(window, cx) {
3900 match validation_color_and_message {
3901 Some((color, _)) => color,
3902 None => item_colors.focused,
3903 }
3904 } else {
3905 bg_color
3906 };
3907
3908 let border_hover_color =
3909 if !self.mouse_down && is_active && self.focus_handle.contains_focused(window, cx) {
3910 match validation_color_and_message {
3911 Some((color, _)) => color,
3912 None => item_colors.focused,
3913 }
3914 } else {
3915 bg_hover_color
3916 };
3917
3918 let folded_directory_drag_target = self.folded_directory_drag_target;
3919 let is_highlighted = {
3920 if let Some(highlight_entry_id) = self
3921 .drag_target_entry
3922 .as_ref()
3923 .and_then(|drag_target| drag_target.highlight_entry_id)
3924 {
3925 // Highlight if same entry or it's children
3926 if entry_id == highlight_entry_id {
3927 true
3928 } else {
3929 maybe!({
3930 let worktree = self.project.read(cx).worktree_for_id(worktree_id, cx)?;
3931 let highlight_entry = worktree.read(cx).entry_for_id(highlight_entry_id)?;
3932 Some(path.starts_with(&highlight_entry.path))
3933 })
3934 .unwrap_or(false)
3935 }
3936 } else {
3937 false
3938 }
3939 };
3940
3941 let show_sticky_shadow = details.sticky.as_ref().map_or(false, |item| {
3942 if item.is_last {
3943 let is_scrollable = self.scroll_handle.is_scrollable();
3944 let is_scrolled = self.scroll_handle.offset().y < px(0.);
3945 is_scrollable && is_scrolled
3946 } else {
3947 false
3948 }
3949 });
3950 let shadow_color_top = hsla(0.0, 0.0, 0.0, 0.15);
3951 let shadow_color_bottom = hsla(0.0, 0.0, 0.0, 0.);
3952 let sticky_shadow = div()
3953 .absolute()
3954 .left_0()
3955 .bottom_neg_1p5()
3956 .h_1p5()
3957 .w_full()
3958 .bg(linear_gradient(
3959 0.,
3960 linear_color_stop(shadow_color_top, 1.),
3961 linear_color_stop(shadow_color_bottom, 0.),
3962 ));
3963
3964 div()
3965 .id(entry_id.to_proto() as usize)
3966 .relative()
3967 .group(GROUP_NAME)
3968 .cursor_pointer()
3969 .rounded_none()
3970 .bg(bg_color)
3971 .border_1()
3972 .border_r_2()
3973 .border_color(border_color)
3974 .hover(|style| style.bg(bg_hover_color).border_color(border_hover_color))
3975 .when(show_sticky_shadow, |this| this.child(sticky_shadow))
3976 .when(!is_sticky, |this| {
3977 this
3978 .when(is_highlighted && folded_directory_drag_target.is_none(), |this| this.border_color(transparent_white()).bg(item_colors.drag_over))
3979 .on_drag_move::<ExternalPaths>(cx.listener(
3980 move |this, event: &DragMoveEvent<ExternalPaths>, _, cx| {
3981 let is_current_target = this.drag_target_entry.as_ref()
3982 .map(|entry| entry.entry_id) == Some(entry_id);
3983
3984 if !event.bounds.contains(&event.event.position) {
3985 // Entry responsible for setting drag target is also responsible to
3986 // clear it up after drag is out of bounds
3987 if is_current_target {
3988 this.drag_target_entry = None;
3989 }
3990 return;
3991 }
3992
3993 if is_current_target {
3994 return;
3995 }
3996
3997 let Some((entry_id, highlight_entry_id)) = maybe!({
3998 let target_worktree = this.project.read(cx).worktree_for_id(selection.worktree_id, cx)?.read(cx);
3999 let target_entry = target_worktree.entry_for_path(&path_for_external_paths)?;
4000 let highlight_entry_id = this.highlight_entry_for_external_drag(target_entry, target_worktree);
4001 Some((target_entry.id, highlight_entry_id))
4002 }) else {
4003 return;
4004 };
4005
4006 this.drag_target_entry = Some(DragTargetEntry {
4007 entry_id,
4008 highlight_entry_id,
4009 });
4010 this.marked_entries.clear();
4011 },
4012 ))
4013 .on_drop(cx.listener(
4014 move |this, external_paths: &ExternalPaths, window, cx| {
4015 this.drag_target_entry = None;
4016 this.hover_scroll_task.take();
4017 this.drop_external_files(external_paths.paths(), entry_id, window, cx);
4018 cx.stop_propagation();
4019 },
4020 ))
4021 .on_drag_move::<DraggedSelection>(cx.listener(
4022 move |this, event: &DragMoveEvent<DraggedSelection>, window, cx| {
4023 let is_current_target = this.drag_target_entry.as_ref()
4024 .map(|entry| entry.entry_id) == Some(entry_id);
4025
4026 if !event.bounds.contains(&event.event.position) {
4027 // Entry responsible for setting drag target is also responsible to
4028 // clear it up after drag is out of bounds
4029 if is_current_target {
4030 this.drag_target_entry = None;
4031 }
4032 return;
4033 }
4034
4035 if is_current_target {
4036 return;
4037 }
4038
4039 let drag_state = event.drag(cx);
4040 let Some((entry_id, highlight_entry_id)) = maybe!({
4041 let target_worktree = this.project.read(cx).worktree_for_id(selection.worktree_id, cx)?.read(cx);
4042 let target_entry = target_worktree.entry_for_path(&path_for_dragged_selection)?;
4043 let highlight_entry_id = this.highlight_entry_for_selection_drag(target_entry, target_worktree, drag_state, cx);
4044 Some((target_entry.id, highlight_entry_id))
4045 }) else {
4046 return;
4047 };
4048
4049 this.drag_target_entry = Some(DragTargetEntry {
4050 entry_id,
4051 highlight_entry_id,
4052 });
4053 if drag_state.items().count() == 1 {
4054 this.marked_entries.clear();
4055 this.marked_entries.insert(drag_state.active_selection);
4056 }
4057 this.hover_expand_task.take();
4058
4059 if !kind.is_dir()
4060 || this
4061 .expanded_dir_ids
4062 .get(&details.worktree_id)
4063 .map_or(false, |ids| ids.binary_search(&entry_id).is_ok())
4064 {
4065 return;
4066 }
4067
4068 let bounds = event.bounds;
4069 this.hover_expand_task =
4070 Some(cx.spawn_in(window, async move |this, cx| {
4071 cx.background_executor()
4072 .timer(Duration::from_millis(500))
4073 .await;
4074 this.update_in(cx, |this, window, cx| {
4075 this.hover_expand_task.take();
4076 if this.drag_target_entry.as_ref().map(|entry| entry.entry_id) == Some(entry_id)
4077 && bounds.contains(&window.mouse_position())
4078 {
4079 this.expand_entry(worktree_id, entry_id, cx);
4080 this.update_visible_entries(
4081 Some((worktree_id, entry_id)),
4082 cx,
4083 );
4084 cx.notify();
4085 }
4086 })
4087 .ok();
4088 }));
4089 },
4090 ))
4091 .on_drag(
4092 dragged_selection,
4093 move |selection, click_offset, _window, cx| {
4094 cx.new(|_| DraggedProjectEntryView {
4095 details: details.clone(),
4096 click_offset,
4097 selection: selection.active_selection,
4098 selections: selection.marked_selections.clone(),
4099 })
4100 },
4101 )
4102 .on_drop(
4103 cx.listener(move |this, selections: &DraggedSelection, window, cx| {
4104 this.drag_target_entry = None;
4105 this.hover_scroll_task.take();
4106 this.hover_expand_task.take();
4107 if folded_directory_drag_target.is_some() {
4108 return;
4109 }
4110 this.drag_onto(selections, entry_id, kind.is_file(), window, cx);
4111 }),
4112 )
4113 })
4114 .on_mouse_down(
4115 MouseButton::Left,
4116 cx.listener(move |this, _, _, cx| {
4117 this.mouse_down = true;
4118 cx.propagate();
4119 }),
4120 )
4121 .on_click(
4122 cx.listener(move |this, event: &gpui::ClickEvent, window, cx| {
4123 if event.down.button == MouseButton::Right
4124 || event.down.first_mouse
4125 || show_editor
4126 {
4127 return;
4128 }
4129 if event.down.button == MouseButton::Left {
4130 this.mouse_down = false;
4131 }
4132 cx.stop_propagation();
4133
4134 if let Some(selection) = this.selection.filter(|_| event.modifiers().shift) {
4135 let current_selection = this.index_for_selection(selection);
4136 let clicked_entry = SelectedEntry {
4137 entry_id,
4138 worktree_id,
4139 };
4140 let target_selection = this.index_for_selection(clicked_entry);
4141 if let Some(((_, _, source_index), (_, _, target_index))) =
4142 current_selection.zip(target_selection)
4143 {
4144 let range_start = source_index.min(target_index);
4145 let range_end = source_index.max(target_index) + 1;
4146 let mut new_selections = BTreeSet::new();
4147 this.for_each_visible_entry(
4148 range_start..range_end,
4149 window,
4150 cx,
4151 |entry_id, details, _, _| {
4152 new_selections.insert(SelectedEntry {
4153 entry_id,
4154 worktree_id: details.worktree_id,
4155 });
4156 },
4157 );
4158
4159 this.marked_entries = this
4160 .marked_entries
4161 .union(&new_selections)
4162 .cloned()
4163 .collect();
4164
4165 this.selection = Some(clicked_entry);
4166 this.marked_entries.insert(clicked_entry);
4167 }
4168 } else if event.modifiers().secondary() {
4169 if event.down.click_count > 1 {
4170 this.split_entry(entry_id, cx);
4171 } else {
4172 this.selection = Some(selection);
4173 if !this.marked_entries.insert(selection) {
4174 this.marked_entries.remove(&selection);
4175 }
4176 }
4177 } else if kind.is_dir() {
4178 this.marked_entries.clear();
4179 if event.modifiers().alt {
4180 this.toggle_expand_all(entry_id, window, cx);
4181 } else {
4182 this.toggle_expanded(entry_id, window, cx);
4183 }
4184 } else {
4185 let preview_tabs_enabled = PreviewTabsSettings::get_global(cx).enabled;
4186 let click_count = event.up.click_count;
4187 let focus_opened_item = !preview_tabs_enabled || click_count > 1;
4188 let allow_preview = preview_tabs_enabled && click_count == 1;
4189 this.open_entry(entry_id, focus_opened_item, allow_preview, cx);
4190 }
4191
4192 if is_sticky {
4193 if let Some((_, _, index)) = this.index_for_entry(entry_id, worktree_id) {
4194 let strategy = sticky_index
4195 .map(ScrollStrategy::ToPosition)
4196 .unwrap_or(ScrollStrategy::Top);
4197 this.scroll_handle.scroll_to_item(index, strategy);
4198 cx.notify();
4199 }
4200 }
4201 }),
4202 )
4203 .child(
4204 ListItem::new(entry_id.to_proto() as usize)
4205 .indent_level(depth)
4206 .indent_step_size(px(settings.indent_size))
4207 .spacing(match settings.entry_spacing {
4208 project_panel_settings::EntrySpacing::Comfortable => ListItemSpacing::Dense,
4209 project_panel_settings::EntrySpacing::Standard => {
4210 ListItemSpacing::ExtraDense
4211 }
4212 })
4213 .selectable(false)
4214 .when_some(canonical_path, |this, path| {
4215 this.end_slot::<AnyElement>(
4216 div()
4217 .id("symlink_icon")
4218 .pr_3()
4219 .tooltip(move |window, cx| {
4220 Tooltip::with_meta(
4221 path.to_string(),
4222 None,
4223 "Symbolic Link",
4224 window,
4225 cx,
4226 )
4227 })
4228 .child(
4229 Icon::new(IconName::ArrowUpRight)
4230 .size(IconSize::Indicator)
4231 .color(filename_text_color),
4232 )
4233 .into_any_element(),
4234 )
4235 })
4236 .child(if let Some(icon) = &icon {
4237 if let Some((_, decoration_color)) =
4238 entry_diagnostic_aware_icon_decoration_and_color(diagnostic_severity)
4239 {
4240 let is_warning = diagnostic_severity
4241 .map(|severity| matches!(severity, DiagnosticSeverity::WARNING))
4242 .unwrap_or(false);
4243 div().child(
4244 DecoratedIcon::new(
4245 Icon::from_path(icon.clone()).color(Color::Muted),
4246 Some(
4247 IconDecoration::new(
4248 if kind.is_file() {
4249 if is_warning {
4250 IconDecorationKind::Triangle
4251 } else {
4252 IconDecorationKind::X
4253 }
4254 } else {
4255 IconDecorationKind::Dot
4256 },
4257 bg_color,
4258 cx,
4259 )
4260 .group_name(Some(GROUP_NAME.into()))
4261 .knockout_hover_color(bg_hover_color)
4262 .color(decoration_color.color(cx))
4263 .position(Point {
4264 x: px(-2.),
4265 y: px(-2.),
4266 }),
4267 ),
4268 )
4269 .into_any_element(),
4270 )
4271 } else {
4272 h_flex().child(Icon::from_path(icon.to_string()).color(Color::Muted))
4273 }
4274 } else {
4275 if let Some((icon_name, color)) =
4276 entry_diagnostic_aware_icon_name_and_color(diagnostic_severity)
4277 {
4278 h_flex()
4279 .size(IconSize::default().rems())
4280 .child(Icon::new(icon_name).color(color).size(IconSize::Small))
4281 } else {
4282 h_flex()
4283 .size(IconSize::default().rems())
4284 .invisible()
4285 .flex_none()
4286 }
4287 })
4288 .child(
4289 if let (Some(editor), true) = (Some(&self.filename_editor), show_editor) {
4290 h_flex().h_6().w_full().child(editor.clone())
4291 } else {
4292 h_flex().h_6().map(|mut this| {
4293 if let Some(folded_ancestors) = self.ancestors.get(&entry_id) {
4294 let components = Path::new(&file_name)
4295 .components()
4296 .map(|comp| {
4297 let comp_str =
4298 comp.as_os_str().to_string_lossy().into_owned();
4299 comp_str
4300 })
4301 .collect::<Vec<_>>();
4302
4303 let components_len = components.len();
4304 let active_index = components_len
4305 - 1
4306 - folded_ancestors.current_ancestor_depth;
4307 const DELIMITER: SharedString =
4308 SharedString::new_static(std::path::MAIN_SEPARATOR_STR);
4309 for (index, component) in components.into_iter().enumerate() {
4310 if index != 0 {
4311 let delimiter_target_index = index - 1;
4312 let target_entry_id = folded_ancestors.ancestors.get(components_len - 1 - delimiter_target_index).cloned();
4313 this = this.child(
4314 div()
4315 .when(!is_sticky, |div| {
4316 div
4317 .on_drop(cx.listener(move |this, selections: &DraggedSelection, window, cx| {
4318 this.hover_scroll_task.take();
4319 this.drag_target_entry = None;
4320 this.folded_directory_drag_target = None;
4321 if let Some(target_entry_id) = target_entry_id {
4322 this.drag_onto(selections, target_entry_id, kind.is_file(), window, cx);
4323 }
4324 }))
4325 .on_drag_move(cx.listener(
4326 move |this, event: &DragMoveEvent<DraggedSelection>, _, _| {
4327 if event.bounds.contains(&event.event.position) {
4328 this.folded_directory_drag_target = Some(
4329 FoldedDirectoryDragTarget {
4330 entry_id,
4331 index: delimiter_target_index,
4332 is_delimiter_target: true,
4333 }
4334 );
4335 } else {
4336 let is_current_target = this.folded_directory_drag_target
4337 .map_or(false, |target|
4338 target.entry_id == entry_id &&
4339 target.index == delimiter_target_index &&
4340 target.is_delimiter_target
4341 );
4342 if is_current_target {
4343 this.folded_directory_drag_target = None;
4344 }
4345 }
4346
4347 },
4348 ))
4349 })
4350 .child(
4351 Label::new(DELIMITER.clone())
4352 .single_line()
4353 .color(filename_text_color)
4354 )
4355 );
4356 }
4357 let id = SharedString::from(format!(
4358 "project_panel_path_component_{}_{index}",
4359 entry_id.to_usize()
4360 ));
4361 let label = div()
4362 .id(id)
4363 .when(!is_sticky,| div| {
4364 div
4365 .when(index != components_len - 1, |div|{
4366 let target_entry_id = folded_ancestors.ancestors.get(components_len - 1 - index).cloned();
4367 div
4368 .on_drag_move(cx.listener(
4369 move |this, event: &DragMoveEvent<DraggedSelection>, _, _| {
4370 if event.bounds.contains(&event.event.position) {
4371 this.folded_directory_drag_target = Some(
4372 FoldedDirectoryDragTarget {
4373 entry_id,
4374 index,
4375 is_delimiter_target: false,
4376 }
4377 );
4378 } else {
4379 let is_current_target = this.folded_directory_drag_target
4380 .as_ref()
4381 .map_or(false, |target|
4382 target.entry_id == entry_id &&
4383 target.index == index &&
4384 !target.is_delimiter_target
4385 );
4386 if is_current_target {
4387 this.folded_directory_drag_target = None;
4388 }
4389 }
4390 },
4391 ))
4392 .on_drop(cx.listener(move |this, selections: &DraggedSelection, window,cx| {
4393 this.hover_scroll_task.take();
4394 this.drag_target_entry = None;
4395 this.folded_directory_drag_target = None;
4396 if let Some(target_entry_id) = target_entry_id {
4397 this.drag_onto(selections, target_entry_id, kind.is_file(), window, cx);
4398 }
4399 }))
4400 .when(folded_directory_drag_target.map_or(false, |target|
4401 target.entry_id == entry_id &&
4402 target.index == index
4403 ), |this| {
4404 this.bg(item_colors.drag_over)
4405 })
4406 })
4407 })
4408 .on_click(cx.listener(move |this, _, _, cx| {
4409 if index != active_index {
4410 if let Some(folds) =
4411 this.ancestors.get_mut(&entry_id)
4412 {
4413 folds.current_ancestor_depth =
4414 components_len - 1 - index;
4415 cx.notify();
4416 }
4417 }
4418 }))
4419 .child(
4420 Label::new(component)
4421 .single_line()
4422 .color(filename_text_color)
4423 .when(
4424 index == active_index
4425 && (is_active || is_marked),
4426 |this| this.underline(),
4427 ),
4428 );
4429
4430 this = this.child(label);
4431 }
4432
4433 this
4434 } else {
4435 this.child(
4436 Label::new(file_name)
4437 .single_line()
4438 .color(filename_text_color),
4439 )
4440 }
4441 })
4442 },
4443 )
4444 .on_secondary_mouse_down(cx.listener(
4445 move |this, event: &MouseDownEvent, window, cx| {
4446 // Stop propagation to prevent the catch-all context menu for the project
4447 // panel from being deployed.
4448 cx.stop_propagation();
4449 // Some context menu actions apply to all marked entries. If the user
4450 // right-clicks on an entry that is not marked, they may not realize the
4451 // action applies to multiple entries. To avoid inadvertent changes, all
4452 // entries are unmarked.
4453 if !this.marked_entries.contains(&selection) {
4454 this.marked_entries.clear();
4455 }
4456 this.deploy_context_menu(event.position, entry_id, window, cx);
4457 },
4458 ))
4459 .overflow_x(),
4460 )
4461 .when_some(
4462 validation_color_and_message,
4463 |this, (color, message)| {
4464 this
4465 .relative()
4466 .child(
4467 deferred(
4468 div()
4469 .occlude()
4470 .absolute()
4471 .top_full()
4472 .left(px(-1.)) // Used px over rem so that it doesn't change with font size
4473 .right(px(-0.5))
4474 .py_1()
4475 .px_2()
4476 .border_1()
4477 .border_color(color)
4478 .bg(cx.theme().colors().background)
4479 .child(
4480 Label::new(message)
4481 .color(Color::from(color))
4482 .size(LabelSize::Small)
4483 )
4484 )
4485 )
4486 }
4487 )
4488 }
4489
4490 fn details_for_entry(
4491 &self,
4492 entry: &Entry,
4493 worktree_id: WorktreeId,
4494 root_name: &OsStr,
4495 entries_paths: &HashSet<Arc<Path>>,
4496 git_status: GitSummary,
4497 sticky: Option<StickyDetails>,
4498 _window: &mut Window,
4499 cx: &mut Context<Self>,
4500 ) -> EntryDetails {
4501 let (show_file_icons, show_folder_icons) = {
4502 let settings = ProjectPanelSettings::get_global(cx);
4503 (settings.file_icons, settings.folder_icons)
4504 };
4505
4506 let expanded_entry_ids = self
4507 .expanded_dir_ids
4508 .get(&worktree_id)
4509 .map(Vec::as_slice)
4510 .unwrap_or(&[]);
4511 let is_expanded = expanded_entry_ids.binary_search(&entry.id).is_ok();
4512
4513 let icon = match entry.kind {
4514 EntryKind::File => {
4515 if show_file_icons {
4516 FileIcons::get_icon(&entry.path, cx)
4517 } else {
4518 None
4519 }
4520 }
4521 _ => {
4522 if show_folder_icons {
4523 FileIcons::get_folder_icon(is_expanded, cx)
4524 } else {
4525 FileIcons::get_chevron_icon(is_expanded, cx)
4526 }
4527 }
4528 };
4529
4530 let (depth, difference) =
4531 ProjectPanel::calculate_depth_and_difference(&entry, entries_paths);
4532
4533 let filename = match difference {
4534 diff if diff > 1 => entry
4535 .path
4536 .iter()
4537 .skip(entry.path.components().count() - diff)
4538 .collect::<PathBuf>()
4539 .to_str()
4540 .unwrap_or_default()
4541 .to_string(),
4542 _ => entry
4543 .path
4544 .file_name()
4545 .map(|name| name.to_string_lossy().into_owned())
4546 .unwrap_or_else(|| root_name.to_string_lossy().to_string()),
4547 };
4548
4549 let selection = SelectedEntry {
4550 worktree_id,
4551 entry_id: entry.id,
4552 };
4553 let is_marked = self.marked_entries.contains(&selection);
4554 let is_selected = self.selection == Some(selection);
4555
4556 let diagnostic_severity = self
4557 .diagnostics
4558 .get(&(worktree_id, entry.path.to_path_buf()))
4559 .cloned();
4560
4561 let filename_text_color =
4562 entry_git_aware_label_color(git_status, entry.is_ignored, is_marked);
4563
4564 let is_cut = self
4565 .clipboard
4566 .as_ref()
4567 .map_or(false, |e| e.is_cut() && e.items().contains(&selection));
4568
4569 EntryDetails {
4570 filename,
4571 icon,
4572 path: entry.path.clone(),
4573 depth,
4574 kind: entry.kind,
4575 is_ignored: entry.is_ignored,
4576 is_expanded,
4577 is_selected,
4578 is_marked,
4579 is_editing: false,
4580 is_processing: false,
4581 is_cut,
4582 sticky,
4583 filename_text_color,
4584 diagnostic_severity,
4585 git_status,
4586 is_private: entry.is_private,
4587 worktree_id,
4588 canonical_path: entry.canonical_path.clone(),
4589 }
4590 }
4591
4592 fn render_vertical_scrollbar(&self, cx: &mut Context<Self>) -> Option<Stateful<Div>> {
4593 if !Self::should_show_scrollbar(cx)
4594 || !(self.show_scrollbar || self.vertical_scrollbar_state.is_dragging())
4595 {
4596 return None;
4597 }
4598 Some(
4599 div()
4600 .occlude()
4601 .id("project-panel-vertical-scroll")
4602 .on_mouse_move(cx.listener(|_, _, _, cx| {
4603 cx.notify();
4604 cx.stop_propagation()
4605 }))
4606 .on_hover(|_, _, cx| {
4607 cx.stop_propagation();
4608 })
4609 .on_any_mouse_down(|_, _, cx| {
4610 cx.stop_propagation();
4611 })
4612 .on_mouse_up(
4613 MouseButton::Left,
4614 cx.listener(|this, _, window, cx| {
4615 if !this.vertical_scrollbar_state.is_dragging()
4616 && !this.focus_handle.contains_focused(window, cx)
4617 {
4618 this.hide_scrollbar(window, cx);
4619 cx.notify();
4620 }
4621
4622 cx.stop_propagation();
4623 }),
4624 )
4625 .on_scroll_wheel(cx.listener(|_, _, _, cx| {
4626 cx.notify();
4627 }))
4628 .h_full()
4629 .absolute()
4630 .right_1()
4631 .top_1()
4632 .bottom_1()
4633 .w(px(12.))
4634 .cursor_default()
4635 .children(Scrollbar::vertical(
4636 // percentage as f32..end_offset as f32,
4637 self.vertical_scrollbar_state.clone(),
4638 )),
4639 )
4640 }
4641
4642 fn render_horizontal_scrollbar(&self, cx: &mut Context<Self>) -> Option<Stateful<Div>> {
4643 if !Self::should_show_scrollbar(cx)
4644 || !(self.show_scrollbar || self.horizontal_scrollbar_state.is_dragging())
4645 {
4646 return None;
4647 }
4648 Scrollbar::horizontal(self.horizontal_scrollbar_state.clone()).map(|scrollbar| {
4649 div()
4650 .occlude()
4651 .id("project-panel-horizontal-scroll")
4652 .on_mouse_move(cx.listener(|_, _, _, cx| {
4653 cx.notify();
4654 cx.stop_propagation()
4655 }))
4656 .on_hover(|_, _, cx| {
4657 cx.stop_propagation();
4658 })
4659 .on_any_mouse_down(|_, _, cx| {
4660 cx.stop_propagation();
4661 })
4662 .on_mouse_up(
4663 MouseButton::Left,
4664 cx.listener(|this, _, window, cx| {
4665 if !this.horizontal_scrollbar_state.is_dragging()
4666 && !this.focus_handle.contains_focused(window, cx)
4667 {
4668 this.hide_scrollbar(window, cx);
4669 cx.notify();
4670 }
4671
4672 cx.stop_propagation();
4673 }),
4674 )
4675 .on_scroll_wheel(cx.listener(|_, _, _, cx| {
4676 cx.notify();
4677 }))
4678 .w_full()
4679 .absolute()
4680 .right_1()
4681 .left_1()
4682 .bottom_1()
4683 .h(px(12.))
4684 .cursor_default()
4685 .child(scrollbar)
4686 })
4687 }
4688
4689 fn dispatch_context(&self, window: &Window, cx: &Context<Self>) -> KeyContext {
4690 let mut dispatch_context = KeyContext::new_with_defaults();
4691 dispatch_context.add("ProjectPanel");
4692 dispatch_context.add("menu");
4693
4694 let identifier = if self.filename_editor.focus_handle(cx).is_focused(window) {
4695 "editing"
4696 } else {
4697 "not_editing"
4698 };
4699
4700 dispatch_context.add(identifier);
4701 dispatch_context
4702 }
4703
4704 fn should_show_scrollbar(cx: &App) -> bool {
4705 let show = ProjectPanelSettings::get_global(cx)
4706 .scrollbar
4707 .show
4708 .unwrap_or_else(|| EditorSettings::get_global(cx).scrollbar.show);
4709 match show {
4710 ShowScrollbar::Auto => true,
4711 ShowScrollbar::System => true,
4712 ShowScrollbar::Always => true,
4713 ShowScrollbar::Never => false,
4714 }
4715 }
4716
4717 fn should_autohide_scrollbar(cx: &App) -> bool {
4718 let show = ProjectPanelSettings::get_global(cx)
4719 .scrollbar
4720 .show
4721 .unwrap_or_else(|| EditorSettings::get_global(cx).scrollbar.show);
4722 match show {
4723 ShowScrollbar::Auto => true,
4724 ShowScrollbar::System => cx
4725 .try_global::<ScrollbarAutoHide>()
4726 .map_or_else(|| cx.should_auto_hide_scrollbars(), |autohide| autohide.0),
4727 ShowScrollbar::Always => false,
4728 ShowScrollbar::Never => true,
4729 }
4730 }
4731
4732 fn hide_scrollbar(&mut self, window: &mut Window, cx: &mut Context<Self>) {
4733 const SCROLLBAR_SHOW_INTERVAL: Duration = Duration::from_secs(1);
4734 if !Self::should_autohide_scrollbar(cx) {
4735 return;
4736 }
4737 self.hide_scrollbar_task = Some(cx.spawn_in(window, async move |panel, cx| {
4738 cx.background_executor()
4739 .timer(SCROLLBAR_SHOW_INTERVAL)
4740 .await;
4741 panel
4742 .update(cx, |panel, cx| {
4743 panel.show_scrollbar = false;
4744 cx.notify();
4745 })
4746 .log_err();
4747 }))
4748 }
4749
4750 fn reveal_entry(
4751 &mut self,
4752 project: Entity<Project>,
4753 entry_id: ProjectEntryId,
4754 skip_ignored: bool,
4755 cx: &mut Context<Self>,
4756 ) -> Result<()> {
4757 let worktree = project
4758 .read(cx)
4759 .worktree_for_entry(entry_id, cx)
4760 .context("can't reveal a non-existent entry in the project panel")?;
4761 let worktree = worktree.read(cx);
4762 if skip_ignored
4763 && worktree
4764 .entry_for_id(entry_id)
4765 .map_or(true, |entry| entry.is_ignored && !entry.is_always_included)
4766 {
4767 anyhow::bail!("can't reveal an ignored entry in the project panel");
4768 }
4769
4770 let worktree_id = worktree.id();
4771 self.expand_entry(worktree_id, entry_id, cx);
4772 self.update_visible_entries(Some((worktree_id, entry_id)), cx);
4773 self.marked_entries.clear();
4774 self.marked_entries.insert(SelectedEntry {
4775 worktree_id,
4776 entry_id,
4777 });
4778 self.autoscroll(cx);
4779 cx.notify();
4780 Ok(())
4781 }
4782
4783 fn find_active_indent_guide(
4784 &self,
4785 indent_guides: &[IndentGuideLayout],
4786 cx: &App,
4787 ) -> Option<usize> {
4788 let (worktree, entry) = self.selected_entry(cx)?;
4789
4790 // Find the parent entry of the indent guide, this will either be the
4791 // expanded folder we have selected, or the parent of the currently
4792 // selected file/collapsed directory
4793 let mut entry = entry;
4794 loop {
4795 let is_expanded_dir = entry.is_dir()
4796 && self
4797 .expanded_dir_ids
4798 .get(&worktree.id())
4799 .map(|ids| ids.binary_search(&entry.id).is_ok())
4800 .unwrap_or(false);
4801 if is_expanded_dir {
4802 break;
4803 }
4804 entry = worktree.entry_for_path(&entry.path.parent()?)?;
4805 }
4806
4807 let (active_indent_range, depth) = {
4808 let (worktree_ix, child_offset, ix) = self.index_for_entry(entry.id, worktree.id())?;
4809 let child_paths = &self.visible_entries[worktree_ix].1;
4810 let mut child_count = 0;
4811 let depth = entry.path.ancestors().count();
4812 while let Some(entry) = child_paths.get(child_offset + child_count + 1) {
4813 if entry.path.ancestors().count() <= depth {
4814 break;
4815 }
4816 child_count += 1;
4817 }
4818
4819 let start = ix + 1;
4820 let end = start + child_count;
4821
4822 let (_, entries, paths) = &self.visible_entries[worktree_ix];
4823 let visible_worktree_entries =
4824 paths.get_or_init(|| entries.iter().map(|e| (e.path.clone())).collect());
4825
4826 // Calculate the actual depth of the entry, taking into account that directories can be auto-folded.
4827 let (depth, _) = Self::calculate_depth_and_difference(entry, visible_worktree_entries);
4828 (start..end, depth)
4829 };
4830
4831 let candidates = indent_guides
4832 .iter()
4833 .enumerate()
4834 .filter(|(_, indent_guide)| indent_guide.offset.x == depth);
4835
4836 for (i, indent) in candidates {
4837 // Find matches that are either an exact match, partially on screen, or inside the enclosing indent
4838 if active_indent_range.start <= indent.offset.y + indent.length
4839 && indent.offset.y <= active_indent_range.end
4840 {
4841 return Some(i);
4842 }
4843 }
4844 None
4845 }
4846
4847 fn render_sticky_entries(
4848 &self,
4849 child: StickyProjectPanelCandidate,
4850 window: &mut Window,
4851 cx: &mut Context<Self>,
4852 ) -> SmallVec<[AnyElement; 8]> {
4853 let project = self.project.read(cx);
4854
4855 let Some((worktree_id, entry_ref)) = self.entry_at_index(child.index) else {
4856 return SmallVec::new();
4857 };
4858
4859 let Some((_, visible_worktree_entries, entries_paths)) = self
4860 .visible_entries
4861 .iter()
4862 .find(|(id, _, _)| *id == worktree_id)
4863 else {
4864 return SmallVec::new();
4865 };
4866
4867 let Some(worktree) = project.worktree_for_id(worktree_id, cx) else {
4868 return SmallVec::new();
4869 };
4870 let worktree = worktree.read(cx).snapshot();
4871
4872 let paths = entries_paths.get_or_init(|| {
4873 visible_worktree_entries
4874 .iter()
4875 .map(|e| e.path.clone())
4876 .collect()
4877 });
4878
4879 let mut sticky_parents = Vec::new();
4880 let mut current_path = entry_ref.path.clone();
4881
4882 'outer: loop {
4883 if let Some(parent_path) = current_path.parent() {
4884 for ancestor_path in parent_path.ancestors() {
4885 if paths.contains(ancestor_path) {
4886 if let Some(parent_entry) = worktree.entry_for_path(ancestor_path) {
4887 sticky_parents.push(parent_entry.clone());
4888 current_path = parent_entry.path.clone();
4889 continue 'outer;
4890 }
4891 }
4892 }
4893 }
4894 break 'outer;
4895 }
4896
4897 if sticky_parents.is_empty() {
4898 return SmallVec::new();
4899 }
4900
4901 sticky_parents.reverse();
4902
4903 let git_status_enabled = ProjectPanelSettings::get_global(cx).git_status;
4904 let root_name = OsStr::new(worktree.root_name());
4905
4906 let git_summaries_by_id = if git_status_enabled {
4907 visible_worktree_entries
4908 .iter()
4909 .map(|e| (e.id, e.git_summary))
4910 .collect::<HashMap<_, _>>()
4911 } else {
4912 Default::default()
4913 };
4914
4915 // already checked if non empty above
4916 let last_item_index = sticky_parents.len() - 1;
4917 sticky_parents
4918 .iter()
4919 .enumerate()
4920 .map(|(index, entry)| {
4921 let git_status = git_summaries_by_id
4922 .get(&entry.id)
4923 .copied()
4924 .unwrap_or_default();
4925 let sticky_details = Some(StickyDetails {
4926 sticky_index: index,
4927 is_last: index == last_item_index,
4928 });
4929 let details = self.details_for_entry(
4930 entry,
4931 worktree_id,
4932 root_name,
4933 paths,
4934 git_status,
4935 sticky_details,
4936 window,
4937 cx,
4938 );
4939 self.render_entry(entry.id, details, window, cx).into_any()
4940 })
4941 .collect()
4942 }
4943}
4944
4945#[derive(Clone)]
4946struct StickyProjectPanelCandidate {
4947 index: usize,
4948 depth: usize,
4949}
4950
4951impl StickyCandidate for StickyProjectPanelCandidate {
4952 fn depth(&self) -> usize {
4953 self.depth
4954 }
4955}
4956
4957fn item_width_estimate(depth: usize, item_text_chars: usize, is_symlink: bool) -> usize {
4958 const ICON_SIZE_FACTOR: usize = 2;
4959 let mut item_width = depth * ICON_SIZE_FACTOR + item_text_chars;
4960 if is_symlink {
4961 item_width += ICON_SIZE_FACTOR;
4962 }
4963 item_width
4964}
4965
4966impl Render for ProjectPanel {
4967 fn render(&mut self, window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
4968 let has_worktree = !self.visible_entries.is_empty();
4969 let project = self.project.read(cx);
4970 let indent_size = ProjectPanelSettings::get_global(cx).indent_size;
4971 let show_indent_guides =
4972 ProjectPanelSettings::get_global(cx).indent_guides.show == ShowIndentGuides::Always;
4973 let show_sticky_scroll = ProjectPanelSettings::get_global(cx).sticky_scroll;
4974 let is_local = project.is_local();
4975
4976 if has_worktree {
4977 let item_count = self
4978 .visible_entries
4979 .iter()
4980 .map(|(_, worktree_entries, _)| worktree_entries.len())
4981 .sum();
4982
4983 fn handle_drag_move<T: 'static>(
4984 this: &mut ProjectPanel,
4985 e: &DragMoveEvent<T>,
4986 window: &mut Window,
4987 cx: &mut Context<ProjectPanel>,
4988 ) {
4989 if let Some(previous_position) = this.previous_drag_position {
4990 // Refresh cursor only when an actual drag happens,
4991 // because modifiers are not updated when the cursor is not moved.
4992 if e.event.position != previous_position {
4993 this.refresh_drag_cursor_style(&e.event.modifiers, window, cx);
4994 }
4995 }
4996 this.previous_drag_position = Some(e.event.position);
4997
4998 if !e.bounds.contains(&e.event.position) {
4999 this.drag_target_entry = None;
5000 return;
5001 }
5002 this.hover_scroll_task.take();
5003 let panel_height = e.bounds.size.height;
5004 if panel_height <= px(0.) {
5005 return;
5006 }
5007
5008 let event_offset = e.event.position.y - e.bounds.origin.y;
5009 // How far along in the project panel is our cursor? (0. is the top of a list, 1. is the bottom)
5010 let hovered_region_offset = event_offset / panel_height;
5011
5012 // We want the scrolling to be a bit faster when the cursor is closer to the edge of a list.
5013 // These pixels offsets were picked arbitrarily.
5014 let vertical_scroll_offset = if hovered_region_offset <= 0.05 {
5015 8.
5016 } else if hovered_region_offset <= 0.15 {
5017 5.
5018 } else if hovered_region_offset >= 0.95 {
5019 -8.
5020 } else if hovered_region_offset >= 0.85 {
5021 -5.
5022 } else {
5023 return;
5024 };
5025 let adjustment = point(px(0.), px(vertical_scroll_offset));
5026 this.hover_scroll_task = Some(cx.spawn_in(window, async move |this, cx| {
5027 loop {
5028 let should_stop_scrolling = this
5029 .update(cx, |this, cx| {
5030 this.hover_scroll_task.as_ref()?;
5031 let handle = this.scroll_handle.0.borrow_mut();
5032 let offset = handle.base_handle.offset();
5033
5034 handle.base_handle.set_offset(offset + adjustment);
5035 cx.notify();
5036 Some(())
5037 })
5038 .ok()
5039 .flatten()
5040 .is_some();
5041 if should_stop_scrolling {
5042 return;
5043 }
5044 cx.background_executor()
5045 .timer(Duration::from_millis(16))
5046 .await;
5047 }
5048 }));
5049 }
5050 h_flex()
5051 .id("project-panel")
5052 .group("project-panel")
5053 .on_drag_move(cx.listener(handle_drag_move::<ExternalPaths>))
5054 .on_drag_move(cx.listener(handle_drag_move::<DraggedSelection>))
5055 .size_full()
5056 .relative()
5057 .on_modifiers_changed(cx.listener(
5058 |this, event: &ModifiersChangedEvent, window, cx| {
5059 this.refresh_drag_cursor_style(&event.modifiers, window, cx);
5060 },
5061 ))
5062 .on_hover(cx.listener(|this, hovered, window, cx| {
5063 if *hovered {
5064 this.show_scrollbar = true;
5065 this.hide_scrollbar_task.take();
5066 cx.notify();
5067 } else if !this.focus_handle.contains_focused(window, cx) {
5068 this.hide_scrollbar(window, cx);
5069 }
5070 }))
5071 .on_click(cx.listener(|this, _event, _, cx| {
5072 cx.stop_propagation();
5073 this.selection = None;
5074 this.marked_entries.clear();
5075 }))
5076 .key_context(self.dispatch_context(window, cx))
5077 .on_action(cx.listener(Self::select_next))
5078 .on_action(cx.listener(Self::select_previous))
5079 .on_action(cx.listener(Self::select_first))
5080 .on_action(cx.listener(Self::select_last))
5081 .on_action(cx.listener(Self::select_parent))
5082 .on_action(cx.listener(Self::select_next_git_entry))
5083 .on_action(cx.listener(Self::select_prev_git_entry))
5084 .on_action(cx.listener(Self::select_next_diagnostic))
5085 .on_action(cx.listener(Self::select_prev_diagnostic))
5086 .on_action(cx.listener(Self::select_next_directory))
5087 .on_action(cx.listener(Self::select_prev_directory))
5088 .on_action(cx.listener(Self::expand_selected_entry))
5089 .on_action(cx.listener(Self::collapse_selected_entry))
5090 .on_action(cx.listener(Self::collapse_all_entries))
5091 .on_action(cx.listener(Self::open))
5092 .on_action(cx.listener(Self::open_permanent))
5093 .on_action(cx.listener(Self::confirm))
5094 .on_action(cx.listener(Self::cancel))
5095 .on_action(cx.listener(Self::copy_path))
5096 .on_action(cx.listener(Self::copy_relative_path))
5097 .on_action(cx.listener(Self::new_search_in_directory))
5098 .on_action(cx.listener(Self::unfold_directory))
5099 .on_action(cx.listener(Self::fold_directory))
5100 .on_action(cx.listener(Self::remove_from_project))
5101 .when(!project.is_read_only(cx), |el| {
5102 el.on_action(cx.listener(Self::new_file))
5103 .on_action(cx.listener(Self::new_directory))
5104 .on_action(cx.listener(Self::rename))
5105 .on_action(cx.listener(Self::delete))
5106 .on_action(cx.listener(Self::trash))
5107 .on_action(cx.listener(Self::cut))
5108 .on_action(cx.listener(Self::copy))
5109 .on_action(cx.listener(Self::paste))
5110 .on_action(cx.listener(Self::duplicate))
5111 .on_click(cx.listener(|this, event: &gpui::ClickEvent, window, cx| {
5112 if event.up.click_count > 1 {
5113 if let Some(entry_id) = this.last_worktree_root_id {
5114 let project = this.project.read(cx);
5115
5116 let worktree_id = if let Some(worktree) =
5117 project.worktree_for_entry(entry_id, cx)
5118 {
5119 worktree.read(cx).id()
5120 } else {
5121 return;
5122 };
5123
5124 this.selection = Some(SelectedEntry {
5125 worktree_id,
5126 entry_id,
5127 });
5128
5129 this.new_file(&NewFile, window, cx);
5130 }
5131 }
5132 }))
5133 })
5134 .when(project.is_local(), |el| {
5135 el.on_action(cx.listener(Self::reveal_in_finder))
5136 .on_action(cx.listener(Self::open_system))
5137 .on_action(cx.listener(Self::open_in_terminal))
5138 })
5139 .when(project.is_via_ssh(), |el| {
5140 el.on_action(cx.listener(Self::open_in_terminal))
5141 })
5142 .on_mouse_down(
5143 MouseButton::Right,
5144 cx.listener(move |this, event: &MouseDownEvent, window, cx| {
5145 // When deploying the context menu anywhere below the last project entry,
5146 // act as if the user clicked the root of the last worktree.
5147 if let Some(entry_id) = this.last_worktree_root_id {
5148 this.deploy_context_menu(event.position, entry_id, window, cx);
5149 }
5150 }),
5151 )
5152 .track_focus(&self.focus_handle(cx))
5153 .child(
5154 uniform_list("entries", item_count, {
5155 cx.processor(|this, range: Range<usize>, window, cx| {
5156 let mut items = Vec::with_capacity(range.end - range.start);
5157 this.for_each_visible_entry(
5158 range,
5159 window,
5160 cx,
5161 |id, details, window, cx| {
5162 items.push(this.render_entry(id, details, window, cx));
5163 },
5164 );
5165 items
5166 })
5167 })
5168 .when(show_indent_guides, |list| {
5169 list.with_decoration(
5170 ui::indent_guides(
5171 cx.entity().clone(),
5172 px(indent_size),
5173 IndentGuideColors::panel(cx),
5174 |this, range, window, cx| {
5175 let mut items =
5176 SmallVec::with_capacity(range.end - range.start);
5177 this.iter_visible_entries(
5178 range,
5179 window,
5180 cx,
5181 |entry, _, entries, _, _| {
5182 let (depth, _) = Self::calculate_depth_and_difference(
5183 entry, entries,
5184 );
5185 items.push(depth);
5186 },
5187 );
5188 items
5189 },
5190 )
5191 .on_click(cx.listener(
5192 |this, active_indent_guide: &IndentGuideLayout, window, cx| {
5193 if window.modifiers().secondary() {
5194 let ix = active_indent_guide.offset.y;
5195 let Some((target_entry, worktree)) = maybe!({
5196 let (worktree_id, entry) = this.entry_at_index(ix)?;
5197 let worktree = this
5198 .project
5199 .read(cx)
5200 .worktree_for_id(worktree_id, cx)?;
5201 let target_entry = worktree
5202 .read(cx)
5203 .entry_for_path(&entry.path.parent()?)?;
5204 Some((target_entry, worktree))
5205 }) else {
5206 return;
5207 };
5208
5209 this.collapse_entry(target_entry.clone(), worktree, cx);
5210 }
5211 },
5212 ))
5213 .with_render_fn(
5214 cx.entity().clone(),
5215 move |this, params, _, cx| {
5216 const LEFT_OFFSET: Pixels = px(14.);
5217 const PADDING_Y: Pixels = px(4.);
5218 const HITBOX_OVERDRAW: Pixels = px(3.);
5219
5220 let active_indent_guide_index =
5221 this.find_active_indent_guide(¶ms.indent_guides, cx);
5222
5223 let indent_size = params.indent_size;
5224 let item_height = params.item_height;
5225
5226 params
5227 .indent_guides
5228 .into_iter()
5229 .enumerate()
5230 .map(|(idx, layout)| {
5231 let offset = if layout.continues_offscreen {
5232 px(0.)
5233 } else {
5234 PADDING_Y
5235 };
5236 let bounds = Bounds::new(
5237 point(
5238 layout.offset.x * indent_size + LEFT_OFFSET,
5239 layout.offset.y * item_height + offset,
5240 ),
5241 size(
5242 px(1.),
5243 layout.length * item_height - offset * 2.,
5244 ),
5245 );
5246 ui::RenderedIndentGuide {
5247 bounds,
5248 layout,
5249 is_active: Some(idx) == active_indent_guide_index,
5250 hitbox: Some(Bounds::new(
5251 point(
5252 bounds.origin.x - HITBOX_OVERDRAW,
5253 bounds.origin.y,
5254 ),
5255 size(
5256 bounds.size.width + HITBOX_OVERDRAW * 2.,
5257 bounds.size.height,
5258 ),
5259 )),
5260 }
5261 })
5262 .collect()
5263 },
5264 ),
5265 )
5266 })
5267 .when(show_sticky_scroll, |list| {
5268 list.with_decoration(ui::sticky_items(
5269 cx.entity().clone(),
5270 |this, range, window, cx| {
5271 let mut items = SmallVec::with_capacity(range.end - range.start);
5272 this.iter_visible_entries(
5273 range,
5274 window,
5275 cx,
5276 |entry, index, entries, _, _| {
5277 let (depth, _) =
5278 Self::calculate_depth_and_difference(entry, entries);
5279 let candidate =
5280 StickyProjectPanelCandidate { index, depth };
5281 items.push(candidate);
5282 },
5283 );
5284 items
5285 },
5286 |this, marker_entry, window, cx| {
5287 this.render_sticky_entries(marker_entry, window, cx)
5288 },
5289 ))
5290 })
5291 .size_full()
5292 .with_sizing_behavior(ListSizingBehavior::Infer)
5293 .with_horizontal_sizing_behavior(ListHorizontalSizingBehavior::Unconstrained)
5294 .with_width_from_item(self.max_width_item_index)
5295 .track_scroll(self.scroll_handle.clone()),
5296 )
5297 .children(self.render_vertical_scrollbar(cx))
5298 .when_some(self.render_horizontal_scrollbar(cx), |this, scrollbar| {
5299 this.pb_4().child(scrollbar)
5300 })
5301 .children(self.context_menu.as_ref().map(|(menu, position, _)| {
5302 deferred(
5303 anchored()
5304 .position(*position)
5305 .anchor(gpui::Corner::TopLeft)
5306 .child(menu.clone()),
5307 )
5308 .with_priority(3)
5309 }))
5310 } else {
5311 v_flex()
5312 .id("empty-project_panel")
5313 .size_full()
5314 .p_4()
5315 .track_focus(&self.focus_handle(cx))
5316 .child(
5317 Button::new("open_project", "Open a project")
5318 .full_width()
5319 .key_binding(KeyBinding::for_action_in(
5320 &OpenRecent::default(),
5321 &self.focus_handle,
5322 window,
5323 cx,
5324 ))
5325 .on_click(cx.listener(|this, _, window, cx| {
5326 this.workspace
5327 .update(cx, |_, cx| {
5328 window.dispatch_action(OpenRecent::default().boxed_clone(), cx);
5329 })
5330 .log_err();
5331 })),
5332 )
5333 .when(is_local, |div| {
5334 div.drag_over::<ExternalPaths>(|style, _, _, cx| {
5335 style.bg(cx.theme().colors().drop_target_background)
5336 })
5337 .on_drop(cx.listener(
5338 move |this, external_paths: &ExternalPaths, window, cx| {
5339 this.drag_target_entry = None;
5340 this.hover_scroll_task.take();
5341 if let Some(task) = this
5342 .workspace
5343 .update(cx, |workspace, cx| {
5344 workspace.open_workspace_for_paths(
5345 true,
5346 external_paths.paths().to_owned(),
5347 window,
5348 cx,
5349 )
5350 })
5351 .log_err()
5352 {
5353 task.detach_and_log_err(cx);
5354 }
5355 cx.stop_propagation();
5356 },
5357 ))
5358 })
5359 }
5360 }
5361}
5362
5363impl Render for DraggedProjectEntryView {
5364 fn render(&mut self, _: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
5365 let ui_font = ThemeSettings::get_global(cx).ui_font.clone();
5366 h_flex()
5367 .font(ui_font)
5368 .pl(self.click_offset.x + px(12.))
5369 .pt(self.click_offset.y + px(12.))
5370 .child(
5371 div()
5372 .flex()
5373 .gap_1()
5374 .items_center()
5375 .py_1()
5376 .px_2()
5377 .rounded_lg()
5378 .bg(cx.theme().colors().background)
5379 .map(|this| {
5380 if self.selections.len() > 1 && self.selections.contains(&self.selection) {
5381 this.child(Label::new(format!("{} entries", self.selections.len())))
5382 } else {
5383 this.child(if let Some(icon) = &self.details.icon {
5384 div().child(Icon::from_path(icon.clone()))
5385 } else {
5386 div()
5387 })
5388 .child(Label::new(self.details.filename.clone()))
5389 }
5390 }),
5391 )
5392 }
5393}
5394
5395impl EventEmitter<Event> for ProjectPanel {}
5396
5397impl EventEmitter<PanelEvent> for ProjectPanel {}
5398
5399impl Panel for ProjectPanel {
5400 fn position(&self, _: &Window, cx: &App) -> DockPosition {
5401 match ProjectPanelSettings::get_global(cx).dock {
5402 ProjectPanelDockPosition::Left => DockPosition::Left,
5403 ProjectPanelDockPosition::Right => DockPosition::Right,
5404 }
5405 }
5406
5407 fn position_is_valid(&self, position: DockPosition) -> bool {
5408 matches!(position, DockPosition::Left | DockPosition::Right)
5409 }
5410
5411 fn set_position(&mut self, position: DockPosition, _: &mut Window, cx: &mut Context<Self>) {
5412 settings::update_settings_file::<ProjectPanelSettings>(
5413 self.fs.clone(),
5414 cx,
5415 move |settings, _| {
5416 let dock = match position {
5417 DockPosition::Left | DockPosition::Bottom => ProjectPanelDockPosition::Left,
5418 DockPosition::Right => ProjectPanelDockPosition::Right,
5419 };
5420 settings.dock = Some(dock);
5421 },
5422 );
5423 }
5424
5425 fn size(&self, _: &Window, cx: &App) -> Pixels {
5426 self.width
5427 .unwrap_or_else(|| ProjectPanelSettings::get_global(cx).default_width)
5428 }
5429
5430 fn set_size(&mut self, size: Option<Pixels>, window: &mut Window, cx: &mut Context<Self>) {
5431 self.width = size;
5432 cx.notify();
5433 cx.defer_in(window, |this, _, cx| {
5434 this.serialize(cx);
5435 });
5436 }
5437
5438 fn icon(&self, _: &Window, cx: &App) -> Option<IconName> {
5439 ProjectPanelSettings::get_global(cx)
5440 .button
5441 .then_some(IconName::FileTree)
5442 }
5443
5444 fn icon_tooltip(&self, _window: &Window, _cx: &App) -> Option<&'static str> {
5445 Some("Project Panel")
5446 }
5447
5448 fn toggle_action(&self) -> Box<dyn Action> {
5449 Box::new(ToggleFocus)
5450 }
5451
5452 fn persistent_name() -> &'static str {
5453 "Project Panel"
5454 }
5455
5456 fn starts_open(&self, _: &Window, cx: &App) -> bool {
5457 let project = &self.project.read(cx);
5458 project.visible_worktrees(cx).any(|tree| {
5459 tree.read(cx)
5460 .root_entry()
5461 .map_or(false, |entry| entry.is_dir())
5462 })
5463 }
5464
5465 fn activation_priority(&self) -> u32 {
5466 0
5467 }
5468}
5469
5470impl Focusable for ProjectPanel {
5471 fn focus_handle(&self, _cx: &App) -> FocusHandle {
5472 self.focus_handle.clone()
5473 }
5474}
5475
5476impl ClipboardEntry {
5477 fn is_cut(&self) -> bool {
5478 matches!(self, Self::Cut { .. })
5479 }
5480
5481 fn items(&self) -> &BTreeSet<SelectedEntry> {
5482 match self {
5483 ClipboardEntry::Copied(entries) | ClipboardEntry::Cut(entries) => entries,
5484 }
5485 }
5486
5487 fn to_copy_entry(self) -> Self {
5488 match self {
5489 ClipboardEntry::Copied(_) => self,
5490 ClipboardEntry::Cut(entries) => ClipboardEntry::Copied(entries),
5491 }
5492 }
5493}
5494
5495#[cfg(test)]
5496mod project_panel_tests;