Skip to content

Commit d570166

Browse files
Luau: fix indentation of leading token in union/intersection when hanging (#933)
* Add test case * Fix indentation formatting of leading token in hanging type info * Update changelog and snapshots
1 parent f581279 commit d570166

File tree

4 files changed

+40
-4
lines changed

4 files changed

+40
-4
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1010
### Fixed
1111

1212
- Fixed regression where configuration present in current working directory not used when formatting from stdin and no `--stdin-filepath` is provided ([#928](https://github.com/JohnnyMorganz/StyLua/issues/928))
13+
- Luau: fixed incorrect indentation for leading token in union / intersection when hanging ([#932](https://github.com/JohnnyMorganz/StyLua/issues/932))
1314

1415
## [2.0.1] - 2024-11-18
1516

src/formatters/luau.rs

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -841,8 +841,15 @@ fn hang_type_info(
841841
is_first = false;
842842
}
843843

844-
// TODO: handle leading
845-
TypeInfo::Union(TypeUnion::new(union.leading().cloned(), types))
844+
// TODO: leading should play a role in the shape computation
845+
// TODO(#910): we should decide whether we add or remove a leading token (maybe if hang_level = 0?)
846+
847+
TypeInfo::Union(TypeUnion::new(
848+
union
849+
.leading()
850+
.map(|token| fmt_symbol!(ctx, token, "| ", shape)),
851+
types,
852+
))
846853
}
847854

848855
TypeInfo::Intersection(intersection) => {
@@ -884,9 +891,13 @@ fn hang_type_info(
884891
is_first = false;
885892
}
886893

887-
// TODO: handle leading
894+
// TODO: leading should play a role in the shape computation
895+
// TODO(#910): we should decide whether we add or remove a leading token (maybe if hang_level = 0?)
896+
888897
TypeInfo::Intersection(TypeIntersection::new(
889-
intersection.leading().cloned(),
898+
intersection
899+
.leading()
900+
.map(|token| fmt_symbol!(ctx, token, "& ", shape)),
890901
types,
891902
))
892903
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
export type Foo =
2+
| numberaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
3+
| numberaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
4+
| numberaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
5+
6+
export type Foo =
7+
& numberaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
8+
& numberaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
9+
& numberaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa

tests/snapshots/[email protected]

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
---
2+
source: tests/tests.rs
3+
expression: "format(&contents, LuaVersion::Luau)"
4+
input_file: tests/inputs-luau/type-hanging-leading-token-1.lua
5+
---
6+
export type Foo =
7+
| numberaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
8+
| numberaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
9+
| numberaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
10+
11+
export type Foo =
12+
& numberaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
13+
& numberaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
14+
& numberaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
15+

0 commit comments

Comments
 (0)