bug-gnu-emacs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

bug#62717: 29.0.60; c-ts-mode does not indent the first line in a functi


From: Dmitry Gutov
Subject: bug#62717: 29.0.60; c-ts-mode does not indent the first line in a function after RET
Date: Sat, 8 Apr 2023 04:57:53 +0300
User-agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101 Thunderbird/102.9.0

On 07/04/2023 22:48, Daniel Martín via Bug reports for GNU Emacs, the Swiss army knife of text editors wrote:
emacs -Q
C-x b sample.c RET
M-x c-ts-mode RET

int main() {

If I press RET at the end of the line, the point is not indented.

The Tree-sitter tree for the code is

(translation_unit
  (function_definition type: (primitive_type)
   declarator:
    (function_declarator declarator: (identifier)
     parameters: (parameter_list ( )))
   body: (compound_statement { })))

If I insert a closing bracket, that is:

int main() {
}

The newline indents correctly.  The Tree-sitter tree is now

(function_definition type: (primitive_type)
  declarator:
   (function_declarator declarator: (identifier)
    parameters: (parameter_list ( )))
  body: (compound_statement { }))

It seems that Tree-sitter parses both code snippets without errors and
Emacs should automatically indent after RET in the first case.

I've looked at what nvim-treesitter does for indentation, and at least one of the steps looks like this:

https://github.com/nvim-treesitter/nvim-treesitter/blob/584ccea56e2d37b31ba292da2b539e1a4bb411ca/lua/nvim-treesitter/indent.lua#L129-L134

If the current line is empty, look at the end of the previous line and compute based on the node there.

I'm not sure how this meshes with the fact that tree-sitter inserts a "virtual" closer node at the end of the previous line, but the approach is worth examining.

Daniel, you posted about testing nvim-treesitter with several scenarios. Does it do the right thing with this one?





reply via email to

[Prev in Thread] Current Thread [Next in Thread]