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

Print some trivia before properties. #1012

Merged
merged 2 commits into from
Aug 19, 2020
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
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 @@ -2832,14 +2833,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