on master help sign in
Minor cleanup of file picker file gathering logic (#1683)

* Refactor file picker filetype filter logic to remove panic, make clearer An unwrap was unneccesarily present due to a prior contribution of mine which was before I had any understanding of error handling in Rust. I've also swapped a match for an if let, as was originally suggested in the original pull request adding filetype filtering, but was merged before I could address. * Add some comments to the file picker code for clarity * Switch to expect instead of ignoring type def error

commitbdbf42387667b5fa4fa3b89bdb0687cf8cfd2eb0 authorDaniel S Poulin <dan.poulin@digitalsurgeons.com> dateMon 28 Feb 2022, 20:16 (5 years ago) parent7bb1db3
1 file changed+22−13
helix-term/src/ui/mod.rs+22−13
helix-term/src/ui/mod.rs+22−13
@@ -101,8 +101,6 @@ pub fn file_picker(root: PathBuf, config: &helix_view::editor::Config) -> FilePi
101101 use ignore::{types::TypesBuilder, WalkBuilder};
102102 use std::time;
103103
104− // We want to exclude files that the editor can't handle yet
105− let mut type_builder = TypesBuilder::new();
106104 let mut walk_builder = WalkBuilder::new(&root);
107105 walk_builder
108106 .hidden(config.file_picker.hidden)
@@ -117,18 +115,25 @@ pub fn file_picker(root: PathBuf, config: &helix_view::editor::Config) -> FilePi
117115 // in our picker.
118116 .filter_entry(|entry| entry.file_name() != ".git");
119117
120− let walk_builder = match type_builder.add(
121− "compressed",
122− "*.{zip,gz,bz2,zst,lzo,sz,tgz,tbz2,lz,lz4,lzma,lzo,z,Z,xz,7z,rar,cab}",
123− ) {
124− Err(_) => &walk_builder,
125− _ => {
126− type_builder.negate("all");
127− let excluded_types = type_builder.build().unwrap();
128− walk_builder.types(excluded_types)
129− }
130− };
118+ // We want to exclude files that the editor can't handle yet
119+ let mut type_builder = TypesBuilder::new();
120+
121+ type_builder
122+ .add(
123+ "compressed",
124+ "*.{zip,gz,bz2,zst,lzo,sz,tgz,tbz2,lz,lz4,lzma,lzo,z,Z,xz,7z,rar,cab}",
125+ )
126+ // This shouldn't panic as the above is static, but if it ever
127+ // is changed and becomes invalid it will catch here rather than
128+ // being unnoticed.
129+ .expect("Invalid type definition");
130+ type_builder.negate("all");
131+
132+ if let Ok(excluded_types) = type_builder.build() {
133+ walk_builder.types(excluded_types);
134+ }
131135
136+ // We want files along with their modification date for sorting
132137 let files = walk_builder.build().filter_map(|entry| {
133138 let entry = entry.ok()?;
134139 // Path::is_dir() traverses symlinks, so we use it over DirEntry::is_dir
@@ -148,6 +153,8 @@ pub fn file_picker(root: PathBuf, config: &helix_view::editor::Config) -> FilePi
148153 Some((entry.into_path(), time))
149154 });
150155
156+ // Cap the number of files if we aren't in a git project, preventing
157+ // hangs when using the picker in your home directory
151158 let mut files: Vec<_> = if root.join(".git").is_dir() {
152159 files.collect()
153160 } else {
@@ -155,8 +162,10 @@ pub fn file_picker(root: PathBuf, config: &helix_view::editor::Config) -> FilePi
155162 files.take(MAX).collect()
156163 };
157164
165+ // Most recently modified first
158166 files.sort_by_key(|file| std::cmp::Reverse(file.1));
159167
168+ // Strip the time data so we can send just the paths to the FilePicker
160169 let files = files.into_iter().map(|(path, _)| path).collect();
161170
162171 FilePicker::new(