Skip to content

Commit

Permalink
Print leading newlines when formatting a selection.
Browse files Browse the repository at this point in the history
  • Loading branch information
nojaf committed Jun 11, 2022
1 parent 2e6a107 commit 748e658
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 5 deletions.
2 changes: 2 additions & 0 deletions src/Fantomas.Core.Tests/ContextTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ open Fantomas.Core.Tests.TestHelper
open Fantomas.Core.FormatConfig
open Fantomas.Core

let private dump = dump false

[<Test>]
let ``sepSpace should not add an additional space if the line ends with a space`` () =
let expr = !- "let a = " +> sepSpace
Expand Down
4 changes: 2 additions & 2 deletions src/Fantomas.Core.Tests/FormattingSelectionOnlyTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ let x =
|> should
equal
"
try
something ()
with ex ->
Expand Down Expand Up @@ -125,8 +126,7 @@ let a = 9
equal
"""
let a = 9
"""
let a = 9"""

// 10 25 10 82
//[<Test>]
Expand Down
2 changes: 1 addition & 1 deletion src/Fantomas.Core/CodeFormatterImpl.fs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ let formatWith

context
|> genParsedInput ASTContext.Default ast
|> Context.dump
|> Context.dump (Option.isSome selection)

formattedSourceCode

Expand Down
9 changes: 7 additions & 2 deletions src/Fantomas.Core/Context.fs
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,7 @@ let finalizeWriterModel (ctx: Context) =
else
ctx

let dump (ctx: Context) =
let dump (isSelection: bool) (ctx: Context) =
let ctx = finalizeWriterModel ctx

match ctx.WriterModel.Lines with
Expand All @@ -304,7 +304,12 @@ let dump (ctx: Context) =
// Always trim the last line
h.TrimEnd() :: tail
|> List.rev
|> List.skipWhile ((=) "")
|> fun lines ->
// Don't skip leading newlines when formatting a selection.
if isSelection then
lines
else
List.skipWhile ((=) "") lines
|> String.concat ctx.Config.EndOfLine.NewLineString

let dumpAndContinue (ctx: Context) =
Expand Down

0 comments on commit 748e658

Please sign in to comment.