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: Eshel Yaron <me@eshelyaron.com>
Cc: 66466@debbugs.gnu.org, Stefan Kangas <stefankangas@gmail.com>
Subject: bug#66466: [PATCH] Support lua-ts-mode in align.el
Date: Fri, 13 Oct 2023 21:19:48 -0500	[thread overview]
Message-ID: <87wmvnx5fi.fsf@pub.pink> (raw)
In-Reply-To: <m1zg0ogrug.fsf@sp-byods-145-109-45-251.wireless.uva.nl>

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

Eshel Yaron <me@eshelyaron.com> writes:

> Hi,
>
> Stefan Kangas <stefankangas@gmail.com> writes:
>
>> john muhl via "Bug reports for GNU Emacs, the Swiss army knife of text
>> editors" <bug-gnu-emacs@gnu.org> writes:
>>
>>> Add support for using align in Lua files.
>>
>> nThanks.
>>
>>> diff --git a/lisp/align.el b/lisp/align.el
>>> index a286addb51f..e6e62ce5726 100644
>>> --- a/lisp/align.el
>>> +++ b/lisp/align.el
>>> @@ -577,7 +577,23 @@ align-rules-list
>>>                      "="
>>>                      (group (zero-or-more (syntax whitespace)))))
>>>       (group . (1 2))
>>> -     (modes . '(conf-toml-mode toml-ts-mode))))
>>> +     (modes . '(conf-toml-mode toml-ts-mode)))
>>> +
>>> +    (lua-assignment
>>> +     (regexp   . ,(concat "\\(?:^\\(?:\\s-*\\(?:local\\s-+\\)?\\(?:[,<>_]"
>>> +                          "\\|\\w\\)+\\)+\\(\\s-*\\)=\\(\\s-*\\)\\)"))
>>> +     (group    . (1 2))
>>> +     (modes    . '(lua-ts-mode))
>>> +     (tab-stop . nil))
>>> +
>>> +    (lua-comment
>>> +     (regexp   . "\\(?:\\(\\s-*\\)--.*\\)")
>>> +     (modes    . '(lua-ts-mode))
>>> +     (column   . comment-column)
>>> +     (valid    . ,(lambda ()
>>> +                    (save-excursion
>>> +                      (goto-char (match-beginning 1))
>>> +                      (not (bolp)))))))
>>
>> Should `lua-mode' also be in `modes'?
>
> Another option would be for `lua-ts-mode` to define
> `align-mode-rules-list` locally, instead of extending `align-rules-list`
> globally.
>
> Also, I noticed that we already have several similar rules for aligning
> assignments and comments in `align-rules-list`.  Do none of them work
> for Lua?

The toml-assignment rule works well enough. I added the Lua modes there
now.

> If so, I wonder what specifics of Lua's syntax make the existing rules
> inapplicable.

For comments I only see the open-comment and c++-comment rules.
open-comment doesn’t match trailing line comments. Extending c++-comment
in the obvious way (Lua uses "--" for comment start) causes trouble with
c++ code like:

  int x = 5; // declare
  x--; // decrement

Lua shares comment syntax with at least Ada, Haskell, SQL and VHDL so I
changed the name of the rule to double-dash-comment and added the Lua
modes there.


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

From ac444d12b7a6098bb609a0b9e48c19094ba2be7d Mon Sep 17 00:00:00 2001
From: john muhl <jm@pub.pink>
Date: Tue, 10 Oct 2023 09:18:10 -0500
Subject: [PATCH] Support lua-ts-mode in align.el

* lisp/align.el (align-rules-list): Add lua-ts-mode.
* lisp/progmodes/lua-ts-mode.el (lua-ts-mode): Indent region
before aligning.
* test/lisp/align-tests.el (align-lua):
* test/lisp/align-resources/lua-ts-mode.erts: Add tests.
---
 lisp/align.el                              | 13 ++++-
 lisp/progmodes/lua-ts-mode.el              |  3 +
 test/lisp/align-resources/lua-ts-mode.erts | 67 ++++++++++++++++++++++
 test/lisp/align-tests.el                   |  6 ++
 4 files changed, 88 insertions(+), 1 deletion(-)
 create mode 100644 test/lisp/align-resources/lua-ts-mode.erts

diff --git a/lisp/align.el b/lisp/align.el
index a286addb51f..9fa78525ecb 100644
--- a/lisp/align.el
+++ b/lisp/align.el
@@ -577,7 +577,18 @@ align-rules-list
                     "="
                     (group (zero-or-more (syntax whitespace)))))
      (group . (1 2))
-     (modes . '(conf-toml-mode toml-ts-mode))))
+     (modes . '(conf-toml-mode toml-ts-mode lua-mode lua-ts-mode)))
+
+    (double-dash-comment
+     (regexp . ,(rx (group (zero-or-more (syntax whitespace)))
+                    "--"
+                    (zero-or-more nonl)))
+     (modes  . '(lua-mode lua-ts-mode))
+     (column . comment-column)
+     (valid  . ,(lambda ()
+                  (save-excursion
+                    (goto-char (match-beginning 1))
+                    (not (bolp)))))))
   "A list describing all of the available alignment rules.
 The format is:
 
diff --git a/lisp/progmodes/lua-ts-mode.el b/lisp/progmodes/lua-ts-mode.el
index 224199dff74..8db6816d6e4 100644
--- a/lisp/progmodes/lua-ts-mode.el
+++ b/lisp/progmodes/lua-ts-mode.el
@@ -472,6 +472,9 @@ lua-ts-mode
                                            "function"))
                                   symbol-end)))))
 
