Skip to content

Smart indentation on separated elements should reuse the prior element's starting base indentation #53548

Description

Consider what I'd call some "ideal" smart indentation for the following code samples:

type Foo =
    | "hello"
    | "world";

{| "indentation": 0 |}
let x = 10,
    y = 2;
{| "indentation": 0 |}
let x = 10,
    y = 2,
    {| "indentation": 4 |}
let obj = {
    x: "hello" +
        "world",
    {| "indentation": 4 |}
}
class C {
    x = "hello" +
        "world";
    {| "indentation": 4 |}
}

I've specified the "expected" indentation in each of these cases; however, TypeScript today keeps indentation are the same level as the prior line which is wrong. This is especially annoying for the multi-line union case.

There is some ambiguity when one uses an ASI-assuming style though. In these cases, I would personally err on the side of expecting the start indentation.

type Foo =
| "hello"
| "world"
{| "indentation": 0 |}


```ts
let x = 10,
    y = 2
{| "indentation": 0 |}
class C {
    x = "hello" +
        "world"
    {| "indentation": 4 |}
}

Then, even for cases where ASI doesn't really apply:

let obj = {
    x: "hello" +
        "world"
    {| "indentation": 4 |}
}

I would say we should pick the start element's base (where x starts), since we'll start doing #52899 eventually.

Activity

  1. DanielRosenwasser commented on Mar 27, 2023

    @DanielRosenwasser
    MemberAuthor

    Maria Solano (@MariaSolOs) had a good suggestion which is that (maybe when there's ambiguity over the previous statement is sufficiently "terminated") we should look for the prior line and check its indentation. If it's "less" than the start indentation of the prior statement (if it exists) then we should reset the indentation to that of the prior statement.

    function foo() {
        let x = 10,
            y = 20
            {| "indentation": 8 }
    }
    function foo() {
        let x = 10,
            y = 20
    // <- notice no indentation and one presses enter
        {| "indentation": 4 }
    }

    That way, a user can choose to un-indent themselves.

  2. MariaSolOs commented on Mar 28, 2023

    @MariaSolOs
    Contributor

    I debugged this a bit and didn't see any code in the smart indenter being used. Is this not the indentation implementation controlling this Daniel Rosenwasser (@DanielRosenwasser)?

    Matt Bierner (@mjbvz) do you know if this indentation behavior is controlled by the "on enter" rules of language-configuration.json?

  3. DanielRosenwasser commented on Mar 28, 2023

    @DanielRosenwasser
    MemberAuthor

    You're right. I didn't realize it, but TSServer doesn't even provide smart indentation. I guess this change is going to be editor specific.

  4. fatcerberus commented on Mar 29, 2023

    @fatcerberus

    Smart indentation

    But what if I want stupid indentation instead

  5. DanielRosenwasser commented on Mar 29, 2023

    @DanielRosenwasser
    MemberAuthor

    Bruce Pascoe (@fatcerberus) you can get that by using any of the examples I provided. 😅

  6. fatcerberus commented on Mar 29, 2023

    @fatcerberus

    On a more serious note, this is definitely something I've noticed in day-to-day. Particularly fun is this case:

    if (a
        && b
        && c)
        {
            // cursor is now here
        } // <-- automatically added

    So now I get to go up a line, press Home twice, Shift+Down three times, Shift+Tab, then finagle the cursor back inside the braces so I can enter my code. It's not a showstopper, but is distracting enough to make me lose my train of thought sometimes.

    Full-fat VS tends to handle this much better than VSCode, in general.

  7. MariaSolOs commented on Mar 30, 2023

    @MariaSolOs
    Contributor
  8. fatcerberus commented on Mar 30, 2023

    @fatcerberus

    Maria Solano (@MariaSolOs) I was under the impression that was handled by the same machinery that this issue is about?

  9. MariaSolOs commented on Mar 30, 2023

    @MariaSolOs
    Contributor

    Bruce Pascoe (@fatcerberus) didn't you say that you only experience that in VS Code?

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Domain: LS: Smart IndentationAutomatic indenting after a newline (not quite formatter/formatting)RescheduledThis issue was previously scheduled to an earlier milestoneSuggestionAn idea for TypeScript

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions