Skip to content

Commit

Permalink
Split multiple parameters across multiple lines if they exceed page w…
Browse files Browse the repository at this point in the history
…idth (#847)

* keep return type in same line for member defs with multiple parameters

* Put parameters on multiple lines if they exceed page width

* Fix indentation

* Add more tests
  • Loading branch information
Bobface committed May 20, 2020
1 parent 5035a1d commit 694f4c3
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 6 deletions.
72 changes: 72 additions & 0 deletions src/Fantomas.Tests/TypeDeclarationTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -1162,3 +1162,75 @@ type SomeType() =
member SomeOtherMember() = printfn "b"
"""

[<Test>]
let ``split multiple parameters over multiple lines`` () =
formatSourceString false """type SomeType =
static member SomeMember (looooooooooooooooooooooooooooooooooooooooooooooooooooooooooooong1: string) (looooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooong2: string) : string =
printfn "a"
"b"
""" config
|> prepend newline
|> should equal """
type SomeType =
static member SomeMember
(looooooooooooooooooooooooooooooooooooooooooooooooooooooooooooong1: string)
(looooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooong2: string)
: string
=
printfn "a"
"b"
"""

[<Test>]
let ``split multiple parameters over multiple lines and have correct indentation afterwards`` () =
formatSourceString false """type SomeType =
static member SomeMember (looooooooooooooooooooooooooooooooooooooooooooooooooooooooooooong1: string) (looooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooong2: string) : string =
printfn "a"
"b"
static member SomeOtherMember () = printfn "c"
""" config
|> prepend newline
|> should equal """
type SomeType =
static member SomeMember
(looooooooooooooooooooooooooooooooooooooooooooooooooooooooooooong1: string)
(looooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooong2: string)
: string
=
printfn "a"
"b"
static member SomeOtherMember() = printfn "c"
"""

[<Test>]
let ``member with one long parameter and return type`` () =
formatSourceString false """type SomeType =
static member SomeMember loooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooong1 : string =
printfn "a"
"b"
""" config
|> prepend newline
|> should equal """
type SomeType =
static member SomeMember loooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooong1: string =
printfn "a"
"b"
"""

[<Test>]
let ``member with one long parameter and no return type`` () =
formatSourceString false """type SomeType =
static member SomeMember loooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooong1 =
printfn "a"
"b"
""" config
|> prepend newline
|> should equal """
type SomeType =
static member SomeMember loooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooong1 =
printfn "a"
"b"
"""
17 changes: 11 additions & 6 deletions src/Fantomas/CodePrinter.fs
Original file line number Diff line number Diff line change
Expand Up @@ -2664,7 +2664,7 @@ and genPat astContext pat =
let s = if s = "``new``" then "new" else s

match ps with
| [] -> aoc -- s +> tpsoc
| [] -> aoc -- s +> tpsoc
| [(_, PatTuple [p1; p2])] when s = "(::)" ->
aoc +> genPat astContext p1 -- " :: " +> genPat astContext p2
| [(ido, p) as ip] ->
Expand All @@ -2675,12 +2675,17 @@ and genPat astContext pat =
| ps ->
let hasBracket = ps |> Seq.map fst |> Seq.exists Option.isSome
let genName = aoc -- s +> tpsoc +> sepSpace
let genParameters = colAutoNlnSkip0 (ifElse hasBracket sepSemi sepSpace) ps (genPatWithIdent astContext)

atCurrentColumn (genName
+> ifElse hasBracket sepOpenT sepNone
+> genParameters
+> ifElse hasBracket sepCloseT sepNone)
let genParameters =
(expressionFitsOnRestOfLine
(atCurrentColumn (col (ifElse hasBracket sepSemi sepSpace) ps (genPatWithIdent astContext)))
(indent +> sepNln +> col sepNln ps (genPatWithIdent astContext)))


genName
+> ifElse hasBracket sepOpenT sepNone
+> genParameters
+> ifElse hasBracket sepCloseT sepNone

| PatParen(PatConst(Const "()", _)) -> !- "()"
| PatParen(p) -> sepOpenT +> genPat astContext p +> sepCloseT
Expand Down

0 comments on commit 694f4c3

Please sign in to comment.