From a2d31f3f8135e61a1849021296cc42200cb2226b Mon Sep 17 00:00:00 2001 From: nojaf Date: Wed, 17 Mar 2021 22:00:02 +0100 Subject: [PATCH] Add extra space before pipe if multiline line or pattern in list/array. Fixes #1522. --- src/Fantomas.Tests/PatternMatchingTests.fs | 119 +++++++++++++++++++++ src/Fantomas/CodePrinter.fs | 26 +++++ src/Fantomas/SourceParser.fs | 11 +- 3 files changed, 151 insertions(+), 5 deletions(-) diff --git a/src/Fantomas.Tests/PatternMatchingTests.fs b/src/Fantomas.Tests/PatternMatchingTests.fs index 7a25e427cb..686536dd3b 100644 --- a/src/Fantomas.Tests/PatternMatchingTests.fs +++ b/src/Fantomas.Tests/PatternMatchingTests.fs @@ -1410,3 +1410,122 @@ match x with y | None -> 42 """ + +[] +let ``or pattern in list with when clause, 1522`` () = + formatSourceString + false + """ +let args = + match args with + | [SynPatErrorSkip(SynPat.Tuple (false, args, _)) | SynPatErrorSkip(SynPat.Paren(SynPatErrorSkip(SynPat.Tuple (false, args, _)), _))] when numArgTys > 1 -> args + | _ -> failwith "meh" +""" + config + |> prepend newline + |> should + equal + """ +let args = + match args with + | [ SynPatErrorSkip (SynPat.Tuple (false, args, _)) + | SynPatErrorSkip (SynPat.Paren (SynPatErrorSkip (SynPat.Tuple (false, args, _)), _)) ] when numArgTys > 1 -> + args + | _ -> failwith "meh" +""" + +[] +let ``triple or in list, short`` () = + formatSourceString + false + """ +let args = + match args with + | [ LongPatIndentifierOne + | LongPatIndentifierTwo + | LongPatIndentifierThree ] -> + args + | _ -> failwith "meh" +""" + config + |> prepend newline + |> should + equal + """ +let args = + match args with + | [ LongPatIndentifierOne | LongPatIndentifierTwo | LongPatIndentifierThree ] -> args + | _ -> failwith "meh" +""" + +[] +let ``triple or in list, long`` () = + formatSourceString + false + """ +let args = + match args with + | [ LongPatIndentifierOne + | LongPatIndentifierTwo + | LongPatIndentifierThree ] -> + args + | _ -> failwith "meh" +""" + { config with MaxLineLength = 60 } + |> prepend newline + |> should + equal + """ +let args = + match args with + | [ LongPatIndentifierOne + | LongPatIndentifierTwo + | LongPatIndentifierThree ] -> args + | _ -> failwith "meh" +""" + +[] +let ``triple or in array, short`` () = + formatSourceString + false + """ +let args = + match args with + | [| LongPatIndentifierOne | LongPatIndentifierTwo | LongPatIndentifierThree |] -> + args + | _ -> failwith "meh" +""" + config + |> prepend newline + |> should + equal + """ +let args = + match args with + | [| LongPatIndentifierOne | LongPatIndentifierTwo | LongPatIndentifierThree |] -> args + | _ -> failwith "meh" +""" + +[] +let ``triple or in array, long`` () = + formatSourceString + false + """ +let args = + match args with + | [| LongPatIndentifierOne | LongPatIndentifierTwo | LongPatIndentifierThree |] -> + args + | _ -> failwith "meh" +""" + { config with MaxLineLength = 60 } + |> prepend newline + |> should + equal + """ +let args = + match args with + | [| LongPatIndentifierOne + | LongPatIndentifierTwo + | LongPatIndentifierThree |] -> args + | _ -> failwith "meh" +""" diff --git a/src/Fantomas/CodePrinter.fs b/src/Fantomas/CodePrinter.fs index 7e0ef01c22..773a7474e8 100644 --- a/src/Fantomas/CodePrinter.fs +++ b/src/Fantomas/CodePrinter.fs @@ -4739,6 +4739,32 @@ and genPat astContext pat = +> sepOpenT +> atCurrentColumn (colAutoNlnSkip0 sepComma ps (genPat astContext)) +> sepCloseT + | PatSeq (patListType, [ PatOrs (patOrs) ]) -> + let sepOpen, sepClose = + match patListType with + | PatArray -> sepOpenA, sepCloseA + | PatList -> sepOpenL, sepCloseL + + let short = + sepOpen + +> col (sepSpace +> sepBar) patOrs (genPat astContext) + +> sepClose + + let long = + sepOpen + +> atCurrentColumnIndent ( + match patOrs with + | [] -> sepNone + | hp :: pats -> + genPat astContext hp +> sepNln -- " " + +> atCurrentColumn ( + sepBar + +> col (sepNln +> sepBar) pats (genPat astContext) + ) + ) + +> sepClose + + expressionFitsOnRestOfLine short long | PatSeq (PatList, ps) -> ifElse ps.IsEmpty diff --git a/src/Fantomas/SourceParser.fs b/src/Fantomas/SourceParser.fs index 9d6decdfc2..374a2fe77a 100644 --- a/src/Fantomas/SourceParser.fs +++ b/src/Fantomas/SourceParser.fs @@ -1,7 +1,6 @@ module internal Fantomas.SourceParser open System -open System.Diagnostics open FSharp.Compiler.SourceCodeServices.PrettyNaming open FSharp.Compiler.SourceCodeServices.FSharpKeywords open FSharp.Compiler.Text @@ -14,10 +13,6 @@ type Composite<'a, 'b> = | Pair of 'b * 'b | Single of 'a -#if INTERACTIVE -type Debug = Console -#endif - [] let MaxLength = 512 @@ -1102,6 +1097,12 @@ let (|PatOr|_|) = | SynPat.Or (p1, p2, _) -> Some(p1, p2) | _ -> None +let rec (|PatOrs|_|) = + function + | PatOr (PatOrs (pats), p2) -> Some [ yield! pats; yield p2 ] + | PatOr (p1, p2) -> Some [ p1; p2 ] + | _ -> None + let (|PatAnds|_|) = function | SynPat.Ands (ps, _) -> Some ps