Skip to content

Commit 78ec64f

Browse files
Fix table field shape calculation (#552)
* Add test case * Fix table shape computation * Changelog * Snapshots
1 parent 934393d commit 78ec64f

File tree

6 files changed

+23
-27
lines changed

6 files changed

+23
-27
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1616
- Take into account the extra line created when hanging at equals token in an assignment. This should prevent unnecessary hanging ([#542](https://github.com/JohnnyMorganz/StyLua/issues/542))
1717
- Fixed comments added to a newly created trailing comment not being formatted ([#547](https://github.com/JohnnyMorganz/StyLua/issues/547))
1818
- Fixed call chain with a small prefix not being kept inlined causing unstable formatting ([#514](https://github.com/JohnnyMorganz/StyLua/issues/514))
19+
- Fix shape computation for table fields causing unnecessary expansion ([#551](https://github.com/JohnnyMorganz/StyLua/issues/551))
1920

2021
## [0.14.2] - 2022-07-27
2122

src/formatters/table.rs

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ use full_moon::{
2020
Expression, Field, TableConstructor, Value,
2121
},
2222
node::Node,
23-
tokenizer::{Token, TokenKind, TokenReference, TokenType},
23+
tokenizer::{Token, TokenReference, TokenType},
2424
};
2525

2626
/// Used to provide information about the table
@@ -477,27 +477,24 @@ pub fn format_table_constructor(
477477
let additional_shape = match (
478478
start_brace
479479
.trailing_trivia()
480-
.any(|x| x.token_kind() == TokenKind::Whitespace),
480+
.any(trivia_util::trivia_is_whitespace),
481481
// A space will be present on the end of the last field, not the start of the end brace
482482
match (last_field.value(), last_field.punctuation()) {
483483
(_, Some(token)) => token
484484
.trailing_trivia()
485-
.any(|x| x.token_kind() == TokenKind::Whitespace),
485+
.any(trivia_util::trivia_is_whitespace),
486486
(field, None) => table_field_trailing_trivia(field)
487487
.iter()
488-
.any(|x| x.token_kind() == TokenKind::Whitespace),
488+
.any(trivia_util::trivia_is_whitespace),
489489
},
490490
) {
491491
(true, true) => 0,
492492
(true, false) | (false, true) => 1,
493493
(false, false) => 2,
494494
};
495495

496-
let singleline_shape = shape
497-
+ (braces_range.1 - braces_range.0)
498-
+ additional_shape
499-
+ BRACE_LEN
500-
+ BRACE_LEN;
496+
let singleline_shape =
497+
shape + (braces_range.1 - braces_range.0) + additional_shape + BRACE_LEN; // End brace is not included in braces range
501498

502499
match singleline_shape.over_budget() {
503500
true => TableType::MultiLine,

src/formatters/trivia_util.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1133,7 +1133,7 @@ pub fn table_field_trailing_trivia(field: &Field) -> Vec<Token> {
11331133
match field {
11341134
Field::ExpressionKey { value, .. } => get_expression_trailing_trivia(value),
11351135
Field::NameKey { value, .. } => get_expression_trailing_trivia(value),
1136-
Field::NoKey(expression) => get_expression_leading_trivia(expression),
1136+
Field::NoKey(expression) => get_expression_trailing_trivia(expression),
11371137
other => panic!("unknown node {:?}", other),
11381138
}
11391139
}

tests/inputs/table-8.lua

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
-- https://github.com/JohnnyMorganz/StyLua/issues/551
2+
local test = {
3+
{ "http://example.com/b//c//d;p?q#blarg", "http://u:p@h.com/p/a/t/h?s#hash2", "http://u:p@h.com/p/a/t/h?s#hash2" },
4+
}
Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,7 @@
11
---
22
source: tests/tests.rs
33
expression: format(&contents)
4-
54
---
6-
local foo = {
7-
"aaaa",
8-
"aaaa",
9-
"aaaa",
10-
"aaaa",
11-
"aaaa",
12-
"aaaa",
13-
"aaaa",
14-
"aaaa",
15-
"aaaa",
16-
"aaaa",
17-
"aaaa",
18-
"aaaa",
19-
"aaaa",
20-
"aaaa",
21-
}
5+
local foo =
6+
{ "aaaa", "aaaa", "aaaa", "aaaa", "aaaa", "aaaa", "aaaa", "aaaa", "aaaa", "aaaa", "aaaa", "aaaa", "aaaa", "aaaa" }
227

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
---
2+
source: tests/tests.rs
3+
expression: format(&contents)
4+
---
5+
-- https://github.com/JohnnyMorganz/StyLua/issues/551
6+
local test = {
7+
{ "http://example.com/b//c//d;p?q#blarg", "http://u:p@h.com/p/a/t/h?s#hash2", "http://u:p@h.com/p/a/t/h?s#hash2" },
8+
}
9+

0 commit comments

Comments
 (0)