all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
* bug#66845: 29.1; cmake-ts-mode indentation broken
@ 2023-10-31 11:35 Juan Palacios
  2023-10-31 15:50 ` Randy Taylor
  2023-11-01  2:45 ` Randy Taylor
  0 siblings, 2 replies; 11+ messages in thread
From: Juan Palacios @ 2023-10-31 11:35 UTC (permalink / raw)
  To: 66845

Using cmake grammar from https://github.com/uyha/tree-sitter-cmake

Reproduction:
1. emacs -q
2. C-x C-f test.cmake
3. M-x cmake-ts-mode
4. Insert the following text:
if(TRUE)
message("indent")
endif()
5. Move the point to the beginning of the second line.
6. TAB
7. Notice that no indentation is performed.

treesit-simple-indent-rules local value:
((cmake
  ((node-is ")")
   parent-bol 0)
  ((node-is "else_command")
   parent-bol 0)
  ((node-is "elseif_command")
   parent-bol 0)
  ((node-is "endforeach_command")
   parent-bol 0)
  ((node-is "endfunction_command")
   parent-bol 0)
  ((node-is "endif_command")
   parent-bol 0)
  ((parent-is "foreach_loop")
   parent-bol cmake-ts-mode-indent-offset)
  ((parent-is "function_def")
   parent-bol cmake-ts-mode-indent-offset)
  ((parent-is "if_condition")
   parent-bol cmake-ts-mode-indent-offset)
  ((parent-is "normal_command")
   parent-bol cmake-ts-mode-indent-offset)))

treesit-explore-mode shows:
(if_condition
 (if_command (if) (
  (argument_list
   (argument (unquoted_argument)))
  ))
 (body
  (normal_command (identifier) (
   (argument_list
    (argument
     (quoted_argument " (quoted_element) ")))
   )))
 (endif_command (endif) ( )))

In GNU Emacs 29.1 (build 1, x86_64-pc-linux-gnu, GTK+ Version 3.24.38,
cairo version 1.17.8)
Windowing system distributor 'The X.Org Foundation', version 11.0.12101009
System Description: Arch Linux

Configured using:
 'configure --with-x-toolkit=gtk3 --with-native-compilation=aot
 --sysconfdir=/etc --prefix=/usr --libexecdir=/usr/lib
 --with-tree-sitter --localstatedir=/var --with-cairo
 --disable-build-details --with-harfbuzz --with-libsystemd
 --with-modules 'CFLAGS=-march=x86-64 -mtune=generic -O2 -pipe -fno-plt
 -fexceptions -Wp,-D_FORTIFY_SOURCE=2 -Wformat -Werror=format-security
 -fstack-clash-protection -fcf-protection -g
 -ffile-prefix-map=/build/emacs/src=/usr/src/debug/emacs -flto=auto'
 'LDFLAGS=-Wl,-O1,--sort-common,--as-needed,-z,relro,-z,now -flto=auto'
 'CXXFLAGS=-march=x86-64 -mtune=generic -O2 -pipe -fno-plt -fexceptions
 -Wp,-D_FORTIFY_SOURCE=2 -Wformat -Werror=format-security
 -fstack-clash-protection -fcf-protection -Wp,-D_GLIBCXX_ASSERTIONS -g
 -ffile-prefix-map=/build/emacs/src=/usr/src/debug/emacs -flto=auto''

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

Important settings:
  value of $LANG: es_ES.utf8
  locale-coding-system: utf-8-unix

Major mode: CMake

Minor modes in effect:
  tooltip-mode: t
  global-eldoc-mode: t
  show-paren-mode: t
  electric-indent-mode: t
  mouse-wheel-mode: t
  tool-bar-mode: t
  menu-bar-mode: t
  file-name-shadow-mode: t
  global-font-lock-mode: t
  font-lock-mode: t
  blink-cursor-mode: t
  line-number-mode: t
  indent-tabs-mode: t
  transient-mark-mode: t
  auto-composition-mode: t
  auto-encryption-mode: t
  auto-compression-mode: t

Load-path shadows:
None found.

