sile-typesetter / sile

The SILE Typesetter — Simon’s Improved Layout Engine
https://sile-typesetter.org
MIT License
1.66k stars 98 forks source link

Normalize Lua code formatting #606

Open alerque opened 5 years ago

alerque commented 5 years ago

I can't find a code beautifier that formats Lua the way I personally prefer, but any kind of normalization is going to be better than none. After reviewing about half a dozen options the two easiest to implement and most complete seem to be lua_code_formatter and lua-fmt, while luaformatter is interesting in that it is a let less aggressive but still works alright (with honorable mention to FormatLua). We should probably start assuming one of those is our go-to preference for code formatting and run it over most of our existing code base.

I can't find any really comprehensive Lua coding style guides. If anybody knows of one (especially if they like it) let me know.

Worth mentioning, the auto-indent for Lua in NeoVim does not match any of these, but I actually perfer it. None of these tools are customization without forking them.

Here is a little shell script to make it easier to play with various Lua code formatters. It should be used as a filter that takes STDIN and writes STDOUT, and can be called with a single argument of which backend to use.

#!/usr/bin/env zsh

case $1 in
    3|formatter)
        luaformatter -s 2 -t 0 -d unix /dev/stdin
        ;;
    2|reformat)
        lua.reformat /dev/stdin >(cat) 1>/dev/null
        ;;
    1|fmt|*)
        luafmt -i 2 --stdin
        ;;
esac
pkulchenko commented 5 years ago

@alerque, I've implemented Lua re-indenter in ZeroBrane Studio and it satisfies my (picky) requirements quite well. Note that it won't change the line content, only the indentation. You'll need to trigger the re-indentation from the IDE, but if you want to run it as an external command, then something like this may work: zbstudio -cfg "package{onAppLoad = function(self)ide:GetMainFrame():ProcessEvent(wx.wxCommandEvent(wx.wxEVT_COMMAND_MENU_SELECTED, ID.REINDENT))ide:GetDocument(ide:GetEditor()):Save()ide:Exit()end}" %1

alerque commented 5 years ago

@pkulchenko Thanks, I'll have a look at your implementation too — when I can get ZBS to run.

alerque commented 5 years ago

I did a bunch of format normalization along the way in #681, but it was hardly exhaustive, just basic stuff in files that had lint warning.