Skip to content
Merged
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 12 additions & 0 deletions src/cli/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,18 @@ fn find_config_file(mut directory: PathBuf, recursive: bool) -> Result<Option<Co
}
}

pub fn find_ignore_file_path(mut directory: PathBuf, recursive: bool) -> Option<PathBuf> {
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<Option<Config>> {
// Look in `$XDG_CONFIG_HOME`
Expand Down
53 changes: 47 additions & 6 deletions src/cli/main.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -179,9 +181,13 @@ fn format_string(
range: Option<Range>,
opt: &opt::Opt,
verify_output: OutputVerification,
should_skip: bool,
) -> Result<FormatResult> {
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")
Expand All @@ -198,6 +204,23 @@ fn format_string(
}
}

fn get_ignore(
directory: &Path,
search_parent_directories: bool,
) -> Result<Gitignore, ignore::Error> {
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<i32> {
if opt.files.is_empty() {
bail!("no files provided");
Expand Down Expand Up @@ -341,12 +364,30 @@ fn format(opt: opt::Opt) -> Result<i32> {
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(),
Expand Down