Lua code formatters and beautifiers

classic Classic list List threaded Threaded
7 messages Options
Reply | Threaded
Open this post in threaded view
|

Lua code formatters and beautifiers

Luiz Henrique de Figueiredo
To complement the discussion on code style, what Lua code formatters and
beautifiers are available and which ones do you recommend?

The wiki has one but not a list:
        http://lua-users.org/wiki/SourceCodeFormatter

I wish my ltokenp could be used for that but it discards comments
because it uses the lexer from the Lua core library.

Reply | Threaded
Open this post in threaded view
|

Re: Lua code formatters and beautifiers

Russell Haley
Sorry for the top post ( my BlackBerry phone doesn't do online comments very well)

An option to NOT discard the comments (or perhaps a secondary api call?) ‎sounds like a great little 'tweak' to the core library that would provide a desirable benefit. How much effort would that so called 'little tweak' be (just curious, not volunteering)?

Russ

Sent from my BlackBerry 10 smartphone on the Virgin Mobile network.
  Original Message  
From: Luiz Henrique de Figueiredo
Sent: Saturday, June 10, 2017 4:14 AM
To: [hidden email]
Reply To: Lua mailing list
Subject: Lua code formatters and beautifiers

To complement the discussion on code style, what Lua code formatters and
beautifiers are available and which ones do you recommend?

The wiki has one but not a list:
http://lua-users.org/wiki/SourceCodeFormatter

I wish my ltokenp could be used for that but it discards comments
because it uses the lexer from the Lua core library.


Reply | Threaded
Open this post in threaded view
|

Re: Lua code formatters and beautifiers

Luiz Henrique de Figueiredo
> An option to NOT discard the comments (or perhaps a secondary api call?) ???sounds like a great little 'tweak' to the core library that would provide a desirable benefit. How much effort would that so called 'little tweak' be (just curious, not volunteering)?

Not much effort at all, it seems. Here are the diffs in llex.c from Lua 5.3.4:

450c450
<             read_long_string(ls, NULL, sep);  /* skip long comment */
---
>             read_long_string(ls, seminfo, sep);  /* skip long comment */
451a452
>             return 'C';
457c458,465
<           next(ls);  /* skip until end of line (or end of file) */
---
>           save_and_next(ls); /* skip until end of line (or end of file) */
>  {
>           TString *ts;
>           ts = luaX_newstring(ls, luaZ_buffer(ls->buff),
>                                   luaZ_bufflen(ls->buff));
>           seminfo->ts = ts;
>  }
>             return 'c';

The patched code returns 'C' for a long comment and 'c' for a short
comment, with the contents of the comment in seminfo->ts. There is a
small bug that the first char in long comments is dropped but it is
probably easy to fix; I haven't tried.

I just added support for this to proxy.c in ltokenp and it works:

  switch (t)
  {
    case 'C':
    case 'c':
    case TK_STRING:
    case TK_NAME:
     lua_pushstring(L,getstr(seminfo->ts));
     break;
     ...

Reply | Threaded
Open this post in threaded view
|

Re: Lua code formatters and beautifiers

Jay Carlson
For any purpose other than destructive indentation, it would be nice to have both the whitespace and comments ("greyspace") associated with the token stream. It should be possible to write a lua-lex-cat program equivalent to cat for (at least) well-formed Lua programs.

 Jay

On Jun 10, 2017 2:35 PM, "Luiz Henrique de Figueiredo" <[hidden email]> wrote:
> An option to NOT discard the comments (or perhaps a secondary api call?) ???sounds like a great little 'tweak' to the core library that would provide a desirable benefit. How much effort would that so called 'little tweak' be (just curious, not volunteering)?

Not much effort at all, it seems. Here are the diffs in llex.c from Lua 5.3.4:

450c450
<             read_long_string(ls, NULL, sep);  /* skip long comment */
---
>             read_long_string(ls, seminfo, sep);  /* skip long comment */
451a452
>             return 'C';
457c458,465
<           next(ls);  /* skip until end of line (or end of file) */
---
>           save_and_next(ls); /* skip until end of line (or end of file) */
>         {
>           TString *ts;
>           ts = luaX_newstring(ls, luaZ_buffer(ls->buff),
>                                   luaZ_bufflen(ls->buff));
>           seminfo->ts = ts;
>         }
>             return 'c';

The patched code returns 'C' for a long comment and 'c' for a short
comment, with the contents of the comment in seminfo->ts. There is a
small bug that the first char in long comments is dropped but it is
probably easy to fix; I haven't tried.

I just added support for this to proxy.c in ltokenp and it works:

  switch (t)
  {
    case 'C':
    case 'c':
    case TK_STRING:
    case TK_NAME:
     lua_pushstring(L,getstr(seminfo->ts));
     break;
     ...


Reply | Threaded
Open this post in threaded view
|

Re: Lua code formatters and beautifiers

Martin
In reply to this post by Russell Haley
On 06/10/2017 09:07 AM, Russell Haley wrote:
> An option to NOT discard the comments (or perhaps a secondary api call?) ‎sounds like a great little 'tweak' to the core library that would provide a desirable benefit. How much effort would that so called 'little tweak' be (just curious, not volunteering)?

For me the problem in code formatter with comments is what to do with
them? They can occur at any whitespace, and in many cases I see no way
to represent them in formatted code nicely. (There are some personally
disguising formatting styles in a wild which do not conform with style
reformatter wish.)

For example:

  local function
  -- bla-blah
  foo(  a -- parameter "a"
      , b -- parameter "b"
     )
    return   -- "return" returns values
           a -- ref to "a"
           + -- plus means sum
           b -- b!
  end

More mad cases may be constructed:

  local t = { [ --[[ hey, why not place comment here? ]] 1] = 1}

Simplest option is to drop comments at all. Other way is to group them
before parent statement. (I'm going to try latter in my code formatter
"lcf" someday.)

-- Martin

Reply | Threaded
Open this post in threaded view
|

Re: Lua code formatters and beautifiers

Patrick Donnelly
In reply to this post by Luiz Henrique de Figueiredo
On Sat, Jun 10, 2017 at 4:13 AM, Luiz Henrique de Figueiredo
<[hidden email]> wrote:
> To complement the discussion on code style, what Lua code formatters and
> beautifiers are available and which ones do you recommend?

I wrote Lunadry using LPeg but it's not very configurable:
https://github.com/batrick/lunadry

--
Patrick Donnelly

Reply | Threaded
Open this post in threaded view
|

Re: Lua code formatters and beautifiers

Luiz Henrique de Figueiredo
In reply to this post by Luiz Henrique de Figueiredo
This patch in llex.c no longer drops characters in comments:

$ diff llex.c,orig llex.c
448d447
<           luaZ_resetbuffer(ls->buff);  /* 'skip_sep' may dirty the buffer */
450c449
<             read_long_string(ls, NULL, sep);  /* skip long comment */
---
>             read_long_string(ls, seminfo, sep);  /* skip long comment */
452c451
<             break;
---
>             return 'C';
457,458c456,459
<           next(ls);  /* skip until end of line (or end of file) */
<         break;
---
>           save_and_next(ls); /* skip until end of line (or end of file) */
>         seminfo->ts = luaX_newstring(ls, luaZ_buffer(ls->buff),
>                                   luaZ_bufflen(ls->buff));
>         return 'c';

As before, the patched code returns 'C' for a long comment and 'c' for a short
comment, with the contents of the comment in seminfo->ts.
 
Again, adding support for this in ltokenp is just adding two lines in proxy.c:
    switch (t)
    {
      case 'C':
      case 'c':
      case TK_STRING:
      case TK_NAME:
       lua_pushstring(L,getstr(seminfo->ts));
       break;
       ...

All feedback welcome.