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.
Same as format_quoted/4
, 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.
Same as format_string/4
, 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 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
Returns which features this plugin should plug into.
Receives a string to be formatted with options and returns said string.
@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
@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.
@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.
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.
@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
.
@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
.
@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
.
@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 tonofile.ex
.
@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.
Same as format_quoted/2
, but raises a Rewrite.DotFormatterError
exception
in case of failure.
Same as format_quoted/4
, but raises a Rewrite.DotFormatterError
exception
in case of failure.
@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 tonofile.ex
.
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.
Same as format_string/2
, but raises a Rewrite.DotFormatterError
exception
in case of failure.
Same as format_string/4
, but raises a Rewrite.DotFormatterError
exception
in case of failure.
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.
Returns the formatter options for the given dot_formatter
.
This functions ignores the sub-formatters of the given dot_formatter
.
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.
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.
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.
@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 totrue
. Defaults tofalse
.ignore_missing_sub_formatters
- ignores missign sub formatters when set totrue
, Defaults tofalse
.
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.
The function only checks the timestamps in the dot_formatter
struct with the
timestamp of the underlying file or source in the project
.
@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
.