From 2f08d8d8b320c5599838d2ea9f09ad37ac6c799d Mon Sep 17 00:00:00 2001 From: mo8it Date: Sat, 24 Feb 2024 23:34:00 +0100 Subject: [PATCH] Deduplicate building a walk builder from config --- Cargo.lock | 1 + Cargo.toml | 1 + helix-term/Cargo.toml | 2 +- helix-term/src/commands.rs | 20 +++++--------------- helix-term/src/ui/mod.rs | 12 ++---------- helix-view/Cargo.toml | 1 + helix-view/src/editor.rs | 20 ++++++++++++++++++++ 7 files changed, 31 insertions(+), 26 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 2b8a25c85..1662c949e 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1443,6 +1443,7 @@ dependencies = [ "helix-stdx", "helix-tui", "helix-vcs", + "ignore", "libc", "log", "once_cell", diff --git a/Cargo.toml b/Cargo.toml index 91f6e7cae..ff750b0a6 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -39,6 +39,7 @@ package.helix-term.opt-level = 2 [workspace.dependencies] tree-sitter = { version = "0.20", git = "https://github.com/helix-editor/tree-sitter", rev = "660481dbf71413eba5a928b0b0ab8da50c1109e0" } nucleo = "0.2.0" +ignore = "0.4" [workspace.package] version = "23.10.0" diff --git a/helix-term/Cargo.toml b/helix-term/Cargo.toml index 1e21ec161..854863094 100644 --- a/helix-term/Cargo.toml +++ b/helix-term/Cargo.toml @@ -51,7 +51,7 @@ log = "0.4" # File picker nucleo.workspace = true -ignore = "0.4" +ignore.workspace = true # markdown doc rendering pulldown-cmark = { version = "0.10", default-features = false } # file type detection diff --git a/helix-term/src/commands.rs b/helix-term/src/commands.rs index 51a1ede9b..fab735945 100644 --- a/helix-term/src/commands.rs +++ b/helix-term/src/commands.rs @@ -74,7 +74,7 @@ use url::Url; use grep_regex::RegexMatcherBuilder; use grep_searcher::{sinks, BinaryDetection, SearcherBuilder}; -use ignore::{DirEntry, WalkBuilder, WalkState}; +use ignore::{DirEntry, WalkState}; pub type OnKeyCallback = Box; @@ -2281,26 +2281,16 @@ fn global_search(cx: &mut Context) { .canonicalize() .unwrap_or_else(|_| search_root.clone()); let injector_ = injector.clone(); + let mut walk_builder = file_picker_config.walk_builder(search_root); std::thread::spawn(move || { let searcher = SearcherBuilder::new() .binary_detection(BinaryDetection::quit(b'\x00')) .build(); - let mut walk_builder = WalkBuilder::new(search_root); - - walk_builder - .hidden(file_picker_config.hidden) - .parents(file_picker_config.parents) - .ignore(file_picker_config.ignore) - .follow_links(file_picker_config.follow_symlinks) - .git_ignore(file_picker_config.git_ignore) - .git_global(file_picker_config.git_global) - .git_exclude(file_picker_config.git_exclude) - .max_depth(file_picker_config.max_depth) - .filter_entry(move |entry| { - filter_picker_entry(entry, &absolute_root, dedup_symlinks) - }); + walk_builder.filter_entry(move |entry| { + filter_picker_entry(entry, &absolute_root, dedup_symlinks) + }); walk_builder .add_custom_ignore_filename(helix_loader::config_dir().join("ignore")); diff --git a/helix-term/src/ui/mod.rs b/helix-term/src/ui/mod.rs index 0873116cb..7fd9149e2 100644 --- a/helix-term/src/ui/mod.rs +++ b/helix-term/src/ui/mod.rs @@ -156,7 +156,7 @@ pub fn regex_prompt( } pub fn file_picker(root: PathBuf, config: &helix_view::editor::Config) -> Picker { - use ignore::{types::TypesBuilder, WalkBuilder}; + use ignore::types::TypesBuilder; use std::time::Instant; let now = Instant::now(); @@ -164,17 +164,9 @@ pub fn file_picker(root: PathBuf, config: &helix_view::editor::Config) -> Picker let dedup_symlinks = config.file_picker.deduplicate_links; let absolute_root = root.canonicalize().unwrap_or_else(|_| root.clone()); - let mut walk_builder = WalkBuilder::new(&root); + let mut walk_builder = config.file_picker.walk_builder(&root); walk_builder - .hidden(config.file_picker.hidden) - .parents(config.file_picker.parents) - .ignore(config.file_picker.ignore) - .follow_links(config.file_picker.follow_symlinks) - .git_ignore(config.file_picker.git_ignore) - .git_global(config.file_picker.git_global) - .git_exclude(config.file_picker.git_exclude) .sort_by_file_name(|name1, name2| name1.cmp(name2)) - .max_depth(config.file_picker.max_depth) .filter_entry(move |entry| filter_picker_entry(entry, &absolute_root, dedup_symlinks)); walk_builder.add_custom_ignore_filename(helix_loader::config_dir().join("ignore")); diff --git a/helix-view/Cargo.toml b/helix-view/Cargo.toml index b1b444f90..88ed81e43 100644 --- a/helix-view/Cargo.toml +++ b/helix-view/Cargo.toml @@ -48,6 +48,7 @@ log = "~0.4" parking_lot = "0.12.1" +ignore.workspace = true [target.'cfg(windows)'.dependencies] clipboard-win = { version = "5.1", features = ["std"] } diff --git a/helix-view/src/editor.rs b/helix-view/src/editor.rs index fffbe6207..f3d5527ee 100644 --- a/helix-view/src/editor.rs +++ b/helix-view/src/editor.rs @@ -17,6 +17,7 @@ use helix_vcs::DiffProviderRegistry; use futures_util::stream::select_all::SelectAll; use futures_util::{future, StreamExt}; use helix_lsp::Call; +use ignore::WalkBuilder; use tokio_stream::wrappers::UnboundedReceiverStream; use std::{ @@ -212,6 +213,25 @@ impl Default for FilePickerConfig { } } +impl FilePickerConfig { + pub fn walk_builder

(&self, path: P) -> WalkBuilder + where + P: AsRef, + { + let mut builder = WalkBuilder::new(path); + builder + .hidden(self.hidden) + .follow_links(self.follow_symlinks) + .parents(self.parents) + .ignore(self.ignore) + .git_ignore(self.git_ignore) + .git_global(self.git_global) + .git_exclude(self.git_exclude) + .max_depth(self.max_depth); + builder + } +} + #[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] #[serde(rename_all = "kebab-case", default, deny_unknown_fields)] pub struct Config {