Script indenter plugin

I made a plugin that indents poorly/un-indented scripts. I saw lots of complaints on scriptinghelpers.org and its discord about people who write strangely indented code that nobody else can read very easily, so I tried automating indentation. Indenting a 1000 line script takes less than 1 second.

Select a script and click indent.

source: plugin:CreateToolbar("Indenter"):CreateButton("Indent","Indent","").Click:connec - Pastebin.com

1 Like

Your source code makes me an unhappy person.

It’s iterative layers of parsing that breaks the code into sections that can be interpreted separately. Comments, strings, new lines, words and individual symbols are each their own index in a table that gets run through, and if a word is something like “do” or “end” then it changes the level of indentation.

No, I understood all of that, it just is not at all pleasing to the eye.

Could it be?

If the variables weren’t single letter variables and it had a few comments, yes, but beyond that not really.

Doesn’t really matter, though. People like me aren’t the ones the plugin is for, and the people it’s aimed at will probably never read the source.

the source looks fine, but the target audience aren’t the kind of peope who would think to use a plugin to make life easier–they straight up don’t know the value of indentation

this plugin is nice for those of us who have to decode their code

I am wondering if there’s any input (that doesn’t error) that breaks the plugin. I’m thinking all of the parsers are foolproof so that something like comments inside a string and strings inside comments, etc. shouldn’t cause an issue.

some fixes plugin:CreateToolbar("Indenter"):CreateButton("Indent","Indent","").Click:connec - Pastebin.com
things like print("--") would cause confusion.

I just use Stravant’s Minifier plugin, as it also has an option “Beautify”. Had to fix an issue with it, though, as in one or two cases it would error:

  • If you had “\k” (or any other non-\-able character), it would error, while Lua just parses it as “k”
  • There was something related to binary statements, I think if nonBoolean then, that would error

@1waffle1 might try the “parse then unparse” approach too, as it allows much better formatting, even beyond the scale of “just indenting”. Good job on yours though, not a lot of people aren’t that good wih parsing stuff.

The closest I have to a Lua parser (not counting my semi-finished Java one) is one that knows the difference between comments, strings and “other code”. Actually, because I use it in my Java to detect Lua code between {}, I also made it count the non-string/comment { and } so I know when it reached the end of native { LUACODE } (unless it’s invalid Lua code and has a mismatched {} thing). You could try something like that, which should fix having – in strings or code-looking stuff in (long) comments (if that’s still an issue)

now I feel like making a “Clean up” (ala eclipse, beyond just formatting) plugin too, but I’m too lazy right now