Skip to content

Commit

Permalink
Each pattern is on a newline.
Browse files Browse the repository at this point in the history
Improves fsprojects#283.
  • Loading branch information
nojaf committed Jan 16, 2019
1 parent c5597b7 commit 7863828
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 5 deletions.
61 changes: 58 additions & 3 deletions src/Fantomas.Tests/PatternMatchingTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ let ``match expressions``() =
|> should equal """
let filter123 x =
match x with
| 1 | 2 | 3 -> printfn "Found 1, 2, or 3!"
| 1
| 2
| 3 -> printfn "Found 1, 2, or 3!"
| a -> printfn "%d" a
"""

Expand All @@ -29,7 +31,9 @@ let ``function keyword``() =
|> should equal """
let filterNumbers =
function
| 1 | 2 | 3 -> printfn "Found 1, 2, or 3!"
| 1
| 2
| 3 -> printfn "Found 1, 2, or 3!"
| a -> printfn "%d" a
"""

Expand Down Expand Up @@ -75,7 +79,9 @@ let detectZeroAND point =
|> should equal """
let detectZeroOR point =
match point with
| (0, 0) | (0, _) | (_, 0) -> printfn "Zero found."
| (0, 0)
| (0, _)
| (_, 0) -> printfn "Zero found."
| _ -> printfn "Both nonzero."
let detectZeroAND point =
Expand Down Expand Up @@ -329,4 +335,53 @@ let ``should support rational powers on units of measures``() =
|> should equal """
[<Measure>]
type X = cm^(1/2) / W
"""

[<Test>]
let ``should add each case on newline`` () =
formatSourceString false """
let (|OneLine|MultiLine|) b =
match b with
| Red
| Green
| Blue ->
OneLinerBinding b
| _ -> MultilineBinding b
""" config
|> prepend newline
|> should equal """
let (|OneLine|MultiLine|) b =
match b with
| Red
| Green
| Blue -> OneLinerBinding b
| _ -> MultilineBinding b
"""

[<Test>]
let ``each pattern should be on newline`` () =
formatSourceString false """
let (|OneLinerBinding|MultilineBinding|) b =
match b with
| LetBinding([], PreXmlDoc [||], _, _, _, _, OneLinerExpr _)
| DoBinding([], PreXmlDoc [||], OneLinerExpr _)
| MemberBinding([], PreXmlDoc [||], _, _, _, _, OneLinerExpr _)
| PropertyBinding([], PreXmlDoc [||], _, _, _, _, OneLinerExpr _)
| ExplicitCtor([], PreXmlDoc [||], _, _, OneLinerExpr _, _) ->
OneLinerBinding b
| _ -> MultilineBinding b
""" config
|> prepend newline
|> should equal """
let (|OneLinerBinding|MultilineBinding|) b =
match b with
| LetBinding([], PreXmlDoc [||], _, _, _, _, OneLinerExpr _)
| DoBinding([], PreXmlDoc [||], OneLinerExpr _)
| MemberBinding([], PreXmlDoc [||], _, _, _, _, OneLinerExpr _)
| PropertyBinding([], PreXmlDoc [||], _, _, _, _, OneLinerExpr _)
| ExplicitCtor([], PreXmlDoc [||], _, _, OneLinerExpr _, _) ->
OneLinerBinding b
| _ -> MultilineBinding b
"""
5 changes: 3 additions & 2 deletions src/Fantomas/CodePrinter.fs
Original file line number Diff line number Diff line change
Expand Up @@ -1219,10 +1219,11 @@ and genPatRecordFieldName astContext (PatRecordFieldName(s1, s2, p)) =
and genPatWithIdent astContext (ido, p) =
opt (sepEq +> sepSpace) ido (!-) +> genPat astContext p

and genPat astContext = function
and genPat astContext pat =
match pat with
| PatOptionalVal(s) -> !- (sprintf "?%s" s)
| PatAttrib(p, ats) -> genOnelinerAttributes astContext ats +> genPat astContext p
| PatOr(p1, p2) -> genPat astContext p1 -- " | " +> genPat astContext p2
| PatOr(p1, p2) -> genPat astContext p1 +> sepNln -- "| " +> genPat astContext p2
| PatAnds(ps) -> col (!- " & ") ps (genPat astContext)
| PatNullary PatNull -> !- "null"
| PatNullary PatWild -> sepWild
Expand Down

0 comments on commit 7863828

Please sign in to comment.