Skip to content

Commit

Permalink
Keep track of newline inside trivia via special event WriteLineInside…
Browse files Browse the repository at this point in the history
…Trivia. Fixes #1538. (#1752)
  • Loading branch information
nojaf committed May 25, 2021
1 parent 8611499 commit b4ccb7b
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 2 deletions.
25 changes: 25 additions & 0 deletions src/Fantomas.Tests/ColMultilineItemTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -527,3 +527,28 @@ let a = p
let i = 0
let y = p
"""

[<Test>]
let ``two short application followed by trailing block comment, 1538`` () =
formatSourceString
false
"""
printfn "%s" @"c:\def\ghi\jkl"
printfn "%s" "c:\\def\\ghi\\jkl"
(*
xyz
*)
"""
config
|> prepend newline
|> should
equal
"""
printfn "%s" @"c:\def\ghi\jkl"
printfn "%s" "c:\\def\\ghi\\jkl"
(*
xyz
*)
"""
11 changes: 9 additions & 2 deletions src/Fantomas/Context.fs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ type WriterEvent =
| WriteLineInsideStringConst
| WriteBeforeNewline of string
| WriteLineBecauseOfTrivia
| WriteLineInsideTrivia
| IndentBy of int
| UnIndentBy of int
| SetIndent of int
Expand Down Expand Up @@ -94,7 +95,8 @@ module WriterModel =
match cmd with
| WriteLine
| WriteLineBecauseOfTrivia -> doNewline m
| WriteLineInsideStringConst ->
| WriteLineInsideStringConst
| WriteLineInsideTrivia ->
{ m with
Lines = "" :: m.Lines
Column = 0 }
Expand Down Expand Up @@ -150,9 +152,14 @@ module WriterEvents =
let normalize ev =
match ev with
| Write s when s.Contains("\n") ->
let writeLine =
match ev with
| CommentOrDefineEvent _ -> WriteLineInsideTrivia
| _ -> WriteLineInsideStringConst

s.Split('\n')
|> Seq.map (fun x -> [ Write x ])
|> Seq.reduce (fun x y -> x @ [ WriteLineInsideStringConst ] @ y)
|> Seq.reduce (fun x y -> x @ [ writeLine ] @ y)
|> Seq.toList
| _ -> [ ev ]

Expand Down

0 comments on commit b4ccb7b

Please sign in to comment.