unofficial mirror of bug-gnu-emacs@gnu.org 
 help / color / mirror / code / Atom feed
* bug#68297: [PATCH] Support indented continuation lines in lua-ts-mode
@ 2024-01-06 18:56 john muhl via Bug reports for GNU Emacs, the Swiss army knife of text editors
  2024-01-06 22:37 ` john muhl via Bug reports for GNU Emacs, the Swiss army knife of text editors
  2024-01-07  0:39 ` john muhl via Bug reports for GNU Emacs, the Swiss army knife of text editors
  0 siblings, 2 replies; 5+ messages in thread
From: john muhl via Bug reports for GNU Emacs, the Swiss army knife of text editors @ 2024-01-06 18:56 UTC (permalink / raw)
  To: 68297

Add rules for indenting multi-line variables and if/elseif
statements, e.g.:

  local very_long_variable_name =
      "ABC"..
      "XYZ"
  local v = 123 *
      456 +
      789

  if a
      and b
      or c then
      print(1)
  elseif b
      and c
      or a then
      print(0)
  end

The lua-ts-indent-continuation-lines option can be set to nil to
keep if statements flush left:

  if a
  and b
  or c then
      print(1)
  elseif b
  and c
  or a then
      print(0)
  end

The option controlling this behavior in the EmmyLua code
formatter does not affect multi-line variables so neither does
lua-ts-indent-continuation-lines.





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

* bug#68297: [PATCH] Support indented continuation lines in lua-ts-mode
  2024-01-06 18:56 bug#68297: [PATCH] Support indented continuation lines in lua-ts-mode john muhl via Bug reports for GNU Emacs, the Swiss army knife of text editors
