unofficial mirror of bug-gnu-emacs@gnu.org 
 help / color / mirror / code / Atom feed
From: john muhl via "Bug reports for GNU Emacs, the Swiss army knife of text editors" <bug-gnu-emacs@gnu.org>
To: Andrey Listopadov <andreyorst@gmail.com>
Cc: 66159@debbugs.gnu.org
Subject: bug#66159: 30.0.50; lua-ts-mode semantic indentation problems
Date: Sun, 24 Sep 2023 10:06:50 -0500	[thread overview]
Message-ID: <87v8bz3905.fsf@pub.pink> (raw)
In-Reply-To: <87fs36vvxl.fsf@gmail.com>

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

Andrey Listopadov <andreyorst@gmail.com> writes:

> I've been editing Lua using the long-existing lua-mode[1] and it works
> well enough for me, but it's indentation implementation is very slow.
> Indenting a file with just 600 lines takes several seconds on my
> machine.  So when I saw that Emacs now features a lua-ts-mode package
> in its core, I tried to move to it, in hopes that it would be faster.

Thanks for trying.

> However, a lot of code is now indented differently, not how I used to,
> and this creates a lot of noise in commits, as I often re-indent the
> whole buffer before saving the file. Here are some examples.

The "then" and table examples should indent how you like with the
attached patch. Arguments and parameters now also work like your table
example so you can have:

  local function fn(a, b, c,
                    another) end

  fn(1,
     2,
     3,
     {a="first",
      b="second"})

> Another thing that bothers me is that I prefer Gassanenko-style packing
> of `end' keywords so that they vertically align with the scope of the
> opened block, as it saves so much vertical space and is easier for me to
> read, but lua-ts-mode moves it to the latest innermost indentation
> level, as opposed to the outermost depending on the count of ends in the
> line itself:
>
> function lowest_entropy_cell(world)
> |   local lowest,res=math.huge,nil
> |   for y=1,world.height do
> |   |   for x=1,world.width do
> |   |   |   local cell=world:get(x,y)
> |   |   |   if cell.is_set then
> |   |   |   |   local e=cell_enthropy(cell)
> |   |   |   |   trace(e)
> |   |   |   |   if e <= lowest then
> |   |   |   |   |   lowest,res=e,{x,y}
> |   end end end end
> |   return res or {math.random(world.width),math.random(world.height)}
> end

I don't see any reason not to support that style but I'm not sure how to
do it. A patch would be welcome but I'll try to figure it out sometime.

> Should I expect this to be the default new way of indenting or the
> indentation can be made to match the lua-mode where it is sensible?

Sure. It's a new mode so nothing is really set in stone. Let me know if
you have other suggestions.


[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: 0001-Improve-indentation-in-lus-ts-mode-Bug-66159.patch --]
[-- Type: text/x-patch, Size: 5287 bytes --]

From d24d1d128fd29ead355334612ccca3f5e2e1fbd2 Mon Sep 17 00:00:00 2001
From: john muhl <jm@pub.pink>
Date: Sat, 23 Sep 2023 10:49:11 -0500
Subject: [PATCH] Improve indentation in lus-ts-mode (Bug#66159)

- Align "then" with if/elseif/else/end when it appears at the
  beginning of a line.

- Anchor indent of the first child in arguments, parameters and
  tables to the parent and the other children to the first.

* lisp/progmodes/lua-ts-mode.el (lua-ts--first-child)
(lua-ts--simple-indent-rules): Improve indentation.
* test/lisp/progmodes/lua-ts-mode-resources/indent.erts: Add
tests.
---
 lisp/progmodes/lua-ts-mode.el                 |  17 ++-
 .../lua-ts-mode-resources/indent.erts         | 125 ++++++++++++++++++
 2 files changed, 139 insertions(+), 3 deletions(-)

diff --git a/lisp/progmodes/lua-ts-mode.el b/lisp/progmodes/lua-ts-mode.el
index 030a3585158..3d946dd1de2 100644
--- a/lisp/progmodes/lua-ts-mode.el
+++ b/lisp/progmodes/lua-ts-mode.el
@@ -233,6 +233,10 @@ lua-ts--font-lock-settings
    '((ERROR) @font-lock-warning-face))
   "Tree-sitter font-lock settings for `lua-ts-mode'.")
 
