Skip to content

Commit

Permalink
Fix for additional newline. fsprojects#289
Browse files Browse the repository at this point in the history
  • Loading branch information
nojaf committed Nov 19, 2018
1 parent bf8757b commit 5ebe20c
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 11 deletions.
Binary file modified .paket/paket.exe
Binary file not shown.
48 changes: 48 additions & 0 deletions src/Fantomas.Tests/CommentTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -384,3 +384,51 @@ let x =
// another comment 2
printf "c"
"""

[<Test>]
let ``preserve newline should not add additional newline`` () =
let c = { config with PreserveEndOfLine = true }
let source = """
type T() =
let x = 123
// override private x.ToString() = ""
"""

formatSourceString false source c
|> should equal """
type T() =
let x = 123
// override private x.ToString() = ""
"""

[<Test>]
let ``preserve newline false should not add additional newline`` () =
let source = """
type T() =
let x = 123
// override private x.ToString() = ""
"""

formatSourceString false source config
|> prepend newline
|> should equal """
type T() =
let x = 123
// override private x.ToString() = ""
"""

[<Test>]
let ``preserve newline should not add additional newline II`` () =
let c = { config with PreserveEndOfLine = true }
let source = """
let test n = [ n..-1..1 ]
let y = ()
// Some comments
"""

formatSourceString false source c
|> should equal """
let test n = [ n ..- 1..1 ]
let y = ()
// Some comments
"""
16 changes: 5 additions & 11 deletions src/Fantomas/CodeFormatter.fsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ open Fantomas

CodeFormatter.Parse ("Program.fs", """let plus a b = a + b""")

let config = FormatConfig.Default
let config = { FormatConfig.Default with PreserveEndOfLine = true }

let formatSrc (s : string) =
formatSourceString false (s.Replace("\r\n", "\n")) config |> printfn "%A";;
Expand All @@ -26,16 +26,10 @@ fsi.AddPrinter (fun (p : pos) -> p.ToString())
fsi.AddPrinter (fun (r : range) -> r.ToString())

"""
Log.Logger <-
LoggerConfiguration()
// Suave.SerilogExtensions has native destructuring mechanism
// this helps Serilog deserialize the fsharp types like unions/records
.Destructure.FSharpTypes()
// use package Serilog.Sinks.Console
// https://github.com/serilog/serilog-sinks-console
.WriteTo.Console()
// add more sinks etc.
.CreateLogger()"""
type T() =
let x = 123
// override private x.ToString() = ""
"""
|> formatSrc

// le nojaf
Expand Down
5 changes: 5 additions & 0 deletions src/Fantomas/TokenMatcher.fs
Original file line number Diff line number Diff line change
Expand Up @@ -738,6 +738,11 @@ let integrateComments isPreserveEOL compilationDefines (originalText : string) (
loop origTokens moreNewTokens

// Process the last line or block comments
| (LineCommentChunk false (commentTokensText, moreOrigTokens)), [] when (isPreserveEOL) ->
Debug.WriteLine("injecting the last line comment '{0}'", String.concat "" commentTokensText |> box)
for x in commentTokensText do addText x
loop moreOrigTokens newTokens

| (LineCommentChunk false (commentTokensText, moreOrigTokens)), []
| (BlockCommentChunk (commentTokensText, moreOrigTokens)), [] ->
Debug.WriteLine("injecting the last line or block comment '{0}'", String.concat "" commentTokensText |> box)
Expand Down

0 comments on commit 5ebe20c

Please sign in to comment.