Skip to content

Commit

Permalink
Added multiline upcast/downcast formatting. Fixes #1203. (#1210)
Browse files Browse the repository at this point in the history
  • Loading branch information
nojaf committed Oct 30, 2020
1 parent be9212d commit 496c72c
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 5 deletions.
53 changes: 53 additions & 0 deletions src/Fantomas.Tests/CastTests.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
module Fantomas.Tests.CastTests

open NUnit.Framework
open FsUnit
open Fantomas.Tests.TestHelper

[<Test>]
let ``multiline downcast expression, `` () =
formatSourceString false """
longMethodName
longArgument
longArgument2
:?> List<bool>
"""
{ config with
MaxLineLength = 30
SpaceBeforeUppercaseInvocation = true
SpaceBeforeColon = true
SpaceBeforeSemicolon = true
AlignFunctionSignatureToIndentation = true
AlternativeLongMemberDefinitions = true
DisableElmishSyntax = true }
|> prepend newline
|> should equal """
longMethodName
longArgument
longArgument2
:?> List<bool>
"""

[<Test>]
let ``multiline upcast expression, `` () =
formatSourceString false """
longMethodName
longArgument
longArgument2
:> List<bool>
"""
{ config with
MaxLineLength = 30
SpaceBeforeUppercaseInvocation = true
SpaceBeforeColon = true
SpaceBeforeSemicolon = true
AlignFunctionSignatureToIndentation = true
AlternativeLongMemberDefinitions = true
DisableElmishSyntax = true }
|> prepend newline
|> should equal """
longMethodName
longArgument
longArgument2
:> List<bool>
"""
3 changes: 2 additions & 1 deletion src/Fantomas.Tests/ComputationExpressionTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -1519,7 +1519,8 @@ let sendPushNotifications =
subscriptions
s.Origin
s.Endpoint
} :> Task)
}
:> Task)
|> Task.WhenAll)
"""

Expand Down
1 change: 1 addition & 0 deletions src/Fantomas.Tests/Fantomas.Tests.fsproj
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@
<Compile Include="NumberOfItemsMultilineInfixOperatorExpressionTests.fs" />
<Compile Include="SynExprNewTests.fs" />
<Compile Include="DisableElmishTests.fs" />
<Compile Include="CastTests.fs" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Fantomas.Extras\Fantomas.Extras.fsproj" />
Expand Down
22 changes: 18 additions & 4 deletions src/Fantomas/CodePrinter.fs
Original file line number Diff line number Diff line change
Expand Up @@ -1357,11 +1357,25 @@ and genExpr astContext synExpr ctx =
+> ifElse (hasParenthesis e) sepNone sepSpace
+> genExpr astContext e
| TypedExpr (Downcast, e, t) ->
genExpr astContext e -- " :?> "
+> genType astContext false t
let shortExpr =
genExpr astContext e -- " :?> "
+> genType astContext false t

let longExpr =
genExpr astContext e +> sepNln -- ":?> "
+> genType astContext false t

expressionFitsOnRestOfLine shortExpr longExpr
| TypedExpr (Upcast, e, t) ->
genExpr astContext e -- " :> "
+> genType astContext false t
let shortExpr =
genExpr astContext e -- " :> "
+> genType astContext false t

let longExpr =
genExpr astContext e +> sepNln -- ":> "
+> genType astContext false t

expressionFitsOnRestOfLine shortExpr longExpr
| TypedExpr (Typed, e, t) ->
genExpr astContext e
+> sepColon
Expand Down

0 comments on commit 496c72c

Please sign in to comment.