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

ExperimentalKeepIndentInBranch should fire when first branch was all on one line #2973

Open
4 tasks
Smaug123 opened this issue Nov 1, 2023 · 5 comments
Open
4 tasks

Comments

@Smaug123
Copy link
Contributor

Smaug123 commented Nov 1, 2023

Issue created from fantomas-online

Code

module Program =
    let main _ =
        if false then 1 else
        printfn "hi!"
        0

Result

module Program =
    let main _ =
        if false then
            1
        else
            printfn "hi!"
            0

Desired result

module Program =
    let main _ =
        if false then
            1
        else
        printfn "hi!"
        0

Problem description

This is a completely new request which we've never asked you for before, not a bug. (In general the ExperimentalKeepIndentInBranch feature works great nowadays, and I now instinctively expect it to do the right thing!)

If ExperimentalKeepIndentInBranch, it would be great if we could retain the indentation when there's a one-line "deal with some particular case" that we've reformatted onto multiple lines. I think this new pattern should only fire if the format is like this:

if blah then baz else
more stuff at the same level of indentation as the initial `if`

That is, the following reformatting shouldn't take place:

if blah then baz else foo

//

if blah then
    baz
else
foo // note the lack of indent, which here would be incorrect

Rationale: if I've "clearly been trying" to keep the main body of my else block unindented, by explicitly putting it at the same level of indentation as the if keyword, then I think we shouldn't break that attempt merely because we reformatted the body of the if branch.

The actual code in the wild which triggered me to write this report is:

async {
    if not (proc.Start ()) then return Error "failed to start" else
    use stdout = proc.StandardOutput
    let! ct = Async.CancellationToken
    ...
}

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.
  • I would like a release if this problem is solved.

Options

Fantomas main branch at 2023-10-30T10:39:28Z - 3f293e7

    { config with
                ExperimentalKeepIndentInBranch = true }

Did you know that you can ignore files when formatting by using a .fantomasignore file?
PS: It's unlikely that someone else will solve your specific issue, as it's something that you have a personal stake in.

@nojaf
Copy link
Contributor

nojaf commented Nov 2, 2023

Hi Patrick,

I've encountered this myself as well. I see your point.
The additional trigger should be the elseBranch matching the indentation of the if keyword right?
I'll take a look at this.

@Smaug123
Copy link
Contributor Author

Smaug123 commented Nov 2, 2023

Yep, exactly.

@nojaf
Copy link
Contributor

nojaf commented Nov 3, 2023

I released a new preview version with this: https://www.nuget.org/packages/fantomas/6.3.0-alpha-001

@josh-degraw
Copy link
Contributor

Is this related to #1361?

@nojaf
Copy link
Contributor

nojaf commented Nov 12, 2023

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

No branches or pull requests

3 participants