Skip to content

Commit

Permalink
Luau: fix indentation of leading token in union/intersection when han…
Browse files Browse the repository at this point in the history
…ging (#933)

* Add test case

* Fix indentation formatting of leading token in hanging type info

* Update changelog and snapshots
  • Loading branch information
JohnnyMorganz authored Nov 30, 2024
1 parent f581279 commit d570166
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 4 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Fixed

- 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))
- Luau: fixed incorrect indentation for leading token in union / intersection when hanging ([#932](https://github.com/JohnnyMorganz/StyLua/issues/932))

## [2.0.1] - 2024-11-18

Expand Down
19 changes: 15 additions & 4 deletions src/formatters/luau.rs
Original file line number Diff line number Diff line change
Expand Up @@ -841,8 +841,15 @@ fn hang_type_info(
is_first = false;
}

// TODO: handle leading
TypeInfo::Union(TypeUnion::new(union.leading().cloned(), types))
// TODO: leading should play a role in the shape computation
// TODO(#910): we should decide whether we add or remove a leading token (maybe if hang_level = 0?)

TypeInfo::Union(TypeUnion::new(
union
.leading()
.map(|token| fmt_symbol!(ctx, token, "| ", shape)),
types,
))
}

TypeInfo::Intersection(intersection) => {
Expand Down Expand Up @@ -884,9 +891,13 @@ fn hang_type_info(
is_first = false;
}

// TODO: handle leading
// TODO: leading should play a role in the shape computation
// TODO(#910): we should decide whether we add or remove a leading token (maybe if hang_level = 0?)

TypeInfo::Intersection(TypeIntersection::new(
intersection.leading().cloned(),
intersection
.leading()
.map(|token| fmt_symbol!(ctx, token, "& ", shape)),
types,
))
}
Expand Down
9 changes: 9 additions & 0 deletions tests/inputs-luau/type-hanging-leading-token-1.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
export type Foo =
| numberaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
| numberaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
| numberaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa

export type Foo =
& numberaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
& numberaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
& numberaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
15 changes: 15 additions & 0 deletions tests/snapshots/tests__luau@type-hanging-leading-token-1.lua.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
---
source: tests/tests.rs
expression: "format(&contents, LuaVersion::Luau)"
input_file: tests/inputs-luau/type-hanging-leading-token-1.lua
---
export type Foo =
| numberaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
| numberaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
| numberaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa

export type Foo =
& numberaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
& numberaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
& numberaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa

0 comments on commit d570166

Please sign in to comment.