Skip to content

Commit

Permalink
Limit the range for triva from bar token (fsprojects#1086)
Browse files Browse the repository at this point in the history
* Limit the range for triva from bar token. Fixes fsprojects#1083

* Removed unused active pattern
  • Loading branch information
nojaf authored Sep 4, 2020
1 parent 848afb8 commit 6dd19cf
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 3 deletions.
31 changes: 31 additions & 0 deletions src/Fantomas.Tests/PatternMatchingTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -740,3 +740,34 @@ List.tryFind (fun { Type = t; Range = r } ->
| _ -> false)
"""

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

0 comments on commit 6dd19cf

Please sign in to comment.