Skip to content

Commit

Permalink
Put return type on newline if expression does not fit on rest of line (
Browse files Browse the repository at this point in the history
…#935)

* Put return type on newline if expression exceeds PageWidth

* Put return type of member on the next line.

Co-authored-by: nojaf <florian.verdonck@outlook.com>
  • Loading branch information
Bobface and nojaf committed Jul 6, 2020
1 parent 095c74b commit 344d442
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 17 deletions.
7 changes: 4 additions & 3 deletions src/Fantomas.Tests/AppTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,6 @@ let a s =
(llloooooooooooooooooooooooooo s)
"


[<Test>]
let ``should split parameters over multiple lines when they exceed page width``() =
formatSourceString false """module Caching =
Expand All @@ -98,7 +97,8 @@ let ``should split parameters over multiple lines when they exceed page width``(
module Caching =
type MainCache() =
member __.RetrieveLastCompoundBalance (address: PublicAddress)
(currency: Currency): NotFresh<decimal> =
(currency: Currency)
: NotFresh<decimal> =
lock cacheFiles.CachedNetworkData (fun _ ->
match balance with
| NotAvailable -> NotAvailable
Expand Down Expand Up @@ -134,7 +134,8 @@ let ``should split single parameter over multiple lines when it exceeds page wid
module Caching =
type MainCache() =
member __.RetrieveLastCompoundBalance (address: PublicAddress)
(currency: Currency): NotFresh<decimal> =
(currency: Currency)
: NotFresh<decimal> =
lock cacheFiles.CachedNetworkData (fun _ ->
match balance with
| NotAvailable -> NotAvailable
Expand Down
21 changes: 14 additions & 7 deletions src/Fantomas.Tests/TypeDeclarationTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -1077,7 +1077,8 @@ let ``long type member with return type should have parameters on separate lines
type C () =
member __.LongMethodWithLotsOfParameters(aVeryLongType: AVeryLongTypeThatYouNeedToUse,
aSecondVeryLongType: AVeryLongTypeThatYouNeedToUse,
aThirdVeryLongType: AVeryLongTypeThatYouNeedToUse): int =
aThirdVeryLongType: AVeryLongTypeThatYouNeedToUse)
: int =
aVeryLongType aSecondVeryLongType aThirdVeryLongType
"""

Expand Down Expand Up @@ -1144,7 +1145,8 @@ let ``keep correct indentation after multiline typed member definition`` () =
|> should equal """
type SomeType() =
member SomeMember(looooooooooooooooooooooooooooooooooong1: A,
looooooooooooooooooooooooooooooooooong2: A): string =
looooooooooooooooooooooooooooooooooong2: A)
: string =
printfn "a"
"a"
Expand All @@ -1162,7 +1164,8 @@ let ``split multiple parameters over multiple lines`` () =
|> should equal """
type SomeType =
static member SomeMember (looooooooooooooooooooooooooooooooooooooooooooooooooooooooooooong1: string)
(looooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooong2: string): string =
(looooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooong2: string)
: string =
printfn "a"
"b"
"""
Expand All @@ -1180,7 +1183,8 @@ let ``split multiple parameters over multiple lines and have correct indentation
|> should equal """
type SomeType =
static member SomeMember (looooooooooooooooooooooooooooooooooooooooooooooooooooooooooooong1: string)
(looooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooong2: string): string =
(looooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooong2: string)
: string =
printfn "a"
"b"
Expand All @@ -1197,7 +1201,8 @@ let ``member with one long parameter and return type, 850`` () =
|> prepend newline
|> should equal """
type SomeType =
static member SomeMember loooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooong1: string =
static member SomeMember loooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooong1
: string =
printfn "a"
"b"
"""
Expand Down Expand Up @@ -1237,7 +1242,8 @@ type SomeType =
static member Serialize(loooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooong2: SomeType) =
Encode.string v.Meh
static member Deserialize(loooooooooooooooooooooooooooooooooooooooooooooooooooooonnnnnnnnnnnnnnnnnnnnngggggggggggJsonVaaaaalueeeeeeeeeeeeeeee): SomeType =
static member Deserialize(loooooooooooooooooooooooooooooooooooooooooooooooooooooonnnnnnnnnnnnnnnnnnnnngggggggggggJsonVaaaaalueeeeeeeeeeeeeeee)
: SomeType =
Decode.SomeType
loooooooooooooooooooooooooooooooooooooooooooooooooooooonnnnnnnnnnnnnnnnnnnnngggggggggggJsonVaaaaalueeeeeeeeeeeeeeee
"""
Expand Down Expand Up @@ -1275,7 +1281,8 @@ type C() =
type C() =
member _.LongMethodWithLotsOfParameters(aVeryLongType : int,
aSecondVeryLongType : int,
aThirdVeryLongType : int) : int =
aThirdVeryLongType : int)
: int =
aVeryLongType + aSecondVeryLongType + aThirdVeryLongType
"""

Expand Down
41 changes: 34 additions & 7 deletions src/Fantomas/CodePrinter.fs
Original file line number Diff line number Diff line change
Expand Up @@ -679,20 +679,47 @@ and genMemberBinding astContext b =

let prefix =
genMemberFlagsForMemberBinding astContext mf b.RangeOfBindingAndRhs
+> ifElse isInline (!- "inline ") sepNone +> opt sepSpace ao genAccess +> genPat ({ astContext with IsMemberDefinition = true }) p
+> ifElse isInline (!- "inline ") sepNone +> opt sepSpace ao genAccess

match e with
| TypedExpr(Typed, e, t) ->
genAttributesAndXmlDoc
+> prefix
+> sepColon
+> genType astContext false t
+> sepEq
+> sepSpaceOrIndentAndNlnIfExpressionExceedsPageWidth (genExpr astContext e)
let genName, genParameters, spaceBeforeColon =
match p with
| PatLongIdent(ao, s, ps, tpso) ->
let aoc = opt sepSpace ao genAccess
let tpsoc = opt sepNone tpso (fun (ValTyparDecls(tds, _, tcs)) -> genTypeParamPostfix astContext tds tcs)
let s = if s = "``new``" then "new" else s
let hasBracket = ps |> Seq.map snd |> Seq.exists hasParenInPat
let multipleParameters = List.length ps > 1
let spaceAfter ctx = onlyIf (ctx.Config.SpaceBeforeMember || multipleParameters || not hasBracket) sepSpace ctx
let name = (aoc -- s +> tpsoc +> spaceAfter)

let parameters =
expressionFitsOnRestOfLine
(atCurrentColumn (col sepSpace ps (genPatWithIdent astContext)))
(atCurrentColumn (col sepNln ps (genPatWithIdent astContext)))

name, parameters, onlyIf (hasBracket && not multipleParameters) !- " "
| _ -> sepNone, sepNone, sepNone

let memberDefinition =
prefix
+> expressionFitsOnRestOfLine
(genName +> genParameters +> sepColon +> genType astContext false t +> sepEq)
(genName +> atCurrentColumn (genParameters +> sepNln +> spaceBeforeColon +> sepColon +> genType astContext false t +> sepEq))

genAttributesAndXmlDoc
+> leadingExpressionIsMultiline
memberDefinition
(fun mdLong ->
ifElse
mdLong
(indent +> sepNln +> genExpr astContext e +> unindent)
(sepSpaceOrIndentAndNlnIfExpressionExceedsPageWidth (genExpr astContext e)))
| e ->
genAttributesAndXmlDoc
+> prefix
+> genPat ({ astContext with IsMemberDefinition = true }) p
+> sepEq
+> sepSpace
+> (fun ctx -> (isShortExpressionOrAddIndentAndNewline (if isFunctionBinding p then ctx.Config.MaxFunctionBindingWidth else ctx.Config.MaxValueBindingWidth) (genExpr astContext e)) ctx)
Expand Down

0 comments on commit 344d442

Please sign in to comment.