Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP Allow specifying glob pattern to ignore config option #3413

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ bytecount = "0.5"
unicode-width = "0.1.5"
unicode_categories = "0.1.1"
dirs = "1.0.4"
globset = "0.4"

# A noop dependency that changes in the Rust repository, it's a bit of a hack.
# See the `src/tools/rustc-workspace-hack/README.md` file in `rust-lang/rust`
Expand Down
25 changes: 11 additions & 14 deletions src/config/options.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
use std::collections::HashSet;
use std::collections::{hash_set, HashSet};
use std::path::{Path, PathBuf};

use atty;

use crate::config::config_type::ConfigType;
use crate::config::lists::*;
use crate::config::{Config, FileName};
use crate::config::Config;

/// Macro that will stringify the enum variants or a provided textual repr
#[macro_export]
Expand Down Expand Up @@ -398,6 +398,15 @@ impl Default for EmitMode {
#[derive(Default, Deserialize, Serialize, Clone, Debug, PartialEq)]
pub struct IgnoreList(HashSet<PathBuf>);

impl<'a> IntoIterator for &'a IgnoreList {
type Item = &'a PathBuf;
type IntoIter = hash_set::Iter<'a, PathBuf>;

fn into_iter(self) -> Self::IntoIter {
self.0.iter()
}
}

impl IgnoreList {
pub fn add_prefix(&mut self, dir: &Path) {
self.0 = self
Expand All @@ -414,18 +423,6 @@ impl IgnoreList {
})
.collect();
}

fn skip_file_inner(&self, file: &Path) -> bool {
self.0.iter().any(|path| file.starts_with(path))
}

pub fn skip_file(&self, file: &FileName) -> bool {
if let FileName::Real(ref path) = file {
self.skip_file_inner(path)
} else {
false
}
}
}

impl ::std::str::FromStr for IgnoreList {
Expand Down
12 changes: 10 additions & 2 deletions src/formatting.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ use syntax::source_map::{FilePathMapping, SourceMap, Span};

use crate::comment::{CharClasses, FullCodeCharKind};
use crate::config::{Config, FileName, Verbosity};
use crate::ignore::IgnorePathSet;
use crate::issues::BadIssueSeeker;
use crate::visitor::{FmtVisitor, SnippetProvider};
use crate::{modules, source_file, ErrorKind, FormatReport, Input, Session};
Expand Down Expand Up @@ -86,10 +87,14 @@ fn format_project<T: FormatHandler>(
parse_session.span_diagnostic = Handler::with_emitter(true, false, silent_emitter);

let mut context = FormatContext::new(&krate, report, parse_session, config, handler);
let ignore_path_set = match IgnorePathSet::from_ignore_list(&config.ignore()) {
Ok(set) => set,
Err(e) => return Err(ErrorKind::InvalidGlobPattern(e)),
};

let files = modules::list_files(&krate, context.parse_session.source_map())?;
for (path, module) in files {
if (config.skip_children() && path != main_file) || config.ignore().skip_file(&path) {
if (config.skip_children() && path != main_file) || ignore_path_set.is_match(&path) {
continue;
}
should_emit_verbose(input_is_stdin, config, || println!("Formatting {}", path));
Expand Down Expand Up @@ -259,7 +264,10 @@ impl FormattingError {
| ErrorKind::IoError(_)
| ErrorKind::ParseError
| ErrorKind::LostComment => "internal error:",
ErrorKind::LicenseCheck | ErrorKind::BadAttr | ErrorKind::VersionMismatch => "error:",
ErrorKind::LicenseCheck
| ErrorKind::BadAttr
| ErrorKind::InvalidGlobPattern(..)
| ErrorKind::VersionMismatch => "error:",
ErrorKind::BadIssue(_) | ErrorKind::DeprecatedAttr => "warning:",
}
}
Expand Down
26 changes: 26 additions & 0 deletions src/ignore.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
use globset::{self, Glob, GlobSet, GlobSetBuilder};

use crate::config::{FileName, IgnoreList};

pub struct IgnorePathSet {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you write few words documenting this struct ?

ignore_glob_set: GlobSet,
}

impl IgnorePathSet {
pub fn from_ignore_list(ignore_list: &IgnoreList) -> Result<Self, globset::Error> {
let mut globset_builder = GlobSetBuilder::new();
for ignore_path in ignore_list {
globset_builder.add(Glob::new(&ignore_path.to_string_lossy())?);
}
Ok(IgnorePathSet {
ignore_glob_set: globset_builder.build()?,
})
}

pub fn is_match(&self, file_name: &FileName) -> bool {
match file_name {
FileName::Stdin => false,
FileName::Real(p) => self.ignore_glob_set.is_match(p),
}
}
}
4 changes: 4 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ mod comment;
pub(crate) mod config;
mod expr;
pub(crate) mod formatting;
mod ignore;
mod imports;
mod issues;
mod items;
Expand Down Expand Up @@ -108,6 +109,9 @@ pub enum ErrorKind {
/// If we had formatted the given node, then we would have lost a comment.
#[fail(display = "not formatted because a comment would be lost")]
LostComment,
/// Invalid glob pattern in `ignore` configuration option.
#[fail(display = "Invalid glob pattern found in ignore list: {}", _0)]
InvalidGlobPattern(globset::Error),
}

impl ErrorKind {
Expand Down