RegexFormatter (RegexFormatter v0.1.3)

View Source

RegexFormatter

Don't fear the regex. Malleable 'mix format' via powerful code-search/replace.

Usage

Configure and run Regex Formatter in three steps:

  1. Add RegexFormatter to plugins list in .formatter.exs.
  2. Add regex_formatter config to .formatter.exs following examples below.
  3. Run mix format — Regex Formatter rules will run after normal format operations.
[                                        #  ┌── [1] Add RegexFormatter to plugins.
  ...                                    #  │
  plugins: [Phoenix.LiveView.HTMLFormatter, RegexFormatter],
  regex_formatter: [  #  <───── [2] Add configuration for RegexFormatter.
    [
      extensions: [".ex", ".exs"],  #  <─── [3] Configure file types to replace on.
      replacements: [
        {
          ~r/~u["]\s+/s,  #  <───── [4] Define search pattern.
          "~u\""          #  <───── [5] Define replacement pattern.
        },
        {
          ~r/(~u["][^"]+[^"\s])  +([^"\s])/s,
          ~S'\1 \2',   #  <─────── [6] Replace with matched groups.
          repeat: 100,
          # Repeat substitutions to correctly handle overlapping matches.
          # (repeated substitution will stop as soon as text stops changing)
        }
      ]
    ],
    [
      sigils: [:SQL],  #  <───────── [7] Try substitution within sigils.
      replacements: [
        {
          ~r/(~u["][^"]+[^"\s])\s+"/s,
          ~S'\1"'
        }
      ]
    ],
    [                               #  ┌──── [8] Try handy substitution presets.
      extensions: [".ex", ".exs"],  #  │
      preset_trim_sigil_whitespace: [:u],
      preset_collapse_sigil_whitespace: [:u, :SQL],
      preset_do_on_separate_line_after_multiline_keyword_args: true
    ]
  ]
]