diff --git a/.fantomasignore b/.fantomasignore new file mode 100644 index 0000000000..9cff2011b3 --- /dev/null +++ b/.fantomasignore @@ -0,0 +1,2 @@ +**/src/Fantomas.Tests/StringTests.fs +**/src/Fantomas.Tests/CommentTests.fs \ No newline at end of file diff --git a/src/Fantomas.Tests/CommentTests.fs b/src/Fantomas.Tests/CommentTests.fs index b8f83db4ae..af44a7cf2a 100644 --- a/src/Fantomas.Tests/CommentTests.fs +++ b/src/Fantomas.Tests/CommentTests.fs @@ -1685,3 +1685,22 @@ with with | d -> () """ + +[] +let ``trailing spaces in comments should be removed`` () = + formatSourceString + false + """ +// foo +// bar +let a = 9 +""" + config + |> prepend newline + |> should + equal + """ +// foo +// bar +let a = 9 +""" \ No newline at end of file diff --git a/src/Fantomas.Tests/ContextTests.fs b/src/Fantomas.Tests/ContextTests.fs index 4daaf04c4e..99e308fe7a 100644 --- a/src/Fantomas.Tests/ContextTests.fs +++ b/src/Fantomas.Tests/ContextTests.fs @@ -66,6 +66,7 @@ let ``don't add space before block comment`` () = Long comment *)""" + |> String.normalizeNewLine let expr = sepNone +> sepSpace -- comment diff --git a/src/Fantomas.Tests/StringTests.fs b/src/Fantomas.Tests/StringTests.fs index d82cba2b67..018142452f 100644 --- a/src/Fantomas.Tests/StringTests.fs +++ b/src/Fantomas.Tests/StringTests.fs @@ -323,3 +323,21 @@ let ``single string with compiler define`` () = """ "#if FOO" """ + +[] +let ``trailing spaces in string should be preserved, 1941`` () = + formatSourceString + false + " +let s = \"\"\"aaaa +bbb\"\"\" +" + config + |> prepend newline + |> should + equal + " +let s = + \"\"\"aaaa +bbb\"\"\" +" \ No newline at end of file diff --git a/src/Fantomas/Context.fs b/src/Fantomas/Context.fs index 46333fec70..676e2ec360 100644 --- a/src/Fantomas/Context.fs +++ b/src/Fantomas/Context.fs @@ -83,11 +83,17 @@ module WriterModel = { m with Indent = max m.Indent m.AtColumn } + let nextLine = String.replicate m.Indent " " + + let currentLine = + String + .Concat(List.head m.Lines, m.WriteBeforeNewline) + .TrimEnd() + + let otherLines = List.tail m.Lines + { m with - Lines = - String.replicate m.Indent " " - :: (List.head m.Lines + m.WriteBeforeNewline) - :: (List.tail m.Lines) + Lines = nextLine :: currentLine :: otherLines WriteBeforeNewline = "" Column = m.Indent } @@ -95,11 +101,17 @@ module WriterModel = match cmd with | WriteLine | WriteLineBecauseOfTrivia -> doNewline m - | WriteLineInsideStringConst - | WriteLineInsideTrivia -> + | WriteLineInsideStringConst -> { m with - Lines = "" :: m.Lines + Lines = String.empty :: m.Lines Column = 0 } + | WriteLineInsideTrivia -> + let lines = + match m.Lines with + | [] -> [ String.empty ] + | h :: tail -> String.empty :: (h.TrimEnd()) :: tail + + { m with Lines = lines; Column = 0 } | Write s -> { m with Lines = (List.head m.Lines + s) :: (List.tail m.Lines) @@ -319,10 +331,13 @@ let internal finalizeWriterModel (ctx: Context) = let internal dump (ctx: Context) = let ctx = finalizeWriterModel ctx - ctx.WriterModel.Lines + match ctx.WriterModel.Lines with + | [] -> [] + | h :: tail -> + // Always trim the last line + h.TrimEnd() :: tail |> List.rev |> List.skipWhile ((=) "") - |> List.map (fun line -> line.TrimEnd()) |> String.concat ctx.Config.EndOfLine.NewLineString let internal dumpAndContinue (ctx: Context) = diff --git a/src/Fantomas/Utils.fs b/src/Fantomas/Utils.fs index 7aa7161ee7..e7b61f1323 100644 --- a/src/Fantomas/Utils.fs +++ b/src/Fantomas/Utils.fs @@ -80,8 +80,6 @@ module String = else b') - - let empty = String.Empty let isNotNullOrEmpty = String.IsNullOrEmpty >> not