From 4134342765e616c5d067fc77d23db905206fd25a Mon Sep 17 00:00:00 2001 From: nojaf Date: Thu, 1 Apr 2021 15:16:21 +0200 Subject: [PATCH] Print trivia for ident of ValSpfn. Fixes #1561. --- src/Fantomas.Tests/SignatureTests.fs | 27 +++++++++++++++++++++++++++ src/Fantomas/AstTransformer.fs | 3 ++- src/Fantomas/CodePrinter.fs | 18 ++++++++++-------- src/Fantomas/SourceParser.fs | 6 ++++-- 4 files changed, 43 insertions(+), 11 deletions(-) diff --git a/src/Fantomas.Tests/SignatureTests.fs b/src/Fantomas.Tests/SignatureTests.fs index 725b8adc51..6626b47e4f 100644 --- a/src/Fantomas.Tests/SignatureTests.fs +++ b/src/Fantomas.Tests/SignatureTests.fs @@ -1298,3 +1298,30 @@ exception FileNameNotResolved of string (*description of searched locations*) * exception LoadedSourceNotFoundIgnoring of string * range (*filename*) """ + +[] +let ``comment between attribute and val, 1561`` () = + formatSourceString + true + """ +namespace Baz + +module Bar = + + [] + /// + val f : unit -> unit +""" + config + |> prepend newline + |> should + equal + """ +namespace Baz + +module Bar = + + [] + /// + val f : unit -> unit +""" diff --git a/src/Fantomas/AstTransformer.fs b/src/Fantomas/AstTransformer.fs index 925b82c436..df0a024f6c 100644 --- a/src/Fantomas/AstTransformer.fs +++ b/src/Fantomas/AstTransformer.fs @@ -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 diff --git a/src/Fantomas/CodePrinter.fs b/src/Fantomas/CodePrinter.fs index d861424dc0..25c70f962f 100644 --- a/src/Fantomas/CodePrinter.fs +++ b/src/Fantomas/CodePrinter.fs @@ -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 @@ -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) @@ -3897,7 +3899,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 = diff --git a/src/Fantomas/SourceParser.fs b/src/Fantomas/SourceParser.fs index 3a859d05eb..be67fdae60 100644 --- a/src/Fantomas/SourceParser.fs +++ b/src/Fantomas/SourceParser.fs @@ -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