Skip to content

Commit

Permalink
No newline before lambda unless really needed. Fix 503 (#529)
Browse files Browse the repository at this point in the history
* Tests

* autoNlnByFutureLazy for App(Lambda). Fix #503
  • Loading branch information
jindraivanek authored Oct 21, 2019
1 parent 0e4e6b6 commit 3d6fc2a
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 8 deletions.
22 changes: 22 additions & 0 deletions src/Fantomas.Tests/AppTests.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
module Fantomas.Tests.AppTests

open NUnit.Framework
open FsUnit
open Fantomas.Tests.TestHelper

// the current behavior results in a compile error since the |> is merged to the last line
[<Test>]
let ``no nln before lambda #503``() =
formatSourceString false """
let a =
b
|> List.exists (fun p ->
p.a && p.b |> List.exists (fun o -> o.a = "lorem ipsum dolor sit amet"))
""" { config with PageWidth = 80 }
|> prepend newline
|> should equal """
let a =
b
|> List.exists (fun p ->
p.a && p.b |> List.exists (fun o -> o.a = "lorem ipsum dolor sit amet"))
"""
3 changes: 2 additions & 1 deletion src/Fantomas.Tests/Fantomas.Tests.fsproj
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">
<Import Project="..\netfx.props" />
<PropertyGroup>
<Version>3.0.0</Version>
Expand All @@ -20,6 +20,7 @@
<Compile Include="ComparisonTests.fs" />
<Compile Include="ControlStructureTests.fs" />
<Compile Include="PipingTests.fs" />
<Compile Include="AppTests.fs" />
<Compile Include="ModuleTests.fs" />
<Compile Include="UnionTests.fs" />
<Compile Include="RecordTests.fs" />
Expand Down
3 changes: 1 addition & 2 deletions src/Fantomas.Tests/PatternMatchingTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -201,8 +201,7 @@ with
|> should equal """
try
fst
(find
(fun (s, (s', ty): int * int) ->
(find (fun (s, (s', ty): int * int) ->
s' = s0 && can (type_match ty ty0) []) (!the_interface))
with Failure _ -> s0
"""
Expand Down
4 changes: 2 additions & 2 deletions src/Fantomas/CodePrinter.fs
Original file line number Diff line number Diff line change
Expand Up @@ -730,10 +730,10 @@ and genExpr astContext synExpr =
let appNlnFun e =
match e with
| CompExpr _
| Lambda _
| MatchLambda _
| Paren (Lambda _)
| Paren (MatchLambda _) -> autoNln
| Lambda _
| Paren (Lambda _) -> autoNlnByFutureLazy
| _ -> autoNlnByFuture

let kw tokenName f = tokN synExpr.Range tokenName f
Expand Down
15 changes: 12 additions & 3 deletions src/Fantomas/Context.fs
Original file line number Diff line number Diff line change
Expand Up @@ -399,7 +399,7 @@ let internal autoNlnCheck f sep (ctx : Context) =
col > ctx.Config.PageWidth

let internal futureNlnCheckMem = Cache.memoizeBy (fun (f, ctx : Context) -> Cache.LambdaEqByRef f, ctx.MemoizeProjection) <| fun (f, ctx) ->
if ctx.Writer.IsDummy || not ctx.BreakLines then false else
if ctx.Writer.IsDummy || not ctx.BreakLines then (false, false) else
// Create a dummy context to evaluate length of current operation
use colWriter = new ColumnIndentedTextWriter(new StringWriter(), isDummy = true)
let dummyCtx = ctx.With(colWriter, true)
Expand All @@ -410,13 +410,22 @@ let internal futureNlnCheckMem = Cache.memoizeBy (fun (f, ctx : Context) -> Cach
|> Seq.indexed |> Seq.filter (fun (i, _) -> i % 2 = 0) |> Seq.map snd |> String.concat System.String.Empty
let lines = withoutStringConst.Split([|Environment.NewLine|], StringSplitOptions.None)

(lines |> Seq.length) >= 2 || writer.Column > ctx.Config.PageWidth
(lines |> Seq.length) >= 2, writer.Column > ctx.Config.PageWidth

let internal futureNlnCheck f (ctx : Context) = futureNlnCheckMem (f, ctx)
let internal futureNlnCheck f (ctx : Context) =
let (isMultiLine, isLong) = futureNlnCheckMem (f, ctx)
isMultiLine || isLong

let internal futureNlnCheckLazy f (ctx : Context) =
let (isMultiLine, isLong) = futureNlnCheckMem (f, ctx)
if isMultiLine then false else isLong

let internal autoNlnByFuture f = ifElseCtx (futureNlnCheck f) (sepNln +> f) f
let internal autoIndentNlnByFuture f = ifElseCtx (futureNlnCheck f) (indent +> sepNln +> f +> unindent) f

/// like autoNlnByFuture but don't do nln if there is another nln inside f
let internal autoNlnByFutureLazy f = ifElseCtx (futureNlnCheckLazy f) (sepNln +> f) f

/// Set a checkpoint to break at an appropriate column
let internal autoNlnOrAddSep f sep (ctx : Context) =
let isNln = autoNlnCheck f sep ctx
Expand Down

0 comments on commit 3d6fc2a

Please sign in to comment.