Features:
(shadow sort mail-extr emacsbug message mailcap yank-media puny dired
dired-loaddefs rfc822 mml mml-sec password-cache epa derived epg rfc6068
epg-config gnus-util text-property-search mm-decode mm-bodies mm-encode
mail-parse rfc2231 mailabbrev gmm-utils mailheader sendmail rfc2047
rfc2045 ietf-drums mm-util mail-prsvr mail-utils time-date cmake-ts-mode
treesit cl-loaddefs comp comp-cstr warnings icons subr-x rx cl-seq
cl-macs gv cl-extra help-mode bytecomp byte-compile cl-lib rmc
iso-transl tooltip cconv eldoc paren electric uniquify ediff-hook
vc-hooks lisp-float-type elisp-mode mwheel term/x-win x-win
term/common-win x-dnd tool-bar dnd fontset image regexp-opt fringe
tabulated-list replace newcomment text-mode lisp-mode prog-mode register
page tab-bar menu-bar rfn-eshadow isearch easymenu timer select
scroll-bar mouse jit-lock font-lock syntax font-core term/tty-colors
frame minibuffer nadvice seq simple cl-generic indonesian philippine
cham georgian utf-8-lang misc-lang vietnamese tibetan thai tai-viet lao
korean japanese eucjp-ms cp51932 hebrew greek romanian slovak czech
european ethiopic indian cyrillic chinese composite emoji-zwj charscript
charprop case-table epa-hook jka-cmpr-hook help abbrev obarray oclosure
cl-preloaded button loaddefs theme-loaddefs faces cus-face macroexp
files window text-properties overlay sha1 md5 base64 format env
code-pages mule custom widget keymap hashtable-print-readable backquote
threads dbusbind inotify lcms2 dynamic-setting system-font-setting
font-render-setting cairo move-toolbar gtk x-toolkit xinput2 x multi-tty
make-network-process native-compile emacs)

Memory information:
((conses 16 82323 7814)
 (symbols 48 7373 0)
 (strings 32 20469 1882)
 (string-bytes 1 626948)
 (vectors 16 16173)
 (vector-slots 8 335652 13542)
 (floats 8 32 42)
 (intervals 56 404 0)
 (buffers 984 13))





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

* bug#66845: 29.1; cmake-ts-mode indentation broken
  2023-10-31 11:35 bug#66845: 29.1; cmake-ts-mode indentation broken Juan Palacios
@ 2023-10-31 15:50 ` Randy Taylor
  2023-11-01  2:45 ` Randy Taylor
  1 sibling, 0 replies; 11+ messages in thread
From: Randy Taylor @ 2023-10-31 15:50 UTC (permalink / raw)
  To: Juan Palacios; +Cc: 66845

On Tuesday, October 31st, 2023 at 07:35, Juan Palacios <jpalaciosdev@gmail.com> wrote:
> Using cmake grammar from https://github.com/uyha/tree-sitter-cmake
> 
> Reproduction:
> 1. emacs -q
> 2. C-x C-f test.cmake
> 3. M-x cmake-ts-mode
> 4. Insert the following text:
> if(TRUE)
> message("indent")
> endif()
> 5. Move the point to the beginning of the second line.
> 6. TAB
> 7. Notice that no indentation is performed.
> 
> treesit-simple-indent-rules local value:
> ((cmake
> ((node-is ")")
> parent-bol 0)
> ((node-is "else_command")
> parent-bol 0)
> ((node-is "elseif_command")
> parent-bol 0)
> ((node-is "endforeach_command")
> parent-bol 0)
> ((node-is "endfunction_command")
> parent-bol 0)
> ((node-is "endif_command")
> parent-bol 0)
> ((parent-is "foreach_loop")
> parent-bol cmake-ts-mode-indent-offset)
> ((parent-is "function_def")
> parent-bol cmake-ts-mode-indent-offset)
> ((parent-is "if_condition")
> parent-bol cmake-ts-mode-indent-offset)
> ((parent-is "normal_command")
> parent-bol cmake-ts-mode-indent-offset)))
> 
> treesit-explore-mode shows:
> (if_condition
> (if_command (if) (
> (argument_list
> (argument (unquoted_argument)))
> ))
> (body
> (normal_command (identifier) (
> (argument_list
> (argument
> (quoted_argument " (quoted_element) ")))
> )))
> (endif_command (endif) ( )))
> 

Thanks for the report, I'll take a look tonight.





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

* bug#66845: 29.1; cmake-ts-mode indentation broken
  2023-10-31 11:35 bug#66845: 29.1; cmake-ts-mode indentation broken Juan Palacios
  2023-10-31 15:50 ` Randy Taylor
@ 2023-11-01  2:45 ` Randy Taylor
  2023-11-05 14:32   ` Juan Palacios
  2023-11-07  8:25   ` Yuan Fu
  1 sibling, 2 replies; 11+ messages in thread
From: Randy Taylor @ 2023-11-01  2:45 UTC (permalink / raw)
  To: Juan Palacios; +Cc: 66845, Yuan Fu

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

On Tuesday, October 31st, 2023 at 07:35, Juan Palacios <jpalaciosdev@gmail.com> wrote:
> Using cmake grammar from https://github.com/uyha/tree-sitter-cmake
> 
> Reproduction:
> 1. emacs -q
> 2. C-x C-f test.cmake
> 3. M-x cmake-ts-mode
> 4. Insert the following text:
> if(TRUE)
> message("indent")
> endif()
> 5. Move the point to the beginning of the second line.
> 6. TAB
> 7. Notice that no indentation is performed.
> 
> treesit-simple-indent-rules local value:
> ((cmake
> ((node-is ")")
> parent-bol 0)
> ((node-is "else_command")
> parent-bol 0)
> ((node-is "elseif_command")
> parent-bol 0)
> ((node-is "endforeach_command")
> parent-bol 0)
> ((node-is "endfunction_command")
> parent-bol 0)
> ((node-is "endif_command")
> parent-bol 0)
> ((parent-is "foreach_loop")
> parent-bol cmake-ts-mode-indent-offset)
> ((parent-is "function_def")
> parent-bol cmake-ts-mode-indent-offset)
> ((parent-is "if_condition")
> parent-bol cmake-ts-mode-indent-offset)
> ((parent-is "normal_command")
> parent-bol cmake-ts-mode-indent-offset)))
> 
> treesit-explore-mode shows:
> (if_condition
> (if_command (if) (
> (argument_list
> (argument (unquoted_argument)))
> ))
> (body
> (normal_command (identifier) (
> (argument_list
> (argument
> (quoted_argument " (quoted_element) ")))
> )))
> (endif_command (endif) ( )))
> 

