unofficial mirror of bug-gnu-emacs@gnu.org 
 help / color / mirror / code / Atom feed
* bug#60602: 29.0.60; treesit-simple-indent doesn't work for top-level nodes
@ 2023-01-06 13:40 Piotr Trojanek
  2023-01-06 14:55 ` Dmitry Gutov
  2023-01-09  3:13 ` Yuan Fu
  0 siblings, 2 replies; 7+ messages in thread
From: Piotr Trojanek @ 2023-01-06 13:40 UTC (permalink / raw)
  To: 60602

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





^ permalink raw reply	[flat|nested] 7+ messages in thread

* bug#60602: 29.0.60; treesit-simple-indent doesn't work for top-level nodes
  2023-01-06 13:40 bug#60602: 29.0.60; treesit-simple-indent doesn't work for top-level nodes Piotr Trojanek
@ 2023-01-06 14:55 ` Dmitry Gutov
  2023-01-09  3:13 ` Yuan Fu
  1 sibling, 0 replies; 7+ messages in thread
From: Dmitry Gutov @ 2023-01-06 14:55 UTC (permalink / raw)
  To: Piotr Trojanek, 60602

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.





^ permalink raw reply	[flat|nested] 7+ messages in thread

* bug#60602: 29.0.60; treesit-simple-indent doesn't work for  top-level nodes
  2023-01-06 13:40 bug#60602: 29.0.60; treesit-simple-indent doesn't work for top-level nodes Piotr Trojanek
  2023-01-06 14:55 ` Dmitry Gutov
@ 2023-01-09  3:13 ` Yuan Fu
  2023-01-20 21:40   ` Theodor Thornhill via Bug reports for GNU Emacs, the Swiss army knife of text editors
  1 sibling, 1 reply; 7+ messages in thread
From: Yuan Fu @ 2023-01-09  3:13 UTC (permalink / raw)
  To: piotr.trojanek; +Cc: 60602


Piotr Trojanek <piotr.trojanek@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





^ permalink raw reply	[flat|nested] 7+ messages in thread

* bug#60602: 29.0.60; treesit-simple-indent doesn't work for top-level nodes
  2023-01-09  3:13 ` Yuan Fu
@ 2023-01-20 21:40   ` Theodor Thornhill via Bug reports for GNU Emacs, the Swiss army knife of text editors
  2023-01-20 21:45     ` Dmitry Gutov
  0 siblings, 1 reply; 7+ messages in thread
From: Theodor Thornhill via Bug reports for GNU Emacs, the Swiss army knife of text editors @ 2023-01-20 21:40 UTC (permalink / raw)
  To: Yuan Fu; +Cc: Dmitry Gutov, 60602, piotr.trojanek

