Skip to content

Commit

Permalink
Fix 383 (#392)
Browse files Browse the repository at this point in the history
Fixes #383.
  • Loading branch information
nojaf committed Jan 17, 2019
1 parent 44a4bfc commit e7ac8f8
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 3 deletions.
46 changes: 45 additions & 1 deletion src/Fantomas.Tests/PatternMatchingTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -337,7 +337,6 @@ let ``should support rational powers on units of measures``() =
type X = cm^(1/2) / W
"""

[<Test>]
let ``should add each case on newline`` () =
formatSourceString false """
let (|OneLine|MultiLine|) b =
Expand Down Expand Up @@ -384,4 +383,49 @@ let (|OneLinerBinding|MultilineBinding|) b =
| ExplicitCtor([], PreXmlDoc [||], _, _, OneLinerExpr _, _) ->
OneLinerBinding b
| _ -> MultilineBinding b
"""

[<Test>]
let ``should split constructor and function call correctly, double formatting`` () =
let config80 = { config with PageWidth = 80 }

let original = """
let update msg model =
let res =
match msg with
| AMessage -> { model with AFieldWithAVeryVeryVeryLooooooongName = 10 }.RecalculateTotal()
| AnotherMessage -> model
res
"""

let afterFirstFormat = formatSourceString false original config80

formatSourceString false afterFirstFormat config80
|> prepend newline
|> should equal """
let update msg model =
let res =
match msg with
| AMessage ->
{ model with AFieldWithAVeryVeryVeryLooooooongName = 10 }
.RecalculateTotal()
| AnotherMessage -> model
res
"""

[<Test>]
let ``updated record with function call should be on newline, even though short`` () =
formatSourceString false """
let x = { Value = 36 }.Times(9)
match b with
| _ -> { Value = 42 }.Times(8)
""" config
|> prepend newline
|> should equal """
let x = { Value = 36 }.Times(9)
match b with
| _ ->
{ Value = 42 }.Times(8)
"""
9 changes: 8 additions & 1 deletion src/Fantomas/CodePrinter.fs
Original file line number Diff line number Diff line change
Expand Up @@ -1098,7 +1098,14 @@ and genInterfaceImpl astContext (InterfaceImpl(t, bs, range)) =

and genClause astContext hasBar (Clause(p, e, eo)) =
ifElse hasBar sepBar sepNone +> genPat astContext p
+> optPre (!- " when ") sepNone eo (genExpr astContext) +> sepArrow +> preserveBreakNln astContext e
+> optPre (!- " when ") sepNone eo (genExpr astContext) +> sepArrow +> (fun ctx ->
let alreadyMultiline = checkPreserveBreakForExpr e ctx
match alreadyMultiline, e with
| false, SynExpr.App(_,_,SynExpr.DotGet(SynExpr.Record(_, copyInfo,recordFields,_), _, LongIdentWithDots(lid), range),_,_) ->
(breakNln astContext true e) ctx
| _ ->
(breakNln astContext alreadyMultiline e) ctx
)

/// Each multiline member definition has a pre and post new line.
and genMemberDefnList astContext (*(interfaceRange:Microsoft.FSharp.Compiler.Range.range)*) = function
Expand Down
3 changes: 2 additions & 1 deletion src/Fantomas/SourceTransformer.fs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,8 @@ let rec multiline synExpr =
| Tuple es ->
List.exists multiline es

| App(e, (ConstExpr c :: _)) -> multiline e || isConstMultiline c
| App(e, (ConstExpr c :: _)) ->
multiline e || isConstMultiline c

// An infix app is multiline if it contains at least two new line infix ops
| InfixApps(e, es) ->
Expand Down

0 comments on commit e7ac8f8

Please sign in to comment.