Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Print trivia assigned to brackets of ArrayOrListOfSeqExpr. #1542

Merged
merged 1 commit into from
Mar 26, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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