ivy.nvim/rust/finder.rs
Ade Attwood 7ecc6f1226 feat: don't require a git directory to use .gitignore files
When we are searching we don't want to have to initialize git repo to use a
.gitignore file. This should just ignore any file in a .gitignore always
2024-01-17 21:02:27 +00:00

38 lines
1.2 KiB
Rust

use ignore::{overrides::OverrideBuilder, WalkBuilder};
use std::fs;
pub struct Options {
pub directory: String,
}
pub fn find_files(options: Options) -> Vec<String> {
let mut files: Vec<String> = Vec::new();
let base_path = &fs::canonicalize(options.directory).unwrap();
let mut builder = WalkBuilder::new(base_path);
// Search for hidden files and directories
builder.hidden(false);
// Don't require a git repo to use .gitignore files. We want to use the .gitignore files
// wherever we are
builder.require_git(false);
// TODO(ade): Remove unwraps and find a good way to get the errors into the UI. Currently there
// is no way to handel errors in the rust library
let mut override_builder = OverrideBuilder::new("");
override_builder.add("!.git").unwrap();
let overrides = override_builder.build().unwrap();
builder.overrides(overrides);
for result in builder.build() {
let absolute_candidate = result.unwrap();
let candidate_path = absolute_candidate.path().strip_prefix(base_path).unwrap();
if candidate_path.is_dir() {
continue;
}
files.push(candidate_path.to_str().unwrap().to_string());
}
files
}