Skip to content

Commit 8acfc55

Browse files
feat: add file explorer options (#13888)
1 parent 1426e1f commit 8acfc55

File tree

4 files changed

+112
-36
lines changed

4 files changed

+112
-36
lines changed

book/src/editor.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,24 @@ Example:
224224
!.gitattributes
225225
```
226226

227+
### `[editor.file-explorer]` Section
228+
229+
In addition to the options for the file picker and global search, a similar set of options is presented to configure the file explorer separately. However, unlike the file picker, the defaults are set to avoid ignoring most files.
230+
231+
Note that the ignore files consulted by the file explorer when `ignore` is set to true are the same ones used by the file picker, including the aforementioned Helix-specific ignore files.
232+
233+
234+
| Key | Description | Default |
235+
|--|--|---------|
236+
|`hidden` | Enables ignoring hidden files | `false`
237+
|`follow-symlinks` | Follow symlinks instead of ignoring them | `false`
238+
|`parents` | Enables reading ignore files from parent directories | `false`
239+
|`ignore` | Enables reading `.ignore` files | `false`
240+
|`git-ignore` | Enables reading `.gitignore` files | `false`
241+
|`git-global` | Enables reading global `.gitignore`, whose path is specified in git's config: `core.excludesfile` option | `false`
242+
|`git-exclude` | Enables reading `.git/info/exclude` files | `false`
243+
244+
227245
### `[editor.auto-pairs]` Section
228246

229247
Enables automatic insertion of pairs to parentheses, brackets, etc. Can be a

helix-term/src/ui/mod.rs

Lines changed: 65 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,22 @@ pub fn raw_regex_prompt(
185185
cx.push_layer(Box::new(prompt));
186186
}
187187

188+
/// We want to exclude files that the editor can't handle yet
189+
fn get_excluded_types() -> ignore::types::Types {
190+
use ignore::types::TypesBuilder;
191+
let mut type_builder = TypesBuilder::new();
192+
type_builder
193+
.add(
194+
"compressed",
195+
"*.{zip,gz,bz2,zst,lzo,sz,tgz,tbz2,lz,lz4,lzma,lzo,z,Z,xz,7z,rar,cab}",
196+
)
197+
.expect("Invalid type definition");
198+
type_builder.negate("all");
199+
type_builder
200+
.build()
201+
.expect("failed to build excluded_types")
202+
}
203+
188204
#[derive(Debug)]
189205
pub struct FilePickerData {
190206
root: PathBuf,
@@ -193,7 +209,7 @@ pub struct FilePickerData {
193209
type FilePicker = Picker<PathBuf, FilePickerData>;
194210

195211
pub fn file_picker(editor: &Editor, root: PathBuf) -> FilePicker {
196-
use ignore::{types::TypesBuilder, WalkBuilder};
212+
use ignore::WalkBuilder;
197213
use std::time::Instant;
198214

199215
let config = editor.config();
@@ -208,7 +224,8 @@ pub fn file_picker(editor: &Editor, root: PathBuf) -> FilePicker {
208224
let absolute_root = root.canonicalize().unwrap_or_else(|_| root.clone());
209225

210226
let mut walk_builder = WalkBuilder::new(&root);
211-
walk_builder
227+
228+
let mut files = walk_builder
212229
.hidden(config.file_picker.hidden)
213230
.parents(config.file_picker.parents)
214231
.ignore(config.file_picker.ignore)
@@ -218,31 +235,18 @@ pub fn file_picker(editor: &Editor, root: PathBuf) -> FilePicker {
218235
.git_exclude(config.file_picker.git_exclude)
219236
.sort_by_file_name(|name1, name2| name1.cmp(name2))
220237
.max_depth(config.file_picker.max_depth)
221-
.filter_entry(move |entry| filter_picker_entry(entry, &absolute_root, dedup_symlinks));
222-
223-
walk_builder.add_custom_ignore_filename(helix_loader::config_dir().join("ignore"));
224-
walk_builder.add_custom_ignore_filename(".helix/ignore");
225-
226-
// We want to exclude files that the editor can't handle yet
227-
let mut type_builder = TypesBuilder::new();
228-
type_builder
229-
.add(
230-
"compressed",
231-
"*.{zip,gz,bz2,zst,lzo,sz,tgz,tbz2,lz,lz4,lzma,lzo,z,Z,xz,7z,rar,cab}",
232-
)
233-
.expect("Invalid type definition");
234-
type_builder.negate("all");
235-
let excluded_types = type_builder
238+
.filter_entry(move |entry| filter_picker_entry(entry, &absolute_root, dedup_symlinks))
239+
.add_custom_ignore_filename(helix_loader::config_dir().join("ignore"))
240+
.add_custom_ignore_filename(".helix/ignore")
241+
.types(get_excluded_types())
236242
.build()
237-
.expect("failed to build excluded_types");
238-
walk_builder.types(excluded_types);
239-
let mut files = walk_builder.build().filter_map(|entry| {
240-
let entry = entry.ok()?;
241-
if !entry.file_type()?.is_file() {
242-
return None;
243-
}
244-
Some(entry.into_path())
245-
});
243+
.filter_map(|entry| {
244+
let entry = entry.ok()?;
245+
if !entry.file_type()?.is_file() {
246+
return None;
247+
}
248+
Some(entry.into_path())
249+
});
246250
log::debug!("file_picker init {:?}", Instant::now().duration_since(now));
247251

248252
let columns = [PickerColumn::new(
@@ -304,7 +308,7 @@ type FileExplorer = Picker<(PathBuf, bool), (PathBuf, Style)>;
304308

305309
pub fn file_explorer(root: PathBuf, editor: &Editor) -> Result<FileExplorer, std::io::Error> {
306310
let directory_style = editor.theme.get("ui.text.directory");
307-
let directory_content = directory_content(&root)?;
311+
let directory_content = directory_content(&root, editor)?;
308312

309313
let columns = [PickerColumn::new(
310314
"path",
@@ -350,21 +354,47 @@ pub fn file_explorer(root: PathBuf, editor: &Editor) -> Result<FileExplorer, std
350354
Ok(picker)
351355
}
352356

353-
fn directory_content(path: &Path) -> Result<Vec<(PathBuf, bool)>, std::io::Error> {
354-
let mut content: Vec<_> = std::fs::read_dir(path)?
355-
.flatten()
356-
.map(|entry| {
357-
(
358-
entry.path(),
359-
std::fs::metadata(entry.path()).is_ok_and(|metadata| metadata.is_dir()),
360-
)
357+
fn directory_content(path: &Path, editor: &Editor) -> Result<Vec<(PathBuf, bool)>, std::io::Error> {
358+
use ignore::WalkBuilder;
359+
360+
let config = editor.config();
361+
362+
let mut walk_builder = WalkBuilder::new(path);
363+
364+
let mut content: Vec<(PathBuf, bool)> = walk_builder
365+
.hidden(config.file_explorer.hidden)
366+
.parents(config.file_explorer.parents)
367+
.ignore(config.file_explorer.ignore)
368+
.follow_links(config.file_explorer.follow_symlinks)
369+
.git_ignore(config.file_explorer.git_ignore)
370+
.git_global(config.file_explorer.git_global)
371+
.git_exclude(config.file_explorer.git_exclude)
372+
.max_depth(Some(1))
373+
.add_custom_ignore_filename(helix_loader::config_dir().join("ignore"))
374+
.add_custom_ignore_filename(".helix/ignore")
375+
.types(get_excluded_types())
376+
.build()
377+
.filter_map(|entry| {
378+
entry
379+
.map(|entry| {
380+
(
381+
entry.path().to_path_buf(),
382+
entry
383+
.file_type()
384+
.is_some_and(|file_type| file_type.is_dir()),
385+
)
386+
})
387+
.ok()
388+
.filter(|entry| entry.0 != path)
361389
})
362390
.collect();
363391

364392
content.sort_by(|(path1, is_dir1), (path2, is_dir2)| (!is_dir1, path1).cmp(&(!is_dir2, path2)));
393+
365394
if path.parent().is_some() {
366395
content.insert(0, (path.join(".."), true));
367396
}
397+
368398
Ok(content)
369399
}
370400

helix-term/src/ui/picker.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -610,7 +610,7 @@ impl<T: 'static + Send + Sync, D: 'static + Send + Sync> Picker<T, D> {
610610
let preview = std::fs::metadata(&path)
611611
.and_then(|metadata| {
612612
if metadata.is_dir() {
613-
let files = super::directory_content(&path)?;
613+
let files = super::directory_content(&path, editor)?;
614614
let file_names: Vec<_> = files
615615
.iter()
616616
.filter_map(|(path, is_dir)| {

helix-view/src/editor.rs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,32 @@ impl Default for FilePickerConfig {
221221
}
222222
}
223223

224+
#[derive(Default, Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
225+
#[serde(rename_all = "kebab-case", default, deny_unknown_fields)]
226+
pub struct FileExplorerConfig {
227+
/// IgnoreOptions
228+
/// Enables ignoring hidden files.
229+
/// Whether to hide hidden files in file explorer and global search results. Defaults to false.
230+
pub hidden: bool,
231+
/// Enables following symlinks.
232+
/// Whether to follow symbolic links in file picker and file or directory completions. Defaults to true.
233+
pub follow_symlinks: bool,
234+
/// Enables reading ignore files from parent directories. Defaults to true.
235+
pub parents: bool,
236+
/// Enables reading `.ignore` files.
237+
/// Whether to hide files listed in .ignore in file picker and global search results. Defaults to true.
238+
pub ignore: bool,
239+
/// Enables reading `.gitignore` files.
240+
/// Whether to hide files listed in .gitignore in file picker and global search results. Defaults to true.
241+
pub git_ignore: bool,
242+
/// Enables reading global .gitignore, whose path is specified in git's config: `core.excludefile` option.
243+
/// Whether to hide files listed in global .gitignore in file picker and global search results. Defaults to true.
244+
pub git_global: bool,
245+
/// Enables reading `.git/info/exclude` files.
246+
/// Whether to hide files listed in .git/info/exclude in file picker and global search results. Defaults to true.
247+
pub git_exclude: bool,
248+
}
249+
224250
fn serialize_alphabet<S>(alphabet: &[char], serializer: S) -> Result<S::Ok, S::Error>
225251
where
226252
S: Serializer,
@@ -318,6 +344,7 @@ pub struct Config {
318344
/// Whether to display infoboxes. Defaults to true.
319345
pub auto_info: bool,
320346
pub file_picker: FilePickerConfig,
347+
pub file_explorer: FileExplorerConfig,
321348
/// Configuration of the statusline elements
322349
pub statusline: StatusLineConfig,
323350
/// Shape for cursor in each mode
@@ -1038,6 +1065,7 @@ impl Default for Config {
10381065
completion_trigger_len: 2,
10391066
auto_info: true,
10401067
file_picker: FilePickerConfig::default(),
1068+
file_explorer: FileExplorerConfig::default(),
10411069
statusline: StatusLineConfig::default(),
10421070
cursor_shape: CursorShapeConfig::default(),
10431071
true_color: false,

0 commit comments

Comments
 (0)