GNU bug report logs -
#71086
30.0.50; Setting treesit-indent-function is not effective is treesit-simple-indent-rules is nil
Previous Next
Reported by: Óscar Fuentes <ofv <at> wanadoo.es>
Date: Tue, 21 May 2024 01:33:02 UTC
Severity: normal
Found in version 30.0.50
Done: Óscar Fuentes <ofv <at> wanadoo.es>
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 71086 in the body.
You can then email your comments to 71086 AT debbugs.gnu.org in the normal way.
Toggle the display of automated, internal messages from the tracker.
Report forwarded
to
bug-gnu-emacs <at> gnu.org
:
bug#71086
; Package
emacs
.
(Tue, 21 May 2024 01:33:02 GMT)
Full text and
rfc822 format available.
Acknowledgement sent
to
Óscar Fuentes <ofv <at> wanadoo.es>
:
New bug report received and forwarded. Copy sent to
bug-gnu-emacs <at> gnu.org
.
(Tue, 21 May 2024 01:33:02 GMT)
Full text and
rfc822 format available.
Message #5 received at submit <at> debbugs.gnu.org (full text, mbox):
treesit-major-mode-setup setups indentation if and only if
treesit-simple-indent-rules is non-nil. So the major mode author is
forced to set that variable even when he makes no use of the
treesit-simple-indent facilities and instead sets
treesit-indent-function to his own function.
Having to assign a dummy value to a variable that belongs to an API that
is not used is surprising. Furthermore, it is unspecified what kind of
values are acceptable on that scenario.
I would expect that treesit-major-mode-setup shall setup indentation
when treesit-indent-function is non-nil.
OTOH, the info node for "Parser-based Indentation" says:
To enable the parser-based indentation engine, either set
‘treesit-simple-indent-rules’ and call ‘treesit-major-mode-setup’, or
equivalently, set the value of ‘indent-line-function’ to
‘treesit-indent’.
However, treesit-major-mode-setup does more than what's described after
"equivalently":
(when treesit-simple-indent-rules
(setq-local treesit-simple-indent-rules
(treesit--indent-rules-optimize
treesit-simple-indent-rules))
(setq-local indent-line-function #'treesit-indent)
(setq-local indent-region-function #'treesit-indent-region))
In GNU Emacs 30.0.50 (build 1, x86_64-pc-linux-gnu, X toolkit, cairo
version 1.18.0) of 2024-05-19 built on sky
Repository revision: 98b83bdc9f4af7798e3314ad8df6ab78efd60f8a
Repository branch: master
Windowing system distributor 'The X.Org Foundation', version 11.0.12101011
System Description: Debian GNU/Linux trixie/sid
Configured using:
'configure --with-native-compilation --without-toolkit-scroll-bars
--with-x-toolkit=lucid --with-modules --without-imagemagick'
Configured features:
CAIRO DBUS FREETYPE GIF GLIB GMP GNUTLS GSETTINGS HARFBUZZ JPEG LIBOTF
LIBSELINUX LIBXML2 MODULES NATIVE_COMP NOTIFY INOTIFY PDUMPER PNG RSVG
SECCOMP SOUND THREADS TIFF TREE_SITTER WEBP X11 XAW3D XDBE XIM XINPUT2
XPM LUCID ZLIB
Important settings:
value of $LANG: C
locale-coding-system: nil
Information forwarded
to
bug-gnu-emacs <at> gnu.org
:
bug#71086
; Package
emacs
.
(Tue, 21 May 2024 11:44:01 GMT)
Full text and
rfc822 format available.
Message #8 received at 71086 <at> debbugs.gnu.org (full text, mbox):
> From: Óscar Fuentes <ofv <at> wanadoo.es>
> Date: Tue, 21 May 2024 03:32:05 +0200
>
>
> treesit-major-mode-setup setups indentation if and only if
> treesit-simple-indent-rules is non-nil. So the major mode author is
> forced to set that variable even when he makes no use of the
> treesit-simple-indent facilities and instead sets
> treesit-indent-function to his own function.
If your mode defines a treesit-indent-function, why do you need the
help of treesit-major-mode-setup for setting up indentation?
The idea of treesit-simple-indent, AFAIU, is to automate the simple
cases, on the assumption that the mode complex cases cannot be
second-guessed up front, and the mode programmer will need to set that
up manually. For example, you can assign your functions to
indent-line-function and indent-region-function. Or what am I
missing?
> OTOH, the info node for "Parser-based Indentation" says:
>
> To enable the parser-based indentation engine, either set
> ‘treesit-simple-indent-rules’ and call ‘treesit-major-mode-setup’, or
> equivalently, set the value of ‘indent-line-function’ to
> ‘treesit-indent’.
>
> However, treesit-major-mode-setup does more than what's described after
> "equivalently":
>
> (when treesit-simple-indent-rules
> (setq-local treesit-simple-indent-rules
> (treesit--indent-rules-optimize
> treesit-simple-indent-rules))
> (setq-local indent-line-function #'treesit-indent)
> (setq-local indent-region-function #'treesit-indent-region))
So this is just a matter of updating the documentation to mention
indent-region-function?
I've added Yuan, in case he has comments or suggestions.
Information forwarded
to
bug-gnu-emacs <at> gnu.org
:
bug#71086
; Package
emacs
.
(Wed, 22 May 2024 06:25:02 GMT)
Full text and
rfc822 format available.
Message #11 received at 71086 <at> debbugs.gnu.org (full text, mbox):
> On May 21, 2024, at 4:43 AM, Eli Zaretskii <eliz <at> gnu.org> wrote:
>
>> From: Óscar Fuentes <ofv <at> wanadoo.es>
>> Date: Tue, 21 May 2024 03:32:05 +0200
>>
>>
>> treesit-major-mode-setup setups indentation if and only if
>> treesit-simple-indent-rules is non-nil. So the major mode author is
>> forced to set that variable even when he makes no use of the
>> treesit-simple-indent facilities and instead sets
>> treesit-indent-function to his own function.
>
> If your mode defines a treesit-indent-function, why do you need the
> help of treesit-major-mode-setup for setting up indentation?
>
> The idea of treesit-simple-indent, AFAIU, is to automate the simple
> cases, on the assumption that the mode complex cases cannot be
> second-guessed up front, and the mode programmer will need to set that
> up manually. For example, you can assign your functions to
> indent-line-function and indent-region-function. Or what am I
> missing?
There are actually another level of customization between setting treesit-simple-indent-rules and setting indent-line-function, which is setting treesit-indent-function. IIUC Óscar is trying to do that but found out treesit-major-mode-setup doesn’t automatically setup indent-line-functions when treesit-indent-function is set. I’ve now fixed that. (Though I didn’t expect someone to use this variable.)
And to Óscar, I’m curious and interested to know what indentation function you came up with; if it’s useful maybe we can add it to Emacs?
>
>> OTOH, the info node for "Parser-based Indentation" says:
>>
>> To enable the parser-based indentation engine, either set
>> ‘treesit-simple-indent-rules’ and call ‘treesit-major-mode-setup’, or
>> equivalently, set the value of ‘indent-line-function’ to
>> ‘treesit-indent’.
>>
>> However, treesit-major-mode-setup does more than what's described after
>> "equivalently":
>>
>> (when treesit-simple-indent-rules
>> (setq-local treesit-simple-indent-rules
>> (treesit--indent-rules-optimize
>> treesit-simple-indent-rules))
>> (setq-local indent-line-function #'treesit-indent)
>> (setq-local indent-region-function #'treesit-indent-region))
>
> So this is just a matter of updating the documentation to mention
> indent-region-function?
>
> I've added Yuan, in case he has comments or suggestions.
I also updated the manual.
Yuan
Information forwarded
to
bug-gnu-emacs <at> gnu.org
:
bug#71086
; Package
emacs
.
(Wed, 22 May 2024 14:16:01 GMT)
Full text and
rfc822 format available.
Message #14 received at 71086 <at> debbugs.gnu.org (full text, mbox):
Yuan Fu <casouri <at> gmail.com> writes:
> There are actually another level of customization between setting
> treesit-simple-indent-rules and setting indent-line-function, which is
> setting treesit-indent-function. IIUC Óscar is trying to do that but
> found out treesit-major-mode-setup doesn’t automatically setup
> indent-line-functions when treesit-indent-function is set.
More specifically, I found that treesit-major-mode-setup doesn't setup
indentation when treesit-simple-indent-rules is *not* set, even when
treesit-indent-function has been set.
> I’ve now fixed that.
Indeed, thank you.
> (Though I didn’t expect someone to use this variable.)
Why not? It is documented and the natural alternative when
treesit-simple-indent-rules is not fit for the job, isn't it?
> And to Óscar, I’m curious and interested to know what indentation
> function you came up with; if it’s useful maybe we can add it to
> Emacs?
I started with treesit-simple-indent-rules until I stumbled on something
that it is not easily workable with the existing functions. My language
has a Lisp syntax, and the problem was with indenting comments (and with
how the presence of comments affected the indentation of other things).
For instance, the typical indentation of
(if p
foo
bar)
That's simple enough to achieve with treesit-simple-indent-rules, but if
we insert a comment:
(if p
;; blah
(foo)
(bar))
things suddenly turn hairy.
Likewise, starting with this:
(progn (foo)
(bar))
... after inserting a comment, this is the desired indentation:
(progn ;; blah
(foo)
(bar))
... not this:
(progn ;; blah
(foo)
(bar))
Maybe a variant of `match' and `nth-sibling' that take an argument of
node types to skip would be enough to cover this case? That was
basically what I did.
Anyways, going the treesit-indent-function route was easy enough, and it
is more powerful, flexible and future-proof. I don't think that
treesit-simple-indent-rules would yield a significantly shorter and
mantainable implementation.
> I also updated the manual.
Thanks!
BTW, the treesit-based implementation of the major mode for this simple
language is a tiny fraction of its traditional counterpart. And
providing a great basis for adding more features :-)
Reply sent
to
Óscar Fuentes <ofv <at> wanadoo.es>
:
You have taken responsibility.
(Sun, 02 Jun 2024 16:56:02 GMT)
Full text and
rfc822 format available.
Notification sent
to
Óscar Fuentes <ofv <at> wanadoo.es>
:
bug acknowledged by developer.
(Sun, 02 Jun 2024 16:56:02 GMT)
Full text and
rfc822 format available.
Message #19 received at 71086-done <at> debbugs.gnu.org (full text, mbox):
As the issues reported here are solved, I'm closing the bug.
bug archived.
Request was from
Debbugs Internal Request <help-debbugs <at> gnu.org>
to
internal_control <at> debbugs.gnu.org
.
(Mon, 01 Jul 2024 11:24:09 GMT)
Full text and
rfc822 format available.
This bug report was last modified 355 days ago.
Previous Next
GNU bug tracking system
Copyright (C) 1999 Darren O. Benham,
1997,2003 nCipher Corporation Ltd,
1994-97 Ian Jackson.