From eff0f0a8ae66db727e857143fbad00e1631c37c6 Mon Sep 17 00:00:00 2001 From: JohnnyMorganz Date: Sat, 13 Sep 2025 14:54:52 +0200 Subject: [PATCH 1/4] Add test case --- tests/inputs-luau/type-hanging-4.lua | 41 ++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 tests/inputs-luau/type-hanging-4.lua diff --git a/tests/inputs-luau/type-hanging-4.lua b/tests/inputs-luau/type-hanging-4.lua new file mode 100644 index 00000000..99993e27 --- /dev/null +++ b/tests/inputs-luau/type-hanging-4.lua @@ -0,0 +1,41 @@ +export type ThemeKey = -- NOTE: Luau will introduce keyof in the new type solver! replace this when available + | "fontFace" + | "monoFontFace" + | "accent" + | "background" + | "foreground" + | "windowBackground" + | "windowBorder" + | "windowTitleBackground" + | "windowTitleForeground" + | "windowShadow" + | "windowButtonMinimize" + | "windowButtonMaximize" + | "windowButtonClose" + | "appBackground" + | "appForeground" + | "buttonBackground" + | "buttonForeground" + | "buttonHoverBackground" + | "buttonHoverForeground" + | "buttonPressedBackground" + | "buttonPressedForeground" + | "buttonDisabledBackground" + | "buttonDisabledForeground" + | "inputBackground" + | "inputForeground" + | "inputPlaceholder" + | "inputBorder" + | "selectionBackground" + | "selectionForeground" + | "scrollbarBackground" + | "scrollbarThumb" + | "menuBackground" + | "menuForeground" + | "menuHighlight" + | "notificationBackground" + | "notificationForeground" + | "error" + | "warning" + | "success" + | "info" From d3903b01e95275e4288d744d33bd98c6c80c1406 Mon Sep 17 00:00:00 2001 From: JohnnyMorganz Date: Sat, 13 Sep 2025 14:55:13 +0200 Subject: [PATCH 2/4] Check if formatted type declaration fits budget after hanging at equal token --- src/formatters/luau.rs | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/formatters/luau.rs b/src/formatters/luau.rs index dc56f133..7a6c560f 100644 --- a/src/formatters/luau.rs +++ b/src/formatters/luau.rs @@ -1101,7 +1101,12 @@ fn attempt_assigned_type_tactics( let declaration = if contains_comments(strip_trivia(type_info)) { hang_type_info(ctx, type_info, TypeInfoContext::new(), shape, 0) } else { - format_type_info(ctx, type_info, shape) + let proper_declaration = format_type_info(ctx, type_info, shape); + if shape.test_over_budget(&proper_declaration) { + hang_type_info(ctx, type_info, TypeInfoContext::new(), shape, 0) + } else { + proper_declaration + } }; // Take the leading comments and format them nicely From 67f341d52eeb829852ac3eeaf36198888c52ebd8 Mon Sep 17 00:00:00 2001 From: JohnnyMorganz Date: Sat, 13 Sep 2025 14:55:38 +0200 Subject: [PATCH 3/4] Update snapshot --- .../tests__luau@type-hanging-4.lua.snap | 46 +++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 tests/snapshots/tests__luau@type-hanging-4.lua.snap diff --git a/tests/snapshots/tests__luau@type-hanging-4.lua.snap b/tests/snapshots/tests__luau@type-hanging-4.lua.snap new file mode 100644 index 00000000..99c4e086 --- /dev/null +++ b/tests/snapshots/tests__luau@type-hanging-4.lua.snap @@ -0,0 +1,46 @@ +--- +source: tests/tests.rs +expression: "format(&contents, LuaVersion::Luau)" +input_file: tests/inputs-luau/type-hanging-4.lua +--- +export type ThemeKey = -- NOTE: Luau will introduce keyof in the new type solver! replace this when available + | "fontFace" + | "monoFontFace" + | "accent" + | "background" + | "foreground" + | "windowBackground" + | "windowBorder" + | "windowTitleBackground" + | "windowTitleForeground" + | "windowShadow" + | "windowButtonMinimize" + | "windowButtonMaximize" + | "windowButtonClose" + | "appBackground" + | "appForeground" + | "buttonBackground" + | "buttonForeground" + | "buttonHoverBackground" + | "buttonHoverForeground" + | "buttonPressedBackground" + | "buttonPressedForeground" + | "buttonDisabledBackground" + | "buttonDisabledForeground" + | "inputBackground" + | "inputForeground" + | "inputPlaceholder" + | "inputBorder" + | "selectionBackground" + | "selectionForeground" + | "scrollbarBackground" + | "scrollbarThumb" + | "menuBackground" + | "menuForeground" + | "menuHighlight" + | "notificationBackground" + | "notificationForeground" + | "error" + | "warning" + | "success" + | "info" From e5fbd5f3fef429217b91811d6558273b2004170f Mon Sep 17 00:00:00 2001 From: JohnnyMorganz Date: Sat, 13 Sep 2025 14:56:22 +0200 Subject: [PATCH 4/4] Update changelog --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 126be3d6..2d4c1341 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -17,6 +17,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Fixed goto not being recognised for LuaJIT ([#986](https://github.com/JohnnyMorganz/StyLua/issues/986)) - Fixed semicolon removed after a statement ending with an if-expression leading to ambiguous syntax when the next line begins with parentheses ([#1010](https://github.com/JohnnyMorganz/StyLua/issues/1010)) - Luau: Fixed malformed formatting when there is a comment after a type specifier in a local assignment ([#995](https://github.com/JohnnyMorganz/StyLua/issues/995)) +- Luau: Fixed long type union formatted onto a single line if there is a comment in between the equals sign and the type union in a type declaration ([#1007](https://github.com/JohnnyMorganz/StyLua/issues/1007)) ## [2.1.0] - 2025-04-21