unofficial mirror of bug-gnu-emacs@gnu.org 
 help / color / mirror / code / Atom feed
From: Andrey Listopadov <andreyorst@gmail.com>
To: john muhl <jm@pub.pink>
Cc: 66159@debbugs.gnu.org, Philip Kaludercic <philipk@posteo.net>,
	Eli Zaretskii <eliz@gnu.org>
Subject: bug#66159: 30.0.50; lua-ts-mode semantic indentation problems
Date: Sat, 30 Sep 2023 12:59:28 +0300	[thread overview]
Message-ID: <87wmw83qg2.fsf@gmail.com> (raw)
In-Reply-To: <87fs302oz3.fsf@pub.pink>


john muhl <jm@pub.pink> writes:

> I agree there’s no immediate need.
>
> Could you explain what the lua-indent-* options do in lua-mode? I tried
> toggling them but didn’t see any difference between on/off.

I don't really know it myself, sorry. There seem to be some specifics
regarding how the content inside parenthesis is indented, but I'm not
using these either.

>> There are also some weirdness in semantic navigation, but it's more
>> likely that I'm just not used to new ts-backed navigation yet.
>
> I’m sure there is room for improvement here too. Suggestions welcome.

Here are a few examples. the █ represents the point:

█(function (x) return x + 1 end)(41)

Pressing C-M-f (forward-sexp) puts the point here:

 (function (x)█return x + 1 end)(41)

I would expect it to go over the whole parenthesized expression:

 (function (x) return x + 1 end)█(41)

Another weird thing here:

 function foo (x)
     if false then
         print "foo"
     elseif true then
         print "bar"
     end
 end█

Pressing C-M-b (backward-sexp) puts the point here:

 function foo (x)
    █if false then
         print "foo"
     elseif true then
         print "bar"
     end
 end

I would expect it to go before the `function' instead:

█function foo (x)
     if false then
         print "foo"
     elseif true then
         print "bar"
     end
 end

>> If you're willing to dig into some (pretty crazy) involved examples, I
>> can send here some really convoluted nested anonymous functions that
>> currently are indented in a weird way in both modes. Neither does it
>> exactly right in my opiion, but I also don't know if there is the right
>> way to indent this.  I can send these examples later this week once I
>> finish an article I'm working on rightnow.
>
> Sure. Whenever you have the time.

I've tried a couple of Lua formatters available, and as far as I can
see, most of them indent my code in a weird way too, so perhaps it's not
really a problem, although I'm not sure.

For example, here's a piece of code that implements an iterator wrapper
with anonymous functions only:

1  function partitioning (n, collect)
2      return function (iterator)
3          return function (...)
4              return (
5                  function (next1, ...)
6                      local count, arr, last = 0, {}
7                      return (function (f, ...)
8                          return (function (i, ...) return i(i, ...) end)(
9                              function (i, ...)
10                                 return f(function (x, ...)
11                                     return i(i, ...)(x, ...)
12                                 end, ...)
13                             end)
14                     end)(function (step)
15                         return function(iterable, ...)
16                             return (function (recur, iterable, ...)
17                                 if nil ~= ... then
18                                     if n-1 == count then
19                                         local arr1, count1 = arr, count
20                                         arr, count = {}, 0
21                                         return (function (...)
22                                             last = ...
23                                             return ...
24                                         end)(collect(arr1, count1+1, ...))
25                                     else
26                                         count = count + 1
27                                         return recur(iterable,
28                                                      (function (...)
29                                                          last = ...
30                                                          return ...
31                                                      end)(collect(arr, count, ...)))
32                                     end
33                                 elseif count ~= 0 then
34                                     count = 0
35                                     return last, arr
36                                 end
37                             end)(step, iterable, next1(iterable, ...))
38                         end
39                     end), ...
40                 end)(iterator(...))
41         end
42     end
43 end


Here are the lines I find strange.

First, the Immediately Invoked Function Expression (IIFE) indentation is
all over the place:

8                          return (function (i, ...) return i(i, ...) end)(
9                              function (i, ...)
10                                 return f(function (x, ...)
11                                     return i(i, ...)(x, ...)
12                                 end, ...)
13                             end)

The I would expect the inner IIFE to be indented like this:

8                          return (function (i, ...) return i(i, ...) end)(
9                              function (i, ...)
10                                 return f(function (x, ...)
11                                              return i(i, ...)(x, ...)
12                                          end, ...)
13                             end)


Another strange snippet:

21                                         return (function (...)
22                                             last = ...
23                                             return ...
24                                         end)(collect(arr1, count1+1, ...))

The `last' and `return' statements are not properly indented to the
function's scope. I expected it to be indented like this:

21                                         return (function (...)
22                                                     last = ...
23                                                     return ...
24                                                 end)(collect(arr1, count1+1, ...))

And, weirdly, enough, a bit further in the code there's a similar
expression, but it is indented correctly:

27                                         return recur(iterable,
28                                                      (function (...)
29                                                          last = ...
30                                                          return ...
31                                                      end)(collect(arr, count, ...)))

I know, that this code is convoluted, and probably no one writes like
this, it was done on purpose for an article about the power of anonymous
functions, so perhaps it isn't really a problem for most Lua
programmers. But it makes reading code a bit harder in rare cases like
this. I have more examples in the article, feel free to grab them if you
need code snippets for testing:

https://andreyor.st/posts/2023-09-26-iterator-based-transducers-in-lua/

Other than that, and the navigation issues I've mentioned above, the
mode works solidly. I've also noticed that some of the `lua-mode'
default shortcuts are missing, like `C-c C-f' (lua-search-documentation)
and `C-c C-l' (lua-send-buffer), but these may be out of the scope of
this module, so let me know if I should just define these myself.

Thanks!


--
Andrey Listopadov





  reply	other threads:[~2023-09-30  9:59 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
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 [this message]
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=87wmw83qg2.fsf@gmail.com \
    --to=andreyorst@gmail.com \
    --cc=66159@debbugs.gnu.org \
    --cc=eliz@gnu.org \
    --cc=jm@pub.pink \
    --cc=philipk@posteo.net \
    /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).