Skip to content

Commit

Permalink
Print some trivia before properties. Fixes fsprojects#1009.
Browse files Browse the repository at this point in the history
  • Loading branch information
nojaf committed Aug 14, 2020
1 parent ee6dd12 commit 1391aa0
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 8 deletions.
38 changes: 38 additions & 0 deletions src/Fantomas.Tests/TypeDeclarationTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -1640,3 +1640,41 @@ type C
class
end
"""

[<Test>]
let ``trivia before properties, 1009`` () =
formatSourceString false """
type Box() =
let mutable color : string = null
// A Box has a color property with get and set.
member x.Color
with get() = color
and set(c) = color <- c
member x.Color2
// A Box has a color property with get and set.
with get() = color
and set(c) = color <- c
// If there's no get/set, the comment is preserved
member x.hello = "world"
""" config
|> prepend newline
|> should equal """
type Box() =
let mutable color: string = null
// A Box has a color property with get and set.
member x.Color
with get () = color
and set (c) = color <- c
member x.Color2
// A Box has a color property with get and set.
with get () = color
and set (c) = color <- c
// If there's no get/set, the comment is preserved
member x.hello = "world"
"""
20 changes: 12 additions & 8 deletions src/Fantomas/CodePrinter.fs
Original file line number Diff line number Diff line change
Expand Up @@ -610,7 +610,7 @@ and genProperty astContext prefix ao propertyKind ps e =
+> genExprSepEqPrependType astContext p e None false
|> genTrivia e.Range

and genPropertyWithGetSet astContext (b1, b2) =
and genPropertyWithGetSet astContext (b1, b2) rangeOfMember =
match b1, b2 with
| PropertyBinding(ats, px, ao, isInline, mf1, PatLongIdent(ao1, s1, ps1, _), e1),
PropertyBinding(_, _, _, _, _, PatLongIdent(ao2, _, ps2, _), e2) ->
Expand All @@ -625,6 +625,7 @@ and genPropertyWithGetSet astContext (b1, b2) =
prefix
+> genTrivia b1.RangeOfBindingAndRhs
(!- s1 +> indent +> sepNln
+> optSingle (fun rom -> enterNodeTokenByName rom "WITH") rangeOfMember
+> genProperty astContext "with " ao1 "get " ps1 e1 +> sepNln)
+> genTrivia b2.RangeOfBindingAndRhs
(genProperty astContext "and " ao2 "set " ps2 e2 +> unindent)
Expand All @@ -645,8 +646,8 @@ and genMemberBindingList astContext node =
| PropertyWithGetSet(gs, rest) ->
leadingExpressionIsMultiline
(expressionFitsOnRestOfLine
(genPropertyWithGetSet astContext gs)
(sepNln +> genPropertyWithGetSet astContext gs +> newlineAfterMultiline rest))
(genPropertyWithGetSet astContext gs None)
(sepNln +> genPropertyWithGetSet astContext gs None +> newlineAfterMultiline rest))
(fun multiline -> onlyIf (not multiline && List.isNotEmpty rest) sepNln)
+> genMemberBindingList astContext rest
| mb::rest ->
Expand Down Expand Up @@ -2797,14 +2798,17 @@ and genMemberDefnList astContext nodes =
| _ -> (col sepNln xs (genMemberDefn astContext) +> rep 2 sepNln +> genMemberDefnList astContext ys) ctx

| PropertyWithGetSetMemberDefn(gs, rest) ->
let rangeOfFirstMember = List.head nodes |> fun m -> m.Range
let m = fst gs
let attrs = getRangesFromAttributesFromSynBinding (fst gs)

sepNlnConsideringTriviaContentBeforeWithAttributes m.RangeOfBindingSansRhs attrs +>
(expressionFitsOnRestOfLine
(genPropertyWithGetSet astContext gs)
(sepNlnBeforeMultilineConstruct m.RangeOfBindingSansRhs attrs +> genPropertyWithGetSet astContext gs +> onlyIf (List.isNotEmpty rest) sepNln))

sepNlnConsideringTriviaContentBeforeWithAttributes rangeOfFirstMember attrs
+> enterNode rangeOfFirstMember
+> (expressionFitsOnRestOfLine
(genPropertyWithGetSet astContext gs (Some rangeOfFirstMember))
(sepNlnBeforeMultilineConstruct m.RangeOfBindingSansRhs attrs
+> genPropertyWithGetSet astContext gs (Some rangeOfFirstMember)
+> onlyIf (List.isNotEmpty rest) sepNln))
+> genMemberDefnList ({ astContext with IsFirstChild = false }) rest

| m::rest ->
Expand Down

0 comments on commit 1391aa0

Please sign in to comment.