Skip to content

Commit

Permalink
Add WriteLineBecauseOfTrivia for trivia newlines. (fsprojects#1720)
Browse files Browse the repository at this point in the history
* Add WriteLineBecauseOfTrivia for trivia newlines. Fixes fsprojects#1718.

* Add additional test to cover other multiline directive trivia.
  • Loading branch information
nojaf authored May 11, 2021
1 parent f10b822 commit 52946a5
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 1 deletion.
85 changes: 85 additions & 0 deletions src/Fantomas.Tests/ColMultilineItemTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -441,3 +441,88 @@ let ``first lamba`` () =
let uppercased = toUpperCase name
Assert.Equal("JOEY", uppercased)
"""

[<Test>]
let ``leading multiline block comment followed by newline should not make item multiline, 1718`` () =
formatSourceString
false
"""
(*
My personal favorite: Discriminated Unions!
This is a feature related to sum types in category theory and incredibly useful for code correctness.
ref: https://docs.microsoft.com/en-us/dotnet/fsharp/language-reference/discriminated-unions
*)
type Days = Days of int // you can alias types in F# to get a DDD kind of vibe.
type StoryPoints = StoryPoints of int
type Money = Money of double
"""
config
|> prepend newline
|> should
equal
"""
(*
My personal favorite: Discriminated Unions!
This is a feature related to sum types in category theory and incredibly useful for code correctness.
ref: https://docs.microsoft.com/en-us/dotnet/fsharp/language-reference/discriminated-unions
*)
type Days = Days of int // you can alias types in F# to get a DDD kind of vibe.
type StoryPoints = StoryPoints of int
type Money = Money of double
"""

[<Test>]
let ``leading comment followed by newlines should not make item multiline`` () =
formatSourceString
false
"""
//
let a = 0
let b = p
"""
config
|> prepend newline
|> should
equal
"""
//
let a = 0
let b = p
"""

[<Test>]
let ``define surrounded by newlines should not make item multiline`` () =
formatSourceString
false
"""
let a = p
#if MEH
()
#endif
let i = 00
let y = p
"""
config
|> prepend newline
|> should
equal
"""
let a = p
#if MEH
()
#endif
let i = 0
let y = p
"""
2 changes: 1 addition & 1 deletion src/Fantomas/Context.fs
Original file line number Diff line number Diff line change
Expand Up @@ -1163,7 +1163,7 @@ let internal printTriviaContent (c: TriviaContent) (ctx: Context) =
-- s
+> sepSpace
+> ifElse after sepNlnForTrivia sepNone
| Newline -> (ifElse addNewline (sepNln +> sepNln) sepNln)
| Newline -> (ifElse addNewline (sepNlnForTrivia +> sepNlnForTrivia) sepNlnForTrivia)
| Keyword _
| Number _
| StringContent _
Expand Down

0 comments on commit 52946a5

Please sign in to comment.