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

Try/With with a single clause #1881

Closed
3 tasks
nojaf opened this issue Sep 8, 2021 · 5 comments · Fixed by #2260
Closed
3 tasks

Try/With with a single clause #1881

nojaf opened this issue Sep 8, 2021 · 5 comments · Fixed by #2260

Comments

@nojaf
Copy link
Contributor

nojaf commented Sep 8, 2021

Issue created from fantomas-online

Code

// OK
try
    persistState currentState
with ex ->
    printfn "Something went wrong: %A" ex

// OK
try
    persistState currentState
with :? System.ApplicationException as ex ->
    printfn "Something went wrong: %A" ex

Result

// OK
try
    persistState currentState
with
| ex -> printfn "Something went wrong: %A" ex

// OK
try
    persistState currentState
with
| :? System.ApplicationException as ex -> printfn "Something went wrong: %A" ex

Problem description

The MS F# style guide was updated recently and Fantomas does no longer adhere to it.

To solve this issue, a new setting will have to be introduced to still adhere to the GR style guide.
The guides diverge on this topic.

//cc @Smaug123 @pbiggar

Extra information

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

Options

Fantomas Master at 09/07/2021 15:59:52 - 7d861f4

Default Fantomas configuration

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

@64J0
Copy link

64J0 commented Oct 15, 2021

I want to start contributing, do you have any material to understand the code for newbies @nojaf ?

@nojaf
Copy link
Contributor Author

nojaf commented Oct 16, 2021

Hi there @64J0, thank you for your interest in this.

In general, I'd say our Contribution Guidelines cover a lot of good to know topics. You could start there. As for the code itself, I've made some videos over the years which should help you there.

As for the issue you are trying to solve, this will be quite demanding on and your PR will require a high level of finishing.
First of you should target your PR against the 4.6 branch as it has a stylistic impact on the default settings.
I would start with adding new tests (in ControlStructureTests) with the samples of the guide. Focus on that first just so you know that you get the behaviour right.
The changes in CodePrinter will likely be in

| TryWith (e, cs) ->
atCurrentColumn (
kw TRY !- "try "
+> indent
+> sepNln
+> genExpr astContext e
+> unindent
+> kw WITH !+~ "with"
+> indentOnWith
+> sepNln
+> col sepNln cs (genClause astContext true)
+> unindentOnWith
)

You want to do something different in the case of a single clause.

Now, there is a twist here, [the G-Research style guide has a different opinion on this](https://github.com/G-Research/fsharp-formatting-conventions#formatting-trywith-expressions), so we will need to introduce a new setting to support this as well. Setting names are hard, so perhaps go with `BarBeforeTryWithClause` for now, until further notice. The default should be false, and when set to true it should always put the bar before the clause. Best to create a new unit test file `BarBeforeTryWithClauseTests` to cover this, similar to [BarBeforeDiscriminatedUnionDeclarationTests](https://github.com/fsprojects/fantomas/blob/7451175fc6dfa84bc40ffd5a70f086afc5d04672/src/Fantomas.Tests/BarBeforeDiscriminatedUnionDeclarationTests.fs) for example.

Please read the contribution guidelines carefully as they do contain a lot of pointers (branch name, unit test names, draft PR, ...) and work test-driven (trust me on this).

I hope this helps, let me know if you have any further questions. Good luck!

@Smaug123
Copy link
Contributor

Smaug123 commented Oct 16, 2021

As the one who speaks for the G-Research style guidelines, I really am not very attached to our difference with Microsoft here. Honestly I think I'd be happy for us to change our style guideline, if it makes the Fantomas change substantially simpler; I don't think any of us has a very strong opinion on it.

@nojaf
Copy link
Contributor Author

nojaf commented Oct 17, 2021

Well, that would simplify things a bit. @Smaug123 could we resolve G-Research/fsharp-formatting-conventions#30 with this new information?

@64J0
Copy link

64J0 commented Oct 18, 2021

Thank you very much @nojaf.

nojaf added a commit that referenced this issue May 19, 2022
* Bump Fantomas

* Update style of SynExpr.TryWith with a single clause. Fixes #1881.
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.

3 participants