From bc37086a9acc803fec08c792d167332d772d9748 Mon Sep 17 00:00:00 2001 From: JohnnyMorganz Date: Sun, 17 Nov 2024 16:30:20 +0100 Subject: [PATCH 1/4] Add test case --- tests/inputs-luau/function_types_4.lua | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 tests/inputs-luau/function_types_4.lua diff --git a/tests/inputs-luau/function_types_4.lua b/tests/inputs-luau/function_types_4.lua new file mode 100644 index 00000000..1d6ea54e --- /dev/null +++ b/tests/inputs-luau/function_types_4.lua @@ -0,0 +1,2 @@ +-- https://github.com/JohnnyMorganz/StyLua/issues/896 +local function getData(player: Player): (LongReturnValueWithTypeGeneric1, LongReturnValueWithTypeGeneric2) end From 11b9d744c0a46103268f7c7ca274796e10c70fe7 Mon Sep 17 00:00:00 2001 From: JohnnyMorganz Date: Sun, 17 Nov 2024 16:31:12 +0100 Subject: [PATCH 2/4] Take into account size of function definition when formatting return type --- src/formatters/functions.rs | 26 ++++++++++++++++++++------ 1 file changed, 20 insertions(+), 6 deletions(-) diff --git a/src/formatters/functions.rs b/src/formatters/functions.rs index 00babe60..103344b0 100644 --- a/src/formatters/functions.rs +++ b/src/formatters/functions.rs @@ -750,6 +750,9 @@ pub fn format_function_body( function_body: &FunctionBody, shape: Shape, ) -> FunctionBody { + const SINGLE_PAREN_LEN: usize = "(".len(); + const PARENS_LEN: usize = SINGLE_PAREN_LEN * 2; + // Calculate trivia let leading_trivia = vec![create_indent_trivia(ctx, shape)]; @@ -814,14 +817,26 @@ pub fn format_function_body( shape }; + let type_specifiers = function_body + .type_specifiers() + .map(|x| x.map(|specifier| format_type_specifier(ctx, specifier, parameters_shape))) + .collect::>(); + + let return_type_shape = if multiline_params { + shape.reset() + SINGLE_PAREN_LEN + } else { + shape.take_first_line(&formatted_parameters) + + PARENS_LEN + + type_specifiers.iter().fold(0, |acc, x| { + acc + x.as_ref().map_or(0, |x| x.to_string().len()) + }) + }; + ( - function_body - .type_specifiers() - .map(|x| x.map(|specifier| format_type_specifier(ctx, specifier, parameters_shape))) - .collect::>(), + type_specifiers, function_body .return_type() - .map(|return_type| format_type_specifier(ctx, return_type, shape)), + .map(|return_type| format_type_specifier(ctx, return_type, return_type_shape)), ) }; @@ -834,7 +849,6 @@ pub fn format_function_body( if trivia_util::is_block_empty(function_body.block()) { Block::new() } else { - const PARENS_LEN: usize = "()".len(); let block_shape = shape.take_first_line(&formatted_parameters) + PARENS_LEN; #[cfg(feature = "luau")] From e8aeb2d6dd9c6819c3a3f2f78fce959910a39801 Mon Sep 17 00:00:00 2001 From: JohnnyMorganz Date: Sun, 17 Nov 2024 16:31:19 +0100 Subject: [PATCH 3/4] Add snapshot --- tests/snapshots/tests__luau@function_types_4.lua.snap | 11 +++++++++++ 1 file changed, 11 insertions(+) create mode 100644 tests/snapshots/tests__luau@function_types_4.lua.snap diff --git a/tests/snapshots/tests__luau@function_types_4.lua.snap b/tests/snapshots/tests__luau@function_types_4.lua.snap new file mode 100644 index 00000000..2dedf310 --- /dev/null +++ b/tests/snapshots/tests__luau@function_types_4.lua.snap @@ -0,0 +1,11 @@ +--- +source: tests/tests.rs +expression: "format(&contents, LuaVersion::Luau)" +input_file: tests/inputs-luau/function_types_4.lua +--- +-- https://github.com/JohnnyMorganz/StyLua/issues/896 +local function getData(player: Player): ( + LongReturnValueWithTypeGeneric1, + LongReturnValueWithTypeGeneric2 +) end + From 28a927f8210f6ba08e68979bbb0b01bccbea36f8 Mon Sep 17 00:00:00 2001 From: JohnnyMorganz Date: Sun, 17 Nov 2024 16:31:24 +0100 Subject: [PATCH 4/4] Update changelog --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 02e838a8..7879cb7f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -29,6 +29,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Luau: Fixed incorrect removal of semicolon before compound assignment with parentheses leading to ambiguous syntax error ([#885](https://github.com/JohnnyMorganz/StyLua/issues/885)) - Luau: Fixed incorrect collapsing of union/intersection type value with comments in a type table leading to a syntax error ([#893](https://github.com/JohnnyMorganz/StyLua/issues/893)) - Fixed `--verify` panicing due to overflow for very large Hex numbers ([#875](https://github.com/JohnnyMorganz/StyLua/issues/875), [#889](https://github.com/JohnnyMorganz/StyLua/issues/889)) +- Luau: take into account the size of the current function definition when formatting the return type ([#896](https://github.com/JohnnyMorganz/StyLua/issues/896)) ## [0.20.0] - 2024-01-20