]> git.eshelyaron.com Git - emacs.git/commitdiff
Improve comment indenting in 'lua-ts-mode'
authorjohn muhl <jm@pub.pink>
Sun, 10 Nov 2024 17:26:33 +0000 (11:26 -0600)
committerEshel Yaron <me@eshelyaron.com>
Wed, 20 Nov 2024 12:34:24 +0000 (13:34 +0100)
* 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)

(cherry picked from commit d592832504554c6ee30bea263e55dc84feaec18d)

lisp/progmodes/lua-ts-mode.el
test/lisp/progmodes/lua-ts-mode-resources/indent.erts

index 20bc1f3e158a4b0f22de575a67319cc85f39ea29..8956bd73626dfcbd902094e624ba9ad171afec54 100644 (file)
@@ -289,7 +289,8 @@ values of OVERRIDE."
 
 (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 @@ values of OVERRIDE."
     (= 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 @@ values of OVERRIDE."
                         (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)
index ba7bad1b452145b90634a12c3fdfe5ade1f650fd..b0ece4cc26169b666f8cdb96d8a44b2dc33ff678 100644 (file)
@@ -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