PrettyArrows (pretty_arrows v0.3.0)

Copy Markdown View Source

A mix format plugin that aligns the arrows in with clauses and the options in Ecto field declarations into columns.

The plugin runs Code.format_string!/2 first and then applies the alignments to the result, so it can replace the standard formatter.

Setup

Add the plugin to .formatter.exs:

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

Options

All options live under the :pretty_arrows key:

[
  plugins: [PrettyArrows],
  pretty_arrows: [
    with: [enabled: true, padding: :left],
    ecto: [enabled: true, parens: false, columns: 2]
  ]
]
  • :with - options for with clause alignment.

    • :enabled - true or false. Defaults to true.
    • :padding - :left or :right. Defaults to :left. With :left, the padding goes before each pattern so the patterns end in the same column. With :right, the padding goes after each pattern so the patterns start in the same column.
  • :ecto - options for Ecto field alignment.

    • :enabled - true or false. Defaults to true.
    • :parens - true keeps the parentheses around field arguments, false removes them. Defaults to false.
    • :columns - how many argument columns to align, as a positive integer. Defaults to 2, which aligns the name and the type.

Unknown options and invalid values raise an ArgumentError that names the offending key. The top-level keys :with, :with_padding, :ecto, :prettyarrows, and :formatters from earlier versions also raise, with a pointer to the migration guide in the README.

PrettyArrows covers both the with and the Ecto alignments. To use only one of them, list WithFormatter or EctoFieldFormatter as the plugin instead. For case clauses, use CaseFormatter.

Summary

Functions

Returns the Mix.Tasks.Format features for this plugin.

Formats source and applies both the with and the Ecto alignments.

Formats source and aligns only the Ecto field declarations.

Formats source and aligns only the with clauses.

Functions

features(opts)

Returns the Mix.Tasks.Format features for this plugin.

Registers the plugin for .ex and .exs files. mix format calls this function, so you don't need to call it yourself.

format(source, opts \\ [])

Formats source and applies both the with and the Ecto alignments.

opts are the mix format options. They are passed to Code.format_string!/2, and the :pretty_arrows section configures the alignments. Trailing newlines in source are kept.

If source can't be parsed, the formatting step is skipped and the alignments run on the text as it is.

Raises ArgumentError if the :pretty_arrows options are invalid.

Example

PrettyArrows.format(source, pretty_arrows: [ecto: [columns: 3]])

format_ecto(source, opts \\ [])

Formats source and aligns only the Ecto field declarations.

This is the function behind EctoFieldFormatter. It reads the :ecto section of :pretty_arrows and ignores :with. See format/2 for the opts and error behavior.

format_with(source, opts \\ [])

Formats source and aligns only the with clauses.

This is the function behind WithFormatter. It reads the :with section of :pretty_arrows and ignores :ecto. See format/2 for the opts and error behavior.