Skip to content

Commit c73ba5d

Browse files
authored
Merge pull request #3230 from nojaf/fix-3098
Fix empty array/list trivia indentation in Stroustrup style
2 parents c772d23 + 7f86df6 commit c73ba5d

File tree

3 files changed

+51
-1
lines changed

3 files changed

+51
-1
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
- End of line comments after infix operators are preserved correctly. [#2287](https://github.com/fsprojects/fantomas/issues/2287)
88
- Lambda closing parenthesis in chained method calls is no longer placed on its own line when `MultiLineLambdaClosingNewline` is enabled. [#2553](https://github.com/fsprojects/fantomas/issues/2553)
99
- Long `&` (AND) patterns now break across multiple lines to respect max line length. [#1780](https://github.com/fsprojects/fantomas/issues/1780)
10+
- Empty array with trivia inside now has correct indentation in Stroustrup style. [#3098](https://github.com/fsprojects/fantomas/issues/3098)
1011

1112
## [8.0.0-alpha-002] - 2025-12-15
1213

src/Fantomas.Core.Tests/Stroustrup/SynBindingValueExpressionTests.fs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -832,3 +832,39 @@ let list =
832832
// comment
833833
]
834834
"""
835+
836+
[<Test>]
837+
let ``empty array with blank line inside, 3098`` () =
838+
formatSourceString
839+
"""
840+
let myArray = [|
841+
842+
|]
843+
"""
844+
config
845+
|> prepend newline
846+
|> should
847+
equal
848+
"""
849+
let myArray = [|
850+
851+
|]
852+
"""
853+
854+
[<Test>]
855+
let ``empty array with comment inside, 3098`` () =
856+
formatSourceString
857+
"""
858+
let myArray2 = [|
859+
// Some comment
860+
|]
861+
"""
862+
config
863+
|> prepend newline
864+
|> should
865+
equal
866+
"""
867+
let myArray2 = [|
868+
// Some comment
869+
|]
870+
"""

src/Fantomas.Core/CodePrinter.fs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1778,7 +1778,20 @@ let genRecord smallRecordExpr multilineRecordExpr (node: ExprRecordBaseNode) ctx
17781778

17791779
let genArrayOrList (preferMultilineCramped: bool) (node: ExprArrayOrListNode) =
17801780
if node.Elements.IsEmpty then
1781-
genSingleTextNode node.Opening +> genSingleTextNode node.Closing |> genNode node
1781+
fun ctx ->
1782+
(genSingleTextNode node.Opening
1783+
+> (fun afterOpeningCtx ->
1784+
// When the closing bracket has trivia (blank lines, comments) and we are using Stroustrup style,
1785+
// the trivia needs to be indented inside the brackets. See 3098.
1786+
if
1787+
node.Closing.HasContentBefore
1788+
&& afterOpeningCtx.Config.MultilineBracketStyle = Stroustrup
1789+
then
1790+
(indent +> genSingleTextNode node.Closing +> unindent) afterOpeningCtx
1791+
else
1792+
(genSingleTextNode node.Closing) afterOpeningCtx)
1793+
|> genNode node)
1794+
ctx
17821795
else
17831796
let smallExpression =
17841797
genSingleTextNode node.Opening

0 commit comments

Comments
 (0)