tsi.el icon indicating copy to clipboard operation
tsi.el copied to clipboard

Blank lines double-indented after `{{` and `[{`

Open AdamNiederer opened this issue 2 years ago • 1 comments

tsx-mode indents the line immediately following a {{ or [{ twice (all examples captured with tsi-typescript-indent-offset = 2, | representing the cursor):

{{
    |
}}

[{
    |  
}]

Tokens preceding the first { or non-newline spaces between the {s don't appear to change this:

if(0) {{
    |
}}

() => {{
    |
}}

{ {
    |
} }

{{
    |
    |
}}

Non-blank lines are indented as expected:

{{
  0
}}

But any other blank lines in the {{ are still double-indented:

{{
  0
    |
}}

{{
    |
  0
}}

AdamNiederer avatar Apr 16 '22 00:04 AdamNiederer

This is a tricky case. The tsi-typescript--get-indent-for-current-line function has three pieces of context:

  • the type of the current node at point
  • the type of the parent node
  • is the current line blank?

In cases like this:

{{
  | 
  ^ cursor should be here, indent 2 spaces
}}

the context is:

  • current node type: statement_block
  • parent node type: statement_block
  • is current line blank?: yes

However the following has the same context:

{
  {
    |
    ^ cursor should be here, indent 4 spaces
  } 
}

The context is the same as the first example:

  • current node type: statement_block
  • parent node type: statement_block
  • is current line blank?: yes

So if we fix the first case, we break the second case.

One solution might be to add a fourth piece of context:

  • are the parent and current nodes on the same line?

That way we could differentiate between the two cases here. I'll play with this.

I tend not to notice these cases because prettier will indent these scenarios on multiple lines:

[
  {

  }
]

Still both are valid so it's definitely worth fixing this IMHO.

stevemolitor avatar Apr 27 '22 14:04 stevemolitor