Skip to content

Commit

Permalink
Add extra space before pipe if multiline line or pattern in list/array.
Browse files Browse the repository at this point in the history
  • Loading branch information
nojaf committed Mar 17, 2021
1 parent afed9a7 commit a2d31f3
Show file tree
Hide file tree
Showing 3 changed files with 151 additions and 5 deletions.
119 changes: 119 additions & 0 deletions src/Fantomas.Tests/PatternMatchingTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -1410,3 +1410,122 @@ match x with
y
| None -> 42
"""

[<Test>]
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"
"""

[<Test>]
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"
"""

[<Test>]
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"
"""

[<Test>]
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"
"""

[<Test>]
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"
"""
26 changes: 26 additions & 0 deletions src/Fantomas/CodePrinter.fs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
11 changes: 6 additions & 5 deletions src/Fantomas/SourceParser.fs
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -14,10 +13,6 @@ type Composite<'a, 'b> =
| Pair of 'b * 'b
| Single of 'a

#if INTERACTIVE
type Debug = Console
#endif

[<Literal>]
let MaxLength = 512

Expand Down Expand Up @@ -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
Expand Down

0 comments on commit a2d31f3

Please sign in to comment.