I've attached a patch fixing this while retaining compatibility with
older grammars. 2 new versions of the grammar, v0.3.0 and v0.4.0
(and anything beyond for now) wrap things in new nodes.

Yuan,
It's been awhile since I've touched any tree-sitter stuff so I'm
probably missing something obvious, but it seems to me like the
function I've added in the patch
(cmake-ts-mode--argument-list-body-offset) is working around a bug
somewhere? I'm not sure why the start of line_comment is considered
parent-bol for indentation.

For example, with this test cmake file: (test.cmake)
if(TRUE)
  # Comment.
  message("indent")
endif()

We get:
(source_file
 (if_condition
  (if_command (if) (
   (argument_list
    (argument (unquoted_argument)))
   ))
  (body (line_comment)
   (normal_command (identifier) (
    (argument_list
     (argument
      (quoted_argument " (quoted_element) ")))
    )))
  (endif_command (endif) ( ))))

With cmake-ts-mode--argument-list-body-offset always returning
cmake-ts-mode-indent-offset (which is what I would expect), the
`parent-is "body"` rule will cause this indentation:
if(TRUE)
  # Comment.
    message("indent")
endif()

Am I being silly or is there indeed something odd going on?

[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: 0001-Fix-cmake-ts-mode-indentation-Bug-66845.patch --]
[-- Type: text/x-patch; name=0001-Fix-cmake-ts-mode-indentation-Bug-66845.patch, Size: 3053 bytes --]

From 544396b7afa55c4d12fc6f03198199017582b8df Mon Sep 17 00:00:00 2001
From: Randy Taylor <dev@rjt.dev>
Date: Tue, 31 Oct 2023 22:08:25 -0400
Subject: [PATCH] Fix cmake-ts-mode indentation (Bug#66845)

* lisp/progmodes/cmake-ts-mode.el
(cmake-ts-mode--argument-list-body-offset): New function.
(cmake-ts-mode--indent-rules): Support versions v0.3.0 and v0.4.0 of
the grammar.
(cmake-ts-mode--font-lock-compatibility-fe9b5e0): Fix docstring.
---
 lisp/progmodes/cmake-ts-mode.el | 30 ++++++++++++++++++++++++++++--
 1 file changed, 28 insertions(+), 2 deletions(-)

diff --git a/lisp/progmodes/cmake-ts-mode.el b/lisp/progmodes/cmake-ts-mode.el
index 53d471c381a..d2d66a461f3 100644
--- a/lisp/progmodes/cmake-ts-mode.el
+++ b/lisp/progmodes/cmake-ts-mode.el
@@ -52,6 +52,24 @@ cmake-ts-mode--syntax-table
     table)
   "Syntax table for `cmake-ts-mode'.")
 
