Skip to content

Commit

Permalink
Fix shape calculation for table at column boundary (#300)
Browse files Browse the repository at this point in the history
* Fix shape calculation for table at column boundary

* Update tests

* Update changelog
  • Loading branch information
JohnnyMorganz authored Nov 13, 2021
1 parent 61f3377 commit 1ca00a3
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
## [Unreleased]
- Fixed spaces around brackets string (`[[string]]`) used as an index or table key (i.e. `[ [[string]] ]`) being removed, leading to a syntax error. ([#293](https://github.com/JohnnyMorganz/StyLua/issues/293))
- Fixed incorrect shape calculation leading to arguments incorrectly expanding when under column width. ([#298](https://github.com/JohnnyMorganz/StyLua/issues/298))
- Fixed incorrect shape calculation for singleline table at the column width boundary. ([#296](https://github.com/JohnnyMorganz/StyLua/issues/296))

## [0.11.1] - 2021-11-08
### Changed
Expand Down
12 changes: 10 additions & 2 deletions src/formatters/table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -373,10 +373,18 @@ pub fn format_table_constructor(
// exponential time complexity with respect to how deep the table is.
// TODO: find an improved heuristic whilst comparing against benchmarks
let braces_range = (
start_brace.token().end_position().bytes(),
// Use the position of the last trivia in case there is some present (e.g. whitespace)
// So that we don't include an extra space
if let Some(token) = start_brace.leading_trivia().last() {
token.end_position().bytes()
} else {
start_brace.token().end_position().bytes()
},
end_brace.token().start_position().bytes(),
);
let singleline_shape = shape + (braces_range.1 - braces_range.0) + 4; // 4 = two braces + single space before/after them
let singleline_shape = shape + (braces_range.1 - braces_range.0) + 3; // 4 = two braces + single space before last brace

println!("{:?} {}", singleline_shape, singleline_shape.over_budget());

match singleline_shape.over_budget() {
true => TableType::MultiLine,
Expand Down
3 changes: 3 additions & 0 deletions tests/inputs/table-6.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
-- https://github.com/JohnnyMorganz/StyLua/issues/296
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa({ aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa = false})
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa({ aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa = false })
9 changes: 9 additions & 0 deletions tests/snapshots/[email protected]
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
---
source: tests/tests.rs
expression: format(&contents)

---
-- https://github.com/JohnnyMorganz/StyLua/issues/296
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa({ aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa = false })
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa({ aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa = false })

0 comments on commit 1ca00a3

Please sign in to comment.