+    ;; Align.
+    (setq-local align-indent-before-aligning t)
+
     (treesit-major-mode-setup))
 
   (add-hook 'flymake-diagnostic-functions #'lua-ts-flymake-luacheck nil 'local))
diff --git a/test/lisp/align-resources/lua-ts-mode.erts b/test/lisp/align-resources/lua-ts-mode.erts
new file mode 100644
index 00000000000..b0473ad6cdf
--- /dev/null
+++ b/test/lisp/align-resources/lua-ts-mode.erts
@@ -0,0 +1,67 @@
+Name: align assignments
+
+=-=
+local first=1
+local s <const> =2
+local last=3
+=-=
+local first     = 1
+local s <const> = 2
+local last      = 3
+=-=-=
+
+Name: align fields
+
+=-=
+local Table={
+first=1,
+second=2,
+last=3,
+}
+=-=
+local Table = {
+    first   = 1,
+    second  = 2,
+    last    = 3,
+}
+=-=-=
+
+Name: align comments
+
+=-=
+local first-- 1
+local second -- 2
+local last	 -- 3
+=-=
+local first         -- 1
+local second        -- 2
+local last          -- 3
+=-=-=
+
+Name: align assignments and comments
+
+=-=
+local first=1-- one
+local second=2 -- two
+local last=3	 -- three
+=-=
+local first  = 1    -- one
+local second = 2    -- two
+local last   = 3    -- three
+=-=-=
+
+Name: align fields and comments
+
+=-=
+local T={
+first=1,--one
+second=2, --two
+last=3,	 --three
+}
+=-=
+local T    = {
+    first  = 1,     --one
+    second = 2,     --two
+    last   = 3,     --three
+}
+=-=-=
diff --git a/test/lisp/align-tests.el b/test/lisp/align-tests.el
index a4d9303827f..4216ad95f4b 100644
--- a/test/lisp/align-tests.el
+++ b/test/lisp/align-tests.el
@@ -49,6 +49,12 @@ align-latex
   (ert-test-erts-file (ert-resource-file "latex-mode.erts")
                       (test-align-transform-fun #'latex-mode)))
 
+(ert-deftest align-lua ()
+  (let ((comment-column 20)
+        (indent-tabs-mode nil))
+    (ert-test-erts-file (ert-resource-file "lua-ts-mode.erts")
+                        (test-align-transform-fun #'lua-ts-mode))))
+
 (ert-deftest align-python ()
   (ert-test-erts-file (ert-resource-file "python-mode.erts")
                       (test-align-transform-fun #'python-mode)))
-- 
2.41.0


  parent reply	other threads:[~2023-10-14  2:19 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-10-11 16:57 bug#66466: [PATCH] Support lua-ts-mode in align.el john muhl via Bug reports for GNU Emacs, the Swiss army knife of text editors
2023-10-12  7:35 ` Stefan Kangas
2023-10-12  8:36   ` Eshel Yaron via Bug reports for GNU Emacs, the Swiss army knife of text editors
2023-10-12 11:20     ` Stefan Kangas
2023-10-14  2:19     ` john muhl via Bug reports for GNU Emacs, the Swiss army knife of text editors [this message]
2023-10-21 10:27       ` Stefan Kangas
2023-10-13  3:13   ` john muhl via Bug reports for GNU Emacs, the Swiss army knife of text editors
2023-10-15 14:25     ` Stefan Kangas
2023-10-15 14:59       ` Philip Kaludercic
2023-10-15 15:18         ` Stefan Kangas

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=87wmvnx5fi.fsf@pub.pink \
    --to=bug-gnu-emacs@gnu.org \
    --cc=66466@debbugs.gnu.org \
    --cc=jm@pub.pink \
    --cc=me@eshelyaron.com \
    --cc=stefankangas@gmail.com \
    /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).