Format Erlang code "to the standard": using Emacs' erlang-mode.
Used daily in CI on 2600Hz's Kazoo project à la gofmt
.
Add the plugin to your rebar config:
{plugins, [rebar3_fmt]}.
Then just call your plugin directly in an existing application:
$ rebar3 fmt
.PHONY: fmt
FMT = _build/erlang-formatter-master/fmt.sh
$(FMT):
mkdir -p _build/
curl -f#SL 'https://codeload.github.com/fenollp/erlang-formatter/tar.gz/master' | tar xvz -C _build/
# Pick either this one to go through the whole project
fmt: TO_FMT ?= .
# Or this faster, incremental pass
#fmt: TO_FMT ?= $(shell git --no-pager diff --name-only HEAD origin/master -- '*.app.src' '*.config' '*.config.script' '*.erl' '*.escript' '*.hrl')
fmt: $(FMT)
$(if $(TO_FMT), $(FMT) $(TO_FMT))
# Example:
# TO_FMT='src/a.erl include/b/hrl' make fmt
- emacs ≥ 24
Some alternatives (in no particular order) to this dependency-heavy utility:
- For Vim users this can help
- erl_tidy
- vim-erlang-runtime
- erlang_stdin_formatter
- erlang_string_io
- sourcer
- erl_tidy
- less verbose on no errors
- add newline to end of file when missing
- make it faster
- ensure it respects file-local settings like
%% -*- erlang-indent-level: 2; indent-tabs-mode: nil -*-
- option handling to disable default TABs policy (default: spaces only)
- Reminiscent of
Just like the CAP theorem, I posit the TIA theorem: tabs, indentation, alignment, choose two.
-- Loïc Hoguin
- Reminiscent of
- maybe: do not depend on Emacs
More at https://github.com/fenollp/erlang-formatter/issues
- ERL-204:
$ embedded at the end of a string causes wrong indentation
- ERL-210:
{} inside multiline strings are indented
ERL-212:Linebreak after fun but before ( breaks indentation
- ERL-228:
Defining type as a tuple containing a fun() followed by expression throws Emacs error
- ERL-526:
Binary in a multiline list comprehension nests wrongly
Feel free to report issues encountered with this tool. I will minify & report them.