Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix for additional newline. #289 #356

Merged
merged 1 commit into from
Nov 19, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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