From a81361318627b72b61a6ed6d2efd4163730fa593 Mon Sep 17 00:00:00 2001 From: Florian Verdonck Date: Fri, 24 Apr 2020 17:39:54 +0200 Subject: [PATCH] Don't add newline before member definition. Fix #789 (#790) --- ...ineBlockBracketsOnSameColumnRecordTests.fs | 29 +++++++++++++++++++ src/Fantomas.Tests/RecordTests.fs | 26 +++++++++++++++++ src/Fantomas.Tests/UnionTests.fs | 25 ++++++++++++++++ src/Fantomas/CodePrinter.fs | 10 +++---- 4 files changed, 85 insertions(+), 5 deletions(-) diff --git a/src/Fantomas.Tests/MultilineBlockBracketsOnSameColumnRecordTests.fs b/src/Fantomas.Tests/MultilineBlockBracketsOnSameColumnRecordTests.fs index f19206d0df..91b8b2cb3c 100644 --- a/src/Fantomas.Tests/MultilineBlockBracketsOnSameColumnRecordTests.fs +++ b/src/Fantomas.Tests/MultilineBlockBracketsOnSameColumnRecordTests.fs @@ -487,4 +487,33 @@ type MyRecord = } member Score : unit -> int +""" + + +[] +let ``no newline before first multiline member`` () = + formatSourceString false """ +type ShortExpressionInfo = + { MaxWidth: int + StartColumn: int + ConfirmedMultiline: bool } + member x.IsTooLong maxPageWidth currentColumn = + currentColumn - x.StartColumn > x.MaxWidth // expression is not too long according to MaxWidth + || (currentColumn > maxPageWidth) // expression at current position is not going over the page width + member x.Foo() = () +""" ({ config with NewlineBetweenTypeDefinitionAndMembers = false }) + |> prepend newline + |> should equal """ +type ShortExpressionInfo = + { + MaxWidth : int + StartColumn : int + ConfirmedMultiline : bool + } + member x.IsTooLong maxPageWidth currentColumn = + currentColumn + - x.StartColumn > x.MaxWidth // expression is not too long according to MaxWidth + || (currentColumn > maxPageWidth) // expression at current position is not going over the page width + + member x.Foo() = () """ \ No newline at end of file diff --git a/src/Fantomas.Tests/RecordTests.fs b/src/Fantomas.Tests/RecordTests.fs index 9d4b597bef..1a632931e7 100644 --- a/src/Fantomas.Tests/RecordTests.fs +++ b/src/Fantomas.Tests/RecordTests.fs @@ -909,4 +909,30 @@ let ``line comment after short anonymous record instance syntax`` () = Indent = 8 |} // The number of spaces """ config |> should equal """let formatConfig = {| PageWidth = 70; Indent = 8 |} // The number of spaces +""" + +[] +let ``no newline before first multiline member`` () = + formatSourceString false """ +type ShortExpressionInfo = + { MaxWidth: int + StartColumn: int + ConfirmedMultiline: bool } + member x.IsTooLong maxPageWidth currentColumn = + currentColumn - x.StartColumn > x.MaxWidth // expression is not too long according to MaxWidth + || (currentColumn > maxPageWidth) // expression at current position is not going over the page width + member x.Foo() = () +""" config + |> prepend newline + |> should equal """ +type ShortExpressionInfo = + { MaxWidth: int + StartColumn: int + ConfirmedMultiline: bool } + member x.IsTooLong maxPageWidth currentColumn = + currentColumn + - x.StartColumn > x.MaxWidth // expression is not too long according to MaxWidth + || (currentColumn > maxPageWidth) // expression at current position is not going over the page width + + member x.Foo() = () """ \ No newline at end of file diff --git a/src/Fantomas.Tests/UnionTests.fs b/src/Fantomas.Tests/UnionTests.fs index 3931fca011..d24e123f81 100644 --- a/src/Fantomas.Tests/UnionTests.fs +++ b/src/Fantomas.Tests/UnionTests.fs @@ -56,6 +56,31 @@ type Type = | TyLam of Type * Type | TyVar of string | TyCon of string * Type list + override this.ToString() = + match this with + | TyLam (t1, t2) -> sprintf "(%s -> %s)" (t1.ToString()) (t2.ToString()) + | TyVar a -> a + | TyCon (s, ts) -> s +""" + +[] +let ``newline between discriminated unions and members``() = + formatSourceString false """ +type Type + = TyLam of Type * Type + | TyVar of string + | TyCon of string * Type list + with override this.ToString() = + match this with + | TyLam (t1, t2) -> sprintf "(%s -> %s)" (t1.ToString()) (t2.ToString()) + | TyVar a -> a + | TyCon (s, ts) -> s""" ({ config with NewlineBetweenTypeDefinitionAndMembers = true }) + |> prepend newline + |> should equal """ +type Type = + | TyLam of Type * Type + | TyVar of string + | TyCon of string * Type list override this.ToString() = match this with diff --git a/src/Fantomas/CodePrinter.fs b/src/Fantomas/CodePrinter.fs index a56bb5731c..239fa316b2 100644 --- a/src/Fantomas/CodePrinter.fs +++ b/src/Fantomas/CodePrinter.fs @@ -2360,8 +2360,8 @@ and genClause astContext hasBar (Clause(p, e, eo) as node) = |> genTrivia node.Range /// Each multiline member definition has a pre and post new line. -and genMemberDefnList astContext node = - match node with +and genMemberDefnList astContext nodes = + match nodes with | MDOpenL(xs, ys) -> fun ctx -> match ys with @@ -2377,7 +2377,7 @@ and genMemberDefnList astContext node = (genPropertyWithGetSet astContext gs) (sepNlnBeforeMultilineConstruct m.RangeOfBindingSansRhs attrs +> genPropertyWithGetSet astContext gs +> onlyIf (List.isNotEmpty rest) sepNln)) - +> genMemberDefnList astContext rest + +> genMemberDefnList ({ astContext with IsFirstChild = false }) rest | m::rest -> let attrs = getRangesFromAttributesFromSynMemberDefinition m @@ -2386,11 +2386,11 @@ and genMemberDefnList astContext node = +> enterNode m.Range +> (expressionFitsOnRestOfLine (genMemberDefn astContext m) - (sepNlnBeforeMultilineConstruct m.Range attrs + (onlyIfNot astContext.IsFirstChild (sepNlnBeforeMultilineConstruct m.Range attrs) +> genMemberDefn astContext m +> onlyIf (List.isNotEmpty rest) sepNln)) - +> genMemberDefnList astContext rest + +> genMemberDefnList ({ astContext with IsFirstChild = false }) rest | _ -> sepNone