Skip to content

Commit 8b3b1dd

Browse files
Hang at equals token in assignment if expression cannot hang internally (#440)
* Only use hanging assignment if the expression can be hanged Otherwise, keep hanging at equals token instead * Add test case * Update changelog * Make parentheses an indicator of a hangable expression * Update snapshots
1 parent f1b916a commit 8b3b1dd

File tree

5 files changed

+74
-3
lines changed

5 files changed

+74
-3
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
99
- Fixed leading trivia on semicolon lost when semicolon is removed ([#431](https://github.com/JohnnyMorganz/StyLua/issues/431))
1010
- Fixed shape calculation of the RHS of a binary expression not correctly reset when hanging, causing it to expand unnecessarily ([#432](https://github.com/JohnnyMorganz/StyLua/issues/432))
1111
- Fixed unstable formatting of tables at column width boundary ([#436](https://github.com/JohnnyMorganz/StyLua/issues/436))
12+
- Fixed assignments no longer hanging at equals token if a comment is present, but the expression is not hangable at a binop. ([#439](https://github.com/JohnnyMorganz/StyLua/issues/439))
1213

1314
## [0.13.0] - 2022-03-31
1415
### Added

src/formatters/assignment.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -239,8 +239,9 @@ fn attempt_assignment_tactics(
239239

240240
// Find the better format out of the hanging shape or the normal formatting
241241
// If the expression contains comments, we must hang
242-
if trivia_util::expression_contains_inline_comments(expression)
243-
|| hanging_shape.used_width() < formatting_shape.used_width()
242+
if trivia_util::can_hang_expression(expression)
243+
&& (trivia_util::expression_contains_inline_comments(expression)
244+
|| hanging_shape.used_width() < formatting_shape.used_width())
244245
{
245246
// Hanging version is better
246247
(hanging_expr_list, equal_token)

src/formatters/trivia_util.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ pub fn spans_multiple_lines<T: std::fmt::Display>(item: &T) -> bool {
5858

5959
pub fn can_hang_expression(expression: &Expression) -> bool {
6060
match expression {
61-
Expression::Parentheses { expression, .. } => can_hang_expression(expression),
61+
Expression::Parentheses { .. } => true, // Can always hang parentheses if necessary
6262
Expression::UnaryOperator { expression, .. } => can_hang_expression(expression),
6363
Expression::BinaryOperator { .. } => true, // If a binop is present, then we can hang the expression
6464
Expression::Value { value, .. } => match &**value {
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
-- https://github.com/JohnnyMorganz/StyLua/issues/439
2+
exports.separateDisplayNameAndHOCs =
3+
function(displayName: string | nil, type_: ElementType): (string | nil, Array<string> | nil)
4+
if displayName == nil then
5+
return nil, nil
6+
end
7+
8+
local hocDisplayNames: Array<string>? = nil
9+
10+
if
11+
type_ == ElementTypeClass
12+
or type_ == ElementTypeForwardRef
13+
or type_ == ElementTypeFunction
14+
or type_ == ElementTypeMemo
15+
then
16+
-- ROBLOX deviation: use match instead of indexOf
17+
if (displayName :: string):match("%(") then
18+
-- ROBLOX deviation: use gmatch instead of /[^()]+/g
19+
local matches = (displayName :: string):gmatch("[^()]+")
20+
local nextMatch = matches()
21+
if nextMatch then
22+
displayName = nextMatch
23+
hocDisplayNames = {}
24+
while nextMatch :: any ~= nil do
25+
nextMatch = matches()
26+
table.insert(hocDisplayNames :: Array<string>, nextMatch)
27+
end
28+
end
29+
end
30+
end
31+
end
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
---
2+
source: tests/tests.rs
3+
assertion_line: 30
4+
expression: format(&contents)
5+
6+
---
7+
-- https://github.com/JohnnyMorganz/StyLua/issues/439
8+
exports.separateDisplayNameAndHOCs =
9+
function(displayName: string | nil, type_: ElementType): (string | nil, Array<string> | nil)
10+
if displayName == nil then
11+
return nil, nil
12+
end
13+
14+
local hocDisplayNames: Array<string>? = nil
15+
16+
if
17+
type_ == ElementTypeClass
18+
or type_ == ElementTypeForwardRef
19+
or type_ == ElementTypeFunction
20+
or type_ == ElementTypeMemo
21+
then
22+
-- ROBLOX deviation: use match instead of indexOf
23+
if (displayName :: string):match("%(") then
24+
-- ROBLOX deviation: use gmatch instead of /[^()]+/g
25+
local matches = (displayName :: string):gmatch("[^()]+")
26+
local nextMatch = matches()
27+
if nextMatch then
28+
displayName = nextMatch
29+
hocDisplayNames = {}
30+
while nextMatch :: any ~= nil do
31+
nextMatch = matches()
32+
table.insert(hocDisplayNames :: Array<string>, nextMatch)
33+
end
34+
end
35+
end
36+
end
37+
end
38+

0 commit comments

Comments
 (0)