Skip to content

Commit f4525c3

Browse files
committed
feat(oxfmt): Handle ignoring files
1 parent fda4104 commit f4525c3

15 files changed

+125
-7
lines changed

apps/oxfmt/src/command.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,10 @@ pub struct BasicOptions {
6363
/// Ignore Options
6464
#[derive(Debug, Clone, Bpaf)]
6565
pub struct IgnoreOptions {
66+
/// Path to ignore file(s). Can be specified multiple times.
67+
/// If not specified, .gitignore and .prettierignore in the current directory are used.
68+
#[bpaf(argument("PATH"), many)]
69+
pub ignore_path: Vec<PathBuf>,
6670
/// Format code in node_modules directory (skipped by default)
6771
#[bpaf(switch, hide_usage)]
6872
pub with_node_modules: bool,

apps/oxfmt/src/format.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,12 @@ impl FormatRunner {
6060
}
6161
};
6262

63-
let walker = match Walk::build(&cwd, &paths, ignore_options.with_node_modules) {
63+
let walker = match Walk::build(
64+
&cwd,
65+
&paths,
66+
&ignore_options.ignore_path,
67+
ignore_options.with_node_modules,
68+
) {
6469
Ok(walker) => walker,
6570
Err(err) => {
6671
print_and_flush_stdout(

apps/oxfmt/src/walk.rs

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ impl Walk {
1616
pub fn build(
1717
cwd: &PathBuf,
1818
paths: &[PathBuf],
19+
ignore_paths: &[PathBuf],
1920
with_node_modules: bool,
2021
) -> Result<Self, String> {
2122
let (target_paths, exclude_patterns) = normalize_paths(cwd, paths);
@@ -44,12 +45,12 @@ impl Walk {
4445
inner.overrides(overrides);
4546
}
4647

47-
// TODO: Support ignoring files
48-
// --ignore-path PATH1 --ignore-path PATH2
49-
// or default cwd/{.gitignore,.prettierignore}
50-
// if let Some(err) = inner.add_ignore(path) {
51-
// return Err(format!("Failed to add ignore file: {}", err));
52-
// }
48+
// Handle ignore files
49+
for ignore_path in load_ignore_paths(cwd, ignore_paths) {
50+
if inner.add_ignore(&ignore_path).is_some() {
51+
return Err(format!("Failed to add ignore file: {}", ignore_path.display()));
52+
}
53+
}
5354

5455
// NOTE: If return `false` here, it will not be `visit()`ed at all
5556
inner.filter_entry(move |entry| {
@@ -92,7 +93,10 @@ impl Walk {
9293
.hidden(false)
9394
// Do not respect `.gitignore` automatically, we handle it manually
9495
.ignore(false)
96+
.parents(false)
9597
.git_global(false)
98+
.git_ignore(false)
99+
.git_exclude(false)
96100
.build_parallel();
97101
Ok(Self { inner })
98102
}
@@ -155,6 +159,25 @@ fn normalize_paths(cwd: &Path, input_paths: &[PathBuf]) -> (Vec<PathBuf>, Vec<St
155159
(target_paths, exclude_patterns)
156160
}
157161

162+
fn load_ignore_paths(cwd: &Path, ignore_paths: &[PathBuf]) -> Vec<PathBuf> {
163+
// If specified, just resolves absolute paths
164+
if !ignore_paths.is_empty() {
165+
return ignore_paths
166+
.iter()
167+
.map(|path| if path.is_absolute() { path.clone() } else { cwd.join(path) })
168+
.collect();
169+
}
170+
171+
// Else, search for default ignore files in cwd
172+
[".gitignore", ".prettierignore"]
173+
.iter()
174+
.filter_map(|file_name| {
175+
let path = cwd.join(file_name);
176+
if path.exists() { Some(path) } else { None }
177+
})
178+
.collect::<Vec<_>>()
179+
}
180+
158181
// ---
159182

160183
pub struct WalkEntry {
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
not-formatted/
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
ignored/
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
class Foo {}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
ignored/
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
class Foo {}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
class Baz { }
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
class Baz2 { }

0 commit comments

Comments
 (0)