Skip to content

Commit

Permalink
Add newline between assembly level attributes. Fixes fsprojects#796
Browse files Browse the repository at this point in the history
  • Loading branch information
nojaf committed May 1, 2020
1 parent 7a6d380 commit cbd354f
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 3 deletions.
28 changes: 28 additions & 0 deletions src/Fantomas.Tests/AttributeTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -305,4 +305,32 @@ let main argv =
|> Array.map getJsonNetJson
|> Array.iter(printfn "%s")
0 // return an integer exit code
"""

[<Test>]
let ``multiple assembly attributes, 796`` () =
formatSourceString false """namespace Foo.AssemblyInfo
open System.Reflection
open System.Runtime.CompilerServices
open System.Runtime.InteropServices
[<assembly: AssemblyTitle("Foo")>]
[<assembly: AssemblyDescription("")>]
do
()
""" config
|> prepend newline
|> should equal """
namespace Foo.AssemblyInfo
open System.Reflection
open System.Runtime.CompilerServices
open System.Runtime.InteropServices
[<assembly:AssemblyTitle("Foo")>]
[<assembly:AssemblyDescription("")>]
do ()
"""
22 changes: 19 additions & 3 deletions src/Fantomas/CodePrinter.fs
Original file line number Diff line number Diff line change
Expand Up @@ -278,9 +278,25 @@ and genSigModuleDeclList astContext node =
and genModuleDecl astContext (node: SynModuleDecl) =
match node with
| Attributes(ats) ->
col sepNone ats
(fun a -> col sepNln a.Attributes (genAttribute astContext)
|> genTrivia a.Range)
fun ctx ->
let attributesExpr =
// attributes can have trivia content before or after
// we do extra detection to ensure no additional newline is introduced
// first attribute should not have a newline anyway
List.fold (fun (prevContentAfterPresent, prevExpr) (a: SynAttributeList) ->
let expr =
ifElse prevContentAfterPresent sepNone (sepNlnConsideringTriviaContentBefore a.Range)
+> ((col sepNln a.Attributes (genAttribute astContext)) |> genTrivia a.Range)

let hasContentAfter =
TriviaHelpers.``has content after after that matches``
(fun tn -> tn.Range = a.Range)
(function | Directive(_) -> true | _ -> false)
ctx.Trivia
(hasContentAfter, prevExpr +> expr)
) (true, sepNone) ats
|> snd
(attributesExpr +> dumpAndContinue) ctx
| DoExpr(e) ->
genExpr astContext e
| Exception(ex) ->
Expand Down

0 comments on commit cbd354f

Please sign in to comment.