Skip to content

Commit

Permalink
Consider Trivia Content before multiline members - fixes fsprojects#569
Browse files Browse the repository at this point in the history
The pattern matching above uses nested `sepMember` function
to consider Trivia Content. The difference between those
pattern matching cases is that latter one matches on when
there are trailing non-multiline members.
There's some code duplication between those two now, which
I'm happy to refactor - feedback welcome.
  • Loading branch information
theimowski committed Dec 3, 2019
1 parent 6413638 commit 6d37c52
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 2 deletions.
17 changes: 16 additions & 1 deletion src/Fantomas.Tests/ClassTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -382,4 +382,19 @@ type Exception with
type Exception with
member inline __.FirstLine = __.Message.Split([| Environment.NewLine |], StringSplitOptions.RemoveEmptyEntries).[0]
"""
"""

[<Test>]
let ``No extra new lines between type members, 569``() =
let original = """type A() =
member this.MemberA =
if true then 0 else 1
member this.MemberB =
if true then 2 else 3
member this.MemberC = 0
"""

formatSourceString false original config |> should equal original
11 changes: 10 additions & 1 deletion src/Fantomas/CodePrinter.fs
Original file line number Diff line number Diff line change
Expand Up @@ -1950,8 +1950,17 @@ and genMemberDefnList astContext node =
sepNlnConsideringTriviaContentBeforeWithAttributes xsh.Range attributes
| _ -> sepNln

let sepMember (m:Composite<SynMemberDefn, SynBinding>) =
match m with
| Pair(x1,_) ->
let attributes = getRangesFromAttributesFromSynBinding x1
sepNln +> sepNlnConsideringTriviaContentBeforeWithAttributes x1.RangeOfBindingSansRhs attributes
| Single x ->
let attributes = getRangesFromAttributesFromSynMemberDefinition x
sepNln +> sepNlnConsideringTriviaContentBeforeWithAttributes x.Range attributes

sepNln +> sepNlnFirstExpr
+> col (rep 2 sepNln) xs (function
+> colEx sepMember xs (function
| Pair(x1, x2) -> genPropertyWithGetSet astContext (x1, x2)
| Single x -> genMemberDefn astContext x)
+> sepNln +> genMemberDefnList astContext ys
Expand Down

0 comments on commit 6d37c52

Please sign in to comment.