From fcf04c0b8bc8006f57631a87874a3a9c2b3a278a Mon Sep 17 00:00:00 2001 From: JohnnyMorganz Date: Sat, 23 Dec 2023 12:45:25 +0100 Subject: [PATCH 1/3] Add test case --- tests/inputs/function-definition-multiline-2.lua | 4 ++++ 1 file changed, 4 insertions(+) create mode 100644 tests/inputs/function-definition-multiline-2.lua diff --git a/tests/inputs/function-definition-multiline-2.lua b/tests/inputs/function-definition-multiline-2.lua new file mode 100644 index 00000000..fad02772 --- /dev/null +++ b/tests/inputs/function-definition-multiline-2.lua @@ -0,0 +1,4 @@ +-- https://github.com/JohnnyMorganz/StyLua/issues/830 +local a_very_long_variable_name_given_that_is_bigger_than_width_upper_limit_but_unfortunately_can_not_be_made_shorter = function() + print("Hello") +end From 38394b68e250d3934ce3da047c49cb1c0a185366 Mon Sep 17 00:00:00 2001 From: JohnnyMorganz Date: Sat, 23 Dec 2023 12:45:32 +0100 Subject: [PATCH 2/3] Don't put on multiple lines --- src/formatters/functions.rs | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/formatters/functions.rs b/src/formatters/functions.rs index 33ff8c80..0ceb814d 100644 --- a/src/formatters/functions.rs +++ b/src/formatters/functions.rs @@ -572,6 +572,10 @@ fn should_parameters_format_multiline( const PARENS_LEN: usize = "()".len(); const SINGLELINE_END_LEN: usize = " end".len(); + if function_body.parameters().is_empty() { + return false; + } + // Check the length of the parameters. We need to format them first onto a single line to check if required let mut line_length = format_singleline_parameters(ctx, function_body, shape) .to_string() From 85caf1127e83c810ee2679ea8ace5ec27b45a304 Mon Sep 17 00:00:00 2001 From: JohnnyMorganz Date: Sat, 23 Dec 2023 12:45:37 +0100 Subject: [PATCH 3/3] Update snapshots and changelog --- CHANGELOG.md | 1 + ...__standard@function-definition-multiline-2.lua.snap | 10 ++++++++++ 2 files changed, 11 insertions(+) create mode 100644 tests/snapshots/tests__standard@function-definition-multiline-2.lua.snap diff --git a/CHANGELOG.md b/CHANGELOG.md index 1cf0bc9e..bde0ec44 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -13,6 +13,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Fixed missing space when table is inside of Luau interpolated string expression (`{{` is invalid syntax) - The CLI tool will now only write files if the contents differ, and not modify if no change (#827) - Fixed parentheses around a Luau compound type inside of a type table indexer being removed causing a syntax error (#828) +- Fixed function body parentheses being placed on multiple lines unnecessarily when there are no parameters (#830) ## [0.19.1] - 2023-11-15 diff --git a/tests/snapshots/tests__standard@function-definition-multiline-2.lua.snap b/tests/snapshots/tests__standard@function-definition-multiline-2.lua.snap new file mode 100644 index 00000000..d2cf3556 --- /dev/null +++ b/tests/snapshots/tests__standard@function-definition-multiline-2.lua.snap @@ -0,0 +1,10 @@ +--- +source: tests/tests.rs +expression: format(&contents) +input_file: tests/inputs/function-definition-multiline-2.lua +--- +-- https://github.com/JohnnyMorganz/StyLua/issues/830 +local a_very_long_variable_name_given_that_is_bigger_than_width_upper_limit_but_unfortunately_can_not_be_made_shorter = function() + print("Hello") +end +