Skip to content

Commit

Permalink
Correctly format type definitions with access modifier. Fixes #830 (#831
Browse files Browse the repository at this point in the history
)
  • Loading branch information
nojaf authored May 16, 2020
1 parent 5155008 commit 030ec66
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -516,4 +516,35 @@ type ShortExpressionInfo =
|| (currentColumn > maxPageWidth) // expression at current position is not going over the page width
member x.Foo() = ()
"""

[<Test>]
let ``internal keyword before multiline record type`` () =
formatSourceString false """
type A = internal { ALongIdentifier: string; YetAnotherLongIdentifier: bool }""" config
|> prepend newline
|> should equal """
type A =
internal
{
ALongIdentifier : string
YetAnotherLongIdentifier : bool
}
"""

[<Test>]
let ``internal keyword before multiline record type in signature file`` () =
formatSourceString true """namespace Bar
type A = internal { ALongIdentifier: string; YetAnotherLongIdentifier: bool }""" config
|> prepend newline
|> should equal """
namespace Bar
type A =
internal
{
ALongIdentifier : string
YetAnotherLongIdentifier : bool
}
"""
29 changes: 29 additions & 0 deletions src/Fantomas.Tests/SignatureTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -552,4 +552,33 @@ namespace Foo
type internal Blah =
abstract Baz: unit
"""

[<Test>]
let ``internal keyword before short record type, 830`` () =
formatSourceString true """namespace Bar
type 'a Baz =
internal {
Value : 'a
}
""" config
|> prepend newline
|> should equal """
namespace Bar
type 'a Baz = internal { Value: 'a }
"""

[<Test>]
let ``internal keyword before long record type`` () =
formatSourceString true """namespace Bar
type A = internal { ALongIdentifier: string; YetAnotherLongIdentifier: bool }""" config
|> prepend newline
|> should equal """
namespace Bar
type A =
internal { ALongIdentifier: string
YetAnotherLongIdentifier: bool }
"""
23 changes: 17 additions & 6 deletions src/Fantomas/CodePrinter.fs
Original file line number Diff line number Diff line change
Expand Up @@ -2046,13 +2046,14 @@ and genMultilineSimpleRecordTypeDefn tdr ms ao' fs astContext =

and genMultilineSimpleRecordTypeDefnAlignBrackets tdr ms ao' fs astContext =
// the typeName is already printed
indent +> sepNln +> opt sepSpace ao' genAccess
indent +> sepNln +> opt (indent +> sepNln) ao' genAccess
+> genTrivia tdr.Range
(sepOpenSFixed +> indent +> sepNln
+> atCurrentColumn (leaveLeftBrace tdr.Range +> col sepSemiNln fs (genField astContext ""))
+> unindent +> sepNln +> sepCloseSFixed
+> sepNlnBetweenTypeAndMembers ms
+> genMemberDefnList { astContext with InterfaceRange = None } ms
+> onlyIf (Option.isSome ao') unindent
+> unindent)

and sepNlnBetweenSigTypeAndMembers (ms: SynMemberSig list) =
Expand Down Expand Up @@ -2120,9 +2121,18 @@ and genSigTypeDefn astContext (SigTypeDef(ats, px, ao, tds, tcs, tdr, ms, s, pre
+> unindent

| SigSimple(TDSRRecord(ao', fs)) ->
ifAlignBrackets
(genSigSimpleRecordAlignBrackets typeName tdr ms ao' fs astContext)
(genSigSimpleRecord typeName tdr ms ao' fs astContext)
let shortExpr =
typeName +> sepEq +> sepSpace +>
optSingle (fun ao -> genAccess ao +> sepSpace) ao' +>
sepOpenS +> leaveLeftBrace tdr.Range
+> col sepSemi fs (genField astContext "") +> sepCloseS

let longExpr =
ifAlignBrackets
(genSigSimpleRecordAlignBrackets typeName tdr ms ao' fs astContext)
(genSigSimpleRecord typeName tdr ms ao' fs astContext)
fun ctx ->
isShortExpression ctx.Config.MaxRecordWidth shortExpr longExpr ctx

| SigSimple TDSRNone ->
let genMembers =
Expand Down Expand Up @@ -2172,20 +2182,21 @@ and genSigTypeDefn astContext (SigTypeDef(ats, px, ao, tds, tcs, tdr, ms, s, pre

and genSigSimpleRecord typeName tdr ms ao' fs astContext =
typeName +> sepEq
+> indent +> sepNln +> opt sepNln ao' genAccess +> sepOpenS
+> indent +> sepNln +> opt sepSpace ao' genAccess +> sepOpenS
+> atCurrentColumn (leaveLeftBrace tdr.Range +> col sepSemiNln fs (genField astContext "")) +> sepCloseS
+> sepNlnBetweenSigTypeAndMembers ms
+> colPre sepNln sepNln ms (genMemberSig astContext)
+> unindent

and genSigSimpleRecordAlignBrackets typeName tdr ms ao' fs astContext =
typeName +> sepEq
+> indent +> sepNln +> opt sepNln ao' genAccess
+> indent +> sepNln +> opt (indent +> sepNln) ao' genAccess
+> sepOpenSFixed +> indent +> sepNln
+> atCurrentColumn (leaveLeftBrace tdr.Range +> col sepSemiNln fs (genField astContext ""))
+> unindent +> sepNln +> sepCloseSFixed
+> sepNlnBetweenSigTypeAndMembers ms
+> colPre sepNln sepNln ms (genMemberSig astContext)
+> onlyIf (Option.isSome ao') unindent
+> unindent

and genMemberSig astContext node =
Expand Down

0 comments on commit 030ec66

Please sign in to comment.