# `Minga.Editing.Formatter`
[🔗](https://github.com/jsmestad/minga/blob/main/lib/minga/editing/formatter.ex#L1)

Pipes buffer content through external formatters.

Resolves the formatter command for a filetype (user config overrides
defaults), runs the command with the buffer content on stdin, and
returns the formatted output. If the command fails, returns an error
without modifying the buffer.

## Formatter spec

A formatter spec is a shell command string. The placeholder `{file}`
is replaced with the buffer's file path (useful for formatters that
need to know the filename for config resolution).

## Default formatters

    :elixir   → "mix format --stdin-filename {file} -"
    :go       → "gofmt"
    :rust     → "rustfmt --edition 2021"
    :python   → "python3 -m black --quiet -"
    :zig      → "zig fmt --stdin"
    :c / :cpp → "clang-format"
    :javascript / :typescript / :jsx / :tsx → "prettier --stdin-filepath {file}"

# `failure`

```elixir
@type failure() :: Minga.Editing.Formatter.Failure.t() | {:subprocess, String.t()}
```

# `formatter_spec`

```elixir
@type formatter_spec() :: String.t()
```

A shell command string, optionally containing `{file}`.

# `apply_save_transforms`

```elixir
@spec apply_save_transforms(String.t(), atom()) :: String.t()
```

Applies whitespace transforms using the filetype to resolve options.

Reads `trim_trailing_whitespace` and `insert_final_newline` from
`Config.Options` for the given filetype. Prefer the 3-arity version
with explicit booleans when you already have the option values (e.g.,
from buffer-local options).

# `apply_save_transforms`

```elixir
@spec apply_save_transforms(String.t(), boolean(), boolean()) :: String.t()
```

Applies whitespace transforms with explicit boolean flags.

Used by buffer-local option callers that have already resolved the
option values from `Buffer.get_option/2`.

# `default_formatters`

```elixir
@spec default_formatters() :: %{required(atom()) =&gt; formatter_spec()}
```

Returns the default formatter map (filetype atom to command string).

# `format`

```elixir
@spec format(String.t(), formatter_spec()) ::
  {:ok, Minga.Editing.Formatter.Result.t()} | {:error, failure()}
```

Formats content by piping it through the given command.

Writes the content to a temporary file and pipes it to the command via
shell redirection. Standard output is the formatted document and standard error
is retained separately as diagnostics. Returns `{:ok, result}` on success
(exit code 0) or `{:error, reason}` on failure.

# `resolve_formatter`

```elixir
@spec resolve_formatter(atom(), String.t() | nil) :: formatter_spec() | nil
```

Resolves the formatter command for a filetype.

Checks user config (`:formatter` option with filetype override) first,
then falls back to the built-in default. Returns `nil` if no formatter
is configured for the filetype.

---

*Consult [api-reference.md](api-reference.md) for complete listing*
