From 9cb5a66ea5724940450b8d7c34829ae5d18f25d3 Mon Sep 17 00:00:00 2001 From: nojaf Date: Wed, 15 Apr 2020 18:41:32 +0200 Subject: [PATCH] Fix restoring line comment above static member in signature file. Fixes #680 --- src/Fantomas.Tests/SignatureTests.fs | 35 +++++++++++++++++++++++++++- src/Fantomas/CodePrinter.fs | 10 +++++++- src/Fantomas/SourceParser.fs | 3 ++- src/Fantomas/Trivia.fs | 2 ++ 4 files changed, 47 insertions(+), 3 deletions(-) diff --git a/src/Fantomas.Tests/SignatureTests.fs b/src/Fantomas.Tests/SignatureTests.fs index d43e5febec..89dcf2866b 100644 --- a/src/Fantomas.Tests/SignatureTests.fs +++ b/src/Fantomas.Tests/SignatureTests.fs @@ -477,4 +477,37 @@ type T = type T with member Foo: int -""" \ No newline at end of file +""" + +[] +let ``comment above static member, 680`` () = + formatSourceString true """ +namespace Fantomas + +open Fantomas.FormatConfig +open Fantomas.SourceOrigin +open FSharp.Compiler.Ast +open FSharp.Compiler.Range +open FSharp.Compiler.SourceCodeServices + +[] +type CodeFormatter = + /// Parse a source string using given config + static member ParseAsync : fileName:string * source:SourceOrigin * parsingOptions: FSharpParsingOptions * checker:FSharpChecker -> Async<(ParsedInput * string list) array> +""" config + |> prepend newline + |> should equal """ +namespace Fantomas + +open Fantomas.FormatConfig +open Fantomas.SourceOrigin +open FSharp.Compiler.Ast +open FSharp.Compiler.Range +open FSharp.Compiler.SourceCodeServices + +[] +type CodeFormatter = + /// Parse a source string using given config + static member ParseAsync: fileName:string * source:SourceOrigin * parsingOptions:FSharpParsingOptions * checker:FSharpChecker + -> Async<(ParsedInput * string list) array> +""" diff --git a/src/Fantomas/CodePrinter.fs b/src/Fantomas/CodePrinter.fs index 7acc213acf..6695ea4f57 100644 --- a/src/Fantomas/CodePrinter.fs +++ b/src/Fantomas/CodePrinter.fs @@ -218,10 +218,18 @@ and genSigModuleDeclList astContext node = | [x] -> genSigModuleDecl astContext x | SigOpenL(xs, ys) -> + let sepXsAndYs = + match List.tryHead ys with + | Some hs -> + let attrs = getRangesFromAttributesFromSynModuleSigDeclaration hs + sepNln +> sepNlnConsideringTriviaContentBeforeWithAttributes hs.Range attrs +> dumpAndContinue + | None -> + rep 2 sepNln + fun ctx -> match ys with | [] -> col sepNln xs (genSigModuleDecl astContext) ctx - | _ -> (col sepNln xs (genSigModuleDecl astContext) +> rep 2 sepNln +> genSigModuleDeclList astContext ys) ctx + | _ -> (col sepNln xs (genSigModuleDecl astContext) +> sepXsAndYs +> genSigModuleDeclList astContext ys) ctx | SigHashDirectiveL(xs, ys) -> match ys with diff --git a/src/Fantomas/SourceParser.fs b/src/Fantomas/SourceParser.fs index 8374402b62..079025f2b6 100644 --- a/src/Fantomas/SourceParser.fs +++ b/src/Fantomas/SourceParser.fs @@ -1313,7 +1313,8 @@ let getRangesFromAttributesFromModuleDeclaration (mdl: SynModuleDecl) = let getRangesFromAttributesFromSynModuleSigDeclaration (sdl: SynModuleSigDecl) = match sdl with - | SynModuleSigDecl.NestedModule((SynComponentInfo.ComponentInfo(attrs, _,_,_,_,_,_,_)), _,_,_) -> + | SynModuleSigDecl.NestedModule((SynComponentInfo.ComponentInfo(attrs, _,_,_,_,_,_,_)), _,_,_) + | SynModuleSigDecl.Types (SynTypeDefnSig.TypeDefnSig(SynComponentInfo.ComponentInfo(attrs, _,_,_,_,_,_,_),_,_,_)::_,_) -> collectAttributesRanges attrs | _ -> Seq.empty |> Seq.toList diff --git a/src/Fantomas/Trivia.fs b/src/Fantomas/Trivia.fs index 7212f3f1d7..d74607a1fb 100644 --- a/src/Fantomas/Trivia.fs +++ b/src/Fantomas/Trivia.fs @@ -33,6 +33,8 @@ let filterNodes nodes = "SynModuleOrNamespaceSig.DeclaredNamespace" "SynExpr.LetOrUse" "SynTypeDefnRepr.ObjectModel" + "TypeDefnSig" + "SynTypeDefnSigRepr.ObjectModel" ] nodes |> List.filter (fun (n: Node) -> not (Set.contains n.Type filterOutNodeTypes))