Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Limit the range for triva from bar token #1086

Merged
merged 3 commits into from
Sep 4, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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