diff --git a/CHANGELOG.md b/CHANGELOG.md index a55abdc0..a6857de2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,6 +12,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Added flag `--no-ignore-vcs` to continue formatting files listed in a `.gitignore` file, instead of skipping over them ([#895](https://github.com/JohnnyMorganz/StyLua/issues/895)) - Support `call_parentheses = Input` in editorconfig ([#1057](https://github.com/JohnnyMorganz/StyLua/pull/1057)) - Luau: Support formatting explicit type instantiations (`f<>()`, `obj:method<>()`) +- Support `stylua_syntax` and `stylua_block_newline_gaps` properties in editorconfig ([#1054](https://github.com/JohnnyMorganz/StyLua/issues/1054)) ### Fixed @@ -21,6 +22,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Fixed malformed formatting when a binary expression inside of a function call with comments around the operators is incorrectly collapsed onto one line ([#996](https://github.com/JohnnyMorganz/StyLua/issues/996)) - Fixed repeated unary minus on long lines being collapsed into a comment, e.g. `- - - - -5` becoming `-----5` ([#1075](https://github.com/JohnnyMorganz/StyLua/issues/1075)) +- Fixed `--preserve-block-newline-gaps` CLI flag not being applied as a config override ## [2.3.1] - 2025-11-01 diff --git a/src/cli/config.rs b/src/cli/config.rs index ca316a18..4b8591a2 100644 --- a/src/cli/config.rs +++ b/src/cli/config.rs @@ -289,6 +289,9 @@ fn load_overrides(config: Config, opt: &Opt) -> Config { if let Some(collapse_simple_statement) = opt.format_opts.collapse_simple_statement { new_config.collapse_simple_statement = collapse_simple_statement.into(); } + if let Some(preserve_block_newline_gaps) = opt.format_opts.preserve_block_newline_gaps { + new_config.block_newline_gaps = preserve_block_newline_gaps.into(); + }; if opt.format_opts.sort_requires { new_config.sort_requires = SortRequiresConfig { enabled: true } } diff --git a/src/editorconfig.rs b/src/editorconfig.rs index e965cc44..1dfb4fc8 100644 --- a/src/editorconfig.rs +++ b/src/editorconfig.rs @@ -1,6 +1,6 @@ use crate::{ - CallParenType, CollapseSimpleStatement, Config, IndentType, LineEndings, QuoteStyle, - SortRequiresConfig, SpaceAfterFunctionNames, + BlockNewlineGaps, CallParenType, CollapseSimpleStatement, Config, IndentType, LineEndings, + LuaVersion, QuoteStyle, SortRequiresConfig, SpaceAfterFunctionNames, }; use ec4rs::{ properties_of, @@ -88,6 +88,24 @@ property_choice! { (False, "false") } +property_choice! { + StyluaSyntaxChoice, "stylua_syntax"; + (All, "all"), + (Lua51, "lua51"), + (Lua52, "lua52"), + (Lua53, "lua53"), + (Lua54, "lua54"), + (Luau, "luau"), + (LuaJIT, "luajit"), + (CfxLua, "cfxlua") +} + +property_choice! { + StyluaBlockNewlineGapsChoice, "stylua_block_newline_gaps"; + (Never, "never"), + (Preserve, "preserve") +} + // Override StyLua config with EditorConfig properties fn load(mut config: Config, properties: &Properties) -> Config { if let Ok(end_of_line) = properties.get::() { @@ -157,6 +175,33 @@ fn load(mut config: Config, properties: &Properties) -> Config { SortRequiresChoice::False => SortRequiresConfig { enabled: false }, }; } + if let Ok(syntax) = properties.get::() { + config.syntax = match syntax { + StyluaSyntaxChoice::All => LuaVersion::All, + StyluaSyntaxChoice::Lua51 => LuaVersion::Lua51, + #[cfg(feature = "lua52")] + StyluaSyntaxChoice::Lua52 => LuaVersion::Lua52, + #[cfg(feature = "lua53")] + StyluaSyntaxChoice::Lua53 => LuaVersion::Lua53, + #[cfg(feature = "lua54")] + StyluaSyntaxChoice::Lua54 => LuaVersion::Lua54, + #[cfg(feature = "luau")] + StyluaSyntaxChoice::Luau => LuaVersion::Luau, + #[cfg(feature = "luajit")] + StyluaSyntaxChoice::LuaJIT => LuaVersion::LuaJIT, + #[cfg(feature = "cfxlua")] + StyluaSyntaxChoice::CfxLua => LuaVersion::CfxLua, + // If the feature is not enabled, ignore the value + #[allow(unreachable_patterns)] + _ => config.syntax, + }; + } + if let Ok(block_newline_gaps) = properties.get::() { + config.block_newline_gaps = match block_newline_gaps { + StyluaBlockNewlineGapsChoice::Never => BlockNewlineGaps::Never, + StyluaBlockNewlineGapsChoice::Preserve => BlockNewlineGaps::Preserve, + }; + } config } @@ -426,6 +471,38 @@ mod tests { assert!(!config.sort_requires.enabled); } + #[test] + fn test_stylua_syntax_all() { + let mut properties = Properties::new(); + properties.insert_raw_for_key("stylua_syntax", "all"); + let config = Config::from(&properties); + assert_eq!(config.syntax, LuaVersion::All); + } + + #[test] + fn test_stylua_syntax_lua51() { + let mut properties = Properties::new(); + properties.insert_raw_for_key("stylua_syntax", "Lua51"); + let config = Config::from(&properties); + assert_eq!(config.syntax, LuaVersion::Lua51); + } + + #[test] + fn test_stylua_block_newline_gaps_never() { + let mut properties = Properties::new(); + properties.insert_raw_for_key("stylua_block_newline_gaps", "Never"); + let config = Config::from(&properties); + assert_eq!(config.block_newline_gaps, BlockNewlineGaps::Never); + } + + #[test] + fn test_stylua_block_newline_gaps_preserve() { + let mut properties = Properties::new(); + properties.insert_raw_for_key("stylua_block_newline_gaps", "Preserve"); + let config = Config::from(&properties); + assert_eq!(config.block_newline_gaps, BlockNewlineGaps::Preserve); + } + #[test] fn test_invalid_properties() { let mut properties = Properties::new(); @@ -439,6 +516,8 @@ mod tests { "call_parentheses", "collapse_simple_statement", "sort_requires", + "stylua_syntax", + "stylua_block_newline_gaps", ] { properties.insert_raw_for_key(key, invalid_value); } @@ -457,5 +536,7 @@ mod tests { config.sort_requires.enabled, default_config.sort_requires.enabled ); + assert_eq!(config.syntax, default_config.syntax); + assert_eq!(config.block_newline_gaps, default_config.block_newline_gaps); } }