diff --git a/src/Fantomas.Tests/PatternMatchingTests.fs b/src/Fantomas.Tests/PatternMatchingTests.fs index f706a6573f..2a6aa18eed 100644 --- a/src/Fantomas.Tests/PatternMatchingTests.fs +++ b/src/Fantomas.Tests/PatternMatchingTests.fs @@ -740,3 +740,34 @@ List.tryFind (fun { Type = t; Range = r } -> | _ -> false) """ + +[] +let ``trivia before pipe should not be repeated for each pipe, 1083`` () = + formatSourceString false """ +Seq.takeWhile + (function + | Write "" + // for example: + // type Foo = + // static member Bar () = ... + | IndentBy _ + | WriteLine + | SetAtColumn _ + | Write " -> " + | CommentOrDefineEvent _ -> true + | _ -> false) +""" config + |> prepend newline + |> should equal """ +Seq.takeWhile (function + | Write "" + // for example: + // type Foo = + // static member Bar () = ... + | IndentBy _ + | WriteLine + | SetAtColumn _ + | Write " -> " + | CommentOrDefineEvent _ -> true + | _ -> false) +""" diff --git a/src/Fantomas/CodePrinter.fs b/src/Fantomas/CodePrinter.fs index faf95c6766..d9d03be95f 100644 --- a/src/Fantomas/CodePrinter.fs +++ b/src/Fantomas/CodePrinter.fs @@ -2980,13 +2980,15 @@ and genClause astContext hasBar (Clause(p, e, eo)) = let arrowRange = mkRange "arrowRange" p.Range.End e.Range.Start let pat = genPat astContext p + let body = optPre (!- " when ") sepNone eo (genExpr astContext) +> sepArrow +> leaveNodeTokenByName arrowRange RARROW +> clauseBody e - genTriviaBeforeClausePipe p.Range +> - ifElse hasBar (sepBar +> atCurrentColumnWithPrepend pat body) (pat +> body) + + genTriviaBeforeClausePipe p.Range + +> ifElse hasBar (sepBar +> atCurrentColumnWithPrepend pat body) (pat +> body) /// Each multiline member definition has a pre and post new line. and genMemberDefnList astContext nodes = @@ -3162,9 +3164,11 @@ and genPat astContext pat = | PatOptionalVal(s) -> !- (sprintf "?%s" s) | PatAttrib(p, ats) -> genOnelinerAttributes astContext ats +> genPat astContext p | PatOr(p1, p2) -> + let barRange = mkRange "bar range" p1.Range.End p2.Range.Start + genPat astContext p1 +> sepNln - +> enterNodeTokenByName pat.Range BAR -- "| " + +> enterNodeTokenByName barRange BAR -- "| " +> genPat astContext p2 | PatAnds(ps) -> col (!- " & ") ps (genPat astContext) | PatNullary PatNull -> !- "null"