View Source Rewrite.DotFormatter behaviour (rewrite v1.1.1)

Provides an alternative API to the Elixir dot formatter.

The DotFormatter has the same functionality as the code that provides the mix format task. But DotFormatter provides a struct for the evaluated formatter config to provide a more convenient API.

Summary

Callbacks

Returns which features this plugin should plug into.

Receives a string to be formatted with options and returns said string.

Converts a quoted expression to an algebra document using Elixir's formatter rules.

Functions

Returns a list of conflicting files and their dot-formatter paths in the given dot_formatter.

Returns a %DotFormatter for the given config.

Same as create/2, but raises a Rewrite.DotFormatterError exception in case of failure.

Returns an empty %DotFormatter{} struct with inputs set to **.

Returns an :ok tuple with a list of {path, formatter} tuples for the given dot_formatter.

Formats the files in the current directory that are specified by the given dot_formatter.

Formats the given file using the given dot_formatter and opts.

A convenience function for format_quoted/4 to format a string.

Converts the given quoted expression to a string using the given dot_formatter, file and opts.

Same as format_quoted/2, but raises a Rewrite.DotFormatterError exception in case of failure.

A convenience function for format_string/4 to format a string.

Formats the given string using the specified dot_formatter, file, and options.

Same as format_string/2, but raises a Rewrite.DotFormatterError exception in case of failure.

Returns a formatter function to be used for the given file.

Returns the formatter options for the given dot_formatter.

Returns the formatter options for the given dot_formatter and file.

Creates a %DotFormatter{} struct from a the given formatter_opts.

Returns the %DotFormatter{} struct for the given path or nil when not found.

Returns a list of all :inputs from the given dot_formatter and any sub-formatters.

Returns a %DotFormatter{} struct with the result of invoking fun on the given dot_formatter and any sub-formatters.

Reads the .formatter.exs file in the current directory or the given %Rewrite{} project.

Same as read/2, but raises a Rewrite.DotFormatterError exception in case of failure.

Invokes fun for each dot_formatter and any sub-formatters with the accumulator.

Returns true if the given dot_formatter is up to date.

Updates the given dot_formatter.

Types

@type formatter() :: (term() -> String.t())
@type t() :: %Rewrite.DotFormatter{
  force_do_end_blocks: boolean() | nil,
  import_deps: [atom()] | nil,
  inputs: [GlobEx.t()] | nil,
  locals_without_parens: [{atom(), arity()}] | nil,
  normalize_bitstring_modifiers: boolean() | nil,
  normalize_charlists_as_sigils: boolean() | nil,
  path: Path.t() | nil,
  plugin_opts: keyword(),
  plugins: [module()] | nil,
  sigils: [{atom(), function()}] | nil,
  source: String.t() | nil,
  subdirectories: [GlobEx.t()] | nil,
  subs: [t()],
  timestamp: timestamp() | nil
}
@type timestamp() :: integer()

Callbacks

@callback features(Keyword.t()) :: [sigils: [atom()], extensions: [binary()]]

Returns which features this plugin should plug into.

@callback format(
  String.t(),
  keyword()
) :: String.t()

Receives a string to be formatted with options and returns said string.

Link to this callback

quoted_to_algebra(t, keyword)

View Source
@callback quoted_to_algebra(
  Macro.t(),
  keyword()
) :: Inspect.Algebra.t()

Converts a quoted expression to an algebra document using Elixir's formatter rules.

This function works as an replacement for Code.quoted_to_algebra/2.

Functions

Link to this function

conflicts(dot_formatter, rewrite \\ nil)

View Source
@spec conflicts(t(), Rewrite.t() | nil) :: [{Path.t(), [dot_formatter_path]}]
when dot_formatter_path: Path.t()

Returns a list of conflicting files and their dot-formatter paths in the given dot_formatter.

A conflicting file is a file that is referenced by more than one formatter.

Link to this function

create(rewrite \\ nil, opts)

View Source
@spec create(
  Rewrite.t() | nil,
  keyword()
) :: {:ok, t()} | {:error, Rewrite.DotFormatterError.t()}

Returns a %DotFormatter for the given config.

The opts has the same format as the .formatter.exs file.

Link to this function

