GNU bug report logs - #70785
[PATCH] Improve indentation in 'lua-ts-mode'

Previous Next

Package: emacs;

Reported by: john muhl <jm <at> pub.pink>

Date: Sun, 5 May 2024 12:55:01 UTC

Severity: normal

Tags: patch

Fixed in version 30.1

Done: Yuan Fu <casouri <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 70785 in the body.
You can then email your comments to 70785 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#70785; Package emacs. (Sun, 05 May 2024 12:55:01 GMT) Full text and rfc822 format available.

Acknowledgement sent to john muhl <jm <at> pub.pink>:
New bug report received and forwarded. Copy sent to bug-gnu-emacs <at> gnu.org. (Sun, 05 May 2024 12:55:02 GMT) Full text and rfc822 format available.

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

From: john muhl <jm <at> pub.pink>
To: bug-gnu-emacs <at> gnu.org
Subject: [PATCH] Improve indentation in 'lua-ts-mode'
Date: Sun, 05 May 2024 07:54:04 -0500
Tags: patch

I was contacted off-list about a few tweaks to the indentation.
They seemed fine so here they are.

1. Change it so that comments are ignored when indenting table
fields and function arguments/parameters:

  -- before
  local mt = { -- the comment
               first = 1,
               second = 2,
             }

  -- after
  local mt = { -- the comment
    first = 1,
    second = 2,
  }

2. It also changes how tables are handled in function arguments:

  -- before
  fn(true, {
             x = 1,
             y = 0,
           })

  -- after
  fn(true, {
    x = 1,
    y = 0,
  })

3. And it changes how simple use of an anonymous function is indented:

  -- before
  fn(function()
       print'ok'
     end)

  -- after
  fn(function()
    print'ok'
  end)

In GNU Emacs 30.0.50 (build 1, x86_64-pc-linux-gnu, GTK+ Version
 3.24.41, cairo version 1.18.0) of 2024-05-02 built on localhost
Repository revision: 962bb71b4a813eb9c3832494746695d32d6f9109
Repository branch: master
System Description: Fedora Linux 39 (Thirty Nine)

Configured using:
 'configure --with-pgtk --prefix=/home/jm/.local'




Information forwarded to bug-gnu-emacs <at> gnu.org:
bug#70785; Package emacs. (Sun, 05 May 2024 13:30:02 GMT) Full text and rfc822 format available.

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

From: john muhl <jm <at> pub.pink>
To: 70785 <at> debbugs.gnu.org
Subject: Re: bug#70785: [PATCH] Improve indentation in 'lua-ts-mode'
Date: Sun, 05 May 2024 08:26:44 -0500
[0001-Improve-indentation-in-lua-ts-mode-bug-70785.patch (text/x-patch, attachment)]
From 4d3408607fbf560f5427fbe6193307b16a82ceff Mon Sep 17 00:00:00 2001
From: john muhl <jm <at> pub.pink>
Date: Fri, 3 May 2024 15:51:01 -0500
Subject: [PATCH] Improve indentation in 'lua-ts-mode' (bug#70785)

* lisp/progmodes/lua-ts-mode.el (lua-ts--simple-indent-rules):
- Ignore comments when aligning arguments, parameters and fields.
- Apply simpler rules to simpler usage of anonymous functions.
- Better handling of table as a function argument.
(lua-ts--comment-first-sibling-matcher):
(lua-ts--first-real-sibling-anchor):
(lua-ts--last-arg-function-call-matcher):
(lua-ts--top-level-function-call-matcher): New function.
(lua-ts--g-parent):
(lua-ts--g-g-parent): New function.
(lua-ts--g-g-g-parent): Use it.
* test/lisp/progmodes/lua-ts-mode-resources/indent.erts:
Add tests.
---
 lisp/progmodes/lua-ts-mode.el                 | 63 +++++++++++++++++--
 .../lua-ts-mode-resources/indent.erts         | 40 ++++++++++++
 2 files changed, 97 insertions(+), 6 deletions(-)

diff --git a/lisp/progmodes/lua-ts-mode.el b/lisp/progmodes/lua-ts-mode.el
index 6b55fefbf84..57aa3db1c85 100644
--- a/lisp/progmodes/lua-ts-mode.el
+++ b/lisp/progmodes/lua-ts-mode.el
@@ -291,6 +291,14 @@ lua-ts--simple-indent-rules
           (parent-is "string_content")
           (node-is "]]"))
       no-indent 0)
