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}"
Summary
Functions
Applies whitespace transforms using the filetype to resolve options.
Applies whitespace transforms with explicit boolean flags.
Returns the default formatter map (filetype atom to command string).
Formats content by piping it through the given command.
Resolves the formatter command for a filetype.
Types
@type failure() :: Minga.Editing.Formatter.Failure.t() | {:subprocess, String.t()}
@type formatter_spec() :: String.t()
A shell command string, optionally containing {file}.
Functions
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).
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.
@spec default_formatters() :: %{required(atom()) => formatter_spec()}
Returns the default formatter map (filetype atom to command string).
@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.
@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.