Skip to content

Commit

Permalink
Print trivia assigned to brackets of ArrayOrListOfSeqExpr. Fixes fspr…
Browse files Browse the repository at this point in the history
  • Loading branch information
nojaf committed Mar 26, 2021
1 parent 2cfd9ae commit a1ab76c
Show file tree
Hide file tree
Showing 3 changed files with 120 additions and 14 deletions.
39 changes: 35 additions & 4 deletions src/Fantomas.Tests/ListTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -380,10 +380,12 @@ let ``line comment inside list`` () =
"""
({ config with
SpaceAroundDelimiter = false })
|> prepend newline
|> should
equal
"""[7
// foo
"""
[7
// foo
]
"""

Expand All @@ -396,10 +398,12 @@ let ``line comment inside array`` () =
|]
"""
config
|> prepend newline
|> should
equal
"""[| 7
// foo
"""
[| 7
// foo
|]
"""

Expand Down Expand Up @@ -2222,3 +2226,30 @@ let foo =
[| fun () -> 1
fun () -> 2 |]
"""

[<Test>]
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
]
"""
Original file line number Diff line number Diff line change
Expand Up @@ -330,3 +330,63 @@ let foo =
fun () -> 2
|]
"""

[<Test>]
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
]
"""

[<Test>]
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
|]
"""
35 changes: 25 additions & 10 deletions src/Fantomas/CodePrinter.fs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit a1ab76c

Please sign in to comment.