From 768e11e1364a04bf7fef704144755285b5735e5f Mon Sep 17 00:00:00 2001 From: Florian Verdonck Date: Fri, 31 Mar 2023 07:42:30 +0200 Subject: [PATCH] Don't apply stroustrup when WriteBeforeNewline is not empty. (#2817) --- CHANGELOG.md | 5 ++ .../SynBindingValueExpressionTests.fs | 43 +++++++++++++++++ .../SynMatchClauseExpressionTests.fs | 47 +++++++++++++++++++ src/Fantomas.Core/Context.fs | 11 +++-- 4 files changed, 101 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 979f30120f..5ed71e0283 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,10 @@ # Changelog +## [Unreleased] + +### Fixed +* Idempotency problem when comment after pattern match arrow and Stroustrup. [#2806](https://github.com/fsprojects/fantomas/issues/2806) + ## [6.0.0-alpha-008] - 2023-03-27 ### Fixed diff --git a/src/Fantomas.Core.Tests/Stroustrup/SynBindingValueExpressionTests.fs b/src/Fantomas.Core.Tests/Stroustrup/SynBindingValueExpressionTests.fs index 0ca525fdaa..9b44fb9a65 100644 --- a/src/Fantomas.Core.Tests/Stroustrup/SynBindingValueExpressionTests.fs +++ b/src/Fantomas.Core.Tests/Stroustrup/SynBindingValueExpressionTests.fs @@ -719,3 +719,46 @@ let newState = E = e |} """ + +[] +let ``don't apply stroustrup when the token has trivia after it`` () = + formatSourceString + false + """ +let b = // Build an inbound for the specified subnet. + { + Name = subnet + Location = location + DnsResolverId = Managed(dnsResolvers.resourceId this.Name) + SubnetId = + Unmanaged + { vnetId.ResourceId with + Type = Arm.Network.subnets + Segments = [ subnet ] + } + PrivateIpAllocations = [ DynamicPrivateIp ] + Dependencies = Set.empty + Tags = Map.empty + } +""" + config + |> prepend newline + |> should + equal + """ +let b = // Build an inbound for the specified subnet. + { + Name = subnet + Location = location + DnsResolverId = Managed(dnsResolvers.resourceId this.Name) + SubnetId = + Unmanaged { + vnetId.ResourceId with + Type = Arm.Network.subnets + Segments = [ subnet ] + } + PrivateIpAllocations = [ DynamicPrivateIp ] + Dependencies = Set.empty + Tags = Map.empty + } +""" diff --git a/src/Fantomas.Core.Tests/Stroustrup/SynMatchClauseExpressionTests.fs b/src/Fantomas.Core.Tests/Stroustrup/SynMatchClauseExpressionTests.fs index 959701005b..8ea8da3e50 100644 --- a/src/Fantomas.Core.Tests/Stroustrup/SynMatchClauseExpressionTests.fs +++ b/src/Fantomas.Core.Tests/Stroustrup/SynMatchClauseExpressionTests.fs @@ -456,3 +456,50 @@ let y x = | Case2 -> [ "X" ] | Case3 -> [ "Y" ] """ + +[] +let ``don't apply stroustrup when the token has trivia after it, 2806`` () = + formatSourceString + false + """ +match this.InboundSubnetName with +| None -> () +| Some subnet -> // Build an inbound for the specified subnet. + { + Name = subnet + Location = location + DnsResolverId = Managed(dnsResolvers.resourceId this.Name) + SubnetId = + Unmanaged + { vnetId.ResourceId with + Type = Arm.Network.subnets + Segments = [ subnet ] + } + PrivateIpAllocations = [ DynamicPrivateIp ] + Dependencies = Set.empty + Tags = Map.empty + } +""" + config + |> prepend newline + |> should + equal + """ +match this.InboundSubnetName with +| None -> () +| Some subnet -> // Build an inbound for the specified subnet. + { + Name = subnet + Location = location + DnsResolverId = Managed(dnsResolvers.resourceId this.Name) + SubnetId = + Unmanaged { + vnetId.ResourceId with + Type = Arm.Network.subnets + Segments = [ subnet ] + } + PrivateIpAllocations = [ DynamicPrivateIp ] + Dependencies = Set.empty + Tags = Map.empty + } +""" diff --git a/src/Fantomas.Core/Context.fs b/src/Fantomas.Core/Context.fs index c7876b4907..64f6996655 100644 --- a/src/Fantomas.Core/Context.fs +++ b/src/Fantomas.Core/Context.fs @@ -773,10 +773,11 @@ let isStroustrupStyleType (config: FormatConfig) (t: Type) = | Type.AnonRecord _ when isStroustrupEnabled -> true | _ -> false -let canSafelyUseStroustrup (node: Node) = not node.HasContentBefore +let canSafelyUseStroustrup (node: Node) (ctx: Context) = + not node.HasContentBefore && not (hasWriteBeforeNewlineContent ctx) let sepSpaceOrIndentAndNlnIfExceedsPageWidthUnlessStroustrup isStroustrup f (node: Node) (ctx: Context) = - if isStroustrup && canSafelyUseStroustrup node then + if isStroustrup && canSafelyUseStroustrup node ctx then (sepSpace +> f) ctx else sepSpaceOrIndentAndNlnIfExpressionExceedsPageWidth f ctx @@ -934,7 +935,7 @@ let addParenIfAutoNln expr f = let indentSepNlnUnindentUnlessStroustrup f (e: Expr) (ctx: Context) = let shouldUseStroustrup = - isStroustrupStyleExpr ctx.Config e && canSafelyUseStroustrup (Expr.Node e) + isStroustrupStyleExpr ctx.Config e && canSafelyUseStroustrup (Expr.Node e) ctx if shouldUseStroustrup then f e ctx @@ -943,7 +944,7 @@ let indentSepNlnUnindentUnlessStroustrup f (e: Expr) (ctx: Context) = let autoIndentAndNlnTypeUnlessStroustrup f (t: Type) (ctx: Context) = let shouldUseStroustrup = - isStroustrupStyleType ctx.Config t && canSafelyUseStroustrup (Type.Node t) + isStroustrupStyleType ctx.Config t && canSafelyUseStroustrup (Type.Node t) ctx if shouldUseStroustrup then f t ctx @@ -952,7 +953,7 @@ let autoIndentAndNlnTypeUnlessStroustrup f (t: Type) (ctx: Context) = let autoIndentAndNlnIfExpressionExceedsPageWidthUnlessStroustrup f (e: Expr) (ctx: Context) = let isStroustrup = - isStroustrupStyleExpr ctx.Config e && canSafelyUseStroustrup (Expr.Node e) + isStroustrupStyleExpr ctx.Config e && canSafelyUseStroustrup (Expr.Node e) ctx if isStroustrup then f e ctx