diff --git a/src/Fantomas.Tests/ListTests.fs b/src/Fantomas.Tests/ListTests.fs index 5999e678d3..d2c0216a81 100644 --- a/src/Fantomas.Tests/ListTests.fs +++ b/src/Fantomas.Tests/ListTests.fs @@ -380,10 +380,12 @@ let ``line comment inside list`` () = """ ({ config with SpaceAroundDelimiter = false }) + |> prepend newline |> should equal - """[7 - // foo + """ +[7 +// foo ] """ @@ -396,10 +398,12 @@ let ``line comment inside array`` () = |] """ config + |> prepend newline |> should equal - """[| 7 - // foo + """ +[| 7 +// foo |] """ @@ -2222,3 +2226,30 @@ let foo = [| fun () -> 1 fun () -> 2 |] """ + +[] +let ``comment before closing bracket`` () = + formatSourceString + false + """ +let fns = + [ { x = "long enough to not go to one line" + y = 5 } + // { name = fn "String" "endsWith" 0 + // deprecated = NotDeprecated } + // I think the space at the start of the lines above matter + ] +""" + config + |> prepend newline + |> should + equal + """ +let fns = + [ { x = "long enough to not go to one line" + y = 5 } + // { name = fn "String" "endsWith" 0 + // deprecated = NotDeprecated } + // I think the space at the start of the lines above matter + ] +""" diff --git a/src/Fantomas.Tests/MultilineBlockBracketsOnSameColumnArrayOrListTests.fs b/src/Fantomas.Tests/MultilineBlockBracketsOnSameColumnArrayOrListTests.fs index 5da94d0c68..2be2403037 100644 --- a/src/Fantomas.Tests/MultilineBlockBracketsOnSameColumnArrayOrListTests.fs +++ b/src/Fantomas.Tests/MultilineBlockBracketsOnSameColumnArrayOrListTests.fs @@ -330,3 +330,63 @@ let foo = fun () -> 2 |] """ + +[] +let ``comments before closing bracket`` () = + formatSourceString + false + """ +let fns = + [ { x = "long enough to not go to one line" + y = 5 } + // { name = fn "String" "endsWith" 0 + // deprecated = NotDeprecated } + // I think the space at the start of the lines above matter + ] +""" + config + |> prepend newline + |> should + equal + """ +let fns = + [ + { + x = "long enough to not go to one line" + y = 5 + } + // { name = fn "String" "endsWith" 0 + // deprecated = NotDeprecated } + // I think the space at the start of the lines above matter + ] +""" + +[] +let ``comments before closing bracket, array`` () = + formatSourceString + false + """ +let fns = + [| { x = "long enough to not go to one line" + y = 5 } + // { name = fn "String" "endsWith" 0 + // deprecated = NotDeprecated } + // I think the space at the start of the lines above matter + |] +""" + config + |> prepend newline + |> should + equal + """ +let fns = + [| + { + x = "long enough to not go to one line" + y = 5 + } + // { name = fn "String" "endsWith" 0 + // deprecated = NotDeprecated } + // I think the space at the start of the lines above matter + |] +""" diff --git a/src/Fantomas/CodePrinter.fs b/src/Fantomas/CodePrinter.fs index 35fdec4e9f..79f78cf25a 100644 --- a/src/Fantomas/CodePrinter.fs +++ b/src/Fantomas/CodePrinter.fs @@ -1454,30 +1454,45 @@ and genExpr astContext synExpr ctx = ColMultilineItem(expr, sepNln, r)) |> colWithNlnWhenItemIsMultiline - | ArrayOrListOfSeqExpr (isArray, e) as aNode -> + | ArrayOrListOfSeqExpr (isArray, e) as alNode -> let astContext = { astContext with IsNakedRange = true } + let tokenSize = if isArray then 2 else 1 + + let openingTokenRange = + ctx.MkRangeWith + (alNode.Range.Start.Line, alNode.Range.Start.Column) + (alNode.Range.Start.Line, (alNode.Range.Start.Column + tokenSize)) + + let closingTokenRange = + ctx.MkRangeWith + (alNode.Range.End.Line, (alNode.Range.End.Column - tokenSize)) + (alNode.Range.End.Line, alNode.Range.End.Column) + let shortExpression = ifElse isArray - (sepOpenA + ((tokN openingTokenRange LBRACK_BAR sepOpenA) +> atCurrentColumnIndent (genExpr astContext e) - +> enterRightBracketBar aNode.Range - +> sepCloseA) - (sepOpenL + +> (tokN closingTokenRange BAR_RBRACK sepCloseA)) + ((tokN openingTokenRange LBRACK sepOpenL) +> atCurrentColumnIndent (genExpr astContext e) - +> enterRightBracket aNode.Range - +> sepCloseL - +> leaveNodeTokenByName aNode.Range RBRACK) + +> (tokN closingTokenRange RBRACK sepCloseL)) let bracketsOnSameColumn = - ifElse isArray sepOpenAFixed sepOpenLFixed + ifElse + isArray + (tokN openingTokenRange LBRACK_BAR sepOpenAFixed) + (tokN openingTokenRange LBRACK sepOpenLFixed) +> indent +> sepNln +> genExpr astContext e +> unindent +> sepNln - +> ifElse isArray sepCloseAFixed sepCloseLFixed + +> ifElse + isArray + (tokN closingTokenRange BAR_RBRACK sepCloseAFixed) + (tokN closingTokenRange RBRACK sepCloseLFixed) let multilineExpression = ifAlignBrackets bracketsOnSameColumn shortExpression