+(defun cmake-ts-mode--argument-list-body-offset (_n parent &rest _)
+  "This offset is used for argument_list and body nodes.
+
+For some reason, a comment after an argument_list or body node causes
+the next line to indent as if the comment was the parent. For example:
+
+1 if (TRUE)
+2   # Comment.
+3     func()
+4 endif()
+
+Line 3 is indented as if the comment (line 2) was parent-bol,
+when line 1 should be parent-bol."
+  (when-let (node (treesit-node-child parent 0 t))
+    (if (string-equal "line_comment" (treesit-node-type node))
+        0
+      cmake-ts-mode-indent-offset)))
+
 (defvar cmake-ts-mode--indent-rules
   `((cmake
      ((node-is ")") parent-bol 0)
@@ -60,6 +78,14 @@ cmake-ts-mode--indent-rules
      ((node-is "endforeach_command") parent-bol 0)
      ((node-is "endfunction_command") parent-bol 0)
      ((node-is "endif_command") parent-bol 0)
+     ;;; Release v0.4.0 wraps arguments in an argument_list node.
+     ,@(ignore-errors
+         (treesit-query-capture 'cmake '((argument_list) @capture))
+         `(((parent-is "argument_list") parent-bol cmake-ts-mode--argument-list-body-offset)))
+     ;;; Release v0.3.0 wraps the body of commands into a body node.
+     ,@(ignore-errors
+         (treesit-query-capture 'cmake '((body) @capture))
+         `(((parent-is "body") parent-bol cmake-ts-mode--argument-list-body-offset)))
      ((parent-is "foreach_loop") parent-bol cmake-ts-mode-indent-offset)
      ((parent-is "function_def") parent-bol cmake-ts-mode-indent-offset)
      ((parent-is "if_condition") parent-bol cmake-ts-mode-indent-offset)
@@ -89,8 +115,8 @@ cmake-ts-mode--if-conditions
   "CMake if conditions for tree-sitter font-locking.")
 
 (defun cmake-ts-mode--font-lock-compatibility-fe9b5e0 ()
-  "Indent rules helper, to handle different releases of tree-sitter-cmake.
-Check if a node type is available, then return the right indent rules."
+  "Font lock helper, to handle different releases of tree-sitter-cmake.
+Check if a node type is available, then return the right font lock rules."
   ;; handle commit fe9b5e0
   (condition-case nil
       (progn (treesit-query-capture 'cmake '((argument_list) @capture))
-- 
2.42.0


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

* bug#66845: 29.1; cmake-ts-mode indentation broken
  2023-11-01  2:45 ` Randy Taylor
@ 2023-11-05 14:32   ` Juan Palacios
  2023-11-05 18:16     ` Fu Yuan
  2023-11-07  8:25   ` Yuan Fu
  1 sibling, 1 reply; 11+ messages in thread
From: Juan Palacios @ 2023-11-05 14:32 UTC (permalink / raw)
  To: Randy Taylor; +Cc: 66845

El mié, 1 nov 2023 a las 3:45, Randy Taylor (<dev@rjt.dev>) escribió:
>
> On Tuesday, October 31st, 2023 at 07:35, Juan Palacios <jpalaciosdev@gmail.com> wrote:
> > Using cmake grammar from https://github.com/uyha/tree-sitter-cmake
> >
> > Reproduction:
> > 1. emacs -q
> > 2. C-x C-f test.cmake
> > 3. M-x cmake-ts-mode
> > 4. Insert the following text:
> > if(TRUE)
> > message("indent")
> > endif()
> > 5. Move the point to the beginning of the second line.
> > 6. TAB
> > 7. Notice that no indentation is performed.
> >
> > treesit-simple-indent-rules local value:
> > ((cmake
> > ((node-is ")")
> > parent-bol 0)
> > ((node-is "else_command")
> > parent-bol 0)
> > ((node-is "elseif_command")
> > parent-bol 0)
> > ((node-is "endforeach_command")
> > parent-bol 0)
> > ((node-is "endfunction_command")
> > parent-bol 0)
> > ((node-is "endif_command")
> > parent-bol 0)
> > ((parent-is "foreach_loop")
> > parent-bol cmake-ts-mode-indent-offset)
> > ((parent-is "function_def")
> > parent-bol cmake-ts-mode-indent-offset)
> > ((parent-is "if_condition")
> > parent-bol cmake-ts-mode-indent-offset)
> > ((parent-is "normal_command")
> > parent-bol cmake-ts-mode-indent-offset)))
> >
> > treesit-explore-mode shows:
> > (if_condition
> > (if_command (if) (
> > (argument_list
> > (argument (unquoted_argument)))
> > ))
> > (body
> > (normal_command (identifier) (
> > (argument_list
> > (argument
> > (quoted_argument " (quoted_element) ")))
> > )))
> > (endif_command (endif) ( )))
> >
>
> I've attached a patch fixing this while retaining compatibility with
> older grammars. 2 new versions of the grammar, v0.3.0 and v0.4.0
> (and anything beyond for now) wrap things in new nodes.

I applied the patch to 29.1, and I can confirm that it's working fine.
Thanks.





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

* bug#66845: 29.1; cmake-ts-mode indentation broken
  2023-11-05 14:32   ` Juan Palacios
@ 2023-11-05 18:16     ` Fu Yuan
  0 siblings, 0 replies; 11+ messages in thread
From: Fu Yuan @ 2023-11-05 18:16 UTC (permalink / raw)
  To: Juan Palacios; +Cc: Randy Taylor, 66845



> 在 2023年11月5日,上午6:33,Juan Palacios <jpalaciosdev@gmail.com> 写道:
> 
> El mié, 1 nov 2023 a las 3:45, Randy Taylor (<dev@rjt.dev>) escribió:
>> 
>>> On Tuesday, October 31st, 2023 at 07:35, Juan Palacios <jpalaciosdev@gmail.com> wrote:
>>> Using cmake grammar from https://github.com/uyha/tree-sitter-cmake
>>> 
>>> Reproduction:
>>> 1. emacs -q
>>> 2. C-x C-f test.cmake
>>> 3. M-x cmake-ts-mode
>>> 4. Insert the following text:
>>> if(TRUE)
>>> message("indent")
>>> endif()
>>> 5. Move the point to the beginning of the second line.
>>> 6. TAB
>>> 7. Notice that no indentation is performed.
>>> 
>>> treesit-simple-indent-rules local value:
>>> ((cmake
>>> ((node-is ")")
>>> parent-bol 0)
>>> ((node-is "else_command")
>>> parent-bol 0)
>>> ((node-is "elseif_command")
>>> parent-bol 0)
>>> ((node-is "endforeach_command")
>>> parent-bol 0)
>>> ((node-is "endfunction_command")
>>> parent-bol 0)
>>> ((node-is "endif_command")
>>> parent-bol 0)
>>> ((parent-is "foreach_loop")
>>> parent-bol cmake-ts-mode-indent-offset)
>>> ((parent-is "function_def")
>>> parent-bol cmake-ts-mode-indent-offset)
>>> ((parent-is "if_condition")
>>> parent-bol cmake-ts-mode-indent-offset)
>>> ((parent-is "normal_command")
>>> parent-bol cmake-ts-mode-indent-offset)))
>>> 
>>> treesit-explore-mode shows:
>>> (if_condition
>>> (if_command (if) (
>>> (argument_list
>>> (argument (unquoted_argument)))
>>> ))
>>> (body
>>> (normal_command (identifier) (
>>> (argument_list
>>> (argument
>>> (quoted_argument " (quoted_element) ")))
>>> )))
>>> (endif_command (endif) ( )))
>>> 
>> 
>> I've attached a patch fixing this while retaining compatibility with
>> older grammars. 2 new versions of the grammar, v0.3.0 and v0.4.0
>> (and anything beyond for now) wrap things in new nodes.
> 

Hey Randy,

I just see this, sorry :-) I’ll have a look tonight or tomorrow.

Yuan




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

* bug#66845: 29.1; cmake-ts-mode indentation broken
  2023-11-01  2:45 ` Randy Taylor
  2023-11-05 14:32   ` Juan Palacios
@ 2023-11-07  8:25   ` Yuan Fu
  2023-11-07 17:21     ` Randy Taylor
  1 sibling, 1 reply; 11+ messages in thread
From: Yuan Fu @ 2023-11-07  8:25 UTC (permalink / raw)
  To: Randy Taylor; +Cc: Juan Palacios, 66845

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



> On Oct 31, 2023, at 7:45 PM, Randy Taylor <dev@rjt.dev> wrote:
> 
> On Tuesday, October 31st, 2023 at 07:35, Juan Palacios <jpalaciosdev@gmail.com> wrote:
>> Using cmake grammar from https://github.com/uyha/tree-sitter-cmake
>> 
>> Reproduction:
>> 1. emacs -q
>> 2. C-x C-f test.cmake
>> 3. M-x cmake-ts-mode
>> 4. Insert the following text:
>> if(TRUE)
>> message("indent")
>> endif()
>> 5. Move the point to the beginning of the second line.
>> 6. TAB
>> 7. Notice that no indentation is performed.
>> 
>> treesit-simple-indent-rules local value:
>> ((cmake
>> ((node-is ")")
>> parent-bol 0)
>> ((node-is "else_command")
>> parent-bol 0)
>> ((node-is "elseif_command")
>> parent-bol 0)
>> ((node-is "endforeach_command")
>> parent-bol 0)
>> ((node-is "endfunction_command")
>> parent-bol 0)
>> ((node-is "endif_command")
>> parent-bol 0)
>> ((parent-is "foreach_loop")
>> parent-bol cmake-ts-mode-indent-offset)
>> ((parent-is "function_def")
>> parent-bol cmake-ts-mode-indent-offset)
>> ((parent-is "if_condition")
>> parent-bol cmake-ts-mode-indent-offset)
>> ((parent-is "normal_command")
>> parent-bol cmake-ts-mode-indent-offset)))
>> 
>> treesit-explore-mode shows:
>> (if_condition
>> (if_command (if) (
>> (argument_list
>> (argument (unquoted_argument)))
>> ))
>> (body
>> (normal_command (identifier) (
>> (argument_list
>> (argument
>> (quoted_argument " (quoted_element) ")))
>> )))
>> (endif_command (endif) ( )))
>> 
> 
> I've attached a patch fixing this while retaining compatibility with
> older grammars. 2 new versions of the grammar, v0.3.0 and v0.4.0
> (and anything beyond for now) wrap things in new nodes.
> 
> Yuan,
> It's been awhile since I've touched any tree-sitter stuff so I'm
> probably missing something obvious, but it seems to me like the
> function I've added in the patch
> (cmake-ts-mode--argument-list-body-offset) is working around a bug
> somewhere? I'm not sure why the start of line_comment is considered
> parent-bol for indentation.
> 
> For example, with this test cmake file: (test.cmake)
> if(TRUE)
>  # Comment.
>  message("indent")
> endif()
> 
> We get:
> (source_file
> (if_condition
>  (if_command (if) (
>   (argument_list
>    (argument (unquoted_argument)))
>   ))
>  (body (line_comment)
>   (normal_command (identifier) (
>    (argument_list
>     (argument
>      (quoted_argument " (quoted_element) ")))
>    )))
>  (endif_command (endif) ( ))))
> 
> With cmake-ts-mode--argument-list-body-offset always returning
> cmake-ts-mode-indent-offset (which is what I would expect), the
> `parent-is "body"` rule will cause this indentation:
> if(TRUE)
>  # Comment.
>    message("indent")
> endif()
> 
> Am I being silly or is there indeed something odd going on?<0001-Fix-cmake-ts-mode-indentation-Bug-66845.patch>

IMO it makes more sense to use prev-sibling:

,@(ignore-errors
         (treesit-query-capture 'cmake '((body) @capture))
         `(((parent-is "body") prev-sibling 0)))

