GNU bug report logs - #60602
29.0.60; treesit-simple-indent doesn't work for top-level nodes

Previous Next

Package: emacs;

Reported by: Piotr Trojanek <piotr.trojanek <at> gmail.com>

Date: Fri, 6 Jan 2023 13:42:02 UTC

Severity: normal

Found in version 29.0.60

Fixed in version 30.1

Done: Stefan Kangas <stefankangas <at> gmail.com>

Bug is archived. No further changes may be made.

To add a comment to this bug, you must first unarchive it, by sending
a message to control AT debbugs.gnu.org, with unarchive 60602 in the body.
You can then email your comments to 60602 AT debbugs.gnu.org in the normal way.

Toggle the display of automated, internal messages from the tracker.

View this report as an mbox folder, status mbox, maintainer mbox


Report forwarded to bug-gnu-emacs <at> gnu.org:
bug#60602; Package emacs. (Fri, 06 Jan 2023 13:42:02 GMT) Full text and rfc822 format available.

Acknowledgement sent to Piotr Trojanek <piotr.trojanek <at> gmail.com>:
New bug report received and forwarded. Copy sent to bug-gnu-emacs <at> gnu.org. (Fri, 06 Jan 2023 13:42:02 GMT) Full text and rfc822 format available.

Message #5 received at submit <at> debbugs.gnu.org (full text, mbox):

From: Piotr Trojanek <piotr.trojanek <at> gmail.com>
To: bug-gnu-emacs <at> gnu.org
Subject: 29.0.60; treesit-simple-indent doesn't work for top-level nodes
Date: Fri, 6 Jan 2023 14:40:49 +0100
This problem can be reproduced with a correctly configured c-ts-mode,
which fails to indent the first line of a code like this, i.e. with
extra space at the very beginning of file and cursor positioned at
"main"):

===
   int main (int argc, char *argv[])
{
  return 0;
}
===

The intent of c-ts-mode--indent-styles is clearly to indent this line
to 0th column:

