From 0756a59d4080d6848dd23786fbe42acef4c5350a Mon Sep 17 00:00:00 2001 From: Josh DeGraw Date: Mon, 6 Mar 2023 14:05:11 -0700 Subject: [PATCH 1/2] Fix issue with Stroustrup member indents --- .../SynTypeDefnSimpleReprRecordTests.fs | 60 +++++++++++++++++++ src/Fantomas.Core/CodePrinter.fs | 9 ++- 2 files changed, 66 insertions(+), 3 deletions(-) diff --git a/src/Fantomas.Core.Tests/Stroustrup/SynTypeDefnSimpleReprRecordTests.fs b/src/Fantomas.Core.Tests/Stroustrup/SynTypeDefnSimpleReprRecordTests.fs index f6fb4713cc..a3b9e8e528 100644 --- a/src/Fantomas.Core.Tests/Stroustrup/SynTypeDefnSimpleReprRecordTests.fs +++ b/src/Fantomas.Core.Tests/Stroustrup/SynTypeDefnSimpleReprRecordTests.fs @@ -294,3 +294,63 @@ type MangaDexAtHomeResponse = { |} } """ + +[] +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 } +""" + +[] +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 } +""" diff --git a/src/Fantomas.Core/CodePrinter.fs b/src/Fantomas.Core/CodePrinter.fs index c30f88d703..64c8c9f3e7 100644 --- a/src/Fantomas.Core/CodePrinter.fs +++ b/src/Fantomas.Core/CodePrinter.fs @@ -3234,9 +3234,12 @@ let genTypeDefn (td: TypeDefn) = +> genSingleTextNode node.ClosingBrace let genMembers = - onlyIf hasMembers sepNln - +> sepNlnBetweenTypeAndMembers typeDefnNode - +> genMemberDefnList members + onlyIf + hasMembers + (sepNln + +> sepNlnBetweenTypeAndMembers typeDefnNode + +> genMemberDefnList members + +> onlyIfCtx (fun ctx -> ctx.Config.IsStroustrupStyle) unindent) let anyFieldHasXmlDoc = List.exists (fun (fieldNode: FieldNode) -> fieldNode.XmlDoc.IsSome) node.Fields From c7377284303c90151cf612fbe82c642746ff79b2 Mon Sep 17 00:00:00 2001 From: Josh DeGraw Date: Tue, 7 Mar 2023 09:22:05 -0700 Subject: [PATCH 2/2] Move unindent closer to indent --- src/Fantomas.Core/CodePrinter.fs | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) diff --git a/src/Fantomas.Core/CodePrinter.fs b/src/Fantomas.Core/CodePrinter.fs index 64c8c9f3e7..f6a230d571 100644 --- a/src/Fantomas.Core/CodePrinter.fs +++ b/src/Fantomas.Core/CodePrinter.fs @@ -3234,12 +3234,7 @@ let genTypeDefn (td: TypeDefn) = +> genSingleTextNode node.ClosingBrace let genMembers = - onlyIf - hasMembers - (sepNln - +> sepNlnBetweenTypeAndMembers typeDefnNode - +> genMemberDefnList members - +> onlyIfCtx (fun ctx -> ctx.Config.IsStroustrupStyle) unindent) + onlyIf hasMembers (sepNln +> sepNlnBetweenTypeAndMembers typeDefnNode +> genMemberDefnList members) let anyFieldHasXmlDoc = List.exists (fun (fieldNode: FieldNode) -> fieldNode.XmlDoc.IsSome) node.Fields @@ -3258,8 +3253,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