diff --git a/CHANGELOG.md b/CHANGELOG.md index b29c57f2..2a6fed70 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Fixed - Fixed var expression with comments collapsing leading to malformed formatting ([#500](https://github.com/JohnnyMorganz/StyLua/issues/500)) +- Fixed ignore behavior for `--stdin-filepath` ([#495](https://github.com/JohnnyMorganz/StyLua/issues/495)) ## [0.14.0] - 2022-07-06 ### Added diff --git a/src/cli/config.rs b/src/cli/config.rs index e09664ed..cc491865 100644 --- a/src/cli/config.rs +++ b/src/cli/config.rs @@ -48,6 +48,18 @@ fn find_config_file(mut directory: PathBuf, recursive: bool) -> Result Option { + debug!("config: looking for ignore file in {}", directory.display()); + let file_path = directory.join(".styluaignore"); + if file_path.is_file() { + Some(file_path) + } else if recursive && directory.pop() { + find_ignore_file_path(directory, recursive) + } else { + None + } +} + /// Looks for a configuration file at either `$XDG_CONFIG_HOME`, `$XDG_CONFIG_HOME/stylua`, `$HOME/.config` or `$HOME/.config/stylua` fn search_config_locations() -> Result> { // Look in `$XDG_CONFIG_HOME` diff --git a/src/cli/main.rs b/src/cli/main.rs index d7b5b5fa..605c02be 100644 --- a/src/cli/main.rs +++ b/src/cli/main.rs @@ -1,7 +1,7 @@ use anyhow::{bail, Context, Result}; use clap::StructOpt; use console::style; -use ignore::{overrides::OverrideBuilder, WalkBuilder}; +use ignore::{gitignore::Gitignore, overrides::OverrideBuilder, WalkBuilder}; use log::{LevelFilter, *}; use serde_json::json; use std::fs; @@ -15,6 +15,8 @@ use threadpool::ThreadPool; use stylua_lib::{format_code, Config, OutputVerification, Range}; +use crate::config::find_ignore_file_path; + mod config; mod opt; mod output_diff; @@ -179,9 +181,13 @@ fn format_string( range: Option, opt: &opt::Opt, verify_output: OutputVerification, + should_skip: bool, ) -> Result { - let formatted_contents = - format_code(&input, config, range, verify_output).context("failed to format from stdin")?; + let formatted_contents = if should_skip { + input.clone() + } else { + format_code(&input, config, range, verify_output).context("failed to format from stdin")? + }; if opt.check { let diff = create_diff(opt, &input, &formatted_contents, "stdin") @@ -198,6 +204,23 @@ fn format_string( } } +fn get_ignore( + directory: &Path, + search_parent_directories: bool, +) -> Result { + let file_path = find_ignore_file_path(directory.to_path_buf(), search_parent_directories); + if let Some(file_path) = file_path { + let (ignore, err) = Gitignore::new(file_path); + if let Some(err) = err { + Err(err) + } else { + Ok(ignore) + } + } else { + Ok(Gitignore::empty()) + } +} + fn format(opt: opt::Opt) -> Result { if opt.files.is_empty() { bail!("no files provided"); @@ -341,12 +364,30 @@ fn format(opt: opt::Opt) -> Result { let tx = tx.clone(); let opt = opt.clone(); + let should_skip_format = match &opt.stdin_filepath { + Some(filepath) => { + let ignore = get_ignore( + filepath.parent().expect("cannot get parent directory"), + opt.search_parent_directories, + ) + .context("failed to parse ignore file")?; + + matches!(ignore.matched(&filepath, false), ignore::Match::Ignore(_)) + } + None => false, + }; + pool.execute(move || { let mut buf = String::new(); match stdin().read_to_string(&mut buf) { - Ok(_) => { - tx.send(format_string(buf, config, range, &opt, verify_output)) - } + Ok(_) => tx.send(format_string( + buf, + config, + range, + &opt, + verify_output, + should_skip_format, + )), Err(error) => tx.send( Err(ErrorFileWrapper { file: "stdin".to_string(),