Skip to content

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

@Smaug123

Description

@Smaug123

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.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions