fantomas icon indicating copy to clipboard operation
fantomas copied to clipboard

ExperimentalKeepIndentInBranch should fire when first branch was all on one line

Open Smaug123 opened this issue 2 years ago • 5 comments

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 - 3f293e712175343c2bcab1dfabe35cbb52fe8839

    { 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.

Smaug123 avatar Nov 01 '23 20:11 Smaug123

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.

nojaf avatar Nov 02 '23 07:11 nojaf

Yep, exactly.

Smaug123 avatar Nov 02 '23 10:11 Smaug123

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

nojaf avatar Nov 03 '23 14:11 nojaf

Is this related to #1361?

josh-degraw avatar Nov 12 '23 04:11 josh-degraw