Skip to content

Commit da432e1

Browse files
cartermpKevinRansom
authored andcommitted
Better record and value formatting in tools (#7021)
* Remove semicolons from record tooltips * Update to put a space between braces * Update formatting as best I can, plus some tests I guess * More baseline updates * Anonymous records * Update anon records tests * Add vsbsl lol * Update baselines and reduce a simple filter * Update baselines maybe last time * Update fsharpqa test * make tests pass * Add formatting for values * Update tests * Update test * Update fsharpqa tests * tryit * lol * get yote * shlerp * Update tests again I guess * more update * mother of pearl * this is a real turd
1 parent bf1055c commit da432e1

17 files changed

Lines changed: 1003 additions & 1001 deletions

File tree

src/fsharp/NicePrint.fs

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@ module internal PrintUtilities =
3232
let bracketIfL x lyt = if x then bracketL lyt else lyt
3333
let squareAngleL x = LeftL.leftBracketAngle ^^ x ^^ RightL.rightBracketAngle
3434
let angleL x = sepL Literals.leftAngle ^^ x ^^ rightL Literals.rightAngle
35-
let braceL x = leftL Literals.leftBrace ^^ x ^^ rightL Literals.rightBrace
36-
let braceBarL x = leftL Literals.leftBraceBar ^^ x ^^ rightL Literals.rightBraceBar
35+
let braceL x = wordL Literals.leftBrace ^^ x ^^ wordL Literals.rightBrace
36+
let braceBarL x = wordL Literals.leftBraceBar ^^ x ^^ wordL Literals.rightBraceBar
3737

3838
let comment str = wordL (tagText (sprintf "(* %s *)" str))
3939

@@ -942,7 +942,7 @@ module private PrintTypes =
942942

943943
// Layout a tuple type
944944
| TType_anon (anonInfo, tys) ->
945-
let core = sepListL (wordL (tagPunctuation ";")) (List.map2 (fun nm ty -> wordL (tagField nm) ^^ wordL (tagPunctuation ":") ^^ layoutTypeWithInfoAndPrec denv env prec ty) (Array.toList anonInfo.SortedNames) tys)
945+
let core = sepListL (rightL (tagPunctuation ";")) (List.map2 (fun nm ty -> wordL (tagField nm) ^^ rightL (tagPunctuation ":") ^^ layoutTypeWithInfoAndPrec denv env prec ty) (Array.toList anonInfo.SortedNames) tys)
946946
if evalAnonInfoIsStruct anonInfo then
947947
WordL.keywordStruct --- braceBarL core
948948
else
@@ -1457,7 +1457,7 @@ module private TastDefinitionPrinting =
14571457
let lhs =
14581458
tagRecordField fld.Name
14591459
|> mkNav fld.DefinitionRange
1460-
|> wordL
1460+
|> wordL
14611461
let lhs = (if addAccess then layoutAccessibility denv fld.Accessibility lhs else lhs)
14621462
let lhs = if fld.IsMutable then wordL (tagKeyword "mutable") --- lhs else lhs
14631463
(lhs ^^ RightL.colon) --- layoutType denv fld.FormalType
@@ -1738,8 +1738,15 @@ module private TastDefinitionPrinting =
17381738
let denv = denv.AddAccessibility tycon.TypeReprAccessibility
17391739
match repr with
17401740
| TRecdRepr _ ->
1741-
let recdFieldRefL fld = layoutRecdField false denv fld ^^ rightL (tagPunctuation ";")
1742-
let recdL = tycon.TrueFieldsAsList |> List.map recdFieldRefL |> applyMaxMembers denv.maxMembers |> aboveListL |> braceL
1741+
let recdFieldRefL fld = layoutRecdField false denv fld
1742+
1743+
let recdL =
1744+
tycon.TrueFieldsAsList
1745+
|> List.map recdFieldRefL
1746+
|> applyMaxMembers denv.maxMembers
1747+
|> aboveListL
1748+
|> braceL
1749+
17431750
Some (addMembersAsWithEnd (addReprAccessL recdL))
17441751

17451752
| TFSharpObjectRepr r ->
@@ -1771,8 +1778,7 @@ module private TastDefinitionPrinting =
17711778
| _ -> []
17721779
let vsprs =
17731780
tycon.MembersOfFSharpTyconSorted
1774-
|> List.filter (fun v -> isNil (Option.get v.MemberInfo).ImplementedSlotSigs)
1775-
|> List.filter (fun v -> v.IsDispatchSlot)
1781+
|> List.filter (fun v -> isNil (Option.get v.MemberInfo).ImplementedSlotSigs && v.IsDispatchSlot)
17761782
|> List.map (fun vref -> PrintTastMemberOrVals.prettyLayoutOfValOrMemberNoInst denv vref.Deref)
17771783
let staticValsLs =
17781784
tycon.TrueFieldsAsList

src/utils/sformat.fs

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -759,18 +759,14 @@ namespace Microsoft.FSharp.Text.StructuredPrintfImpl
759759
// pprinter: attributes
760760
// --------------------------------------------------------------------
761761

762-
let makeRecordVerticalL nameXs =
763-
let itemL (name,xL) = let labelL = wordL name in ((labelL ^^ wordL Literals.equals)) -- (xL ^^ (rightL Literals.semicolon))
764-
let braceL xs = (leftL Literals.leftBrace) ^^ xs ^^ (rightL Literals.rightBrace)
765-
braceL (aboveListL (List.map itemL nameXs))
766-
767-
// This is a more compact rendering of records - and is more like tuples
768-
let makeRecordHorizontalL nameXs =
769-
let itemL (name,xL) = let labelL = wordL name in ((labelL ^^ wordL Literals.equals)) -- xL
770-
let braceL xs = (leftL Literals.leftBrace) ^^ xs ^^ (rightL Literals.rightBrace)
771-
braceL (sepListL (rightL Literals.semicolon) (List.map itemL nameXs))
772-
773-
let makeRecordL nameXs = makeRecordVerticalL nameXs
762+
let makeRecordL nameXs =
763+
let itemL (name,xL) = wordL name ^^ wordL Literals.equals -- xL
764+
let braceL xs = (wordL Literals.leftBrace) ^^ xs ^^ (wordL Literals.rightBrace)
765+
766+
nameXs
767+
|> List.map itemL
768+
|> aboveListL
769+
|> braceL
774770

775771
let makePropertiesL nameXs =
776772
let itemL (name,v) =

tests/fsharp/Compiler/Language/AnonRecordTests.fs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ let rAnon = RefClass<struct {| R: int |}>()
3434
FSharpErrorSeverity.Error
3535
1
3636
(3, 13, 3, 42)
37-
"A generic construct requires that the type 'struct {|R : int|}' have reference semantics, but it does not, i.e. it is a struct"
37+
"A generic construct requires that the type 'struct {| R: int |}' have reference semantics, but it does not, i.e. it is a struct"
3838

3939
[<Test>]
4040
let StructConstraintFail() =
@@ -46,4 +46,4 @@ let sAnon = StructClass<{| S: int |}>()
4646
FSharpErrorSeverity.Error
4747
1
4848
(3, 13, 3, 38)
49-
"A generic construct requires that the type '{|S : int|}' is a CLI or F# struct type"
49+
"A generic construct requires that the type '{| S: int |}' is a CLI or F# struct type"

tests/fsharp/core/anon/lib.fs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,8 @@ module KindB1 =
5252
check "coijoiwcnkwle1" {| a = 1 |} {| a = 1 |}
5353
check "coijoiwcnkwle2" {| a = 2 |} {| a = 2 |}
5454

55-
check "coijoiwcnkwle3" (sprintf "%A" {| X = 10 |}) "{X = 10;}"
56-
check "coijoiwcnkwle4" (sprintf "%A" {| X = 10; Y = 1 |} |> fun s -> s.Replace("\n","").Replace("\r","")) ("{X = 10; Y = 1;}".Replace("\n","").Replace("\r",""))
55+
check "coijoiwcnkwle3" (sprintf "%A" {| X = 10 |}) "{ X = 10 }"
56+
check "coijoiwcnkwle4" (sprintf "%A" {| X = 10; Y = 1 |}) "{ X = 10\n Y = 1 }"
5757
check "clekoiew09" (f2 {| X = {| X = 10 |} |}) 10
5858
check "cewkew0oijew" (f2 {| X = {| X = 20 |} |}) 20
5959

tests/fsharp/core/anon/test.fsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,13 @@ module Test =
2525

2626
let testAccess = (KindB1.data1.X, KindB1.data3.X)
2727

28-
check "coijoiwcnkwle2" (sprintf "%A" KindB1.data1) "{X = 1;}"
28+
check "coijoiwcnkwle2" (sprintf "%A" KindB1.data1) "{ X = 1 }"
2929

3030
module Tests2 =
3131

3232
let testAccess = (KindB2.data1.X, KindB2.data3.X, KindB2.data3.Y)
3333

34-
check "coijoiwcnkwle3" (sprintf "%A" KindB2.data1) "{X = 1;}"
34+
check "coijoiwcnkwle3" (sprintf "%A" KindB2.data1) "{ X = 1 }"
3535

3636
let _ = (KindB2.data1 = KindB2.data1)
3737

@@ -49,7 +49,7 @@ module CrossAssemblyTest =
4949
check "vrknvio1" (SampleAPI.SampleFunction {| A=1; B = "abc" |}) 4 // note, this is creating an instance of an anonymous record from another assembly.
5050
check "vrknvio2" (SampleAPI.SampleFunctionAcceptingList [ {| A=1; B = "abc" |}; {| A=2; B = "def" |} ]) [4; 5] // note, this is creating an instance of an anonymous record from another assembly.
5151
check "vrknvio3" (let d = SampleAPI.SampleFunctionReturningAnonRecd() in d.A + d.B.Length) 4
52-
check "vrknvio4" (let d = SampleAPIStruct.SampleFunctionReturningAnonRecd() in d.ToString().Replace("\n","").Replace("\r","")) """{A = 1; B = "abc";}"""
52+
check "vrknvio4" (let d = SampleAPIStruct.SampleFunctionReturningAnonRecd() in d.ToString()) ("{ A = 1\n " + """B = "abc" }""")
5353
tests()
5454

5555
module CrossAssemblyTestStruct =

tests/fsharp/core/members/basics/test.fs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1178,8 +1178,8 @@ module ToStringOnRecordTest = begin
11781178

11791179
let a1 = {A = "201"; B = 7}
11801180
let c1 = {C = "20"; D = 17}
1181-
let expected1 = "{A = \"201\";\n B = 7;}"
1182-
let expected2 = "{C = \"20\";\n D = 17;}"
1181+
let expected1 = "{ A = \"201\"\n B = 7 }"
1182+
let expected2 = "{ C = \"20\"\n D = 17 }"
11831183

11841184
do test "record-tostring-def" (a1.ToString() = expected1)
11851185
do test "record-sprintfO-def" ((sprintf "%O" a1) = expected1)

0 commit comments

Comments
 (0)