This would allow users to manually adjust the indentation of a line and have the rest of the body follow that.

As for why parent-bol returns the BOL of the comment line, that’s expected: The parent is body, and body starts at the comment, so of course the beginning of the parent line is the beginning of the comment line. In the image below, the highlighted portion marks the body node.

Some tree-sitter grammar would mark the beginning of body at the end of if(TRUE), but what tree-sitter-cmake does here is equally valid.


[-- Attachment #2: Screenshot 2023-11-07 at 12.21.36 AM.png --]
[-- Type: image/png, Size: 113254 bytes --]

[-- Attachment #3: Type: text/plain, Size: 7 bytes --]


Yuan


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

* bug#66845: 29.1; cmake-ts-mode indentation broken
  2023-11-07  8:25   ` Yuan Fu
@ 2023-11-07 17:21     ` Randy Taylor
  2023-11-07 20:11       ` Juan Palacios
  2023-11-08  2:52       ` Yuan Fu
  0 siblings, 2 replies; 11+ messages in thread
From: Randy Taylor @ 2023-11-07 17:21 UTC (permalink / raw)
  To: Yuan Fu; +Cc: Juan Palacios, 66845

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

On Tuesday, November 7th, 2023 at 03:25, Yuan Fu <casouri@gmail.com> wrote:
> IMO it makes more sense to use prev-sibling:
> 
> ,@(ignore-errors
> (treesit-query-capture 'cmake '((body) @capture))
> `(((parent-is "body") prev-sibling 0)))
> 
> This would allow users to manually adjust the indentation of a line and have the rest of the body follow that.

This would be much more elegant but unfortunately that and my previous patch failed to account for the following:

1 if(TRUE) # Comment.
2 endif()

And place POINT at the end of line 1 and hit RET. It will indent to the start of the comment :(. However, you gave me an idea: use the grand-parent as the anchor (which in this case is the if - exactly what we want). I've attached a new patch which accounts for that.

Juan, thanks for testing the first patch. Please give this new one a whirl if you're able to.

> 
> As for why parent-bol returns the BOL of the comment line, that’s expected: The parent is body, and body starts at the comment, so of course the beginning of the parent line is the beginning of the comment line. In the image below, the highlighted portion marks the body node.
> 
> Some tree-sitter grammar would mark the beginning of body at the end of if(TRUE), but what tree-sitter-cmake does here is equally valid.

Thanks, I understand what's happening now. Whenever line_comment is NOT the first child, the beginning of body is at the end of if(TRUE). If line_comment is the first child, then the beginning of body is at the start of the comment. I was expecting them to behave the same - that's why I was so confused, and I don't really understand why it was done that way.

[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: 0001-Fix-cmake-ts-mode-indentation-Bug-66845.patch --]
[-- Type: text/x-patch; name=0001-Fix-cmake-ts-mode-indentation-Bug-66845.patch, Size: 2267 bytes --]

From ee400533655bcbe0c803dde9ba8ded95f9d6d1e5 Mon Sep 17 00:00:00 2001
From: Randy Taylor <dev@rjt.dev>
Date: Tue, 31 Oct 2023 22:08:25 -0400
Subject: [PATCH] Fix cmake-ts-mode indentation (Bug#66845)

* lisp/progmodes/cmake-ts-mode.el
(cmake-ts-mode--indent-rules): Support versions v0.3.0 and v0.4.0 of
the grammar.
(cmake-ts-mode--font-lock-compatibility-fe9b5e0): Fix docstring.
---
 lisp/progmodes/cmake-ts-mode.el | 14 +++++++++++---
 1 file changed, 11 insertions(+), 3 deletions(-)

diff --git a/lisp/progmodes/cmake-ts-mode.el b/lisp/progmodes/cmake-ts-mode.el
index 53d471c381a..8fcdcaddc7b 100644
--- a/lisp/progmodes/cmake-ts-mode.el
+++ b/lisp/progmodes/cmake-ts-mode.el
@@ -63,7 +63,15 @@ cmake-ts-mode--indent-rules
      ((parent-is "foreach_loop") parent-bol cmake-ts-mode-indent-offset)
      ((parent-is "function_def") parent-bol cmake-ts-mode-indent-offset)
      ((parent-is "if_condition") parent-bol cmake-ts-mode-indent-offset)
-     ((parent-is "normal_command") parent-bol cmake-ts-mode-indent-offset)))
+     ((parent-is "normal_command") parent-bol cmake-ts-mode-indent-offset)
+     ;;; Release v0.4.0 wraps arguments in an argument_list node.
+     ,@(ignore-errors
+         (treesit-query-capture 'cmake '((argument_list) @capture))
+         `(((parent-is "argument_list") grand-parent cmake-ts-mode-indent-offset)))
+     ;;; Release v0.3.0 wraps the body of commands into a body node.
+     ,@(ignore-errors
+         (treesit-query-capture 'cmake '((body) @capture))
+         `(((parent-is "body") grand-parent cmake-ts-mode-indent-offset)))))
   "Tree-sitter indent rules for `cmake-ts-mode'.")
 
 (defvar cmake-ts-mode--constants
@@ -89,8 +97,8 @@ cmake-ts-mode--if-conditions
   "CMake if conditions for tree-sitter font-locking.")
 
 (defun cmake-ts-mode--font-lock-compatibility-fe9b5e0 ()
-  "Indent rules helper, to handle different releases of tree-sitter-cmake.
-Check if a node type is available, then return the right indent rules."
+  "Font lock helper, to handle different releases of tree-sitter-cmake.
+Check if a node type is available, then return the right font lock rules."
   ;; handle commit fe9b5e0
   (condition-case nil
       (progn (treesit-query-capture 'cmake '((argument_list) @capture))
-- 
2.42.1


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

* bug#66845: 29.1; cmake-ts-mode indentation broken
  2023-11-07 17:21     ` Randy Taylor
@ 2023-11-07 20:11       ` Juan Palacios
  2023-11-08  2:52       ` Yuan Fu
  1 sibling, 0 replies; 11+ messages in thread
From: Juan Palacios @ 2023-11-07 20:11 UTC (permalink / raw)
  To: Randy Taylor; +Cc: Yuan Fu, 66845

El mar, 7 nov 2023 a las 18:22, Randy Taylor (<dev@rjt.dev>) escribió:
>
> On Tuesday, November 7th, 2023 at 03:25, Yuan Fu <casouri@gmail.com> wrote:
> > IMO it makes more sense to use prev-sibling:
> >
> > ,@(ignore-errors
> > (treesit-query-capture 'cmake '((body) @capture))
> > `(((parent-is "body") prev-sibling 0)))
> >
> > This would allow users to manually adjust the indentation of a line and have the rest of the body follow that.
>
> This would be much more elegant but unfortunately that and my previous patch failed to account for the following:
>
> 1 if(TRUE) # Comment.
> 2 endif()
>
> And place POINT at the end of line 1 and hit RET. It will indent to the start of the comment :(. However, you gave me an idea: use the grand-parent as the anchor (which in this case is the if - exactly what we want). I've attached a new patch which accounts for that.
>
> Juan, thanks for testing the first patch. Please give this new one a whirl if you're able to.

This patch is working fine.
Thank you!





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

* bug#66845: 29.1; cmake-ts-mode indentation broken
  2023-11-07 17:21     ` Randy Taylor
  2023-11-07 20:11       ` Juan Palacios
@ 2023-11-08  2:52       ` Yuan Fu
  2023-11-08  3:42         ` Randy Taylor
  1 sibling, 1 reply; 11+ messages in thread
From: Yuan Fu @ 2023-11-08  2:52 UTC (permalink / raw)
  To: Randy Taylor; +Cc: Juan Palacios, 66845



> On Nov 7, 2023, at 9:21 AM, Randy Taylor <dev@rjt.dev> wrote:
> 
> On Tuesday, November 7th, 2023 at 03:25, Yuan Fu <casouri@gmail.com> wrote:
>> IMO it makes more sense to use prev-sibling:
>> 
>> ,@(ignore-errors
>> (treesit-query-capture 'cmake '((body) @capture))
>> `(((parent-is "body") prev-sibling 0)))
>> 
>> This would allow users to manually adjust the indentation of a line and have the rest of the body follow that.
> 
> This would be much more elegant but unfortunately that and my previous patch failed to account for the following:
> 
> 1 if(TRUE) # Comment.
> 2 endif()
> 
> And place POINT at the end of line 1 and hit RET. It will indent to the start of the comment :(. However, you gave me an idea: use the grand-parent as the anchor (which in this case is the if - exactly what we want). I've attached a new patch which accounts for that.

Ah, I was a bit rusty too. The approach I mentioned should also have a rule for the first sibling which anchors on the parent, while the rest siblings anchor on the previous sibling. But anchoring all on the parent is equally valid.

> 
> Juan, thanks for testing the first patch. Please give this new one a whirl if you're able to.
> 
>> 
>> As for why parent-bol returns the BOL of the comment line, that’s expected: The parent is body, and body starts at the comment, so of course the beginning of the parent line is the beginning of the comment line. In the image below, the highlighted portion marks the body node.
>> 
>> Some tree-sitter grammar would mark the beginning of body at the end of if(TRUE), but what tree-sitter-cmake does here is equally valid.
> 
> Thanks, I understand what's happening now. Whenever line_comment is NOT the first child, the beginning of body is at the end of if(TRUE). If line_comment is the first child, then the beginning of body is at the start of the comment. I was expecting them to behave the same - that's why I was so confused, and I don't really understand why it was done that way.<0001-Fix-cmake-ts-mode-indentation-Bug-66845.patch>

You’re right. That’s a strange behavior. We should probably report to tree-sitter-cmake.

Yuan




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

* bug#66845: 29.1; cmake-ts-mode indentation broken
  2023-11-08  2:52       ` Yuan Fu
@ 2023-11-08  3:42         ` Randy Taylor
  2023-11-08 12:33           ` Eli Zaretskii
  0 siblings, 1 reply; 11+ messages in thread
From: Randy Taylor @ 2023-11-08  3:42 UTC (permalink / raw)
  To: Yuan Fu; +Cc: Juan Palacios, 66845

On Tuesday, November 7th, 2023 at 21:52, Yuan Fu <casouri@gmail.com> wrote:
> 
> > On Nov 7, 2023, at 9:21 AM, Randy Taylor dev@rjt.dev wrote:
> > 
> > On Tuesday, November 7th, 2023 at 03:25, Yuan Fu casouri@gmail.com wrote:
> > 
> > > IMO it makes more sense to use prev-sibling:
> > > 
> > > ,@(ignore-errors
> > > (treesit-query-capture 'cmake '((body) @capture))
> > > `(((parent-is "body") prev-sibling 0)))
> > > 
> > > This would allow users to manually adjust the indentation of a line and have the rest of the body follow that.
> > 
> > This would be much more elegant but unfortunately that and my previous patch failed to account for the following:
> > 
> > 1 if(TRUE) # Comment.
> > 2 endif()
> > 
> > And place POINT at the end of line 1 and hit RET. It will indent to the start of the comment :(. However, you gave me an idea: use the grand-parent as the anchor (which in this case is the if - exactly what we want). I've attached a new patch which accounts for that.
> 
> 
> Ah, I was a bit rusty too. The approach I mentioned should also have a rule for the first sibling which anchors on the parent, while the rest siblings anchor on the previous sibling. But anchoring all on the parent is equally valid.

I see.

Could you (or someone else) install the patch to emacs-29?

> 
> > Juan, thanks for testing the first patch. Please give this new one a whirl if you're able to.
> > 
> > > As for why parent-bol returns the BOL of the comment line, that’s expected: The parent is body, and body starts at the comment, so of course the beginning of the parent line is the beginning of the comment line. In the image below, the highlighted portion marks the body node.
> > > 
> > > Some tree-sitter grammar would mark the beginning of body at the end of if(TRUE), but what tree-sitter-cmake does here is equally valid.
> > 
> > Thanks, I understand what's happening now. Whenever line_comment is NOT the first child, the beginning of body is at the end of if(TRUE). If line_comment is the first child, then the beginning of body is at the start of the comment. I was expecting them to behave the same - that's why I was so confused, and I don't really understand why it was done that way.<0001-Fix-cmake-ts-mode-indentation-Bug-66845.patch>
> 
> 
> You’re right. That’s a strange behavior. We should probably report to tree-sitter-cmake.

I will try to get around to that sometime next week.





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

* bug#66845: 29.1; cmake-ts-mode indentation broken
  2023-11-08  3:42         ` Randy Taylor
@ 2023-11-08 12:33           ` Eli Zaretskii
  0 siblings, 0 replies; 11+ messages in thread
From: Eli Zaretskii @ 2023-11-08 12:33 UTC (permalink / raw)
  To: Randy Taylor; +Cc: jpalaciosdev, casouri, 66845-done

> Cc: Juan Palacios <jpalaciosdev@gmail.com>, 66845@debbugs.gnu.org
> Date: Wed, 08 Nov 2023 03:42:37 +0000
> From: Randy Taylor <dev@rjt.dev>
> 
> > Ah, I was a bit rusty too. The approach I mentioned should also have a rule for the first sibling which anchors on the parent, while the rest siblings anchor on the previous sibling. But anchoring all on the parent is equally valid.
> 
> I see.
> 
> Could you (or someone else) install the patch to emacs-29?

Done, thanks.





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

end of thread, other threads:[~2023-11-08 12:33 UTC | newest]

Thread overview: 11+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-10-31 11:35 bug#66845: 29.1; cmake-ts-mode indentation broken Juan Palacios
2023-10-31 15:50 ` Randy Taylor
2023-11-01  2:45 ` Randy Taylor
2023-11-05 14:32   ` Juan Palacios
2023-11-05 18:16     ` Fu Yuan
2023-11-07  8:25   ` Yuan Fu
2023-11-07 17:21     ` Randy Taylor
2023-11-07 20:11       ` Juan Palacios
2023-11-08  2:52       ` Yuan Fu
2023-11-08  3:42         ` Randy Taylor
2023-11-08 12:33           ` Eli Zaretskii

Code repositories for project(s) associated with this external index

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

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.