Lua indentation code in Lua [closed]
Asked Answered
C

6

16

Does there exist Lua code to indent Lua code?

I have a lot of lua code where indenting it would help, and its in a pure lua environment.

In defence of my question: For some of you here the situation here sounds impossible. It is very similar to requesting for Emacs Lisp code to format text. This is a real live working (company internal) Lua environment. This is a pure lua environment. I am looking for a Lua tool which can indent Lua code, not an opinion about the best editor. Paul Kulchenko has provided an amazing solution and so has Yusuf X.

Crin answered 29/5, 2012 at 14:57 Comment(6)
This is not really a Lua question, this is a text editor question. Any editor worth it's salt should be able to indent code. I indent code in Vim by hitting =.Mannino
@Mannino It is most definitely a Lua question. The environment I have has only Lua and nothing else. I know of external editors which can edit Lua.Crin
@"The environment I have has only Lua and nothing else." You have no editor? Really?Mannino
@Mud, yes really. I have a basic editor written in C, with a lot of the support code in Lua. It's not a desktop computer...Crin
here's another one if anyone's still looking. It preserves your strings and comments and can space your operators if you wish and more(sounds like a sales pitch). it's bout 400 LOC.Earthen
You may needs a modern editor linke Sublime Text or Atom.Resupine
C
15

I have written a Lua beautifier and applied it to my project with a large group of Lua files (about 8k lines in total). The beautifier is in Perl (and yes, I saw that the question is about it being in Lua), but (1) it is only 55 lines and it should be easily translatable to Lua (I just haven't got time to do that yet), and (2) you can format your files somewhere else and transfer back to your environment (unless you need this re-formatting to be done on a regular basis).

This beautifier is based on another Lua-based one (udead link as of 3/11/2013; web archive copy), but the original one didn't handle all the cases that I was interested in, so I ended up re-writing it. It may still work for you though.

The beautifier doesn't change the line structure (it was one of the requirements), which makes it easy to check, but changes the indentation and formatting (and handles complex cases of embedded anonymous functions and other similar things). The link includes some examples of what it does. You can also review the long commit I did for our project with most of the converted files: https://github.com/pkulchenko/ZeroBraneStudio/commit/85be27559058ce085e95cef47de2c11185f9211a.

If you are looking only for an easy way to indent your files (rather than to integrate the indentation logic into your application), then you can use ZeroBrane Studio IDE (Edit | Source | Correct Indentation or Ctrl-I).

Cameraman answered 8/6, 2012 at 17:29 Comment(5)
I see that I can adapt the Lua Beautifier to my needs quite easily. That is almost exactly what I was looking for.Crin
The Lua-based link is dead.Spruill
Thanks Oliver; updated the post. Unfortunately I couldn't find a copy of the original Lua code.Cameraman
It does not handle multi-line comments and strings (of [[ ]] and --[[ ]] style) and any non-matching text inside those can easily throw it off track. It also doesn't detect logical operators that are often used in Lua.Literacy
Hi, @JohnSmith could you share your version of Lua Beautifier?Schacker
P
2

Here's one: http://lua-users.org/wiki/SourceCodeFormatter

Postglacial answered 29/5, 2012 at 15:46 Comment(1)
It doesn't look like that actually does real indentation, in the sense of parsing the code and deciding how each line should be indented. Rather, it just just changes existing indentation into some canonical form, depending on the existing indentation to be correct.Yeryerevan
S
1

I tried a number of these but with no real success. Then I hit on the solution: the Eclipse based Lua Development tools.

http://eclipse.org/koneki/ldt/

Download what you need for your version (if you have a 64 bit windows, the 32 bit version doesn't work). Run the .exe (no install required!), then do ctrl+shift+f, and your code is all pretty printed!

Sunstroke answered 22/3, 2015 at 8:24 Comment(0)
P
0

Also https://github.com/LuaDist/luapretty

Although it is Lua-based, it requires some libraries. See the README.

Pani answered 29/5, 2012 at 14:57 Comment(0)
E
0

Try this code formatter written in Lua, it has a few options and you can always edit it if you want to customize it beyond the options:

http://lua-users.org/wiki/SourceCodeFormatter

Erethism answered 3/8, 2012 at 22:44 Comment(0)
P
0

Sounds like Serpent might be what you are looking for; pretty-prints Lua source code with indentation.

Popinjay answered 7/9, 2012 at 20:40 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.