+(defun lua-ts--first-child (node _p _b &rest _)
+  "Matches if NODE is the first among its siblings."
+  (= (treesit-node-index node) 1))
+
 (defvar lua-ts--simple-indent-rules
   `((lua
      ((parent-is "chunk") column-0 0)
@@ -240,6 +244,7 @@ lua-ts--simple-indent-rules
      ((parent-is "block") parent-bol 0)
      ((node-is "}") parent-bol 0)
      ((node-is ")") parent-bol 0)
+     ((node-is "then") standalone-parent 0)
      ((node-is "else_statement") parent-bol 0)
      ((node-is "elseif_statement") parent-bol 0)
      ((node-is "end") parent-bol 0)
@@ -251,9 +256,15 @@ lua-ts--simple-indent-rules
      ((parent-is "else_statement") parent-bol lua-ts-indent-offset)
      ((parent-is "repeat_statement") parent-bol lua-ts-indent-offset)
      ((parent-is "while_statement") parent-bol lua-ts-indent-offset)
-     ((parent-is "table_constructor") parent-bol lua-ts-indent-offset)
-     ((parent-is "arguments") parent-bol lua-ts-indent-offset)
-     ((parent-is "parameters") parent-bol lua-ts-indent-offset)
+     ((and (parent-is "table_constructor") lua-ts--first-child)
+      parent-bol lua-ts-indent-offset)
+     ((parent-is "table_constructor") (nth-sibling 1) 0)
+     ((and (parent-is "arguments") lua-ts--first-child)
+      parent-bol lua-ts-indent-offset)
+     ((parent-is "arguments") (nth-sibling 1) 0)
+     ((and (parent-is "parameters") lua-ts--first-child)
+      parent-bol lua-ts-indent-offset)
+     ((parent-is "parameters") (nth-sibling 1) 0)
      ((parent-is "ERROR") no-indent 0))))
 
 (defvar lua-ts--syntax-table
diff --git a/test/lisp/progmodes/lua-ts-mode-resources/indent.erts b/test/lisp/progmodes/lua-ts-mode-resources/indent.erts
index 040225c8580..999e01e3e37 100644
--- a/test/lisp/progmodes/lua-ts-mode-resources/indent.erts
+++ b/test/lisp/progmodes/lua-ts-mode-resources/indent.erts
@@ -32,6 +32,22 @@ f({
 ;(function()
 return false
 )()
+
+function foo (e)
+    if e == nil
+      then return 1000
+    else return e
+    end
+end
+
+function foo (e)
+    if e == nil
+    then
+        return 1000
+    else
+        return e
+    end
+end
 =-=
 print(
   0,
@@ -57,6 +73,22 @@ f({
 ;(function()
   return false
 )()
+
+function foo (e)
+  if e == nil
+  then return 1000
+  else return e
+  end
+end
+
+function foo (e)
+  if e == nil
+  then
+    return 1000
+  else
+    return e
+  end
+end
 =-=-=
 
 Name: Argument Indent
@@ -77,6 +109,13 @@ cost = 2,
 length = 8,
 	parallelism = 4,
 })
+
+fn(1,
+2,
+     3)
+
+fn(1, 2,
+3)
 =-=
 function h(
   string,
@@ -93,6 +132,13 @@ local p = h(
     length = 8,
     parallelism = 4,
   })
+
+fn(1,
+   2,
+   3)
+
+fn(1, 2,
+   3)
 =-=-=
 
 Name: Continuation Indent
@@ -150,3 +196,82 @@ repeat
   z = z * 2
 until z > 12
 =-=-=
+
+Name: Parameter Indent
+
+=-=
+fn(a,
+b) end
+
+fn(a, b,
+c) end
+
+fn(
+a,
+b
+) end
+=-=
+fn(a,
+   b) end
+
+fn(a, b,
+   c) end
+
+fn(
+  a,
+  b
+) end
+=-=-=
+
+Code:
+  (lambda ()
+    (setq indent-tabs-mode nil)
+    (setq lua-ts-indent-offset 4)
+    (lua-ts-mode)
+    (indent-region (point-min) (point-max)))
+
+Name: Table Indent
+
+=-=
+local Recipe = {
+    Floor={up={Floor=true,Wall=true},
+        down={Floor=true,Wall=true},
+        left={Floor=true,Wall=true},
+        right={Floor=true,Wall=true}},
+    Wall={up={Floor=true,Wall=true},
+        down={Floor=true,Wall=true},
+        left={Floor=true,Wall=true},
+        right={Floor=true,Wall=true}},
+    Corridor={up={Corridoor=true},
+        down={Corridoor=true},
+        left={Corridoor=true},
+        right={Corridoor=true}}
+}
+
+local Other = {
+a = 1,
+ b = 2,
+  c = 3,
+}
+=-=
+local Recipe = {
+    Floor={up={Floor=true,Wall=true},
+           down={Floor=true,Wall=true},
+           left={Floor=true,Wall=true},
+           right={Floor=true,Wall=true}},
+    Wall={up={Floor=true,Wall=true},
+          down={Floor=true,Wall=true},
+          left={Floor=true,Wall=true},
+          right={Floor=true,Wall=true}},
+    Corridor={up={Corridoor=true},
+              down={Corridoor=true},
+              left={Corridoor=true},
+              right={Corridoor=true}}
+}
+
+local Other = {
+    a = 1,
+    b = 2,
+    c = 3,
+}
+=-=-=
-- 
2.41.0


  parent reply	other threads:[~2023-09-24 15:06 UTC|newest]

Thread overview: 25+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-09-22 19:17 bug#66159: 30.0.50; lua-ts-mode semantic indentation problems Andrey Listopadov
2023-09-22 19:49 ` Eli Zaretskii
2023-09-24 15:06 ` john muhl via Bug reports for GNU Emacs, the Swiss army knife of text editors [this message]
2023-09-24 15:44   ` Eli Zaretskii
2023-09-24 16:38   ` Andrey Listopadov
2023-09-24 18:20     ` john muhl via Bug reports for GNU Emacs, the Swiss army knife of text editors
2023-09-26 19:21       ` Andrey Listopadov
2023-09-27  1:18         ` john muhl via Bug reports for GNU Emacs, the Swiss army knife of text editors
2023-09-30  9:59           ` Andrey Listopadov
2023-09-30 13:57             ` john muhl via Bug reports for GNU Emacs, the Swiss army knife of text editors
2023-10-03 15:04               ` john muhl via Bug reports for GNU Emacs, the Swiss army knife of text editors
2023-10-03 19:13                 ` Andrey Listopadov
2023-09-30  7:52       ` Philip Kaludercic
2023-10-06 19:44 ` bug#66159: [PATCH] Various improvements to lua-ts-mode (Bug#66159) john muhl via Bug reports for GNU Emacs, the Swiss army knife of text editors
2023-10-07 10:11   ` Mauro Aranda
2023-10-07 16:15   ` Andrey Listopadov
2023-10-07 18:10     ` john muhl via Bug reports for GNU Emacs, the Swiss army knife of text editors
2023-10-08  9:43       ` Andrey Listopadov
2023-10-09  3:28         ` john muhl via Bug reports for GNU Emacs, the Swiss army knife of text editors
2023-10-17  3:26           ` john muhl via Bug reports for GNU Emacs, the Swiss army knife of text editors
2023-10-20 20:40             ` Stefan Kangas
2023-10-22 20:03               ` john muhl via Bug reports for GNU Emacs, the Swiss army knife of text editors
2023-10-23  8:11                 ` Stefan Kangas
2023-10-21  5:15             ` Andrey
2023-10-21 11:37             ` Andrey

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/emacs/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=87v8bz3905.fsf@pub.pink \
    --to=bug-gnu-emacs@gnu.org \
    --cc=66159@debbugs.gnu.org \
    --cc=andreyorst@gmail.com \
    --cc=jm@pub.pink \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

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

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).