Subject: bug#72184: 31.0.50; typescript-ts-mode does not bind RET to newline-and-indent

From: Jostein Kjønigsen <[email-masked]>
Date: Wed, 31 Jul 2024 10:55:00 +0200

> On 27 Jul 2024, at 09:21, Eli Zaretskii <[email-masked]> wrote:
> 
>> Cc: [email-masked]
>> Date: Sat, 20 Jul 2024 03:22:47 +0300
>> From: Dmitry Gutov <[email-masked]>
>> 
>> Right, we normally handle automatic indentation using 
>> electric-indent-mode (which is on by default).
>> 
>> If RET behaves incorrectly in typescript-ts-mode somehow, more details 
>> should be provided.
> 
> Jostein, could you please respond, so that we could make progress
> here?
> 
> Thanks.

PREFACE

I've now looked further into this issue, and what we're actually seeing here is not indentation code "not working", but indentation rules not taking into account incomplete parse-trees while code is being written.

So as the original bug-submitter, I will have to just declare that the original bug-description is clearly inaccurate. Sorry about that!

Below is my attempt to better express what is actually failing and why.

My hypothesis is that this could be an issue for other tree-sitter based major-modes as well, and maintainers of those modes are encouraged to check for similar issues.

If more major-modes than typescript-ts-mode is affected, the optimum solution may best be decided together, rather than individually per mode.

AS FOR THE BUG - DETAILS

Consider the following pre-existing typescript/javascript function:

function stuff() {
  // TODO implement
}

Using the typescript tree-sitter grammer, it gets the following nodes in its tree-sitter AST:

 (function_declaration function name: (identifier)
  parameters: (formal_parameters ( ))
  body: (statement_block { (comment) }))
 
This matches the rules for indentation in typescript-ts-mode and indents correctly regardless.

But in a bare config with only electric-indent-mode enabled (and not electric-pair-mode), trying to enter a new function will produce a incomplete syntax tree, due to the missing final brace.

The code will look like this:

function gnu2() {  // NOTE: no closing brace!

And the tree-sitter AST will then look like this until completed:

 (expression_statement
  (function function name: (identifier)
   parameters: (formal_parameters ( ))
   body: (statement_block { (comment) }))))

And typescript-ts-mode has no indentation rules for this AST. Thus pressing enter wont cause indentation, despite electric-indent-mode being enabled.

I suspect this sort of issue could possibly affect other treesitter based modes which has been developed mostly in existing configs with "all the bells and whistles" (like electric-pair-mode) enabled.

I'll hold off any fixes or suggestions for such until we have some reports from other maintainers, if that is ok?

--
Jostein