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 range for arrow token trivia in desugared lambda #1085

Merged
merged 1 commit 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
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