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

Comment removed in multi-case pattern matching #813

Closed
theimowski opened this issue May 8, 2020 · 6 comments · Fixed by #834
Closed

Comment removed in multi-case pattern matching #813

theimowski opened this issue May 8, 2020 · 6 comments · Fixed by #834
Labels
good first issue Long hanging fruit: easy issue to get your feet wet!

Comments

@theimowski
Copy link
Member

Issue created from fantomas-online

When there's a comment between two pattern matching cases that end in some branch, it gets removed

Code

let f (a: A) (b: B) =
    match a, b with
    | A, B
    // this comment is lost
    | C, D -> Some ()
    | _ -> None

Result

let f (a: A) (b: B) =
    match a, b with
    | A, B
    | C, D -> Some()
    | _ -> None

Options

Fantomas Next - 4.0.0-alpha-001-1/1/1990

Name Value
IndentSpaceNum 4
PageWidth 120
SemicolonAtEndOfLine false
SpaceBeforeParameter true
SpaceBeforeLowercaseInvocation true
SpaceBeforeUppercaseInvocation false
SpaceBeforeClassConstructor false
SpaceBeforeMember false
SpaceBeforeColon false
SpaceAfterComma true
SpaceBeforeSemicolon false
SpaceAfterSemicolon true
IndentOnTryWith false
SpaceAroundDelimiter true
MaxIfThenElseShortWidth 40
MaxInfixOperatorExpression 50
MaxRecordWidth 40
MaxArrayOrListWidth 40
MaxLetBindingWidth 40
MultilineBlockBracketsOnSameColumn false
NewlineBetweenTypeDefinitionAndMembers false
StrictMode false
@nojaf
Copy link
Contributor

nojaf commented May 8, 2020

The found comment is assigned to the | token before C, D.
image
image

And no trivia is generated around PatOr.

Most likely enterNodeTokenByName before the pipe might do the trick.

Check out this video to learn more about trivia.

@nojaf nojaf added the good first issue Long hanging fruit: easy issue to get your feet wet! label May 8, 2020
@kontomondo
Copy link
Contributor

kontomondo commented May 14, 2020

@nojaf Thank you for this project! This is my first time seeing this codebase, but I figured I could try tackling an issue, I had a test added in CommentTest.fs:

[<Test>]
let ``comments in pattern matching cases should not be removed, 813``() =
    formatSourceString false """
let f (a: A) =
    match a with
    // comment for case A
    | A
    // comment for case C
    | C -> Some()
    | D -> Another() // inline comment
    // comment for case _
    | _ -> None""" config
    |> prepend newline
    |> should equal """
let f (a: A) =
    match a with
    // comment for case A
    | A
    // comment for case C
    | C -> Some()
    | D -> Another() // inline comment
    // comment for case _
    | _ -> None
"""

and added enterNodeTokenByName as you suggested, however I hit an issue with an extra new line being copied after "comment for case C"

    | PatOr(p1, p2) ->
        genPat astContext p1
        +> enterNodeTokenByName pat.Range "BAR"
        +> sepNln -- "| "
        +> genPat astContext p2

so I went with sepNone instead of sepNln

    | PatOr(p1, p2) ->
        genPat astContext p1
        +> enterNodeTokenByName pat.Range "BAR"
        +> sepNone -- "| "
        +> genPat astContext p2

but that may introduce its own issue I figure?

@nojaf
Copy link
Contributor

nojaf commented May 15, 2020

Hello @kontomondo, thanks for your kind words.
It is hard to say what the impact is of changing sepNln to sepNone, I would think that you might need to make a decision based on the trivia of the BAR token to choose one of the other.

Could you maybe make a (draft) PR with this code? Then I can easily check it out and we can see if all the other tests still pass.

@kontomondo
Copy link
Contributor

@nojaf Thank you for the reply! I've opened #834 draft PR

@kontomondo
Copy link
Contributor

@nojaf this issue should be closed since the PR is merged.

@nojaf nojaf linked a pull request May 18, 2020 that will close this issue
@nojaf
Copy link
Contributor

nojaf commented May 18, 2020

It seems like the linking of the PR with the issue didn't happen alright.

@nojaf nojaf closed this as completed May 18, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
good first issue Long hanging fruit: easy issue to get your feet wet!
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants