From 229e1bea9749986307b8dec0750167c96fd2ab8e Mon Sep 17 00:00:00 2001 From: JohnnyMorganz Date: Sun, 17 Nov 2024 13:31:56 +0100 Subject: [PATCH 1/2] Add option to allow formatting files listed in .gitignore --- src/cli/main.rs | 37 ++++++++++++++++++++++++++++++++++++- src/cli/opt.rs | 4 ++++ 2 files changed, 40 insertions(+), 1 deletion(-) diff --git a/src/cli/main.rs b/src/cli/main.rs index 2c96d7ce..32075878 100644 --- a/src/cli/main.rs +++ b/src/cli/main.rs @@ -304,7 +304,7 @@ fn format(opt: opt::Opt) -> Result { } walker_builder - .standard_filters(true) + .standard_filters(!opt.allow_gitignore) .hidden(!opt.allow_hidden) .parents(true) .add_custom_ignore_filename(".styluaignore"); @@ -808,4 +808,39 @@ mod tests { cwd.close().unwrap(); } + + #[test] + fn test_formatting_respects_gitignore() { + let cwd = construct_tree!({ + ".git/dummy.txt": "", // Need a .git folder for .gitignore lookup + ".gitignore": "foo.lua", + "foo.lua": "local x = 1", + }); + + let mut cmd = create_stylua(); + cmd.current_dir(cwd.path()).args(["."]).assert().success(); + + cwd.child("foo.lua").assert("local x = 1"); + + cwd.close().unwrap(); + } + + #[test] + fn test_formatting_still_formats_gitignore_files_if_requested() { + let cwd = construct_tree!({ + ".git/dummy.txt": "", // Need a .git folder for .gitignore lookup + ".gitignore": "foo.lua", + "foo.lua": "local x = 1", + }); + + let mut cmd = create_stylua(); + cmd.current_dir(cwd.path()) + .args(["--allow-gitignore", "."]) + .assert() + .success(); + + cwd.child("foo.lua").assert("local x = 1\n"); + + cwd.close().unwrap(); + } } diff --git a/src/cli/opt.rs b/src/cli/opt.rs index 0e7c5884..4b59c9e6 100644 --- a/src/cli/opt.rs +++ b/src/cli/opt.rs @@ -98,6 +98,10 @@ pub struct Opt { #[structopt(short, long)] pub allow_hidden: bool, + /// Whether to continue formatting files listed in .gitignore / .ignore + #[structopt(long)] + pub allow_gitignore: bool, + /// Disables the EditorConfig feature. /// /// Has no effect if a stylua.toml configuration file is found. From fda3aa4dfe966b595f378c70a72d097809e4dd71 Mon Sep 17 00:00:00 2001 From: JohnnyMorganz Date: Sun, 17 Nov 2024 13:36:21 +0100 Subject: [PATCH 2/2] Update changelog --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 02e838a8..1ca609ec 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -16,6 +16,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Added runtime syntax configuration option `syntax` to help handle ambiguous syntax. By default, StyLua builds and runs with a parser to handle all Lua versions. However, the syntax of some Lua versions conflict with eachother: most notably, Lua 5.2+ goto label syntax `::label::` and Luau type assertion operator `::`. This option allows choosing what syntax to parse, to handle these conflicts. ([#407](https://github.com/JohnnyMorganz/StyLua/issues/407)) - Added configuration option `space_after_function_names` to specify whether to include a space between a function name and parentheses ([#839](https://github.com/JohnnyMorganz/StyLua/issues/839)) +- Added flag `--allow-gitignore` to continue formatting files listed in a `.gitignore` file, instead of skipping over them ([#895](https://github.com/JohnnyMorganz/StyLua/issues/895)) ### Changed