Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Correctly format type definitions with access modifier #831

Merged
merged 4 commits into from
May 16, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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