@ 2024-01-06 22:37 ` john muhl via Bug reports for GNU Emacs, the Swiss army knife of text editors
  2024-01-06 22:57   ` john muhl via Bug reports for GNU Emacs, the Swiss army knife of text editors
  2024-01-07  0:39 ` john muhl via Bug reports for GNU Emacs, the Swiss army knife of text editors
  1 sibling, 1 reply; 5+ messages in thread
From: john muhl via Bug reports for GNU Emacs, the Swiss army knife of text editors @ 2024-01-06 22:37 UTC (permalink / raw)
  To: 68297

[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #1: 0001-Support-indented-continuation-lines-in-lua-ts-mode.patch --]
[-- Type: text/x-patch, Size: 4627 bytes --]

From 8e73025f048bcd5c405a19fc090f7c58cb728c5a Mon Sep 17 00:00:00 2001
From: john muhl <jm@pub.pink>
Date: Sat, 6 Jan 2024 09:36:33 -0600
Subject: [PATCH] Support indented continuation lines in lua-ts-mode

* lisp/progmodes/lua-ts-mode.el (lua-ts--simple-indent-rules):
Add a rule to indent multi-line assignments and if statements.
(lua-ts-indent-continuation-lines): New user option.
* test/lisp/progmodes/lua-ts-mode-resources/indent.erts: Add
tests.  (bug#68279)
---
 lisp/progmodes/lua-ts-mode.el                 | 47 ++++++++++++
 .../lua-ts-mode-resources/indent.erts         | 76 +++++++++++++++++++
 2 files changed, 123 insertions(+)

diff --git a/lisp/progmodes/lua-ts-mode.el b/lisp/progmodes/lua-ts-mode.el
index e9ffb47cdb2..63a33a14ed7 100644
--- a/lisp/progmodes/lua-ts-mode.el
+++ b/lisp/progmodes/lua-ts-mode.el
@@ -150,6 +150,28 @@ lua-ts-align-params-and-props
   :group 'lua-ts
   :version "30.1")
 
+(defcustom lua-ts-indent-continuation-lines t
+  "Controls how multi-line if/else statements are aligned.
+
+If t, then continuation lines are indented by `lua-ts-indent-offset':
+
+  if a
+      and b then
+      print(1)
+  end
+
+If nil, then continuation lines are aligned with the beginning of
+the statement:
+
+  if a
+  and b then
+      print(1)
+  end"
+  :type 'boolean
+  :safe 'booleanp
+  :group 'lua-ts
+  :version "30.1")
+
 (defvar lua-ts--builtins
   '("assert" "bit32" "collectgarbage" "coroutine" "debug" "dofile"
     "error" "getmetatable" "io" "ipairs" "load" "loadfile"
@@ -362,6 +384,17 @@ lua-ts--simple-indent-rules
      ((or (match "end" "function_definition")
           (node-is "end"))
       standalone-parent 0)
+     ((n-p-gp "expression_list" "assignment_statement" "variable_declaration")
+      lua-ts--variable-declaration-continuation-anchor
+      lua-ts-indent-offset)
+     ((and (parent-is "binary_expression")
+           lua-ts--variable-declaration-continuation)
+      lua-ts--variable-declaration-continuation-anchor
+      lua-ts-indent-offset)
+     ((and (lambda (&rest _) lua-ts-indent-continuation-lines)
+           (parent-is "binary_expression"))
+      standalone-parent lua-ts-indent-offset)
+     ((parent-is "binary_expression") standalone-parent 0)
      ((or (parent-is "function_declaration")
           (parent-is "function_definition")
           (parent-is "do_statement")
@@ -448,6 +481,20 @@ 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--variable-declaration-continuation (node &rest _)
+  "Matches if NODE is part of a multi-line variable declaration."
+  (treesit-parent-until node
+                        (lambda (p)
+                          (equal "variable_declaration"
+                                 (treesit-node-type p)))))
+
+(defun lua-ts--variable-declaration-continuation-anchor (node &rest _)
+  "Return the start position of the variable declaration for NODE."
+  (save-excursion
+    (goto-char (treesit-node-start
+                (lua-ts--variable-declaration-continuation node)))
+    (line-beginning-position)))
+
 (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 5e6ce7935f8..2e7384cac88 100644
--- a/test/lisp/progmodes/lua-ts-mode-resources/indent.erts
+++ b/test/lisp/progmodes/lua-ts-mode-resources/indent.erts
@@ -529,6 +529,46 @@ local Other = {
 }
 =-=-=
 
+Name: Continuation Indent
+
+=-=
+local very_long_variable_name =
+"ok"..
+      "ok"
+local n = a +
+b *
+c /
+1
+local x = "A"..
+"B"
+.."C"
+if a
+     and b
+           and c then
+elseif a
+     or b
+    or c then
+end
+=-=
+local very_long_variable_name =
+  "ok"..
+  "ok"
+local n = a +
+  b *
+  c /
+  1
+local x = "A"..
+  "B"
+  .."C"
+if a
+  and b
+  and c then
+elseif a
+  or b
+  or c then
+end
+=-=-=
+
 Code:
   (lambda ()
     (setq indent-tabs-mode nil)
@@ -711,3 +751,39 @@ tbl = {1,2,
   3,4,
   5,6}
 =-=-=
+
+Code:
+  (lambda ()
+    (setq indent-tabs-mode nil)
+    (setq lua-ts-indent-continuation-lines nil)
+    (setq lua-ts-indent-offset 2)
+    (lua-ts-mode)
+    (indent-region (point-min) (point-max)))
+
+Name: Unaligned Continuation Indent
+
+=-=
+local n = a +
+  b *
+  c /
+  1
+if a
+     and b
+and c then
+elseif a
+       or b
+          or c then
+end
+=-=
+local n = a +
+  b *
+  c /
+  1
+if a
+and b
+and c then
+elseif a
+or b
+or c then
+end
+=-=-=
-- 
2.41.0






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

* bug#68297: [PATCH] Support indented continuation lines in lua-ts-mode
  2024-01-06 22:37 ` john muhl via Bug reports for GNU Emacs, the Swiss army knife of text editors
@ 2024-01-06 22:57   ` john muhl via Bug reports for GNU Emacs, the Swiss army knife of text editors
  0 siblings, 0 replies; 5+ messages in thread
From: john muhl via Bug reports for GNU Emacs, the Swiss army knife of text editors @ 2024-01-06 22:57 UTC (permalink / raw)
  To: 68297

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

Ignore the previous. I accidentally made it from the wrong
branch. This one should apply cleanly.


[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: 0001-Support-indented-continuation-lines-in-lua-ts-mode.patch --]
[-- Type: text/x-patch, Size: 4650 bytes --]

From 86b5bba1d3c2850151ff280be6df458d898cdf11 Mon Sep 17 00:00:00 2001
From: john muhl <jm@pub.pink>
Date: Sat, 6 Jan 2024 09:36:33 -0600
Subject: [PATCH] Support indented continuation lines in lua-ts-mode

* lisp/progmodes/lua-ts-mode.el (lua-ts--simple-indent-rules):
Add a rule to indent multi-line assignments and if statements.
(lua-ts-indent-continuation-lines): New user option.
* test/lisp/progmodes/lua-ts-mode-resources/indent.erts: Add
tests.  (bug#68279)
---
 lisp/progmodes/lua-ts-mode.el                 | 47 ++++++++++++
 .../lua-ts-mode-resources/indent.erts         | 76 +++++++++++++++++++
 2 files changed, 123 insertions(+)

diff --git a/lisp/progmodes/lua-ts-mode.el b/lisp/progmodes/lua-ts-mode.el
index 3b600f59521..714fab8e852 100644
--- a/lisp/progmodes/lua-ts-mode.el
+++ b/lisp/progmodes/lua-ts-mode.el
@@ -122,6 +122,28 @@ lua-ts-inferior-history
   :group 'lua-ts
   :version "30.1")
 
+(defcustom lua-ts-indent-continuation-lines t
+  "Controls how multi-line if/else statements are aligned.
+
+If t, then continuation lines are indented by `lua-ts-indent-offset':
+
+  if a
+      and b then
+      print(1)
+  end
+
+If nil, then continuation lines are aligned with the beginning of
+the statement:
+
+  if a
+  and b then
+      print(1)
+  end"
+  :type 'boolean
+  :safe 'booleanp
+  :group 'lua-ts
+  :version "30.1")
+
 (defvar lua-ts--builtins
   '("assert" "bit32" "collectgarbage" "coroutine" "debug" "dofile"
     "error" "getmetatable" "io" "ipairs" "load" "loadfile"
@@ -329,6 +351,17 @@ lua-ts--simple-indent-rules
      ((or (match "end" "function_definition")
           (node-is "end"))
       standalone-parent 0)
+     ((n-p-gp "expression_list" "assignment_statement" "variable_declaration")
+      lua-ts--variable-declaration-continuation-anchor
+      lua-ts-indent-offset)
+     ((and (parent-is "binary_expression")
+           lua-ts--variable-declaration-continuation)
+      lua-ts--variable-declaration-continuation-anchor
+      lua-ts-indent-offset)
+     ((and (lambda (&rest _) lua-ts-indent-continuation-lines)
+           (parent-is "binary_expression"))
+      standalone-parent lua-ts-indent-offset)
+     ((parent-is "binary_expression") standalone-parent 0)
      ((or (parent-is "function_declaration")
           (parent-is "function_definition")
           (parent-is "do_statement")
@@ -415,6 +448,20 @@ 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--variable-declaration-continuation (node &rest _)
+  "Matches if NODE is part of a multi-line variable declaration."
+  (treesit-parent-until node
+                        (lambda (p)
+                          (equal "variable_declaration"
+                                 (treesit-node-type p)))))
+
+(defun lua-ts--variable-declaration-continuation-anchor (node &rest _)
+  "Return the start position of the variable declaration for NODE."
+  (save-excursion
+    (goto-char (treesit-node-start
+                (lua-ts--variable-declaration-continuation node)))
+    (line-beginning-position)))
+
 (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 9797467bbe5..a535c46f871 100644
--- a/test/lisp/progmodes/lua-ts-mode-resources/indent.erts
+++ b/test/lisp/progmodes/lua-ts-mode-resources/indent.erts
@@ -529,6 +529,46 @@ local Other = {
 }
 =-=-=
 
+Name: Continuation Indent
+
+=-=
+local very_long_variable_name =
+"ok"..
+      "ok"
+local n = a +
+b *
+c /
+1
+local x = "A"..
+"B"
+.."C"
+if a
+     and b
+           and c then
+elseif a
+     or b
+    or c then
+end
+=-=
+local very_long_variable_name =
+  "ok"..
+  "ok"
+local n = a +
+  b *
+  c /
+  1
+local x = "A"..
+  "B"
+  .."C"
+if a
+  and b
+  and c then
+elseif a
+  or b
+  or c then
+end
+=-=-=
+
 Code:
   (lambda ()
     (setq indent-tabs-mode nil)
@@ -677,3 +717,39 @@ function e (n, t)
                 end)(i(...))
 end end end
 =-=-=
+
+Code:
+  (lambda ()
+    (setq indent-tabs-mode nil)
+    (setq lua-ts-indent-continuation-lines nil)
+    (setq lua-ts-indent-offset 2)
+    (lua-ts-mode)
+    (indent-region (point-min) (point-max)))
+
+Name: Unaligned Continuation Indent
+
+=-=
+local n = a +
+  b *
+  c /
+  1
+if a
+     and b
+and c then
+elseif a
+       or b
+          or c then
+end
+=-=
+local n = a +
+  b *
+  c /
+  1
+if a
+and b
+and c then
+elseif a
+or b
+or c then
+end
+=-=-=
-- 
2.41.0


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

* bug#68297: [PATCH] Support indented continuation lines in lua-ts-mode
  2024-01-06 18:56 bug#68297: [PATCH] Support indented continuation lines in lua-ts-mode john muhl via Bug reports for GNU Emacs, the Swiss army knife of text editors
  2024-01-06 22:37 ` john muhl via Bug reports for GNU Emacs, the Swiss army knife of text editors
@ 2024-01-07  0:39 ` john muhl via Bug reports for GNU Emacs, the Swiss army knife of text editors
  2024-01-11 20:52   ` Stefan Kangas
  1 sibling, 1 reply; 5+ messages in thread
From: john muhl via Bug reports for GNU Emacs, the Swiss army knife of text editors @ 2024-01-07  0:39 UTC (permalink / raw)
  To: john muhl; +Cc: 68297

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

...and now I see that only handled top-level statements. This
one works with nested ifs and variables too.


[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: 0001-Support-indented-continuation-lines-in-lua-ts-mode.patch --]
[-- Type: text/x-patch, Size: 5123 bytes --]

From ed76e90d51924f9c457e8c4c90a535be01052777 Mon Sep 17 00:00:00 2001
From: john muhl <jm@pub.pink>
Date: Sat, 6 Jan 2024 09:36:33 -0600
Subject: [PATCH] Support indented continuation lines in lua-ts-mode

* lisp/progmodes/lua-ts-mode.el (lua-ts--simple-indent-rules):
Add a rule to indent multi-line assignments and if statements.
(lua-ts-indent-continuation-lines): New user option.
* test/lisp/progmodes/lua-ts-mode-resources/indent.erts: Add
tests.  (bug#68279)
---
 lisp/progmodes/lua-ts-mode.el                 |  49 ++++++++
 .../lua-ts-mode-resources/indent.erts         | 106 ++++++++++++++++++
 2 files changed, 155 insertions(+)

diff --git a/lisp/progmodes/lua-ts-mode.el b/lisp/progmodes/lua-ts-mode.el
index 3b600f59521..05a3ff6d7c6 100644
--- a/lisp/progmodes/lua-ts-mode.el
+++ b/lisp/progmodes/lua-ts-mode.el
@@ -122,6 +122,28 @@ lua-ts-inferior-history
   :group 'lua-ts
   :version "30.1")
 
+(defcustom lua-ts-indent-continuation-lines t
+  "Controls how multi-line if/else statements are aligned.
+
+If t, then continuation lines are indented by `lua-ts-indent-offset':
+
+  if a
+      and b then
+      print(1)
+  end
+
+If nil, then continuation lines are aligned with the beginning of
+the statement:
+
+  if a
+  and b then
+      print(1)
+  end"
+  :type 'boolean
+  :safe 'booleanp
+  :group 'lua-ts
+  :version "30.1")
+
 (defvar lua-ts--builtins
   '("assert" "bit32" "collectgarbage" "coroutine" "debug" "dofile"
     "error" "getmetatable" "io" "ipairs" "load" "loadfile"
@@ -329,6 +351,17 @@ lua-ts--simple-indent-rules
      ((or (match "end" "function_definition")
           (node-is "end"))
       standalone-parent 0)
+     ((n-p-gp "expression_list" "assignment_statement" "variable_declaration")
+      lua-ts--variable-declaration-continuation-anchor
+      lua-ts-indent-offset)
+     ((and (parent-is "binary_expression")
+           lua-ts--variable-declaration-continuation)
+      lua-ts--variable-declaration-continuation-anchor
+      lua-ts-indent-offset)
+     ((and (lambda (&rest _) lua-ts-indent-continuation-lines)
+           (parent-is "binary_expression"))
+      standalone-parent lua-ts-indent-offset)
+     ((parent-is "binary_expression") standalone-parent 0)
      ((or (parent-is "function_declaration")
           (parent-is "function_definition")
           (parent-is "do_statement")
@@ -415,6 +448,22 @@ 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--variable-declaration-continuation (node &rest _)
+  "Matches if NODE is part of a multi-line variable declaration."
+  (treesit-parent-until node
+                        (lambda (p)
+                          (equal "variable_declaration"
+                                 (treesit-node-type p)))))
+
+(defun lua-ts--variable-declaration-continuation-anchor (node &rest _)
+  "Return the start position of the variable declaration for NODE."
+  (save-excursion
+    (goto-char (treesit-node-start
+                (lua-ts--variable-declaration-continuation node)))
+    (when (looking-back (rx bol (* whitespace))
+                        (line-beginning-position))
+      (point))))
+
 (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 9797467bbe5..48184160b4d 100644
--- a/test/lisp/progmodes/lua-ts-mode-resources/indent.erts
+++ b/test/lisp/progmodes/lua-ts-mode-resources/indent.erts
@@ -529,6 +529,58 @@ local Other = {
 }
 =-=-=
 
+Name: Continuation Indent
+
+=-=
+local very_long_variable_name =
+"ok"..
+      "ok"
+local n = a +
+b *
+c /
+1
+local x = "A"..
+"B"
+.."C"
+if a
+     and b
+           and c then
+             if x
+                   and y then
+    local x = 1 +
+2 *
+         3
+     end
+elseif a
+     or b
+    or c then
+end
+=-=
+local very_long_variable_name =
+  "ok"..
+  "ok"
+local n = a +
+  b *
+  c /
+  1
+local x = "A"..
+  "B"
+  .."C"
+if a
+  and b
+  and c then
+  if x
+    and y then
+    local x = 1 +
+      2 *
+      3
+  end
+elseif a
+  or b
+  or c then
+end
+=-=-=
+
 Code:
   (lambda ()
     (setq indent-tabs-mode nil)
@@ -677,3 +729,57 @@ function e (n, t)
                 end)(i(...))
 end end end
 =-=-=
+
+Code:
+  (lambda ()
+    (setq indent-tabs-mode nil)
+    (setq lua-ts-indent-continuation-lines nil)
+    (setq lua-ts-indent-offset 2)
+    (lua-ts-mode)
+    (indent-region (point-min) (point-max)))
+
+Name: Unaligned Continuation Indent
+
+=-=
+local n = a +
+  b *
+  c /
+  1
+if a
+     and b
+and c then
+ if x
+  and y then
+      local x = 1 +
+       2 *
+             3
+   end
+elseif a
+       or b
+          or c then
+          if x
+              or y
+              end
+end
+=-=
+local n = a +
+  b *
+  c /
+  1
+if a
+and b
+and c then
+  if x
+  and y then
+    local x = 1 +
+      2 *
+      3
+  end
+elseif a
+or b
+or c then
+  if x
+  or y
+  end
+end
+=-=-=
-- 
2.41.0


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

* bug#68297: [PATCH] Support indented continuation lines in lua-ts-mode
  2024-01-07  0:39 ` john muhl via Bug reports for GNU Emacs, the Swiss army knife of text editors
@ 2024-01-11 20:52   ` Stefan Kangas
  0 siblings, 0 replies; 5+ messages in thread
From: Stefan Kangas @ 2024-01-11 20:52 UTC (permalink / raw)
  To: john muhl; +Cc: 68297-done

john muhl <jm@pub.pink> writes:

> ...and now I see that only handled top-level statements. This
> one works with nested ifs and variables too.

Thanks, installed on master (43b4993d73b).





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

end of thread, other threads:[~2024-01-11 20:52 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-01-06 18:56 bug#68297: [PATCH] Support indented continuation lines in lua-ts-mode john muhl via Bug reports for GNU Emacs, the Swiss army knife of text editors
2024-01-06 22:37 ` john muhl via Bug reports for GNU Emacs, the Swiss army knife of text editors
2024-01-06 22:57   ` john muhl via Bug reports for GNU Emacs, the Swiss army knife of text editors
2024-01-07  0:39 ` john muhl via Bug reports for GNU Emacs, the Swiss army knife of text editors
2024-01-11 20:52   ` Stefan Kangas

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).