create!(rewrite \\ nil, config)

View Source
@spec create!(
  Rewrite.t() | nil,
  keyword()
) :: t()

Same as create/2, but raises a Rewrite.DotFormatterError exception in case of failure.

@spec default() :: t()

Returns an empty %DotFormatter{} struct with inputs set to **.

This is useful when no .formatter.exs file is present.

Link to this function

expand(dot_formatter, project \\ nil, opts \\ [])

View Source
@spec expand(t(), Rewrite.t() | keyword() | nil, keyword()) ::
  {:ok, [{Path.t(), formatter()}]} | {:error, Rewrite.DotFormatterError.t()}

Returns an :ok tuple with a list of {path, formatter} tuples for the given dot_formatter.

In case of an conflict, an :error tuple is returned. A conflicting file is a file that is referenced by more than one formatter.

The formatter is a two arity function that takes a string as input and options to format the string. For more information see formatter_for_file/3.

Link to this function

format(dot_formatter, opts \\ [])

View Source
@spec format(
  t() | nil,
  keyword()
) :: :ok | {:error, Rewrite.DotFormatterError.t()}

Formats the files in the current directory that are specified by the given dot_formatter.

The options are the same as for DotFormatter.read/2.

To format a %Rewrite{} project, use Rewrite.format/2.

Link to this function

format_file(dot_formatter, file, opts \\ [])

View Source
@spec format_file(t(), Path.t(), keyword()) ::
  :ok | {:error, Rewrite.DotFormatterError.t()}

Formats the given file using the given dot_formatter and opts.

The options are the same as for Code.format_string!/2.

Link to this function

format_quoted(string, opts \\ [])

View Source
@spec format_quoted(
  String.t(),
  keyword()
) :: {:ok, String.t()} | {:error, Rewrite.DotFormatterError.t()}

A convenience function for format_quoted/4 to format a string.

This function reads the %DotFormatter{} with the given opts and calls format_quoted/4. The used file name defaults to nofile.ex and can be set in the opts.

Options

  • The funciton accepts the same options as read/2.

  • :file - the file name to use. Defaults to nofile.ex.

Link to this function

format_quoted(dot_formatter, file, quoted, opts \\ [])

View Source
@spec format_quoted(t(), Path.t(), Macro.t(), keyword()) ::
  {:ok, String.t()} | {:error, term()}
@spec format_quoted(t(), Path.t(), Macro.t(), keyword()) :: String.t()

Converts the given quoted expression to a string using the given dot_formatter, file and opts.

The file is used to determine the formatter. If no formatter is found, an error tuple is returned.

Returns an :ok tuple with the formatted string on success, or an error tuple on failure.

Link to this function

format_quoted!(string, opts \\ [])

View Source
@spec format_quoted!(
  String.t(),
  keyword()
) :: String.t()

Same as format_quoted/2, but raises a Rewrite.DotFormatterError exception in case of failure.

Link to this function

format_quoted!(dot_formatter, file, quoted, opts \\ [])

View Source

Same as format_quoted/4, but raises a Rewrite.DotFormatterError exception in case of failure.

Link to this function

format_string(string, opts \\ [])

View Source
@spec format_string(
  String.t(),
  keyword()
) :: {:ok, String.t()} | {:error, Rewrite.DotFormatterError.t()}

A convenience function for format_string/4 to format a string.

This function reads the %DotFormatter{} with the given opts and calls format_string/4. The used file name defaults to nofile.ex and can be set in the opts.

Options

  • The funciton accepts the same options as read/2.

  • :file - the file name to use. Defaults to nofile.ex.

Link to this function

format_string(dot_formatter, file, string, opts \\ [])

View Source
@spec format_string(t(), Path.t(), String.t(), keyword()) ::
  {:ok, String.t()} | {:error, term()}

Formats the given string using the specified dot_formatter, file, and options.

The file is used to determine the formatter.

Returns an :ok tuple with the formatted string on success, or an error tuple on failure.

Link to this function

format_string!(string, opts \\ [])

View Source
@spec format_string!(
  String.t(),
  keyword()
) :: String.t()

Same as format_string/2, but raises a Rewrite.DotFormatterError exception in case of failure.

Link to this function

