Skip to content

Commit dd38d6c

Browse files
authored
Revisit formatting of SynPat.Record. Fixes #1173. (#1174)
1 parent 746514d commit dd38d6c

File tree

4 files changed

+67
-5
lines changed

4 files changed

+67
-5
lines changed

src/Fantomas.Tests/MultilineBlockBracketsOnSameColumnRecordTests.fs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -441,7 +441,9 @@ let ``SynPat.Record in pattern match with bracketOnSeparateLine`` () =
441441
|> prepend newline
442442
|> should equal """
443443
match foo with
444-
| { Bar = bar ; Level = 12 ; Vibes = plenty ;
444+
| { Bar = bar
445+
Level = 12
446+
Vibes = plenty
445447
Lorem = "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. " } ->
446448
"7"
447449
| _ -> "8"

src/Fantomas.Tests/NumberOfItemsRecordTests.fs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -382,7 +382,6 @@ type MyRecord =
382382
interface IMyInterface
383383
"""
384384

385-
[<Ignore("Issue #1173")>]
386385
[<Test>]
387386
let ``SynPat.Record in pattern match, 1173`` () =
388387
formatSourceString false """match foo with

src/Fantomas.Tests/RecordTests.fs

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1048,3 +1048,53 @@ module Test =
10481048
{ long_enough: string
10491049
also_long_enough: string } // Hi I am a comment
10501050
"""
1051+
1052+
[<Test>]
1053+
let ``multiline SynPat.Record in match clause, 1173`` () =
1054+
formatSourceString false """
1055+
match foo with
1056+
| { Bar = bar
1057+
Level = 12
1058+
Vibes = plenty
1059+
Lorem = "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. " } ->
1060+
"7"
1061+
| _ -> "8"
1062+
""" config
1063+
|> prepend newline
1064+
|> should equal """
1065+
match foo with
1066+
| { Bar = bar
1067+
Level = 12
1068+
Vibes = plenty
1069+
Lorem = "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. " } ->
1070+
"7"
1071+
| _ -> "8"
1072+
"""
1073+
1074+
[<Test>]
1075+
let ``multiline SynPat.Record in let binding destructuring`` () =
1076+
formatSourceString false """
1077+
let internal sepSemi (ctx: Context) =
1078+
let { Config = { SpaceBeforeSemicolon = before; SpaceAfterSemicolon = after } } = ctx
1079+
1080+
match before, after with
1081+
| false, false -> str ";"
1082+
| true, false -> str " ;"
1083+
| false, true -> str "; "
1084+
| true, true -> str " ; "
1085+
<| ctx
1086+
""" config
1087+
|> prepend newline
1088+
|> should equal """
1089+
let internal sepSemi (ctx: Context) =
1090+
let { Config = { SpaceBeforeSemicolon = before
1091+
SpaceAfterSemicolon = after } } =
1092+
ctx
1093+
1094+
match before, after with
1095+
| false, false -> str ";"
1096+
| true, false -> str " ;"
1097+
| false, true -> str "; "
1098+
| true, true -> str " ; "
1099+
<| ctx
1100+
"""

src/Fantomas/CodePrinter.fs

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4298,9 +4298,20 @@ and genPat astContext pat =
42984298
+> sepCloseA)
42994299

43004300
| PatRecord (xs) ->
4301-
sepOpenS
4302-
+> atCurrentColumn (colAutoNlnSkip0 sepSemi xs (genPatRecordFieldName astContext))
4303-
+> sepCloseS
4301+
let smallRecordExpr =
4302+
sepOpenS
4303+
+> col sepSemi xs (genPatRecordFieldName astContext)
4304+
+> sepCloseS
4305+
4306+
// Note that MultilineBlockBracketsOnSameColumn is not taken into account here.
4307+
let multilineRecordExpr =
4308+
sepOpenS
4309+
+> atCurrentColumn (col sepSemiNln xs (genPatRecordFieldName astContext))
4310+
+> sepCloseS
4311+
4312+
fun ctx ->
4313+
let size = getRecordSize ctx xs
4314+
isSmallExpression size smallRecordExpr multilineRecordExpr ctx
43044315
| PatConst (c, r) -> genConst c r
43054316
| PatIsInst (TApp (_, [ _ ], _) as t)
43064317
| PatIsInst (TArray _ as t) ->

0 commit comments

Comments
 (0)