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

Don't trim end of line inside a String. #2010

Merged
merged 1 commit into from
Jan 8, 2022
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
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\"\"\"
"
33 changes: 24 additions & 9 deletions src/Fantomas/Context.fs
Original file line number Diff line number Diff line change
Expand Up @@ -83,23 +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
| 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)
Expand Down Expand Up @@ -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) =
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