Skip to content

Commit

Permalink
Print trivia for ident of ValSpfn. Fixes #1561. (#1568)
Browse files Browse the repository at this point in the history
  • Loading branch information
nojaf committed Apr 1, 2021
1 parent 82615ce commit a16da08
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 11 deletions.
27 changes: 27 additions & 0 deletions src/Fantomas.Tests/SignatureTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -1325,3 +1325,30 @@ type 'a Bar =
///
| Quux
"""

[<Test>]
let ``comment between attribute and val, 1561`` () =
formatSourceString
true
"""
namespace Baz
module Bar =
[<Obsolete "">]
///
val f : unit -> unit
"""
config
|> prepend newline
|> should
equal
"""
namespace Baz
module Bar =
[<Obsolete "">]
///
val f : unit -> unit
"""
3 changes: 2 additions & 1 deletion src/Fantomas/AstTransformer.fs
Original file line number Diff line number Diff line change
Expand Up @@ -778,8 +778,9 @@ module private Ast =

and visitSynValSig (svs: SynValSig) : TriviaNodeAssigner list =
match svs with
| ValSpfn (attrs, _, explicitValDecls, synType, arity, _, _, _, _, expr, range) ->
| ValSpfn (attrs, ident, explicitValDecls, synType, arity, _, _, _, _, expr, range) ->
[ yield mkNode ValSpfn_ range
yield visitIdent ident
yield! (visitSynAttributeLists range attrs)
yield! visitSynValTyparDecls explicitValDecls
yield! visitSynType synType
Expand Down
18 changes: 10 additions & 8 deletions src/Fantomas/CodePrinter.fs
Original file line number Diff line number Diff line change
Expand Up @@ -862,7 +862,7 @@ and genMemberFlagsForMemberBinding astContext (mf: MemberFlags) (rangeOfBindingA
|> Option.defaultValue (!- "override ")
<| ctx

and genVal astContext (Val (ats, px, ao, s, t, vi, isInline, _) as node) =
and genVal astContext (Val (ats, px, ao, s, identRange, t, vi, isInline, _) as node) =
let range, synValTyparDecls =
match node with
| ValSpfn (_, _, synValTyparDecls, _, _, _, _, _, _, _, range) -> range, synValTyparDecls
Expand All @@ -874,12 +874,14 @@ and genVal astContext (Val (ats, px, ao, s, t, vi, isInline, _) as node) =

let (FunType namedArgs) = (t, vi)

genPreXmlDoc px +> genAttributes astContext ats
-- "val "
+> opt sepSpace ao genAccess
+> onlyIf isInline (!- "inline ")
-- s
+> genericParams
genPreXmlDoc px
+> genAttributes astContext ats
+> (!- "val "
+> opt sepSpace ao genAccess
+> onlyIf isInline (!- "inline ")
-- s
+> genericParams
|> genTriviaFor Ident_ identRange)
+> sepColonWithSpacesFixed
+> ifElse
(List.isNotEmpty namedArgs)
Expand Down Expand Up @@ -3898,7 +3900,7 @@ and genMemberSig astContext node =
| SynMemberSig.NestedType (_, r) -> r, SynMemberSig_NestedType

match node with
| MSMember (Val (ats, px, ao, s, t, vi, isInline, ValTyparDecls (tds, _, tcs)), mf) ->
| MSMember (Val (ats, px, ao, s, _, t, vi, isInline, ValTyparDecls (tds, _, tcs)), mf) ->
let (FunType namedArgs) = (t, vi)

let isFunctionProperty =
Expand Down
6 changes: 4 additions & 2 deletions src/Fantomas/SourceParser.fs
Original file line number Diff line number Diff line change
Expand Up @@ -1505,8 +1505,10 @@ let (|MSMember|MSInterface|MSInherit|MSValField|MSNestedType|) =
| SynMemberSig.ValField (f, _) -> MSValField f
| SynMemberSig.NestedType (tds, _) -> MSNestedType tds

let (|Val|) (ValSpfn (ats, IdentOrKeyword (OpNameFullInPattern (s, _)), tds, t, vi, isInline, _, px, ao, _, _)) =
(ats, px, ao, s, t, vi, isInline, tds)
let (|Val|)
(ValSpfn (ats, ((IdentOrKeyword (OpNameFullInPattern (s, _)) as ident)), tds, t, vi, isInline, _, px, ao, _, _))
=
(ats, px, ao, s, ident.idRange, t, vi, isInline, tds)

// Misc

Expand Down

0 comments on commit a16da08

Please sign in to comment.