Skip to content

Commit

Permalink
Don't apply stroustrup when WriteBeforeNewline is not empty. (#2817)
Browse files Browse the repository at this point in the history
  • Loading branch information
nojaf authored Mar 31, 2023
1 parent b5d29bd commit 768e11e
Show file tree
Hide file tree
Showing 4 changed files with 101 additions and 5 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -719,3 +719,46 @@ let newState =
E = e
|}
"""

[<Test>]
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
}
"""
Original file line number Diff line number Diff line change
Expand Up @@ -456,3 +456,50 @@ let y x =
| Case2 -> [ "X" ]
| Case3 -> [ "Y" ]
"""

[<Test>]
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
}
"""
11 changes: 6 additions & 5 deletions src/Fantomas.Core/Context.fs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down

0 comments on commit 768e11e

Please sign in to comment.