[-- Attachment #1: Type: text/plain, Size: 1692 bytes --]


Hi there!

Yuan Fu <casouri@gmail.com> writes:

> Piotr Trojanek <piotr.trojanek@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


[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: 0001-Use-point-min-to-anchor-top-level-constructs-bug-606.patch --]
[-- Type: text/x-patch, Size: 2894 bytes --]

From ab13b78f02452e8e1be633593a6792d9f8b6050c Mon Sep 17 00:00:00 2001
From: Theodor Thornhill <theo@thornhill.no>
Date: Fri, 20 Jan 2023 22:37:47 +0100
Subject: [PATCH] Use point-min to anchor top-level constructs (bug#60602)

* lisp/progmodes/c-ts-mode.el (c-ts-mode--indent-styles): New anchor.
* lisp/progmodes/java-ts-mode.el (java-ts-mode--indent-rules): New
anchor.
* lisp/progmodes/ruby-ts-mode.el (ruby-ts--indent-rules): New anchor.
* lisp/progmodes/typescript-ts-mode.el
(typescript-ts-mode--indent-rules): New anchor.
---
 lisp/progmodes/c-ts-mode.el          | 2 +-
 lisp/progmodes/java-ts-mode.el       | 2 +-
 lisp/progmodes/ruby-ts-mode.el       | 2 +-
 lisp/progmodes/typescript-ts-mode.el | 2 +-
 4 files changed, 4 insertions(+), 4 deletions(-)

diff --git a/lisp/progmodes/c-ts-mode.el b/lisp/progmodes/c-ts-mode.el
index 348d027af1..2370a2c964 100644
--- a/lisp/progmodes/c-ts-mode.el
+++ b/lisp/progmodes/c-ts-mode.el
@@ -115,7 +115,7 @@ c-ts-mode--indent-styles
   "Indent rules supported by `c-ts-mode'.
 MODE is either `c' or `cpp'."
   (let ((common
-         `(((parent-is "translation_unit") parent-bol 0)
+         `(((parent-is "translation_unit") point-min 0)
            ((node-is ")") parent 1)
            ((node-is "]") parent-bol 0)
            ((node-is "else") parent-bol 0)
diff --git a/lisp/progmodes/java-ts-mode.el b/lisp/progmodes/java-ts-mode.el
index 03093e0980..6bcf5d1253 100644
--- a/lisp/progmodes/java-ts-mode.el
+++ b/lisp/progmodes/java-ts-mode.el
@@ -69,7 +69,7 @@ java-ts-mode--syntax-table
 
 (defvar java-ts-mode--indent-rules
   `((java
-     ((parent-is "program") parent-bol 0)
+     ((parent-is "program") point-min 0)
      ((node-is "}") (and parent parent-bol) 0)
      ((node-is ")") parent-bol 0)
      ((node-is "]") parent-bol 0)
diff --git a/lisp/progmodes/ruby-ts-mode.el b/lisp/progmodes/ruby-ts-mode.el
index f0337775d5..809f0da2a0 100644
--- a/lisp/progmodes/ruby-ts-mode.el
+++ b/lisp/progmodes/ruby-ts-mode.el
@@ -535,7 +535,7 @@ ruby-ts--indent-rules
   (let ((common
          `(
            ;; Slam all top level nodes to the left margin
-           ((parent-is "program") parent 0)
+           ((parent-is "program") point-min 0)
 
            ;; Do not indent here docs or the end.  Not sure why it
            ;; takes the grand-parent but ok fine.
diff --git a/lisp/progmodes/typescript-ts-mode.el b/lisp/progmodes/typescript-ts-mode.el
index f7bf7ed7e4..c0c18d65cc 100644
--- a/lisp/progmodes/typescript-ts-mode.el
+++ b/lisp/progmodes/typescript-ts-mode.el
@@ -69,7 +69,7 @@ typescript-ts-mode--indent-rules
   "Rules used for indentation.
 Argument LANGUAGE is either `typescript' or `tsx'."
   `((,language
-     ((parent-is "program") parent-bol 0)
+     ((parent-is "program") point-min 0)
      ((node-is "}") parent-bol 0)
      ((node-is ")") parent-bol 0)
      ((node-is "]") parent-bol 0)
-- 
2.34.1


^ permalink raw reply related	[flat|nested] 7+ messages in thread

* bug#60602: 29.0.60; treesit-simple-indent doesn't work for top-level nodes
  2023-01-20 21:40   ` Theodor Thornhill via Bug reports for GNU Emacs, the Swiss army knife of text editors
@ 2023-01-20 21:45     ` Dmitry Gutov
  2023-01-20 21:52       ` Theodor Thornhill via Bug reports for GNU Emacs, the Swiss army knife of text editors
  0 siblings, 1 reply; 7+ messages in thread
From: Dmitry Gutov @ 2023-01-20 21:45 UTC (permalink / raw)
  To: Theodor Thornhill, Yuan Fu; +Cc: 60602, piotr.trojanek

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

LGTM.





^ permalink raw reply	[flat|nested] 7+ messages in thread

* bug#60602: 29.0.60; treesit-simple-indent doesn't work for top-level nodes
  2023-01-20 21:45     ` Dmitry Gutov
@ 2023-01-20 21:52       ` Theodor Thornhill via Bug reports for GNU Emacs, the Swiss army knife of text editors
  2023-09-06  0:09         ` Stefan Kangas
  0 siblings, 1 reply; 7+ messages in thread
From: Theodor Thornhill via Bug reports for GNU Emacs, the Swiss army knife of text editors @ 2023-01-20 21:52 UTC (permalink / raw)
  To: Dmitry Gutov, Yuan Fu; +Cc: 60602, piotr.trojanek

Dmitry Gutov <dgutov@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





^ permalink raw reply	[flat|nested] 7+ messages in thread

* bug#60602: 29.0.60; treesit-simple-indent doesn't work for top-level nodes
  2023-01-20 21:52       ` Theodor Thornhill via Bug reports for GNU Emacs, the Swiss army knife of text editors
@ 2023-09-06  0:09         ` Stefan Kangas
  0 siblings, 0 replies; 7+ messages in thread
From: Stefan Kangas @ 2023-09-06  0:09 UTC (permalink / raw)
  To: Theodor Thornhill; +Cc: Yuan Fu, 60602-done, piotr.trojanek, Dmitry Gutov

Theodor Thornhill <theo@thornhill.no> writes:

> Dmitry Gutov <dgutov@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.





^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2023-09-06  0:09 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-01-06 13:40 bug#60602: 29.0.60; treesit-simple-indent doesn't work for top-level nodes Piotr Trojanek
2023-01-06 14:55 ` Dmitry Gutov
2023-01-09  3:13 ` Yuan Fu
2023-01-20 21:40   ` Theodor Thornhill via Bug reports for GNU Emacs, the Swiss army knife of text editors
2023-01-20 21:45     ` Dmitry Gutov
2023-01-20 21:52       ` Theodor Thornhill via Bug reports for GNU Emacs, the Swiss army knife of text editors
2023-09-06  0:09         ` Stefan Kangas

Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).