Skip to content

Commit

Permalink
Fix Stroustrup record member declaration indent issue (#2788)
Browse files Browse the repository at this point in the history
* Fix issue with Stroustrup member indents

* Move unindent closer to indent
  • Loading branch information
josh-degraw authored and nojaf committed Mar 27, 2023
1 parent c78a48b commit 7d9f782
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -294,3 +294,63 @@ type MangaDexAtHomeResponse = {
|}
}
"""

[<Test>]
let ``record interface declarations can break with Stroustrup enabled, 2787 `` () =
formatSourceString
false
"""
type IEvent = interface end
type SomeEvent =
{ Id: string
Name: string }
interface IEvent
type UpdatedName = { PreviousName: string }
"""
{ config with
NewlineBetweenTypeDefinitionAndMembers = false }
|> prepend newline
|> should
equal
"""
type IEvent =
interface
end
type SomeEvent = {
Id: string
Name: string
} with
interface IEvent
type UpdatedName = { PreviousName: string }
"""

[<Test>]
let ``record member declarations can break with Stroustrup enabled, 2787 `` () =
formatSourceString
false
"""
type SomeEvent =
{ Id: string
Name: string }
member x.BreakWithOtherStuffAs well = ()
type UpdatedName = { PreviousName: string }
"""
{ config with
NewlineBetweenTypeDefinitionAndMembers = false }
|> prepend newline
|> should
equal
"""
type SomeEvent = {
Id: string
Name: string
} with
member x.BreakWithOtherStuffAs well = ()
type UpdatedName = { PreviousName: string }
"""
7 changes: 2 additions & 5 deletions src/Fantomas.Core/CodePrinter.fs
Original file line number Diff line number Diff line change
Expand Up @@ -3246,9 +3246,7 @@ let genTypeDefn (td: TypeDefn) =
+> genSingleTextNode node.ClosingBrace

let genMembers =
onlyIf hasMembers sepNln
+> sepNlnBetweenTypeAndMembers typeDefnNode
+> genMemberDefnList members
onlyIf hasMembers (sepNln +> sepNlnBetweenTypeAndMembers typeDefnNode +> genMemberDefnList members)

let anyFieldHasXmlDoc =
List.exists (fun (fieldNode: FieldNode) -> fieldNode.XmlDoc.IsSome) node.Fields
Expand All @@ -3267,8 +3265,7 @@ let genTypeDefn (td: TypeDefn) =

genAccessOpt node.Accessibility
+> genRecordFields
+> onlyIf hasMembers (sepSpace +> withKw +> indent)
+> genMembers
+> onlyIf hasMembers (sepSpace +> withKw +> indent +> genMembers +> unindent)

let cramped =
sepNlnUnlessLastEventIsNewline
Expand Down

0 comments on commit 7d9f782

Please sign in to comment.