all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
* bug#70785: [PATCH] Improve indentation in 'lua-ts-mode'
@ 2024-05-05 12:54 john muhl
  2024-05-05 13:26 ` john muhl
  0 siblings, 1 reply; 3+ messages in thread
From: john muhl @ 2024-05-05 12:54 UTC (permalink / raw)
  To: 70785

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'





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

* bug#70785: [PATCH] Improve indentation in 'lua-ts-mode'
  2024-05-05 12:54 bug#70785: [PATCH] Improve indentation in 'lua-ts-mode' john muhl
@ 2024-05-05 13:26 ` john muhl
  2024-05-09  3:38   ` Yuan Fu
  0 siblings, 1 reply; 3+ messages in thread
From: john muhl @ 2024-05-05 13:26 UTC (permalink / raw)
  To: 70785

[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #1: 0001-Improve-indentation-in-lua-ts-mode-bug-70785.patch --]
[-- Type: text/x-patch, Size: 7725 bytes --]

From 4d3408607fbf560f5427fbe6193307b16a82ceff Mon Sep 17 00:00:00 2001
From: john muhl <jm@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






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

* bug#70785: [PATCH] Improve indentation in 'lua-ts-mode'
  2024-05-05 13:26 ` john muhl
@ 2024-05-09  3:38   ` Yuan Fu
  0 siblings, 0 replies; 3+ messages in thread
From: Yuan Fu @ 2024-05-09  3:38 UTC (permalink / raw)
  To: john muhl; +Cc: 70785



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

Merged. Thank you!

Yuan





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

end of thread, other threads:[~2024-05-09  3:38 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-05-05 12:54 bug#70785: [PATCH] Improve indentation in 'lua-ts-mode' john muhl
2024-05-05 13:26 ` john muhl
2024-05-09  3:38   ` Yuan Fu

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.