Skip to content

Commit c91e97e

Browse files
Prefer hanging table field value over expanding (#553)
* Hang table field value instead of expanding it * Changelog * Snapshot * Rethink strategy to hang table field value
1 parent 760737f commit c91e97e

File tree

3 files changed

+26
-13
lines changed

3 files changed

+26
-13
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1818
- Fixed call chain with a small prefix not being kept inlined causing unstable formatting ([#514](https://github.com/JohnnyMorganz/StyLua/issues/514))
1919
- Fixed shape computation for table fields causing unnecessary expansion ([#551](https://github.com/JohnnyMorganz/StyLua/issues/551))
2020
- Fixed hanging the prefix string in `("str"):call` unnecessarily when it provides no benefit ([#508](https://github.com/JohnnyMorganz/StyLua/issues/508))
21+
- Fixed table field value being expanded when it could be hanged instead ([#541](https://github.com/JohnnyMorganz/StyLua/issues/541))
2122

2223
## [0.14.2] - 2022-07-27
2324

src/formatters/table.rs

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -40,17 +40,30 @@ fn format_field_expression_value(
4040
expression: &Expression,
4141
shape: Shape,
4242
) -> Expression {
43-
let singleline_value = format_expression(ctx, expression, shape)
44-
.update_trailing_trivia(FormatTriviaType::Replace(vec![])); // We will remove all the trivia from this value, and place it after the comma
45-
46-
if trivia_util::can_hang_expression(expression)
47-
&& shape.take_first_line(&singleline_value).over_budget()
48-
|| trivia_util::expression_contains_inline_comments(expression)
49-
{
50-
hang_expression(ctx, expression, shape, Some(1))
51-
.update_trailing_trivia(FormatTriviaType::Replace(vec![]))
43+
// Remove all trivia from the output expression as it will be moved after the comma
44+
45+
if trivia_util::can_hang_expression(expression) {
46+
if trivia_util::expression_contains_inline_comments(expression) {
47+
hang_expression(ctx, expression, shape, Some(1))
48+
.update_trailing_trivia(FormatTriviaType::Replace(vec![]))
49+
} else {
50+
let singleline_value = format_expression(ctx, expression, shape)
51+
.update_trailing_trivia(FormatTriviaType::Replace(vec![]));
52+
let hanging_value = hang_expression(ctx, expression, shape, Some(1))
53+
.update_trailing_trivia(FormatTriviaType::Replace(vec![]));
54+
55+
if shape.test_over_budget(&singleline_value)
56+
|| format!("{}", hanging_value).lines().count()
57+
< format!("{}", singleline_value).lines().count()
58+
{
59+
hanging_value
60+
} else {
61+
singleline_value
62+
}
63+
}
5264
} else {
53-
singleline_value
65+
format_expression(ctx, expression, shape)
66+
.update_trailing_trivia(FormatTriviaType::Replace(vec![]))
5467
}
5568
}
5669

tests/snapshots/tests__standard@table-field-hanging-1.lua.snap

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,7 @@ expression: format(&contents)
55
-- https://github.com/JohnnyMorganz/StyLua/issues/542
66
-- https://github.com/JohnnyMorganz/StyLua/issues/541
77
local thisIsATable = {
8-
CreateAnElementFromThisTable = SomethingIsSelected and getTheSelectedThing(TheSelectedItem) or getTheSelectedThing(
9-
NoItemSelected
10-
),
8+
CreateAnElementFromThisTable = SomethingIsSelected and getTheSelectedThing(TheSelectedItem)
9+
or getTheSelectedThing(NoItemSelected),
1110
}
1211

0 commit comments

Comments
 (0)