Skip to content

Commit

Permalink
Revisit formatting of SynPat.Record. Fixes #1173. (#1174)
Browse files Browse the repository at this point in the history
  • Loading branch information
nojaf committed Sep 28, 2020
1 parent 746514d commit dd38d6c
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -441,7 +441,9 @@ let ``SynPat.Record in pattern match with bracketOnSeparateLine`` () =
|> prepend newline
|> should equal """
match foo with
| { Bar = bar ; Level = 12 ; Vibes = plenty ;
| { Bar = bar
Level = 12
Vibes = plenty
Lorem = "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. " } ->
"7"
| _ -> "8"
Expand Down
1 change: 0 additions & 1 deletion src/Fantomas.Tests/NumberOfItemsRecordTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -382,7 +382,6 @@ type MyRecord =
interface IMyInterface
"""

[<Ignore("Issue #1173")>]
[<Test>]
let ``SynPat.Record in pattern match, 1173`` () =
formatSourceString false """match foo with
Expand Down
50 changes: 50 additions & 0 deletions src/Fantomas.Tests/RecordTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -1048,3 +1048,53 @@ module Test =
{ long_enough: string
also_long_enough: string } // Hi I am a comment
"""

[<Test>]
let ``multiline SynPat.Record in match clause, 1173`` () =
formatSourceString false """
match foo with
| { Bar = bar
Level = 12
Vibes = plenty
Lorem = "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. " } ->
"7"
| _ -> "8"
""" config
|> prepend newline
|> should equal """
match foo with
| { Bar = bar
Level = 12
Vibes = plenty
Lorem = "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. " } ->
"7"
| _ -> "8"
"""

[<Test>]
let ``multiline SynPat.Record in let binding destructuring`` () =
formatSourceString false """
let internal sepSemi (ctx: Context) =
let { Config = { SpaceBeforeSemicolon = before; SpaceAfterSemicolon = after } } = ctx

match before, after with
| false, false -> str ";"
| true, false -> str " ;"
| false, true -> str "; "
| true, true -> str " ; "
<| ctx
""" config
|> prepend newline
|> should equal """
let internal sepSemi (ctx: Context) =
let { Config = { SpaceBeforeSemicolon = before
SpaceAfterSemicolon = after } } =
ctx

match before, after with
| false, false -> str ";"
| true, false -> str " ;"
| false, true -> str "; "
| true, true -> str " ; "
<| ctx
"""
17 changes: 14 additions & 3 deletions src/Fantomas/CodePrinter.fs
Original file line number Diff line number Diff line change
Expand Up @@ -4298,9 +4298,20 @@ and genPat astContext pat =
+> sepCloseA)

| PatRecord (xs) ->
sepOpenS
+> atCurrentColumn (colAutoNlnSkip0 sepSemi xs (genPatRecordFieldName astContext))
+> sepCloseS
let smallRecordExpr =
sepOpenS
+> col sepSemi xs (genPatRecordFieldName astContext)
+> sepCloseS

// Note that MultilineBlockBracketsOnSameColumn is not taken into account here.
let multilineRecordExpr =
sepOpenS
+> atCurrentColumn (col sepSemiNln xs (genPatRecordFieldName astContext))
+> sepCloseS

fun ctx ->
let size = getRecordSize ctx xs
isSmallExpression size smallRecordExpr multilineRecordExpr ctx
| PatConst (c, r) -> genConst c r
| PatIsInst (TApp (_, [ _ ], _) as t)
| PatIsInst (TArray _ as t) ->
Expand Down

0 comments on commit dd38d6c

Please sign in to comment.