Skip to content

Commit

Permalink
Merge branch 'master' of https://github.com/fsprojects/fantomas
Browse files Browse the repository at this point in the history
  • Loading branch information
jindraivanek committed Mar 25, 2019
2 parents 4ae9935 + e8fe9d7 commit 281e584
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 3 deletions.
18 changes: 17 additions & 1 deletion src/Fantomas.Tests/RecordTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -259,4 +259,20 @@ type MyExc =
{ inherit Exception(msg)
X = 1
Y = 2 }
"""
"""

[<Test>]
let ``anon record``() =
shouldNotChangeAfterFormat """
let r : {| Foo : int; Bar : string |} =
{| Foo = 123
Bar = "" |}
"""

[<Test>]
let `` anon record - struct``() =
shouldNotChangeAfterFormat """
let r : struct {| Foo : int; Bar : string |} =
struct {| Foo = 123
Bar = "" |}
"""
5 changes: 5 additions & 0 deletions src/Fantomas.Tests/TestHelpers.fs
Original file line number Diff line number Diff line change
Expand Up @@ -130,3 +130,8 @@ let fromSynExpr expr =
[SynModuleDecl.DoExpr(NoSequencePointAtDoBinding, expr, zero)], PreXmlDocEmpty, [], None,
zero)], (true, true)))
Input (tryFormatAST ast None formatConfig)

let shouldNotChangeAfterFormat source =
formatSourceString false source config
|> prepend newline
|> should equal source
22 changes: 22 additions & 0 deletions src/Fantomas/CodePrinter.fs
Original file line number Diff line number Diff line change
Expand Up @@ -473,6 +473,9 @@ and genVal astContext (Val(ats, px, ao, s, t, vi, _)) =
and genRecordFieldName astContext (RecordFieldName(s, eo)) =
opt sepNone eo (fun e -> !- s +> sepEq +> preserveBreakNlnOrAddSpace astContext e)

and genAnonRecordFieldName astContext (AnonRecordFieldName(s, e)) =
!- s +> sepEq +> preserveBreakNlnOrAddSpace astContext e

and genTuple astContext es =
atCurrentColumn (coli sepComma es (fun i ->
if i = 0 then genExpr astContext else noIndentBreakNln astContext
Expand Down Expand Up @@ -539,6 +542,17 @@ and genExpr astContext synExpr =
(fun (typ, expr) -> !- "inherit " +> genType astContext false typ +> genExpr astContext expr) +> recordExpr)
+> sepCloseS

| AnonRecord(isStruct, fields, copyInfo) ->
let recordExpr =
let fieldsExpr = col sepSemiNln fields (genAnonRecordFieldName astContext)
copyInfo |> Option.map (fun e ->
genExpr astContext e +> ifElseCtx (futureNlnCheck fieldsExpr) (!- " with" +> indent +> sepNln +> fieldsExpr +> unindent) (!- " with " +> fieldsExpr))
|> Option.defaultValue fieldsExpr
ifElse isStruct !- "struct " sepNone
+> sepOpenAnonRecd
+> atCurrentColumnIndent recordExpr
+> sepCloseAnonRecd

| ObjExpr(t, eio, bd, ims, range) ->
// Check the role of the second part of eio
let param = opt sepNone (Option.map fst eio) (genExpr astContext)
Expand Down Expand Up @@ -1044,6 +1058,11 @@ and genType astContext outerBracket t =
| TWithGlobalConstraints(TFuns ts, tcs) -> col sepArrow ts loop +> colPre (!- " when ") wordAnd tcs (genTypeConstraint astContext)
| TWithGlobalConstraints(t, tcs) -> loop t +> colPre (!- " when ") wordAnd tcs (genTypeConstraint astContext)
| TLongIdent s -> ifElse astContext.IsCStylePattern (genTypeByLookup astContext t) (!- s)
| TAnonRecord(isStruct, fields) ->
ifElse isStruct !- "struct " sepNone
+> sepOpenAnonRecd
+> col sepSemi fields (genAnonRecordFieldType astContext)
+> sepCloseAnonRecd
| t -> failwithf "Unexpected type: %O" t

and loopTTupleList = function
Expand All @@ -1060,6 +1079,9 @@ and genType astContext outerBracket t =
| TTuple ts -> ifElse outerBracket (sepOpenT +> loopTTupleList ts +> sepCloseT) (loopTTupleList ts)
| _ -> loop t

and genAnonRecordFieldType astContext (AnonRecordFieldType(s, t)) =
!- s +> sepColon +> (genType astContext false t)

and genPrefixTypes astContext = function
| [] -> sepNone
// Where < and ^ meet, we need an extra space. For example: seq< ^a >
Expand Down
12 changes: 10 additions & 2 deletions src/Fantomas/Context.fs
Original file line number Diff line number Diff line change
Expand Up @@ -295,14 +295,22 @@ let internal sepOpenAFixed = !- "[|"
/// closing token of list
let internal sepCloseAFixed = !- "|]"

/// opening token of sequence
/// opening token of sequence or record
let internal sepOpenS (ctx : Context) =
if ctx.Config.SpaceAroundDelimiter then str "{ " ctx else str "{" ctx

/// closing token of sequence
/// closing token of sequence or record
let internal sepCloseS (ctx : Context) =
if ctx.Config.SpaceAroundDelimiter then str " }" ctx else str "}" ctx

/// opening token of anon record
let internal sepOpenAnonRecd (ctx : Context) =
if ctx.Config.SpaceAroundDelimiter then str "{| " ctx else str "{|" ctx

/// closing token of anon record
let internal sepCloseAnonRecd (ctx : Context) =
if ctx.Config.SpaceAroundDelimiter then str " |}" ctx else str "|}" ctx

/// opening token of sequence
let internal sepOpenSFixed = !- "{"

Expand Down
13 changes: 13 additions & 0 deletions src/Fantomas/SourceParser.fs
Original file line number Diff line number Diff line change
Expand Up @@ -808,6 +808,11 @@ let (|Record|_|) = function
Some(inheritOpt, xs, Option.map fst eo)
| _ -> None

let (|AnonRecord|_|) = function
| SynExpr.AnonRecd(isStruct, copyInfo, fields, _) ->
Some(isStruct, fields, Option.map fst copyInfo)
| _ -> None

let (|ObjExpr|_|) = function
| SynExpr.ObjExpr(t, eio, bd, ims, _, range) ->
Some (t, eio, bd, ims, range)
Expand Down Expand Up @@ -1165,6 +1170,11 @@ let (|TLongIdent|_|) = function
Some s
| _ -> None

let (|TAnonRecord|_|) = function
| SynType.AnonRecd(isStruct, fields, _) ->
Some(isStruct, fields)
| _ -> None

// Type parameter

type SingleTyparConstraintKind =
Expand Down Expand Up @@ -1207,6 +1217,9 @@ let (|Val|) (ValSpfn(ats, IdentOrKeyword(OpNameFull s), tds, t, vi, _, _, px, ao

let (|RecordFieldName|) ((LongIdentWithDots s, _) : RecordFieldName, eo : SynExpr option, _) = (s, eo)

let (|AnonRecordFieldName|) ((Ident s): Ident, e: SynExpr) = (s, e)
let (|AnonRecordFieldType|) ((Ident s): Ident, t: SynType) = (s, t)

let (|PatRecordFieldName|) ((LongIdent s1, Ident s2), p) = (s1, s2, p)

let (|ValInfo|) (SynValInfo(aiss, ai)) = (aiss, ai)
Expand Down

0 comments on commit 281e584

Please sign in to comment.