Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix duplicate newlines in object expression. #602

Merged
merged 3 commits into from
Dec 19, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 45 additions & 0 deletions src/Fantomas.Tests/RecordTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -597,4 +597,49 @@ type Element =

/// The qualified name.
Name: Name }
"""

[<Test>]
let ``don't duplicate newlines in object expression, 601`` () =
formatSourceString false """namespace Blah

open System

module Test =
type ISomething =
inherit IDisposable
abstract DoTheThing: string -> unit

let test (something: IDisposable) (somethingElse: IDisposable) =
{ new ISomething with

member __.DoTheThing whatever =
printfn "%s" whatever
printfn "%s" whatever

member __.Dispose() =
something.Dispose()
somethingElse.Dispose() }
""" config
|> prepend newline
|> should equal """
namespace Blah

open System

module Test =
type ISomething =
inherit IDisposable
abstract DoTheThing: string -> unit

let test (something: IDisposable) (somethingElse: IDisposable) =
{ new ISomething with

member __.DoTheThing whatever =
printfn "%s" whatever
printfn "%s" whatever

member __.Dispose() =
something.Dispose()
somethingElse.Dispose() }
"""
22 changes: 19 additions & 3 deletions src/Fantomas/CodePrinter.fs
Original file line number Diff line number Diff line change
Expand Up @@ -589,9 +589,25 @@ and genMemberBindingList astContext node =
| [x] -> genMemberBinding astContext x

| MultilineBindingL(xs, ys) ->
let prefix = sepNln +> col (rep 2 sepNln) xs (function
| Pair(x1, x2) -> genPropertyWithGetSet astContext (x1, x2)
| Single x -> genMemberBinding astContext x)
let prefix =
let genX =
function
| Pair(x1, x2) -> genPropertyWithGetSet astContext (x1, x2)
| Single x -> genMemberBinding astContext x

let sepNlnX (x': Composite<SynBinding, SynBinding>) =
match x' with
| Pair(x, _)
| Single x -> sepNln +> sepNlnConsideringTriviaContentBefore x.RangeOfBindingSansRhs

let firstSepNln =
match xs with
| (Pair(x,_))::_
| (Single x)::_ -> sepNlnConsideringTriviaContentBefore x.RangeOfBindingSansRhs
| _ -> sepNln

firstSepNln +> colEx sepNlnX xs genX

match ys with
| [] -> prefix
| _ -> prefix +> rep 2 sepNln +> genMemberBindingList astContext ys
Expand Down