format_string!(dot_formatter, file, string, opts \\ [])

View Source
@spec format_string!(t(), Path.t(), String.t(), keyword()) :: String.t()

Same as format_string/4, but raises a Rewrite.DotFormatterError exception in case of failure.

Link to this function

formatter_for_file(dot_formatter, file, opts \\ [])

View Source
@spec formatter_for_file(t(), Path.t(), keyword()) :: formatter()

Returns a formatter function to be used for the given file.

The returned function takes a string or an Elixir AST and returns the formatted string. The option :from takes the value :string or :quoted to determine which input type is used by the formatter, defaulting to :string.

The function also accepts the same options as Code.format_string!/2, these are used when the formatter is called.

Link to this function

formatter_opts(dot_formatter)

View Source
@spec formatter_opts(t()) :: keyword()

Returns the formatter options for the given dot_formatter.

This functions ignores the sub-formatters of the given dot_formatter.

Link to this function

formatter_opts_for_file(dot_formatter, file)

View Source
@spec formatter_opts_for_file(t(), Path.t()) :: keyword()

Returns the formatter options for the given dot_formatter and file.

This fucntion searches the formatter for the given file and returns the formater options for that formatter in an :ok tuple.

If no formatter or multiple formatters are found, an :error tuple is returned.

Link to this function

from_formatter_opts(formatter_opts, opts \\ [])

View Source
@spec from_formatter_opts(keyword(), keyword()) :: t()

Creates a %DotFormatter{} struct from a the given formatter_opts.

This function ignores the sub-formatters of the given formatter_opts. It is also assumes that the plugins are already loaded.

Link to this function

get(dot_formatter, path)

View Source
@spec get(t(), path :: Path.t()) :: t()

Returns the %DotFormatter{} struct for the given path or nil when not found.

@spec inputs(t()) :: [GlobEx.t()]

Returns a list of all :inputs from the given dot_formatter and any sub-formatters.

@spec map(t(), (t() -> t())) :: t()

Returns a %DotFormatter{} struct with the result of invoking fun on the given dot_formatter and any sub-formatters.

Link to this function

read(rewrite \\ nil, opts \\ [])

View Source
@spec read(
  rewrite :: Rewrite.t() | keyword() | nil,
  keyword()
) :: {:ok, t()} | {:error, Rewrite.DotFormatterError.t()}

Reads the .formatter.exs file in the current directory or the given %Rewrite{} project.

If a %Rewrite{} project is given to the function, the formatter is searched in the project and the latest version from the source is used. As a fallback, it will search the file system for the required files.

The function returns a %DotFormatter{} struct with all sub-formatters.

Options

  • remove_plugins - a list of plugins to remove from the formatter.

  • replace_plugins - a list of {old, new} tuples to replace plugins in the formatter.

  • ignore_unknown_deps - ingores unknown dependencies in :import_deps when set to true. Defaults to false.

  • ignore_missing_sub_formatters - ignores missign sub formatters when set to true, Defaults to false.

Link to this function

read!(rewrite \\ nil, opts \\ [])

View Source
@spec read!(
  rewrite :: Rewrite.t() | nil,
  keyword()
) :: t()

Same as read/2, but raises a Rewrite.DotFormatterError exception in case of failure.

Link to this function

reduce(dot_formatter, acc \\ [], fun)

View Source
@spec reduce(t(), acc, (t(), acc -> acc)) :: acc when acc: term()

Invokes fun for each dot_formatter and any sub-formatters with the accumulator.

Link to this function

up_to_date?(dot_formatter, project \\ nil)

View Source
@spec up_to_date?(t(), project :: Rewrite.t() | nil) :: boolean()

Returns true if the given dot_formatter is up to date.

The function only checks the timestamps in the dot_formatter struct with the timestamp of the underlying file or source in the project.

Link to this function

update(dot_formatter, rewrite \\ nil, opts \\ [])

View Source
@spec update(t(), Rewrite.t() | keyword() | nil, keyword()) ::
  {:ok, t()} | {:error, Rewrite.DotFormatterError.t()}

Updates the given dot_formatter.

The function checks if the dot_formatter is up to date. If not, the eval function is called.

Accepts the same options as eval/2.