Skip to content

Commit

Permalink
Don't add newline before member definition. Fix #789 (#790)
Browse files Browse the repository at this point in the history
  • Loading branch information
nojaf committed Apr 24, 2020
1 parent 436bac1 commit a813613
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -487,4 +487,33 @@ type MyRecord =
}
member Score : unit -> int
"""


[<Test>]
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() = ()
"""
26 changes: 26 additions & 0 deletions src/Fantomas.Tests/RecordTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -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
"""

[<Test>]
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() = ()
"""
25 changes: 25 additions & 0 deletions src/Fantomas.Tests/UnionTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -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
"""

[<Test>]
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
Expand Down
10 changes: 5 additions & 5 deletions src/Fantomas/CodePrinter.fs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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

Expand Down

0 comments on commit a813613

Please sign in to comment.