diff --git a/src/Fantomas.Tests/PatternMatchingTests.fs b/src/Fantomas.Tests/PatternMatchingTests.fs index 4eab683c87..23869d761a 100644 --- a/src/Fantomas.Tests/PatternMatchingTests.fs +++ b/src/Fantomas.Tests/PatternMatchingTests.fs @@ -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 """ @@ -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 """ @@ -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 = @@ -329,4 +335,53 @@ let ``should support rational powers on units of measures``() = |> should equal """ [] type X = cm^(1/2) / W +""" + +[] +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 +""" + +[] +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 """ \ No newline at end of file diff --git a/src/Fantomas/CodePrinter.fs b/src/Fantomas/CodePrinter.fs index 231a60081e..e72f0d1305 100644 --- a/src/Fantomas/CodePrinter.fs +++ b/src/Fantomas/CodePrinter.fs @@ -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