Skip to content

Commit 904916c

Browse files
kas2020-commitslittleblack111
authored andcommitted
feat: add file explorer options (helix-editor#13888)
1 parent 6d67fd2 commit 904916c

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
@@ -187,6 +187,22 @@ pub fn raw_regex_prompt(
187187
cx.push_layer(Box::new(prompt));
188188
}
189189

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

197213
pub fn file_picker(editor: &Editor, root: PathBuf) -> FilePicker {
198-
use ignore::{types::TypesBuilder, WalkBuilder};
214+
use ignore::WalkBuilder;
199215
use std::time::Instant;
200216

201217
let config = editor.config();
@@ -210,7 +226,8 @@ pub fn file_picker(editor: &Editor, root: PathBuf) -> FilePicker {
210226
let absolute_root = root.canonicalize().unwrap_or_else(|_| root.clone());
211227

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

250254
let columns = [PickerColumn::new(
@@ -313,7 +317,7 @@ type FileExplorer = Picker<(PathBuf, bool), (PathBuf, Style)>;
313317

314318
pub fn file_explorer(root: PathBuf, editor: &Editor) -> Result<FileExplorer, std::io::Error> {
315319
let directory_style = editor.theme.get("ui.text.directory");
316-
let directory_content = directory_content(&root)?;
320+
let directory_content = directory_content(&root, editor)?;
317321

318322
let columns = [PickerColumn::new(
319323
"path",
@@ -373,21 +377,47 @@ pub fn file_explorer(root: PathBuf, editor: &Editor) -> Result<FileExplorer, std
373377
Ok(picker)
374378
}
375379

376-
fn directory_content(path: &Path) -> Result<Vec<(PathBuf, bool)>, std::io::Error> {
377-
let mut content: Vec<_> = std::fs::read_dir(path)?
378-
.flatten()
379-
.map(|entry| {
380-
(
381-
entry.path(),
382-
std::fs::metadata(entry.path()).is_ok_and(|metadata| metadata.is_dir()),
383-
)
380+
fn directory_content(path: &Path, editor: &Editor) -> Result<Vec<(PathBuf, bool)>, std::io::Error> {
381+
use ignore::WalkBuilder;
382+
383+
let config = editor.config();
384+
385+
let mut walk_builder = WalkBuilder::new(path);
386+
387+
let mut content: Vec<(PathBuf, bool)> = walk_builder
388+
.hidden(config.file_explorer.hidden)
389+
.parents(config.file_explorer.parents)
390+
.ignore(config.file_explorer.ignore)
391+
.follow_links(config.file_explorer.follow_symlinks)
392+
.git_ignore(config.file_explorer.git_ignore)
393+
.git_global(config.file_explorer.git_global)
394+
.git_exclude(config.file_explorer.git_exclude)
395+
.max_depth(Some(1))
396+
.add_custom_ignore_filename(helix_loader::config_dir().join("ignore"))
397+
.add_custom_ignore_filename(".helix/ignore")
398+
.types(get_excluded_types())
399+
.build()
400+
.filter_map(|entry| {
401+
entry
402+
.map(|entry| {
403+
(
404+
entry.path().to_path_buf(),
405+
entry
406+
.file_type()
407+
.is_some_and(|file_type| file_type.is_dir()),
408+
)
409+
})
410+
.ok()
411+
.filter(|entry| entry.0 != path)
384412
})
385413
.collect();
386414

387415
content.sort_by(|(path1, is_dir1), (path2, is_dir2)| (!is_dir1, path1).cmp(&(!is_dir2, path2)));
416+
388417
if path.parent().is_some() {
389418
content.insert(0, (path.join(".."), true));
390419
}
420+
391421
Ok(content)
392422
}
393423

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
@@ -1010,6 +1037,7 @@ impl Default for Config {
10101037
completion_trigger_len: 2,
10111038
auto_info: true,
10121039
file_picker: FilePickerConfig::default(),
1040+
file_explorer: FileExplorerConfig::default(),
10131041
statusline: StatusLineConfig::default(),
10141042
cursor_shape: CursorShapeConfig::default(),
10151043
true_color: false,

0 commit comments

Comments
 (0)