Skip to content

Commit c270a2c

Browse files
committed
repalce globset to ignore
- fix from_ignore_list - remove dir from ignore_list - add test - ! and is_none => is_ignore
1 parent db74e49 commit c270a2c

File tree

9 files changed

+129
-46
lines changed

9 files changed

+129
-46
lines changed

Cargo.lock

+61-3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -57,12 +57,12 @@ bytecount = "0.5"
5757
unicode-width = "0.1.5"
5858
unicode_categories = "0.1.1"
5959
dirs = "1.0.4"
60-
globset = "0.4"
6160

6261
# A noop dependency that changes in the Rust repository, it's a bit of a hack.
6362
# See the `src/tools/rustc-workspace-hack/README.md` file in `rust-lang/rust`
6463
# for more information.
6564
rustc-workspace-hack = "1.0.0"
65+
ignore = "0.4.6"
6666

6767
[dev-dependencies]
6868
lazy_static = "1.0.0"

src/config/config_type.rs

+1-5
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ macro_rules! create_config {
141141
ConfigWasSet(self)
142142
}
143143

144-
fn fill_from_parsed_config(mut self, parsed: PartialConfig, dir: &Path) -> Config {
144+
fn fill_from_parsed_config(mut self, parsed: PartialConfig) -> Config {
145145
$(
146146
if let Some(val) = parsed.$i {
147147
if self.$i.3 {
@@ -160,7 +160,6 @@ macro_rules! create_config {
160160
)+
161161
self.set_heuristics();
162162
self.set_license_template();
163-
self.set_ignore(dir);
164163
self
165164
}
166165

@@ -287,9 +286,6 @@ macro_rules! create_config {
287286
}
288287
}
289288

290-
fn set_ignore(&mut self, dir: &Path) {
291-
self.ignore.2.add_prefix(dir);
292-
}
293289

294290
/// Returns `true` if the config key was explicitly set and is the default value.
295291
pub fn is_default(&self, key: &str) -> bool {

src/config/mod.rs

+4-6
Original file line numberDiff line numberDiff line change
@@ -190,8 +190,7 @@ impl Config {
190190
let mut file = File::open(&file_path)?;
191191
let mut toml = String::new();
192192
file.read_to_string(&mut toml)?;
193-
Config::from_toml(&toml, file_path.parent().unwrap())
194-
.map_err(|err| Error::new(ErrorKind::InvalidData, err))
193+
Config::from_toml(&toml).map_err(|err| Error::new(ErrorKind::InvalidData, err))
195194
}
196195

197196
/// Resolves the config for input in `dir`.
@@ -253,7 +252,7 @@ impl Config {
253252
}
254253
}
255254

256-
pub(crate) fn from_toml(toml: &str, dir: &Path) -> Result<Config, String> {
255+
pub(crate) fn from_toml(toml: &str) -> Result<Config, String> {
257256
let parsed: ::toml::Value = toml
258257
.parse()
259258
.map_err(|e| format!("Could not parse TOML: {}", e))?;
@@ -272,7 +271,7 @@ impl Config {
272271
if !err.is_empty() {
273272
eprint!("{}", err);
274273
}
275-
Ok(Config::default().fill_from_parsed_config(parsed_config, dir))
274+
Ok(Config::default().fill_from_parsed_config(parsed_config))
276275
}
277276
Err(e) => {
278277
err.push_str("Error: Decoding config file failed:\n");
@@ -426,8 +425,7 @@ mod test {
426425

427426
#[test]
428427
fn test_was_set() {
429-
use std::path::Path;
430-
let config = Config::from_toml("hard_tabs = true", Path::new("")).unwrap();
428+
let config = Config::from_toml("hard_tabs = true").unwrap();
431429

432430
assert_eq!(config.was_set().hard_tabs(), true);
433431
assert_eq!(config.was_set().verbose(), false);

src/formatting.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use syntax::source_map::{FilePathMapping, SourceMap, Span, DUMMY_SP};
1414

1515
use crate::comment::{CharClasses, FullCodeCharKind};
1616
use crate::config::{Config, FileName, Verbosity};
17-
use crate::ignore::IgnorePathSet;
17+
use crate::ignore_path::IgnorePathSet;
1818
use crate::issues::BadIssueSeeker;
1919
use crate::utils::{count_newlines, get_skip_macro_names};
2020
use crate::visitor::{FmtVisitor, SnippetProvider};
@@ -104,7 +104,8 @@ fn format_project<T: FormatHandler>(
104104
.visit_crate(&krate)
105105
.map_err(|e| io::Error::new(io::ErrorKind::Other, e))?;
106106
for (path, (module, _)) in files {
107-
if (config.skip_children() && path != main_file) || ignore_path_set.is_match(&path) {
107+
let should_ignore = !input_is_stdin && ignore_path_set.is_match(&path);
108+
if (config.skip_children() && path != main_file) || should_ignore {
108109
continue;
109110
}
110111
should_emit_verbose(input_is_stdin, config, || println!("Formatting {}", path));

src/ignore.rs

-26
This file was deleted.

src/ignore_path.rs

+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
use ignore::{self, gitignore};
2+
use std::path::PathBuf;
3+
4+
use crate::config::{FileName, IgnoreList};
5+
6+
pub struct IgnorePathSet {
7+
ignore_set: gitignore::Gitignore,
8+
}
9+
10+
impl IgnorePathSet {
11+
pub fn from_ignore_list(ignore_list: &IgnoreList) -> Result<Self, ignore::Error> {
12+
let mut ignore_builder = gitignore::GitignoreBuilder::new(PathBuf::from(""));
13+
14+
for ignore_path in ignore_list {
15+
ignore_builder.add_line(None, ignore_path.to_str().unwrap())?;
16+
}
17+
18+
Ok(IgnorePathSet {
19+
ignore_set: ignore_builder.build()?,
20+
})
21+
}
22+
23+
pub fn is_match(&self, file_name: &FileName) -> bool {
24+
match file_name {
25+
FileName::Stdin => false,
26+
FileName::Real(p) => self
27+
.ignore_set
28+
.matched_path_or_any_parents(p, false)
29+
.is_ignore(),
30+
}
31+
}
32+
}
33+
34+
#[cfg(test)]
35+
mod test {
36+
use crate::config::{Config, FileName};
37+
use crate::ignore_path::IgnorePathSet;
38+
use std::path::PathBuf;
39+
40+
#[test]
41+
fn test_ignore_path_set() {
42+
let config = Config::from_toml(
43+
"ignore = [
44+
\"foo.rs\",
45+
\"bar_dir/*\",
46+
]",
47+
)
48+
.unwrap();
49+
let ignore_path_set = IgnorePathSet::from_ignore_list(&config.ignore()).unwrap();
50+
51+
assert!(ignore_path_set.is_match(&FileName::Real(PathBuf::from("src/foo.rs"))));
52+
assert!(ignore_path_set.is_match(&FileName::Real(PathBuf::from("bar_dir/baz.rs"))));
53+
assert!(!ignore_path_set.is_match(&FileName::Real(PathBuf::from("src/bar.rs"))));
54+
}
55+
}

src/lib.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ use std::path::PathBuf;
2020
use std::rc::Rc;
2121

2222
use failure::Fail;
23+
use ignore;
2324
use syntax::{ast, parse::DirectoryOwnership};
2425

2526
use crate::comment::LineClasses;
@@ -46,7 +47,7 @@ mod comment;
4647
pub(crate) mod config;
4748
mod expr;
4849
pub(crate) mod formatting;
49-
mod ignore;
50+
mod ignore_path;
5051
mod imports;
5152
mod issues;
5253
mod items;
@@ -113,7 +114,7 @@ pub enum ErrorKind {
113114
LostComment,
114115
/// Invalid glob pattern in `ignore` configuration option.
115116
#[fail(display = "Invalid glob pattern found in ignore list: {}", _0)]
116-
InvalidGlobPattern(globset::Error),
117+
InvalidGlobPattern(ignore::Error),
117118
}
118119

119120
impl ErrorKind {

src/test/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -560,7 +560,7 @@ fn get_config(config_file: Option<&Path>) -> Config {
560560
.read_to_string(&mut def_config)
561561
.expect("Couldn't read config");
562562

563-
Config::from_toml(&def_config, Path::new("tests/config/")).expect("invalid TOML")
563+
Config::from_toml(&def_config).expect("invalid TOML")
564564
}
565565

566566
// Reads significant comments of the form: `// rustfmt-key: value` into a hash map.

0 commit comments

Comments
 (0)