Skip to content

Commit c185fc1

Browse files
Check type declaration size when hanging at equal token due to comment (#1026)
* Add test case * Check if formatted type declaration fits budget after hanging at equal token * Update snapshot * Update changelog
1 parent 2cf31d7 commit c185fc1

File tree

4 files changed

+94
-1
lines changed

4 files changed

+94
-1
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1717
- Fixed goto not being recognised for LuaJIT ([#986](https://github.com/JohnnyMorganz/StyLua/issues/986))
1818
- 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))
1919
- 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))
20+
- 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))
2021

2122
## [2.1.0] - 2025-04-21
2223

src/formatters/luau.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1101,7 +1101,12 @@ fn attempt_assigned_type_tactics(
11011101
let declaration = if contains_comments(strip_trivia(type_info)) {
11021102
hang_type_info(ctx, type_info, TypeInfoContext::new(), shape, 0)
11031103
} else {
1104-
format_type_info(ctx, type_info, shape)
1104+
let proper_declaration = format_type_info(ctx, type_info, shape);
1105+
if shape.test_over_budget(&proper_declaration) {
1106+
hang_type_info(ctx, type_info, TypeInfoContext::new(), shape, 0)
1107+
} else {
1108+
proper_declaration
1109+
}
11051110
};
11061111

11071112
// Take the leading comments and format them nicely
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
export type ThemeKey = -- NOTE: Luau will introduce keyof in the new type solver! replace this when available
2+
| "fontFace"
3+
| "monoFontFace"
4+
| "accent"
5+
| "background"
6+
| "foreground"
7+
| "windowBackground"
8+
| "windowBorder"
9+
| "windowTitleBackground"
10+
| "windowTitleForeground"
11+
| "windowShadow"
12+
| "windowButtonMinimize"
13+
| "windowButtonMaximize"
14+
| "windowButtonClose"
15+
| "appBackground"
16+
| "appForeground"
17+
| "buttonBackground"
18+
| "buttonForeground"
19+
| "buttonHoverBackground"
20+
| "buttonHoverForeground"
21+
| "buttonPressedBackground"
22+
| "buttonPressedForeground"
23+
| "buttonDisabledBackground"
24+
| "buttonDisabledForeground"
25+
| "inputBackground"
26+
| "inputForeground"
27+
| "inputPlaceholder"
28+
| "inputBorder"
29+
| "selectionBackground"
30+
| "selectionForeground"
31+
| "scrollbarBackground"
32+
| "scrollbarThumb"
33+
| "menuBackground"
34+
| "menuForeground"
35+
| "menuHighlight"
36+
| "notificationBackground"
37+
| "notificationForeground"
38+
| "error"
39+
| "warning"
40+
| "success"
41+
| "info"

tests/snapshots/[email protected]

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
---
2+
source: tests/tests.rs
3+
expression: "format(&contents, LuaVersion::Luau)"
4+
input_file: tests/inputs-luau/type-hanging-4.lua
5+
---
6+
export type ThemeKey = -- NOTE: Luau will introduce keyof in the new type solver! replace this when available
7+
| "fontFace"
8+
| "monoFontFace"
9+
| "accent"
10+
| "background"
11+
| "foreground"
12+
| "windowBackground"
13+
| "windowBorder"
14+
| "windowTitleBackground"
15+
| "windowTitleForeground"
16+
| "windowShadow"
17+
| "windowButtonMinimize"
18+
| "windowButtonMaximize"
19+
| "windowButtonClose"
20+
| "appBackground"
21+
| "appForeground"
22+
| "buttonBackground"
23+
| "buttonForeground"
24+
| "buttonHoverBackground"
25+
| "buttonHoverForeground"
26+
| "buttonPressedBackground"
27+
| "buttonPressedForeground"
28+
| "buttonDisabledBackground"
29+
| "buttonDisabledForeground"
30+
| "inputBackground"
31+
| "inputForeground"
32+
| "inputPlaceholder"
33+
| "inputBorder"
34+
| "selectionBackground"
35+
| "selectionForeground"
36+
| "scrollbarBackground"
37+
| "scrollbarThumb"
38+
| "menuBackground"
39+
| "menuForeground"
40+
| "menuHighlight"
41+
| "notificationBackground"
42+
| "notificationForeground"
43+
| "error"
44+
| "warning"
45+
| "success"
46+
| "info"

0 commit comments

Comments
 (0)