Skip to content

Commit

Permalink
Keep newline if all items in list or array already had a newline, fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
nojaf committed Nov 29, 2018
1 parent 0e9ed41 commit 5d24b25
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 5 deletions.
21 changes: 19 additions & 2 deletions src/Fantomas.Tests/PreserveEOLTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ let y = [| 3;4
let z = [7; 8]
""" config
|> should equal """
let x = [ 1;
let x = [ 1
2 ]
let y = [| 3; 4;
5; 6 |]
Expand Down Expand Up @@ -551,4 +551,21 @@ sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
Ut enim ad minim veniam
*)
"""
"""

[<Test>]
let ``preserve end of line should respect semicolon`` () =
formatSourceString false """
let lst =
[ 5
6
7
]
""" config
|> should equal """
let lst =
[ 5
6
7
]
"""
14 changes: 11 additions & 3 deletions src/Fantomas/CodePrinter.fs
Original file line number Diff line number Diff line change
Expand Up @@ -502,10 +502,18 @@ and genExpr astContext synExpr =
| StructTuple es -> !- "struct " +> sepOpenT +> genTuple astContext es +> sepCloseT
| ArrayOrList(isArray, [], _) ->
ifElse isArray (sepOpenAFixed +> sepCloseAFixed) (sepOpenLFixed +> sepCloseLFixed)
| ArrayOrList(isArray, xs, isSimple) ->
| ArrayOrList(isArray, xs, isSimple) ->
let sep = ifElse isSimple sepSemi sepSemiNln
ifElse isArray (sepOpenA +> atCurrentColumn (colAutoNlnSkip0 sep xs (genExpr astContext)) +> sepCloseA)
(sepOpenL +> atCurrentColumn (colAutoNlnSkip0 sep xs (genExpr astContext)) +> sepCloseL)
let sepWithPreserveEndOfLine ctx =
let length = List.length xs
let distinctLength = xs |> List.distinctBy (fun x -> x.Range.StartLine) |> List.length
let useNewline = ctx.Config.PreserveEndOfLine && (length = distinctLength)

ctx
|> ifElse useNewline sepNln sep

ifElse isArray (sepOpenA +> atCurrentColumn (colAutoNlnSkip0 sepWithPreserveEndOfLine xs (genExpr astContext)) +> sepCloseA)
(sepOpenL +> atCurrentColumn (colAutoNlnSkip0 sepWithPreserveEndOfLine xs (genExpr astContext)) +> sepCloseL)

| Record(inheritOpt, xs, eo) ->
sepOpenS
Expand Down

0 comments on commit 5d24b25

Please sign in to comment.