(defun c-ts-mode--indent-styles (mode)
  ...
         `(((parent-is "translation_unit") parent-bol 0)

However, when indenting this line treesit-simple-indent is called with
node=translation_unit and parent=nil. It exits too early and doesn't
consider any indentation rules:

(defun treesit-simple-indent (node parent bol)
  "..."
  (if (null parent)
      (progn (when treesit--indent-verbose
               (message "PARENT is nil, not indenting"))
             (cons nil nil))

Note that other tree-sitter modes have similar indentation rules, e.g.
ruby-ts-mode.el:

   ;; Slam all top level nodes to the left margin
   ((parent-is "program") parent 0)

and same for typescript-ts-mode.el:

  ((parent-is "program") parent-bol 0)

Perhaps the early exit from treesit-simple-indent could be removed, so
the indentation rules can decide how to handle a nil parent.

In GNU Emacs 29.0.60 (build 4, x86_64-pc-linux-gnu, GTK+ Version
 3.24.24, cairo version 1.16.0) of 2023-01-06 built on mobile
Repository revision: 7420b6dcc379617ca9691049c16bfb2d158f9496
Repository branch: emacs-29
Windowing system distributor 'The X.Org Foundation', version 11.0.12011000
System Description: Debian GNU/Linux 11 (bullseye)

Configured using:
 'configure --prefix=/opt/emacs-29 --with-tree-sitter'

Configured features:
ACL CAIRO DBUS FREETYPE GIF GLIB GMP GNUTLS GPM GSETTINGS HARFBUZZ JPEG
JSON LCMS2 LIBOTF LIBSELINUX LIBSYSTEMD LIBXML2 M17N_FLT MODULES NOTIFY
INOTIFY PDUMPER PNG RSVG SECCOMP SOUND THREADS TIFF TOOLKIT_SCROLL_BARS
TREE_SITTER X11 XDBE XIM XINPUT2 XPM GTK3 ZLIB




Information forwarded to bug-gnu-emacs <at> gnu.org:
bug#60602; Package emacs. (Fri, 06 Jan 2023 14:56:02 GMT) Full text and rfc822 format available.

Message #8 received at 60602 <at> debbugs.gnu.org (full text, mbox):

From: Dmitry Gutov <dgutov <at> yandex.ru>
To: Piotr Trojanek <piotr.trojanek <at> gmail.com>, 60602 <at> debbugs.gnu.org
Subject: Re: bug#60602: 29.0.60; treesit-simple-indent doesn't work for
 top-level nodes
Date: Fri, 6 Jan 2023 16:55:07 +0200
On 06/01/2023 15:40, Piotr Trojanek wrote:
> Note that other tree-sitter modes have similar indentation rules, e.g.
> ruby-ts-mode.el:
> 
>     ;; Slam all top level nodes to the left margin
>     ((parent-is "program") parent 0)
> 
> and same for typescript-ts-mode.el:
> 
>    ((parent-is "program") parent-bol 0)

Probably relatedly, in ruby-ts-mode (and probably others), when the 
first child of a block node (e.g. sequence statements) is reindended, 
it's often that its parent is passed to the rules, not that actual 
child. Which makes the indentation rules irregular, forcing us to add 
extra cases.

Note this comment at ruby-ts-mode:635:

           ;; well as statements.  Note that the first statement of a
           ;; body_statement hits the node as "body_statement" and not
           ;; as the assignment, etc.

and the rules below.

Not sure if there is an easy fix for this, however, given that both 
body_statement and its first child start at the same position. Perhaps a 
translation step before the rules are applied? E.g. in case of Ruby 
body_statement or block_body we would translate to the first child node 
first, to then apply the rules to it. Same for 'program', I guess.




Information forwarded to bug-gnu-emacs <at> gnu.org:
bug#60602; Package emacs. (Mon, 09 Jan 2023 03:14:01 GMT) Full text and rfc822 format available.

Message #11 received at 60602 <at> debbugs.gnu.org (full text, mbox):

From: Yuan Fu <casouri <at> gmail.com>
To: piotr.trojanek <at> gmail.com
Cc: 60602 <at> debbugs.gnu.org
Subject: Re: bug#60602: 29.0.60; treesit-simple-indent doesn't work for 
 top-level nodes
Date: Sun, 8 Jan 2023 19:13:38 -0800
Piotr Trojanek <piotr.trojanek <at> gmail.com> writes:

> This problem can be reproduced with a correctly configured c-ts-mode,
> which fails to indent the first line of a code like this, i.e. with
> extra space at the very beginning of file and cursor positioned at
> "main"):
>
> ===
>    int main (int argc, char *argv[])
> {
>   return 0;
> }
> ===
>
> The intent of c-ts-mode--indent-styles is clearly to indent this line
> to 0th column:
>
> (defun c-ts-mode--indent-styles (mode)
>   ...
>          `(((parent-is "translation_unit") parent-bol 0)
>
> However, when indenting this line treesit-simple-indent is called with
> node=translation_unit and parent=nil. It exits too early and doesn't
> consider any indentation rules:
>
> (defun treesit-simple-indent (node parent bol)
>   "..."
>   (if (null parent)
>       (progn (when treesit--indent-verbose
>                (message "PARENT is nil, not indenting"))
>              (cons nil nil))
>
> Note that other tree-sitter modes have similar indentation rules, e.g.
> ruby-ts-mode.el:
>
>    ;; Slam all top level nodes to the left margin
>    ((parent-is "program") parent 0)
>
> and same for typescript-ts-mode.el:
>
>   ((parent-is "program") parent-bol 0)
>
> Perhaps the early exit from treesit-simple-indent could be removed, so
> the indentation rules can decide how to handle a nil parent.

Now NODE is never the root node, so parent is never nil. I believe this
is the best approach.  And personally I think the rule should be

((parent-is "program") point-min 0)

Yuan




Information forwarded to bug-gnu-emacs <at> gnu.org:
bug#60602; Package emacs. (Fri, 20 Jan 2023 21:41:02 GMT) Full text and rfc822 format available.

Message #14 received at 60602 <at> debbugs.gnu.org (full text, mbox):

From: Theodor Thornhill <theo <at> thornhill.no>
To: Yuan Fu <casouri <at> gmail.com>
Cc: Dmitry Gutov <dgutov <at> yandex.ru>, 60602 <at> debbugs.gnu.org,
 piotr.trojanek <at> gmail.com
Subject: Re: bug#60602: 29.0.60; treesit-simple-indent doesn't work for
 top-level nodes
Date: Fri, 20 Jan 2023 22:40:51 +0100
[Message part 1 (text/plain, inline)]
Hi there!

Yuan Fu <casouri <at> gmail.com> writes:

> Piotr Trojanek <piotr.trojanek <at> gmail.com> writes:
>
>> This problem can be reproduced with a correctly configured c-ts-mode,
>> which fails to indent the first line of a code like this, i.e. with
>> extra space at the very beginning of file and cursor positioned at
>> "main"):
>>
>> ===
>>    int main (int argc, char *argv[])
>> {
>>   return 0;
>> }
>> ===
>>
>> The intent of c-ts-mode--indent-styles is clearly to indent this line
>> to 0th column:
>>
>> (defun c-ts-mode--indent-styles (mode)
>>   ...
>>          `(((parent-is "translation_unit") parent-bol 0)
>>
>> However, when indenting this line treesit-simple-indent is called with
>> node=translation_unit and parent=nil. It exits too early and doesn't
>> consider any indentation rules:
>>
>> (defun treesit-simple-indent (node parent bol)
>>   "..."
>>   (if (null parent)
>>       (progn (when treesit--indent-verbose
>>                (message "PARENT is nil, not indenting"))
>>              (cons nil nil))
>>
>> Note that other tree-sitter modes have similar indentation rules, e.g.
>> ruby-ts-mode.el:
>>
>>    ;; Slam all top level nodes to the left margin
>>    ((parent-is "program") parent 0)
>>
>> and same for typescript-ts-mode.el:
>>
>>   ((parent-is "program") parent-bol 0)
>>
>> Perhaps the early exit from treesit-simple-indent could be removed, so
>> the indentation rules can decide how to handle a nil parent.
>
> Now NODE is never the root node, so parent is never nil. I believe this
> is the best approach.  And personally I think the rule should be
>
> ((parent-is "program") point-min 0)
>
> Yuan

You okay with this patch, Piotr and Dmitry?

Theo

[0001-Use-point-min-to-anchor-top-level-constructs-bug-606.patch (text/x-patch, attachment)]

Information forwarded to bug-gnu-emacs <at> gnu.org:
bug#60602; Package emacs. (Fri, 20 Jan 2023 21:46:02 GMT) Full text and rfc822 format available.

Message #17 received at 60602 <at> debbugs.gnu.org (full text, mbox):

From: Dmitry Gutov <dgutov <at> yandex.ru>
To: Theodor Thornhill <theo <at> thornhill.no>, Yuan Fu <casouri <at> gmail.com>
Cc: 60602 <at> debbugs.gnu.org, piotr.trojanek <at> gmail.com
Subject: Re: bug#60602: 29.0.60; treesit-simple-indent doesn't work for
 top-level nodes
Date: Fri, 20 Jan 2023 23:45:36 +0200
On 20/01/2023 23:40, Theodor Thornhill wrote:
> You okay with this patch, Piotr and Dmitry?

LGTM.




Information forwarded to bug-gnu-emacs <at> gnu.org:
bug#60602; Package emacs. (Fri, 20 Jan 2023 21:53:02 GMT) Full text and rfc822 format available.

Message #20 received at 60602 <at> debbugs.gnu.org (full text, mbox):

From: Theodor Thornhill <theo <at> thornhill.no>
To: Dmitry Gutov <dgutov <at> yandex.ru>, Yuan Fu <casouri <at> gmail.com>
Cc: 60602 <at> debbugs.gnu.org, piotr.trojanek <at> gmail.com
Subject: Re: bug#60602: 29.0.60; treesit-simple-indent doesn't work for
 top-level nodes
Date: Fri, 20 Jan 2023 22:52:44 +0100
Dmitry Gutov <dgutov <at> yandex.ru> writes:

> On 20/01/2023 23:40, Theodor Thornhill wrote:
>> You okay with this patch, Piotr and Dmitry?
>
> LGTM.

Ok, installing on emacs-29,

Thanks,
Theo




bug Marked as fixed in versions 30.1. Request was from Theodor Thornhill <theo <at> thornhill.no> to control <at> debbugs.gnu.org. (Fri, 20 Jan 2023 21:59:02 GMT) Full text and rfc822 format available.

Reply sent to Stefan Kangas <stefankangas <at> gmail.com>:
You have taken responsibility. (Wed, 06 Sep 2023 00:10:01 GMT) Full text and rfc822 format available.

Notification sent to Piotr Trojanek <piotr.trojanek <at> gmail.com>:
bug acknowledged by developer. (Wed, 06 Sep 2023 00:10:01 GMT) Full text and rfc822 format available.

Message #27 received at 60602-done <at> debbugs.gnu.org (full text, mbox):

From: Stefan Kangas <stefankangas <at> gmail.com>
To: Theodor Thornhill <theo <at> thornhill.no>
Cc: Yuan Fu <casouri <at> gmail.com>, 60602-done <at> debbugs.gnu.org,
 piotr.trojanek <at> gmail.com, Dmitry Gutov <dgutov <at> yandex.ru>
Subject: Re: bug#60602: 29.0.60; treesit-simple-indent doesn't work for
 top-level nodes
Date: Tue, 5 Sep 2023 17:09:04 -0700
Theodor Thornhill <theo <at> thornhill.no> writes:

> Dmitry Gutov <dgutov <at> yandex.ru> writes:
>
>> On 20/01/2023 23:40, Theodor Thornhill wrote:
>>> You okay with this patch, Piotr and Dmitry?
>>
>> LGTM.
>
> Ok, installing on emacs-29,
>
> Thanks,
> Theo

This seems to have been fixed already.  I'm therefore closing this bug
report.

If this conclusion is incorrect and this is still an issue, please reply
to this email (use "Reply to all" in your email client) and we can
reopen the bug report.




bug archived. Request was from Debbugs Internal Request <help-debbugs <at> gnu.org> to internal_control <at> debbugs.gnu.org. (Wed, 04 Oct 2023 11:24:05 GMT) Full text and rfc822 format available.

This bug report was last modified 204 days ago.

Previous Next


GNU bug tracking system
Copyright (C) 1999 Darren O. Benham, 1997,2003 nCipher Corporation Ltd, 1994-97 Ian Jackson.