Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Lua indentation code in Lua [closed]

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.

like image 509
John Smith Avatar asked May 29 '12 14:05

John Smith


People also ask

Does lua care about indentation?

Lua codes are not like Python when it comes to indentation. So, most of the code you will write will work even if it falls on another line, and you don't necessarily need to have the nested code to be intended by a certain tab size.

How do I fix indentation code?

Select your code and press Tab or Shift + Tab to indent or un-indent it. Other tools for altering your code can be found in the Edit section.

Should I indent my code?

No. HTML code does not need to be indented, and all browsers and search engines ignore indentation and extra spacing. However, for any human reader, it's a good idea to indent your text because it makes the code easier to scan and read.

How do you add an indentation code?

Just use the CSS type selector p and set the text-indent property to the value you want. In the example below, let's use a percentage. It will only indent the first line by default.


1 Answers

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

like image 128
Paul Kulchenko Avatar answered Sep 18 '22 15:09

Paul Kulchenko