Skip to content

Commit d6bb9c7

Browse files
Fix collapsing when comment present in complex expr (#538)
* Add test case * If expression contains comments, then use hanging version * Snapshot * changelog
1 parent 6cb6848 commit d6bb9c7

File tree

4 files changed

+72
-13
lines changed

4 files changed

+72
-13
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1111
- Fixed macOS aarch64 target in release workflow ([#528](https://github.com/JohnnyMorganz/StyLua/issues/528))
1212
- Long union/interesection types inside of a parentheses will now cause the parentheses to expand multiline ([#531](https://github.com/JohnnyMorganz/StyLua/issues/531))
1313
- Fixed leading comments lost from an expression when excessive parentheses are removed from it ([#530](https://github.com/JohnnyMorganz/StyLua/issues/530))
14+
- Fixed comments present in a complex expression not forcing multiline hanging leading to a syntax error ([#524](https://github.com/JohnnyMorganz/StyLua/issues/524))
1415

1516
## [0.14.2] - 2022-07-27
1617

src/formatters/expression.rs

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1038,24 +1038,36 @@ fn hang_binop_expression(
10381038
hang_binop_expression(
10391039
ctx,
10401040
*lhs,
1041-
if same_op_level { top_binop } else { binop },
1041+
if same_op_level {
1042+
top_binop
1043+
} else {
1044+
binop.clone()
1045+
},
10421046
lhs_shape,
10431047
lhs_range,
10441048
),
1045-
format_expression_internal(
1046-
ctx,
1047-
&rhs,
1048-
ExpressionContext::UnaryOrBinary,
1049-
rhs_shape,
1050-
),
1049+
if contains_comments(&*rhs) {
1050+
hang_binop_expression(ctx, *rhs, binop, shape, lhs_range)
1051+
} else {
1052+
format_expression_internal(
1053+
ctx,
1054+
&rhs,
1055+
ExpressionContext::UnaryOrBinary,
1056+
rhs_shape,
1057+
)
1058+
},
10511059
),
10521060
ExpressionSide::Right => (
1053-
format_expression_internal(
1054-
ctx,
1055-
&lhs,
1056-
ExpressionContext::UnaryOrBinary,
1057-
lhs_shape,
1058-
),
1061+
if contains_comments(&*lhs) {
1062+
hang_binop_expression(ctx, *lhs, binop.clone(), shape, lhs_range)
1063+
} else {
1064+
format_expression_internal(
1065+
ctx,
1066+
&lhs,
1067+
ExpressionContext::UnaryOrBinary,
1068+
lhs_shape,
1069+
)
1070+
},
10591071
hang_binop_expression(
10601072
ctx,
10611073
*rhs,
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
-- https://github.com/JohnnyMorganz/StyLua/issues/524
2+
if ( object == "linebreak" or --Force a new line
3+
type(object) == "table" and --Make sure this is an actual object before checking further.
4+
((container.flowMaxPerLine and currentPrimaryLine > container.flowMaxPerLine) or --We went past the max number of columns
5+
currentSecondaryOffset + object["Get"..primaryDirection](object) > container["Get"..primaryDirection](container)) ) then --We went past the max pixel width.
6+
end
7+
8+
if ( name and
9+
((not strictFiltering) and
10+
( tokenTable[subgroup] or tokenTable[className] or (role and tokenTable[role]) or tokenTable[assignedRole] ) -- non-strict filtering
11+
) or
12+
( tokenTable[subgroup] and tokenTable[className] and ((role and tokenTable[role]) or tokenTable[assignedRole]) ) -- strict filtering
13+
) then
14+
15+
end
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
---
2+
source: tests/tests.rs
3+
expression: format(&contents)
4+
---
5+
-- https://github.com/JohnnyMorganz/StyLua/issues/524
6+
if
7+
object == "linebreak" --Force a new line
8+
or type(object) == "table" --Make sure this is an actual object before checking further.
9+
and (
10+
(container.flowMaxPerLine and currentPrimaryLine > container.flowMaxPerLine) --We went past the max number of columns
11+
or currentSecondaryOffset + object["Get" .. primaryDirection](object)
12+
> container["Get" .. primaryDirection](container)
13+
)
14+
then --We went past the max pixel width.
15+
end
16+
17+
if
18+
name
19+
and (
20+
not strictFiltering
21+
and (
22+
tokenTable[subgroup]
23+
or tokenTable[className]
24+
or (role and tokenTable[role])
25+
or tokenTable[assignedRole]
26+
) -- non-strict filtering
27+
)
28+
or (tokenTable[subgroup] and tokenTable[className] and ((role and tokenTable[role]) or tokenTable[assignedRole])) -- strict filtering
29+
then
30+
end
31+

0 commit comments

Comments
 (0)