Subject: bug#74298: [PATCH] ; Improve comment indenting in 'lua-ts-mode'

From: john muhl <[email-masked]>
Date: Sun, 10 Nov 2024 13:00:31 -0600

>From cdcbdeafa2b35a7584b7c4c5a19e999476e3a897 Mon Sep 17 00:00:00 2001
From: john muhl <[email-masked]>
Date: Sun, 10 Nov 2024 11:26:33 -0600
Subject: [PATCH] ; Improve comment indenting in 'lua-ts-mode'

* lisp/progmodes/lua-ts-mode.el (lua-ts--simple-indent-rules):
Align single line comments with the surrounding context.
(lua-ts--comment-first-sibling-matcher): Check that comment is
the first sibling.
(lua-ts--multi-line-comment-start): New function.
* test/lisp/progmodes/lua-ts-mode-resources/indent.erts:
Add tests.  (Bug#74298)
---
 lisp/progmodes/lua-ts-mode.el                 | 17 ++++++--
 .../lua-ts-mode-resources/indent.erts         | 42 +++++++++++++++++++
 2 files changed, 56 insertions(+), 3 deletions(-)

diff --git a/lisp/progmodes/lua-ts-mode.el b/lisp/progmodes/lua-ts-mode.el
index a7763377177..e5a2fafd279 100644
--- a/lisp/progmodes/lua-ts-mode.el
+++ b/lisp/progmodes/lua-ts-mode.el
@@ -289,7 +289,8 @@ lua-ts--font-lock-settings
 
 (defvar lua-ts--simple-indent-rules
   `((lua
-     ((or (node-is "comment")
+     ((or (and (node-is "comment") (parent-is "chunk"))
+          lua-ts--multi-line-comment-start
           (parent-is "comment_content")
           (parent-is "string_content")
           (node-is "]]"))
@@ -473,9 +474,10 @@ lua-ts--nested-function-last-function-matcher
     (= 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."
+  "Matches NODE if its previous sibling is a comment."
   (let ((sibling (treesit-node-prev-sibling node)))
-    (equal "comment" (treesit-node-type sibling))))
+    (and (= 0 (treesit-node-index sibling t))
+         (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."
@@ -508,6 +510,15 @@ lua-ts--variable-declaration-continuation-anchor
                         (line-beginning-position))
       (point))))
 
+(defun lua-ts--multi-line-comment-start (node &rest _)
+  "Matches if NODE is the beginning of a multi-line comment."
+  (and node
+       (equal "comment" (treesit-node-type node))
+       (save-excursion
+         (goto-char (treesit-node-start node))
+         (forward-char 2)               ; Skip the -- part.
+         (looking-at "\\[\\["))))
+
 (defvar lua-ts--syntax-table
   (let ((table (make-syntax-table)))
     (modify-syntax-entry ?+  "."    table)
diff --git a/test/lisp/progmodes/lua-ts-mode-resources/indent.erts b/test/lisp/progmodes/lua-ts-mode-resources/indent.erts
index ba7bad1b452..b0ece4cc261 100644
--- a/test/lisp/progmodes/lua-ts-mode-resources/indent.erts
+++ b/test/lisp/progmodes/lua-ts-mode-resources/indent.erts
@@ -360,6 +360,10 @@ multi-line
     ]]
   return true
 end
+
+  --[[
+Long comment.
+    ]]
 =-=
 --[[
       Multi-line
@@ -373,6 +377,44 @@ multi-line
     ]]
   return true
 end
+
+  --[[
+Long comment.
+    ]]
+=-=-=
+
+Name: Comment Indent
+
+=-=
+local fn1 = function (a, b)
+-- comment
+return a + b
+end
+
+local tb1 = {
+  first = 1,
+-- comment
+  second = 2,
+}
+
+local tb9 = { one = 1,
+-- comment
+   two = 2 }
+=-=
+local fn1 = function (a, b)
+  -- comment
+  return a + b
+end
+
+local tb1 = {
+  first = 1,
+  -- comment
+  second = 2,
+}
+
+local tb9 = { one = 1,
+	      -- comment
+	      two = 2 }
 =-=-=
 
 Name: Argument Indent
-- 
2.47.0

john muhl <[email-masked]> writes:

> Tags: patch
>
> This improves nested comment indenting such that:
>
>   local tbl = {
>     one = 1,
>   -- comment
>     two = 2,
>   }
>
> becomes:
>
>   local tbl = {
>     one = 1,
>     -- comment
>     two = 2,
>   }