diff --git a/assets/settings/default.json b/assets/settings/default.json index ef5e8444744..42e347291d3 100644 --- a/assets/settings/default.json +++ b/assets/settings/default.json @@ -900,7 +900,7 @@ // "icon" // 2. A disclosure chevron: // "chevron" - // 3. A disclosure chevron followed by a folder icon: + // 3. A disclosure chevron followed by a folder icon, with no chevron on empty folders in local projects: // "both" "folder_indicator": "icon", // Whether to show the git status in the project panel. diff --git a/crates/project_panel/src/project_panel.rs b/crates/project_panel/src/project_panel.rs index afc6308b599..c12a79f02c6 100644 --- a/crates/project_panel/src/project_panel.rs +++ b/crates/project_panel/src/project_panel.rs @@ -106,6 +106,9 @@ struct State { /// project entries (and all non-leaf nodes are guaranteed to be directories). ancestors: HashMap, visible_entries: Vec, + /// Directories known to show no rows when expanded, from the same snapshot as + /// `visible_entries`. + empty_dir_ids: HashSet, max_width_item_index: Option, edit_state: Option, temporarily_unfolded_pending_state: Option, @@ -126,6 +129,7 @@ impl State { last_worktree_root_id: None, ancestors: Default::default(), visible_entries: Default::default(), + empty_dir_ids: Default::default(), max_width_item_index: None, edit_state: old.edit_state.clone(), temporarily_unfolded_pending_state: None, @@ -916,6 +920,7 @@ impl ProjectPanel { temporarily_unfolded_pending_state: None, last_worktree_root_id: Default::default(), visible_entries: Default::default(), + empty_dir_ids: Default::default(), ancestors: Default::default(), expanded_dir_ids: Default::default(), unfolded_dir_ids: Default::default(), @@ -4628,7 +4633,10 @@ impl ProjectPanel { let visible_worktrees: Vec<_> = project .visible_worktrees(cx) - .map(|worktree| worktree.read(cx).snapshot()) + .map(|worktree| { + let worktree = worktree.read(cx); + (worktree.snapshot(), worktree.is_local()) + }) .collect(); let hide_root = settings.hide_root && visible_worktrees.len() == 1; let hide_hidden = settings.hide_hidden; @@ -4636,7 +4644,7 @@ impl ProjectPanel { let visible_entries_task = cx.spawn_in(window, async move |this, cx| { let new_state = cx .background_spawn(async move { - for worktree_snapshot in visible_worktrees { + for (worktree_snapshot, is_local) in visible_worktrees { let worktree_id = worktree_snapshot.id(); let mut new_entry_parent_id = None; @@ -4728,11 +4736,6 @@ impl ProjectPanel { } } auto_folded_ancestors.clear(); - if (!hide_gitignore || !entry.is_ignored) - && (!hide_hidden || !entry.is_hidden) - { - visible_worktree_entries.push(entry.to_owned()); - } let precedes_new_entry = if let Some(new_entry_id) = new_entry_parent_id { entry.id == new_entry_id || { @@ -4743,9 +4746,24 @@ impl ProjectPanel { } else { false }; + if is_entry_shown(&entry, hide_gitignore, hide_hidden) { + // A remote host sends a directory it has not loaded yet as `Dir`, + // so only a local one counts. + let is_loaded = is_local && entry.kind == EntryKind::Dir; + // A missing chevron reads as "empty", so a directory keeps it until + // it is loaded. + if is_loaded + && !precedes_new_entry + && !worktree_snapshot.child_entries(&entry.path).any(|child| { + is_entry_shown(child, hide_gitignore, hide_hidden) + }) + { + new_state.empty_dir_ids.insert(entry.id); + } + visible_worktree_entries.push(entry.to_owned()); + } if precedes_new_entry - && (!hide_gitignore || !entry.is_ignored) - && (!hide_hidden || !entry.is_hidden) + && is_entry_shown(&entry, hide_gitignore, hide_hidden) { visible_worktree_entries.push(Self::create_new_git_entry( entry.entry, @@ -7138,6 +7156,10 @@ impl ProjectPanel { let is_expanded = expanded_entry_ids.binary_search(&entry.id).is_ok(); let (chevron, icon) = Self::entry_icons(entry, is_expanded, cx); + // Without a folder icon, the chevron is all that tells a folder from a file. + let hide_chevron = + folder_indicator.shows_icon() && self.state.empty_dir_ids.contains(&entry.id); + let chevron = if hide_chevron { None } else { chevron }; let path_style = self.project.read(cx).path_style(cx); let (depth, difference) = @@ -7513,6 +7535,10 @@ fn entry_file_name<'a>(entry: &'a Entry, root_name: &'a RelPath) -> &'a str { .unwrap_or_else(|| root_name.as_unix_str()) } +fn is_entry_shown(entry: &Entry, hide_gitignore: bool, hide_hidden: bool) -> bool { + (!hide_gitignore || !entry.is_ignored) && (!hide_hidden || !entry.is_hidden) +} + fn item_width_estimate(depth: usize, item_text_chars: usize, is_symlink: bool) -> usize { const ICON_SIZE_FACTOR: usize = 2; let mut item_width = depth * ICON_SIZE_FACTOR + item_text_chars; diff --git a/crates/project_panel/src/project_panel_tests.rs b/crates/project_panel/src/project_panel_tests.rs index 084c9188f76..a11c970238b 100644 --- a/crates/project_panel/src/project_panel_tests.rs +++ b/crates/project_panel/src/project_panel_tests.rs @@ -13504,6 +13504,316 @@ async fn test_file_rows_reserve_the_chevron_slot(cx: &mut gpui::TestAppContext) } } +#[gpui::test] +async fn test_empty_folder_chevrons(cx: &mut gpui::TestAppContext) { + init_test(cx); + + let fs = FakeFs::new(cx.executor()); + fs.insert_tree( + path!("/root"), + json!({ + ".gitignore": "ignored_dir\n*.log", + "empty_dir": {}, + "full_dir": { "file.rs": "" }, + "hidden_only_dir": { ".hidden": "" }, + "ignored_dir": {}, + "ignored_only_dir": { "debug.log": "" }, + }), + ) + .await; + + let project = Project::test(fs.clone(), [path!("/root").as_ref()], cx).await; + let window = cx.add_window(|window, cx| MultiWorkspace::test_new(project.clone(), window, cx)); + let workspace = window + .read_with(cx, |mw, _| mw.workspace().clone()) + .unwrap(); + let cx = &mut VisualTestContext::from_window(window.into(), cx); + let panel = workspace.update_in(cx, ProjectPanel::new); + cx.run_until_parked(); + + let chevron_of = |name: &str, cx: &mut VisualTestContext| { + let (_, chevron, _) = visible_entry_indicators(&panel, 0..50, cx) + .into_iter() + .find(|(filename, _, _)| filename == name) + .unwrap_or_else(|| panic!("no row named {name:?}")); + chevron + }; + let collapsed = Some("chevron_right.svg".to_string()); + + set_folder_indicator(FolderIndicator::Chevron, cx); + cx.run_until_parked(); + assert_eq!( + chevron_of("empty_dir", cx), + collapsed, + "`chevron` mode keeps the chevron so an empty folder is not mistaken for a file" + ); + + set_folder_indicator(FolderIndicator::Both, cx); + cx.run_until_parked(); + assert_eq!(chevron_of("empty_dir", cx), None); + assert_eq!(chevron_of("full_dir", cx), collapsed); + assert_eq!( + chevron_of("hidden_only_dir", cx), + collapsed, + "hidden entries are shown by default, so the directory is not empty" + ); + assert_eq!( + chevron_of("ignored_only_dir", cx), + collapsed, + "gitignored entries are shown by default, so the directory is not empty" + ); + assert_eq!( + chevron_of("ignored_dir", cx), + collapsed, + "an unloaded gitignored directory has unknown contents and keeps its chevron" + ); + toggle_expand_dir(&panel, "root/ignored_dir", cx); + assert_eq!( + chevron_of("ignored_dir", cx), + None, + "expanding loads the directory, and it turns out to be empty" + ); + + cx.update(|_, cx| { + cx.update_global::(|store, cx| { + store.update_user_settings(cx, |settings| { + let project_panel = settings.project_panel.get_or_insert_default(); + project_panel.hide_hidden = Some(true); + project_panel.hide_gitignore = Some(true); + }); + }); + }); + cx.run_until_parked(); + for directory in ["hidden_only_dir", "ignored_only_dir"] { + assert_eq!( + chevron_of(directory, cx), + None, + "a directory holding only entries the panel hides counts as empty" + ); + } + + fs.insert_file(path!("/root/empty_dir/new.rs"), Vec::new()) + .await; + cx.run_until_parked(); + assert_eq!(chevron_of("empty_dir", cx), collapsed); + + fs.remove_file(path!("/root/empty_dir/new.rs").as_ref(), Default::default()) + .await + .unwrap(); + cx.run_until_parked(); + assert_eq!(chevron_of("empty_dir", cx), None); + + select_path(&panel, "root/empty_dir", cx); + panel.update_in(cx, |panel, window, cx| { + panel.new_directory(&NewDirectory, window, cx) + }); + cx.run_until_parked(); + assert_eq!( + visible_entries_as_strings(&panel, 0..3, cx), + &[ + "v root", + " v empty_dir", + " > [EDITOR: ''] <== selected" + ] + ); + assert_eq!( + chevron_of("empty_dir", cx), + Some("chevron_down.svg".to_string()), + "the parent shows the new directory's row, so it keeps its chevron" + ); + let new_directory_row = visible_entry_indicators(&panel, 2..3, cx); + assert_eq!( + new_directory_row + .first() + .map(|(_, chevron, _)| chevron.clone()), + Some(collapsed.clone()), + "a directory being created has not been scanned, so its row keeps its chevron" + ); + panel.update_in(cx, |panel, window, cx| { + panel.cancel(&menu::Cancel, window, cx) + }); + cx.run_until_parked(); + assert_eq!(chevron_of("empty_dir", cx), None); + toggle_expand_dir(&panel, "root/empty_dir", cx); + cx.run_until_parked(); + + // Keyboard expand/collapse on an empty directory still behaves like any other directory. + select_path(&panel, "root/empty_dir", cx); + panel.update_in(cx, |panel, window, cx| { + panel.expand_selected_entry(&ExpandSelectedEntry, window, cx); + }); + cx.run_until_parked(); + assert_eq!(chevron_of("empty_dir", cx), None); + assert_eq!( + visible_entries_as_strings(&panel, 0..3, cx), + &["v root", " v empty_dir <== selected", " > full_dir"] + ); + panel.update_in(cx, |panel, window, cx| { + panel.expand_selected_entry(&ExpandSelectedEntry, window, cx); + }); + cx.run_until_parked(); + assert_eq!( + visible_entries_as_strings(&panel, 0..3, cx), + &["v root", " v empty_dir", " > full_dir <== selected"], + "expanding an already expanded empty directory moves on to the next entry" + ); + panel.update_in(cx, |panel, window, cx| { + panel.select_previous(&SelectPrevious, window, cx); + panel.collapse_selected_entry(&CollapseSelectedEntry, window, cx); + }); + cx.run_until_parked(); + assert_eq!( + visible_entries_as_strings(&panel, 0..3, cx), + &["v root", " > empty_dir <== selected", " > full_dir"] + ); + + toggle_expand_dir(&panel, "root/empty_dir", cx); + cx.run_until_parked(); + + set_folder_indicator(FolderIndicator::Chevron, cx); + cx.run_until_parked(); + assert_eq!( + chevron_of("empty_dir", cx), + Some("chevron_down.svg".to_string()) + ); +} + +#[gpui::test] +async fn test_folded_folder_keeps_chevron_above_new_entry_editor(cx: &mut gpui::TestAppContext) { + init_test(cx); + + let fs = FakeFs::new(cx.executor()); + fs.insert_tree(path!("/root"), json!({ "outer": { "inner": {} } })) + .await; + + let project = Project::test(fs.clone(), [path!("/root").as_ref()], cx).await; + let window = cx.add_window(|window, cx| MultiWorkspace::test_new(project.clone(), window, cx)); + let workspace = window + .read_with(cx, |mw, _| mw.workspace().clone()) + .unwrap(); + let cx = &mut VisualTestContext::from_window(window.into(), cx); + cx.update(|_, cx| { + cx.update_global::(|store, cx| { + store.update_user_settings(cx, |settings| { + settings + .project_panel + .get_or_insert_default() + .auto_fold_dirs = Some(true); + }); + }); + }); + let panel = workspace.update_in(cx, ProjectPanel::new); + set_folder_indicator(FolderIndicator::Both, cx); + cx.run_until_parked(); + + let folded_row_chevron = |cx: &mut VisualTestContext| { + visible_entry_indicators(&panel, 1..2, cx) + .first() + .and_then(|(_, chevron, _)| chevron.clone()) + }; + assert_eq!( + visible_entries_as_strings(&panel, 0..10, cx), + &["v root", " > outer/inner"] + ); + assert_eq!( + folded_row_chevron(cx), + None, + "the row stands for its last folder, which is empty" + ); + + // Selecting the folded-away `outer` itself leaves the chain folded while the editor is open. + select_path(&panel, "root/outer", cx); + panel.update_in(cx, |panel, window, cx| panel.new_file(&NewFile, window, cx)); + cx.run_until_parked(); + assert_eq!( + visible_entries_as_strings(&panel, 0..10, cx), + &[ + "v root", + " > outer/inner", + " [EDITOR: ''] <== selected" + ] + ); + assert!( + folded_row_chevron(cx).is_some(), + "a row with the new entry's editor beneath it has something to show" + ); +} + +#[gpui::test] +async fn test_remote_folders_keep_chevrons(cx: &mut gpui::TestAppContext) { + init_test(cx); + + let fs = FakeFs::new(cx.executor()); + fs.insert_tree(path!("/local"), json!({ "local_empty_dir": {} })) + .await; + let project = Project::test(fs.clone(), [path!("/local").as_ref()], cx).await; + + let remote_worktree = project.update(cx, |project, cx| { + project.add_test_remote_worktree("/remote/project", cx) + }); + let remote_worktree_id = remote_worktree.read_with(cx, |worktree, _| worktree.id().to_proto()); + let remote_entry = |id: u64, path: &str| client::proto::Entry { + id, + is_dir: true, + path: path.to_string(), + inode: id, + mtime: Some(client::proto::Timestamp { + seconds: 0, + nanos: 0, + }), + is_ignored: false, + is_hidden: false, + is_external: false, + is_fifo: false, + size: None, + canonical_path: None, + is_unloaded: false, + }; + remote_worktree.update(cx, |worktree, _| { + worktree + .as_remote() + .unwrap() + .update_from_remote(client::proto::UpdateWorktree { + project_id: 0, + worktree_id: remote_worktree_id, + abs_path: "/remote/project".to_string(), + root_name: "project".to_string(), + // Ids far from the local worktree's, since `empty_dir_ids` is keyed by entry id. + updated_entries: vec![remote_entry(1000, ""), remote_entry(1001, "remote_dir")], + removed_entries: vec![], + scan_id: 1, + is_last_update: false, + updated_repositories: vec![], + removed_repositories: vec![], + root_repo_common_dir: None, + root_repo_is_linked_worktree: false, + }); + }); + + let window = cx.add_window(|window, cx| MultiWorkspace::test_new(project.clone(), window, cx)); + let workspace = window + .read_with(cx, |mw, _| mw.workspace().clone()) + .unwrap(); + let cx = &mut VisualTestContext::from_window(window.into(), cx); + let panel = workspace.update_in(cx, ProjectPanel::new); + set_folder_indicator(FolderIndicator::Both, cx); + cx.run_until_parked(); + + let chevron_of = |name: &str, cx: &mut VisualTestContext| { + let (_, chevron, _) = visible_entry_indicators(&panel, 0..50, cx) + .into_iter() + .find(|(filename, _, _)| filename == name) + .unwrap_or_else(|| panic!("no row named {name:?}")); + chevron + }; + assert_eq!(chevron_of("local_empty_dir", cx), None); + assert_eq!( + chevron_of("remote_dir", cx), + Some("chevron_right.svg".to_string()), + "a remote folder with no entries yet may still be scanning on the host" + ); +} + #[gpui::test] async fn test_file_drag_state_clears_before_window_handoff(cx: &mut TestAppContext) { init_test_with_editor(cx);