erlang-formatter icon indicating copy to clipboard operation
erlang-formatter copied to clipboard

Format Erlang code using Emacs' erlang-mode

erlang-formatter Build Status Hex pm

Format Erlang code "to the standard": using Emacs' erlang-mode.

Used daily in CI on 2600Hz's Kazoo project à la gofmt.

Using as a rebar3 plugin

Add the plugin to your rebar config:

{plugins, [rebar3_fmt]}.

Then just call your plugin directly in an existing application:

$ rebar3 fmt

Using make

.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

Dependencies

  • emacs ≥ 24

Notes

Some alternatives (in no particular order) to this dependency-heavy utility:

To do

  1. less verbose on no errors
  2. add newline to end of file when missing
  3. make it faster
  4. ensure it respects file-local settings like %% -*- erlang-indent-level: 2; indent-tabs-mode: nil -*-
  5. 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
  6. maybe: do not depend on Emacs

More at https://github.com/fenollp/erlang-formatter/issues

Bugs uncovered with this project

  1. ERL-204: $ embedded at the end of a string causes wrong indentation
  2. ERL-210: {} inside multiline strings are indented
  3. ~~ERL-212: Linebreak after fun but before ( breaks indentation~~
  4. ERL-228: Defining type as a tuple containing a fun() followed by expression throws Emacs error
  5. ERL-526: Binary in a multiline list comprehension nests wrongly

Feel free to report issues encountered with this tool. I will minify & report them.