Skip to content

Commit

Permalink
Limit range for arrow token trivia in desugared lambda. Fixes #1082. (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
nojaf authored Sep 4, 2020
1 parent 82a6987 commit 848afb8
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 5 deletions.
29 changes: 29 additions & 0 deletions src/Fantomas.Tests/PatternMatchingTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -711,3 +711,32 @@ let (|AndExpr|_|) =
| ListSplitPick "&&" chooser (e1, e2) -> Some(BoolExpr.And(e1, e2))
| _ -> None
"""

[<Test>]
let ``comment after arrow should not be duplicated, 1082`` () =
formatSourceString false """
List.tryFind(fun { Type = t; Range = r } ->
match t with
| MainNode SynMemberDefn_Member
| MainNode SynMemberSig_Member -> // trying to get AST trivia
RangeHelpers.``range contains`` r rangeOfBindingAndRhs
| Token(MEMBER, _) -> // trying to get token trivia
r.StartLine = rangeOfBindingAndRhs.StartLine
| _ -> false
)
""" config
|> prepend newline
|> should equal """
List.tryFind (fun { Type = t; Range = r } ->
match t with
| MainNode SynMemberDefn_Member
| MainNode SynMemberSig_Member -> // trying to get AST trivia
RangeHelpers.``range contains`` r rangeOfBindingAndRhs
| Token (MEMBER, _) -> // trying to get token trivia
r.StartLine = rangeOfBindingAndRhs.StartLine
| _ -> false)
"""
15 changes: 11 additions & 4 deletions src/Fantomas/CodePrinter.fs
Original file line number Diff line number Diff line change
Expand Up @@ -1312,15 +1312,22 @@ and genExpr astContext synExpr =
| Paren(DesugaredLambda(cps, e)) ->
fun (ctx: Context) ->
let lastLineOnlyContainsParenthesis = lastLineOnlyContains [| ' ';'('|] ctx

let arrowRange =
List.last cps
|> snd
|> fun lastPatRange ->
mkRange "arrow range" lastPatRange.End e.Range.Start

let hasLineCommentAfterArrow =
findTriviaTokenFromName RARROW synExpr.Range ctx
findTriviaTokenFromName RARROW arrowRange ctx
|> Option.isSome

let expr =
sepOpenT
-- "fun "
+> col sepSpace cps (genComplexPats astContext)
+> triviaAfterArrow synExpr.Range
+> col sepSpace cps (fst >> genComplexPats astContext)
+> triviaAfterArrow arrowRange
+> ifElse hasLineCommentAfterArrow (genExpr astContext e)
(ifElse lastLineOnlyContainsParenthesis (autoIndentAndNlnIfExpressionExceedsPageWidth (genExpr astContext e))
(autoNlnIfExpressionExceedsPageWidth (genExpr astContext e)))
Expand All @@ -1329,7 +1336,7 @@ and genExpr astContext synExpr =
expr ctx

| DesugaredLambda(cps, e) ->
!- "fun " +> col sepSpace cps (genComplexPats astContext) +> sepArrow
!- "fun " +> col sepSpace cps (fst >> genComplexPats astContext) +> sepArrow
+> autoIndentAndNlnIfExpressionExceedsPageWidth (genExpr astContext e)
| Paren(Lambda(e, sps)) ->
fun (ctx: Context) ->
Expand Down
7 changes: 6 additions & 1 deletion src/Fantomas/SourceParser.fs
Original file line number Diff line number Diff line change
Expand Up @@ -1098,10 +1098,15 @@ let rec transformPatterns ss = function
List.map loop sps |> ComplexPats
| SPSTyped(sp, t) -> ComplexTyped(transformPatterns ss sp, t)

let getSynSimplePatsRange =
function
| SynSimplePats.SimplePats(_,r)
| SynSimplePats.Typed(_, _,r) -> r

/// Process compiler-generated matches in an appropriate way
let (|DesugaredLambda|_|) = function
| Lambda(DesugaredMatch(ss, e), spss) ->
Some(List.map (transformPatterns ss) spss, e)
Some(List.map (fun sp -> transformPatterns ss sp, getSynSimplePatsRange sp) spss, e)
| _ -> None

// Type definitions
Expand Down

0 comments on commit 848afb8

Please sign in to comment.