Minga.Editing.Formatter (Minga v0.1.0)

Copy Markdown View Source

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

Types

A shell command string, optionally containing {file}.

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

failure()

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

formatter_spec()

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

A shell command string, optionally containing {file}.

Functions

apply_save_transforms(content, filetype)

@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(content, trim_trailing, insert_final_newline)

@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()

@spec default_formatters() :: %{required(atom()) => formatter_spec()}

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

format(content, 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.

resolve_formatter(filetype, file_path \\ nil)

@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.