+     ((and (n-p-gp "field" "table_constructor" "arguments")
+           lua-ts--multi-arg-function-call-matcher
+           lua-ts--last-arg-function-call-matcher)
+      standalone-parent lua-ts-indent-offset)
+     ((and (n-p-gp "}" "table_constructor" "arguments")
+           lua-ts--multi-arg-function-call-matcher
+           lua-ts--last-arg-function-call-matcher)
+      standalone-parent 0)
      ((and (n-p-gp "field" "table_constructor" "arguments")
            lua-ts--multi-arg-function-call-matcher)
       parent lua-ts-indent-offset)
@@ -311,10 +319,15 @@ lua-ts--simple-indent-rules
           (and (parent-is "parameters") lua-ts--first-child-matcher)
           (and (parent-is "table_constructor") lua-ts--first-child-matcher))
       standalone-parent lua-ts-indent-offset)
+     ((and (not lua-ts--comment-first-sibling-matcher)
+           (or (parent-is "arguments")
+               (parent-is "parameters")
+               (parent-is "table_constructor")))
+      lua-ts--first-real-sibling-anchor 0)
      ((or (parent-is "arguments")
           (parent-is "parameters")
           (parent-is "table_constructor"))
-      (nth-sibling 1) 0)
+      standalone-parent lua-ts-indent-offset)
      ((and (n-p-gp "block" "function_definition" "parenthesized_expression")
            lua-ts--nested-function-block-matcher
            lua-ts--nested-function-block-include-matcher)
@@ -337,6 +350,9 @@ lua-ts--simple-indent-rules
            lua-ts--nested-function-end-matcher
            lua-ts--nested-function-last-function-matcher)
       parent 0)
+     ((and (n-p-gp "end" "function_definition" "arguments")
+           lua-ts--top-level-function-call-matcher)
+      standalone-parent 0)
      ((n-p-gp "end" "function_definition" "arguments") parent 0)
      ((or (match "end" "function_definition")
           (node-is "end"))
@@ -385,24 +401,39 @@ lua-ts--function-definition-p
   "Return t if NODE is a function_definition."
   (equal "function_definition" (treesit-node-type node)))
 
+(defun lua-ts--g-parent (node)
+  "Return the grand-parent of NODE."
+  (let ((parent (treesit-node-parent node)))
+    (treesit-node-parent parent)))
+
+(defun lua-ts--g-g-parent (node)
+  "Return the great-grand-parent of NODE."
+  (treesit-node-parent (lua-ts--g-parent node)))
+
 (defun lua-ts--g-g-g-parent (node)
   "Return the great-great-grand-parent of NODE."
-  (let* ((parent (treesit-node-parent node))
-         (g-parent (treesit-node-parent parent))
-         (g-g-parent (treesit-node-parent g-parent)))
-    (treesit-node-parent g-g-parent)))
+  (treesit-node-parent (lua-ts--g-g-parent node)))
 
 (defun lua-ts--multi-arg-function-call-matcher (_n parent &rest _)
   "Matches if PARENT has multiple arguments."
   (> (treesit-node-child-count (treesit-node-parent parent)) 3))
 
+(defun lua-ts--last-arg-function-call-matcher (node parent &rest _)
+  "Matches if NODE's PARENT is the last argument in a function call."
+  (let* ((g-parent (lua-ts--g-parent node))
+         (last (1- (treesit-node-child-count g-parent t))))
+    (treesit-node-eq parent (seq-elt (treesit-node-children g-parent t) last))))
+
 (defun lua-ts--nested-function-argument-matcher (node &rest _)
   "Matches if NODE is in a nested function argument."
   (save-excursion
     (goto-char (treesit-node-start node))
     (treesit-beginning-of-defun)
     (backward-char 2)
-    (not (looking-at ")("))))
+    (and (not (looking-at ")("))
+         (not (equal "chunk"
+                     (treesit-node-type
+                      (lua-ts--g-parent (treesit-node-at (point)))))))))
 
 (defun lua-ts--nested-function-block-matcher (node &rest _)
   "Matches if NODE is in a nested function block."
@@ -438,6 +469,26 @@ lua-ts--nested-function-last-function-matcher
          (treesit-induce-sparse-tree parent #'lua-ts--function-definition-p)))
     (= 1 (length (cadr sparse-tree)))))
 
