Files
oak-gpui/crates/worktree/src/worktree_settings.rs
T
B. Collier JonesandSmit Barmase 1b93242351 project_panel: Add hidden files glob patterns and action toggle hidden files visibility (#41532)
This PR adds the ability to configure which files are considered
"hidden" in the project panel and toggle their visibility with a
keyboard shortcut. Previously, the editor hardcoded dotfiles as hidden -
now users can customize the pattern and quickly show/hide them.

### Release Notes

- Added `project_panel::ToggleHideHidden` action with keyboard shortcuts
to toggle visibility of hidden files
- Added configurable `hidden_files` setting to customize which files are
marked as hidden (defaults to `**/.*` for dotfiles)

### Motivation

This change allows users to:
1. Quickly toggle hidden file visibility with a keyboard shortcut
2. Customize which files are considered "hidden" beyond just dotfiles
3. Better organize their project panel by hiding build artifacts, logs,
or other generated files

### Usage

**Toggle hidden files:**
- **macOS:** `cmd-alt-.`
- **Linux:** `ctrl-alt-.`
- **Windows:** `ctrl-alt-.`

**Customize patterns in settings:**
```json
{
  "hidden_files": ["**/.*", "**/*.tmp", "**/build/**"]
}
```

### Changes

**Core Implementation:**
- Added `hidden_files` setting (defaults to `**/.*` to match current
dotfile behavior)
- Replaced hardcoded `name.starts_with('.')` logic with configurable
pattern matching using `PathMatcher`
- Hidden status propagates through directory hierarchies (if a directory
is hidden, all children inherit that status)

**User-Facing:**
- Added `ToggleHideHidden` action in the project panel
- Added keyboard shortcuts for all platforms
- Added settings UI entry for configuring `hidden_files` patterns

**Testing:**
- Added comprehensive test coverage validating default behavior, custom
patterns, propagation, and settings changes

### Implementation Notes

- Uses `PathMatcher` for efficient glob matching
- Settings changes automatically trigger worktree re-indexing
- No breaking changes - defaults maintain current behavior (hiding
dotfiles)

---

**Disclaimer:** This was implemented with a fair amount of copy/paste
(particularly the gitignore handling), trial and error, and a healthy
dose of Claude.

### Screenshots

**Project Panel with hidden files visible:**
<img width="1368" height="935" alt="Screenshot 2025-10-30 at 3 15 53 AM"
src="https://github.com/user-attachments/assets/1cbe90ce-504c-4f9b-bca8-bef02ab961be"
/>

**Project Panel with hidden files hidden:**
<img width="1363" height="917" alt="Screenshot 2025-10-30 at 3 16 07 AM"
src="https://github.com/user-attachments/assets/9297f43e-98c7-4b19-be8f-3934589d6451"
/>

**Toggle action in command palette:**
<img width="565" height="161" alt="Screenshot 2025-10-30 at 3 17 26 AM"
src="https://github.com/user-attachments/assets/4dc9e7b6-9c29-4972-b886-88d8018905da"
/>

Release Notes:

- Added the ability to configure glob patterns for files treated as
hidden in the project panel using the `hidden_files` setting.
- Added an action `project panel: toggle hidden files` to quickly show
or hide hidden files in the project panel.

---------

Co-authored-by: Smit Barmase <heysmitbarmase@gmail.com>
2025-11-04 20:35:37 +05:30

96 lines
3.5 KiB
Rust

use std::path::Path;
use anyhow::Context as _;
use settings::Settings;
use util::{
ResultExt,
paths::{PathMatcher, PathStyle},
rel_path::RelPath,
};
#[derive(Clone, PartialEq, Eq)]
pub struct WorktreeSettings {
pub project_name: Option<String>,
/// Whether to prevent this project from being shared in public channels.
pub prevent_sharing_in_public_channels: bool,
pub file_scan_exclusions: PathMatcher,
pub file_scan_inclusions: PathMatcher,
/// This field contains all ancestors of the `file_scan_inclusions`. It's used to
/// determine whether to terminate worktree scanning for a given dir.
pub parent_dir_scan_inclusions: PathMatcher,
pub private_files: PathMatcher,
pub hidden_files: PathMatcher,
}
impl WorktreeSettings {
pub fn is_path_private(&self, path: &RelPath) -> bool {
path.ancestors()
.any(|ancestor| self.private_files.is_match(ancestor.as_std_path()))
}
pub fn is_path_excluded(&self, path: &RelPath) -> bool {
path.ancestors()
.any(|ancestor| self.file_scan_exclusions.is_match(ancestor.as_std_path()))
}
pub fn is_path_always_included(&self, path: &RelPath, is_dir: bool) -> bool {
if is_dir {
self.parent_dir_scan_inclusions.is_match(path.as_std_path())
} else {
self.file_scan_inclusions.is_match(path.as_std_path())
}
}
pub fn is_path_hidden(&self, path: &RelPath) -> bool {
path.ancestors()
.any(|ancestor| self.hidden_files.is_match(ancestor.as_std_path()))
}
}
impl Settings for WorktreeSettings {
fn from_settings(content: &settings::SettingsContent) -> Self {
let worktree = content.project.worktree.clone();
let file_scan_exclusions = worktree.file_scan_exclusions.unwrap();
let file_scan_inclusions = worktree.file_scan_inclusions.unwrap();
let private_files = worktree.private_files.unwrap().0;
let hidden_files = worktree.hidden_files.unwrap();
let parsed_file_scan_inclusions: Vec<String> = file_scan_inclusions
.iter()
.flat_map(|glob| {
Path::new(glob)
.ancestors()
.skip(1)
.map(|a| a.to_string_lossy().into())
})
.filter(|p: &String| !p.is_empty())
.collect();
Self {
project_name: worktree.project_name.into_inner(),
prevent_sharing_in_public_channels: worktree.prevent_sharing_in_public_channels,
file_scan_exclusions: path_matchers(file_scan_exclusions, "file_scan_exclusions")
.log_err()
.unwrap_or_default(),
parent_dir_scan_inclusions: path_matchers(
parsed_file_scan_inclusions,
"file_scan_inclusions",
)
.unwrap(),
file_scan_inclusions: path_matchers(file_scan_inclusions, "file_scan_inclusions")
.unwrap(),
private_files: path_matchers(private_files, "private_files")
.log_err()
.unwrap_or_default(),
hidden_files: path_matchers(hidden_files, "hidden_files")
.log_err()
.unwrap_or_default(),
}
}
}
fn path_matchers(mut values: Vec<String>, context: &'static str) -> anyhow::Result<PathMatcher> {
values.sort();
PathMatcher::new(values, PathStyle::local())
.with_context(|| format!("Failed to parse globs from {}", context))
}