Skip to content

Commit

Permalink
Don't add newline to namespace where the first trivia is a newline
Browse files Browse the repository at this point in the history
  • Loading branch information
ErikSchierboom committed May 10, 2020
1 parent d6971e5 commit cbd2cce
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 20 deletions.
15 changes: 1 addition & 14 deletions src/Fantomas.Tests/ClassTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -540,17 +540,4 @@ let ``unit parameter to inherited class`` () =
|> should equal """
type Child() =
inherit Parent()
"""

[<Test>]
let ``class let binding with attribute should not add newline, 786`` () =
formatSourceString false """type A() =
[<Bar>]
let foo = ()
""" config
|> should equal """type A() =
[<Bar>]
let foo = ()
"""
"""
55 changes: 51 additions & 4 deletions src/Fantomas.Tests/ModuleTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -439,10 +439,25 @@ type UrlModel =
Defines: string }
"""

[<TestCase(true)>]
[<TestCase(false)>]
let ``comment is first trivia in module should not add newline, 784`` (isFsiFile) =
formatSourceString isFsiFile """
[<Test>]
let ``comment is first trivia in module should not add newline, 784`` () =
formatSourceString false """
module foo
// bar
// baz
""" config
|> prepend newline
|> should equal """
module foo
// bar
// baz
"""

[<Test>]
let ``comment is first trivia in module in script file should not add newline, 784`` () =
formatSourceString true """
module foo
// bar
Expand All @@ -455,3 +470,35 @@ module foo
// bar
// baz
"""

[<Test>]
let ``comment is first trivia in namespace should not add newline, 784`` () =
formatSourceString false """
namespace foo.quz
// bar
// baz
""" config
|> prepend newline
|> should equal """
namespace foo.quz
// bar
// baz
"""

[<Test>]
let ``comment is first trivia in namespace in signature file should not add newline, 784`` () =
formatSourceString true """
namespace foo.quz
// bar
// baz
""" config
|> prepend newline
|> should equal """
namespace foo.quz
// bar
// baz
"""
10 changes: 8 additions & 2 deletions src/Fantomas/CodePrinter.fs
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,10 @@ and genModuleOrNamespace astContext (ModuleOrNamespace(ats, px, ao, s, mds, isRe
let firstDecl = List.tryHead mds
match firstDecl with
| None ->
sepNlnForEmptyModule node.Range +> sepNln
if moduleKind.IsModule then
sepNlnForEmptyModule node.Range +> sepNln
else
sepNlnForEmptyNamespace node.Range +> sepNln
| Some mdl ->
let attrRanges = getRangesFromAttributesFromModuleDeclaration mdl
sepNlnConsideringTriviaContentBeforeWithAttributes mdl.Range attrRanges +> sepNln
Expand Down Expand Up @@ -166,7 +169,10 @@ and genSigModuleOrNamespace astContext (SigModuleOrNamespace(ats, px, ao, s, mds
let firstDecl = List.tryHead mds
match firstDecl with
| None ->
sepNlnForEmptyModule range +> rep 2 sepNln
if moduleKind.IsModule then
sepNlnForEmptyModule range +> rep 2 sepNln
else
sepNlnForEmptyNamespace range +> sepNln
| Some mdl ->
sepNlnConsideringTriviaContentBefore mdl.Range +> sepNln

Expand Down
14 changes: 14 additions & 0 deletions src/Fantomas/Context.fs
Original file line number Diff line number Diff line change
Expand Up @@ -749,6 +749,12 @@ let private findTriviaMainNodeOrTokenOnEndFromRange nodes (range:range) =
|> List.tryFind(fun n ->
Trivia.isMainNode n && RangeHelpers.rangeEq n.Range range
|| Trivia.isToken n && RangeHelpers.rangeEndEq n.Range range)

let private findTriviaMainNodeOrTokenContainedInRange nodes (range:range) =
nodes
|> List.tryFind(fun n ->
Trivia.isMainNode n && RangeHelpers.``range contains`` range n.Range
|| Trivia.isToken n && RangeHelpers.``range contains`` range n.Range)

let private findTriviaTokenFromRange nodes (range:range) =
nodes
Expand Down Expand Up @@ -891,6 +897,14 @@ let internal sepNlnForEmptyModule (moduleRange:range) ctx =
| _ ->
sepNln ctx

let internal sepNlnForEmptyNamespace (namespaceRange:range) ctx =
let emptyNamespaceRange = mkRange namespaceRange.FileName (mkPos 0 0) namespaceRange.End
match findTriviaMainNodeOrTokenContainedInRange ctx.Trivia emptyNamespaceRange with
| Some node when hasPrintableContent node.ContentBefore || hasPrintableContent node.ContentAfter ->
ctx
| _ ->
sepNln ctx

let internal sepNlnTypeAndMembers (firstMemberRange: range option) ctx =
match firstMemberRange with
| Some range when (ctx.Config.NewlineBetweenTypeDefinitionAndMembers) ->
Expand Down

0 comments on commit cbd2cce

Please sign in to comment.