From 9166cedc584af3c1c10d6112be52c68e7a0a746c Mon Sep 17 00:00:00 2001 From: JohnnyMorganz Date: Sat, 30 Nov 2024 13:45:00 +0100 Subject: [PATCH 1/3] Add test case --- tests/inputs-luau/type-hanging-leading-token-1.lua | 9 +++++++++ 1 file changed, 9 insertions(+) create mode 100644 tests/inputs-luau/type-hanging-leading-token-1.lua diff --git a/tests/inputs-luau/type-hanging-leading-token-1.lua b/tests/inputs-luau/type-hanging-leading-token-1.lua new file mode 100644 index 00000000..b9afa5ab --- /dev/null +++ b/tests/inputs-luau/type-hanging-leading-token-1.lua @@ -0,0 +1,9 @@ +export type Foo = + | numberaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa + | numberaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa + | numberaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa + +export type Foo = + & numberaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa + & numberaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa + & numberaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa From 21c4ee0099e933b29e7a7990080bd31181c52f6f Mon Sep 17 00:00:00 2001 From: JohnnyMorganz Date: Sat, 30 Nov 2024 13:46:04 +0100 Subject: [PATCH 2/3] Fix indentation formatting of leading token in hanging type info --- src/formatters/luau.rs | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/src/formatters/luau.rs b/src/formatters/luau.rs index b0189630..1505b36a 100644 --- a/src/formatters/luau.rs +++ b/src/formatters/luau.rs @@ -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) => { @@ -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, )) } From dd149a1aa3552ec45aac024e4f4d293ebd8a08dc Mon Sep 17 00:00:00 2001 From: JohnnyMorganz Date: Sat, 30 Nov 2024 13:46:12 +0100 Subject: [PATCH 3/3] Update changelog and snapshots --- CHANGELOG.md | 1 + ...ts__luau@type-hanging-leading-token-1.lua.snap | 15 +++++++++++++++ 2 files changed, 16 insertions(+) create mode 100644 tests/snapshots/tests__luau@type-hanging-leading-token-1.lua.snap diff --git a/CHANGELOG.md b/CHANGELOG.md index 64e04512..06172ed0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/tests/snapshots/tests__luau@type-hanging-leading-token-1.lua.snap b/tests/snapshots/tests__luau@type-hanging-leading-token-1.lua.snap new file mode 100644 index 00000000..f9f14b81 --- /dev/null +++ b/tests/snapshots/tests__luau@type-hanging-leading-token-1.lua.snap @@ -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 +