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

invalid F# code when pattern matching on exception with long when guard and 2 space indentation #2784

Closed
1 of 3 tasks
theimowski opened this issue Mar 2, 2023 · 3 comments · Fixed by #2785
Closed
1 of 3 tasks

Comments

@theimowski
Copy link
Member

Issue created from fantomas-online

Code

type A () =
  let b () =
    task {
      if true then
        try
          c ()
        with
        | :? WebSocketException as e when e.WebSocketErrorCode = WebSocketError.ConnectionClosedPrematurely && sourceParty = Agent ->
          ()
    }

Result

type A() =
  let b () =
    task {
      if true then
        try
          c ()
        with :? WebSocketException as e when
          e.WebSocketErrorCode = WebSocketError.ConnectionClosedPrematurely
          && sourceParty = Agent ->
          ()
    }

Problem description

(8,10) (8, 11)Error 10 parse
Incomplete structured construct at or before this point in expression

Extra information

  • The formatted result breaks my code.
  • The formatted result gives compiler warnings.
  • I or my company would be willing to help fix this.

Options

Fantomas v5.2 branch at 1/1/1990

    { config with
                IndentSize = 2 }

Did you know that you can ignore files when formatting from fantomas-tool or the FAKE targets by using a .fantomasignore file?

@nojaf
Copy link
Contributor

nojaf commented Mar 2, 2023

Siema! Thank you for reporting this issue.
This looks similar to #2526.

This is an issue rather specific to the indent_size being 2.

I think you can solve this by changing:

+> optSingle
(fun e -> !- " when" +> sepSpaceOrIndentAndNlnIfExpressionExceedsPageWidth (genExpr e))
clauseNode.WhenExpr

into something that along the lines of:

            +> optSingle
                (fun e ->
                    !- " when"
                    +> expressionFitsOnRestOfLine (sepSpace +> genExpr e) (fun ctx ->
                        // See https://github.com/fsprojects/fantomas/issues/2784
                        let doubleIndent = ctx.Config.IndentSize < 4

                        (indent
                         +> onlyIf doubleIndent indent
                         +> sepNln
                         +> genExpr e
                         +> unindent
                         +> onlyIf doubleIndent unindent)
                            ctx))

the idea is to just add a double indent when the indent_size is lower than the default 4 spaces.

Are you interested in submitting a PR for this?

theimowski added a commit to theimowski/fantomas that referenced this issue Mar 4, 2023
…double indent when the indent_size is lower than the default 4 spaces - fixes fsprojects#2784
@theimowski
Copy link
Member Author

Siema!

This is an issue rather specific to the indent_size being 2.

We do need to switch back to the default 4 indent size one day - 2 spaces might make the code more concise, but they encourage you to write more nested stuff and they make it harder to notice un-wanted whitespace fuckups like we had recently: by accident we moved a piece of logic after a while loop to the loop itself 🤦

I think you can solve this by changing

I like it how it takes you more time to guide me how to fix it rather than fixing it yourself 😂 But I mean it - it just emphasises how good OSS maintainer you are by encouraging others to contribute! 👍

Are you interested in submitting a PR for this?

There you go! #2785

Side note: I'd consider adding a "fast-track" contributing section in https://fsprojects.github.io/fantomas/docs/contributors for busy dads: I appreciate all the rules and guidelines there are in order to contribute, but on the other hand if you had something like a "5 steps to fix a bug" section for a new comer, it could bring in more eager contributors :)

@nojaf
Copy link
Contributor

nojaf commented Mar 6, 2023

Cześć!

Thank you for these kind words 🤗. It may seem strange that I give you a near-working solution instead of just fixing it, but the idea is that we inspire you to save yourself. As this is an open-source project, you will always have the highest stakes in seeing your own problem fixed.

I like the idea of a "fast-track" contributing section. We will give it some thought and see if we can come up with something. Great suggestion!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants