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 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 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" 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"