+(defun lua-ts--comment-first-sibling-matcher (node &rest _)
+  "Matches if NODE if it's previous sibling is a comment."
+  (let ((sibling (treesit-node-prev-sibling node)))
+    (equal "comment" (treesit-node-type sibling))))
+
+(defun lua-ts--top-level-function-call-matcher (node &rest _)
+  "Matches if NODE is within a top-level function call."
+  (let* ((g-g-p (lua-ts--g-g-parent node))
+         (g-g-g-p (lua-ts--g-g-g-parent node)))
+    (and (equal "function_call" (treesit-node-type g-g-p))
+         (equal "chunk" (treesit-node-type g-g-g-p)))))
+
+(defun lua-ts--first-real-sibling-anchor (_n parent _)
+  "Return the start position of the first non-comment child of PARENT."
+  (treesit-node-start
+   (seq-first
+    (seq-filter
+     (lambda (n) (not (equal "comment" (treesit-node-type n))))
+     (treesit-node-children parent t)))))
+
 (defun lua-ts--variable-declaration-continuation (node &rest _)
   "Matches if NODE is part of a multi-line variable declaration."
   (treesit-parent-until node
diff --git a/test/lisp/progmodes/lua-ts-mode-resources/indent.erts b/test/lisp/progmodes/lua-ts-mode-resources/indent.erts
index 48184160b4d..ba7bad1b452 100644
--- a/test/lisp/progmodes/lua-ts-mode-resources/indent.erts
+++ b/test/lisp/progmodes/lua-ts-mode-resources/indent.erts
@@ -66,6 +66,10 @@ end
 return f
 end
 
+f6(function()
+print'ok'
+end)
+
 ;(function ()
  return true
  end)()
@@ -118,6 +122,10 @@ function f6(...)
   return f
 end
 
+f6(function()
+  print'ok'
+end)
+
 ;(function ()
   return true
 end)()
@@ -406,6 +414,15 @@ a = 1,
 b = 2,
 },
 nil)
+
+Test(nil, {
+            a = 1,
+            b = 2,
+          })
+
+fn( -- comment
+    1,
+    2)
 =-=
 h(
   "string",
@@ -443,6 +460,15 @@ Test({
        b = 2,
      },
      nil)
+
+Test(nil, {
+  a = 1,
+  b = 2,
+})
+
+fn( -- comment
+  1,
+  2)
 =-=-=
 
 Name: Parameter Indent
@@ -464,6 +490,9 @@ local f3 = function( a, b,
                 c, d )
 print(a,b,c,d)
 end
+
+local f4 = function(-- comment
+a, b, c)
 =-=
 function f1(
   a,
@@ -481,6 +510,9 @@ local f3 = function( a, b,
                      c, d )
   print(a,b,c,d)
 end
+
+local f4 = function(-- comment
+  a, b, c)
 =-=-=
 
 Name: Table Indent
@@ -506,6 +538,10 @@ a = 1,
  b = 2,
   c = 3,
 }
+
+local a = { -- hello world!
+            b = 10
+}
 =-=
 local Other = {
   First={up={Step=true,Jump=true},
@@ -527,6 +563,10 @@ local Other = {
   b = 2,
   c = 3,
 }
+
+local a = { -- hello world!
+  b = 10
+}
 =-=-=
 
 Name: Continuation Indent
-- 
2.41.0





Information forwarded to bug-gnu-emacs <at> gnu.org:
bug#70785; Package emacs. (Thu, 09 May 2024 03:39:02 GMT) Full text and rfc822 format available.

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

From: Yuan Fu <casouri <at> gmail.com>
To: john muhl <jm <at> pub.pink>
Cc: 70785 <at> debbugs.gnu.org
Subject: Re: bug#70785: [PATCH] Improve indentation in 'lua-ts-mode'
Date: Wed, 8 May 2024 20:38:10 -0700

> On May 5, 2024, at 6:26 AM, john muhl <jm <at> pub.pink> wrote:
> 
> <0001-Improve-indentation-in-lua-ts-mode-bug-70785.patch>

Merged. Thank you!

Yuan




bug marked as fixed in version 30.1, send any further explanations to 70785 <at> debbugs.gnu.org and john muhl <jm <at> pub.pink> Request was from Yuan Fu <casouri <at> gmail.com> to control <at> debbugs.gnu.org. (Thu, 09 May 2024 04:32:02 GMT) Full text and rfc822 format available.

bug archived. Request was from Debbugs Internal Request <help-debbugs <at> gnu.org> to internal_control <at> debbugs.gnu.org. (Thu, 06 Jun 2024 11:24:11 GMT) Full text and rfc822 format available.

This bug report was last modified 1 year and 69 days ago.

Previous Next


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