ExAlign (exalign v0.1.12)

Copy Markdown

A Mix formatter plugin that column-aligns Elixir code, similar to how Go's gofmt aligns struct fields and variable groups.

Patterns aligned

  • Keyword list entries - key: value aligns after the colon
  • Variable assignments - var = value aligns the =
  • Map arrow entries - key => value aligns the =>
  • Module attributes - @attr value aligns the value

Usage

Add to your project's .formatter.exs:

[
  plugins: [ExAlign],
  inputs: ["{mix,.formatter}.exs", "{config,lib,test}/**/*.{ex,exs}"]
]

And add exalign to your mix.exs dependencies.

Global configuration

ExAlign reads default option values from ~/.config/exalign/.formatter.exs when that file exists. The file must evaluate to a keyword list containing any ExAlign-recognised keys (:line_length, :wrap_short_lines, :wrap_with). Options found in the project-local .formatter.exs always take precedence over the global file.

Example ~/.config/exalign/.formatter.exs:

[
  line_length:      120,
  wrap_short_lines: true,
  wrap_with:        :backslash
]

Summary

Functions

Formats the given Elixir source contents string, applying column alignment on top of the standard Code.format_string! pass.

Returns the ExAlign options loaded from the global config file ~/.config/exalign/.formatter.exs, or an empty list if the file does not exist or cannot be evaluated.

Functions

format(contents, opts)

Formats the given Elixir source contents string, applying column alignment on top of the standard Code.format_string! pass.

opts may include any standard formatter options plus the ExAlign-specific keys :wrap_short_lines, :wrap_with, :line_length, :eol_at_eof, and :trim_eol_ws.

:eol_at_eof can be:

  • :remove - remove trailing newline at end of file
  • :add - add trailing newline if not present
  • nil (default) - leave end-of-file newline untouched

:trim_eol_ws can be:

  • true (default) - trim trailing whitespace from each line
  • false - leave trailing whitespace untouched

load_global_config()

Returns the ExAlign options loaded from the global config file ~/.config/exalign/.formatter.exs, or an empty list if the file does not exist or cannot be evaluated.