Skip to content

Commit

Permalink
Don't trim end of line inside a String. Fixes fsprojects#1941.
Browse files Browse the repository at this point in the history
  • Loading branch information
nojaf committed Jan 8, 2022
1 parent c496ae1 commit bcb9e78
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 10 deletions.
2 changes: 2 additions & 0 deletions .fantomasignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
**/src/Fantomas.Tests/StringTests.fs
**/src/Fantomas.Tests/CommentTests.fs
19 changes: 19 additions & 0 deletions src/Fantomas.Tests/CommentTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -1685,3 +1685,22 @@ with
with
| d -> ()
"""

[<Test>]
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
"""
1 change: 1 addition & 0 deletions src/Fantomas.Tests/ContextTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ let ``don't add space before block comment`` () =
Long comment
*)"""
|> String.normalizeNewLine

let expr =
sepNone +> sepSpace -- comment
Expand Down
18 changes: 18 additions & 0 deletions src/Fantomas.Tests/StringTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -323,3 +323,21 @@ let ``single string with compiler define`` () =
"""
"#if FOO"
"""

[<Test>]
let ``trailing spaces in string should be preserved, 1941`` () =
formatSourceString
false
"
let s = \"\"\"aaaa
bbb\"\"\"
"
config
|> prepend newline
|> should
equal
"
let s =
\"\"\"aaaa
bbb\"\"\"
"
32 changes: 24 additions & 8 deletions src/Fantomas/Context.fs
Original file line number Diff line number Diff line change
Expand Up @@ -83,22 +83,35 @@ 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 }

let updateCmd cmd =
match cmd with
| WriteLine
| WriteLineBecauseOfTrivia -> doNewline m
| WriteLineInsideStringConst
| WriteLineInsideStringConst ->
{ m with
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 = "" :: m.Lines
Lines = lines
Column = 0 }
| Write s ->
{ m with
Expand Down Expand Up @@ -319,10 +332,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) =
Expand Down
2 changes: 0 additions & 2 deletions src/Fantomas/Utils.fs
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,6 @@ module String =
else
b')



let empty = String.Empty

let isNotNullOrEmpty = String.IsNullOrEmpty >> not
Expand Down

0 comments on commit bcb9e78

Please sign in to comment.