View Source Rewrite.Source.Ex (rewrite v0.9.0)
An implementation of Rewrite.Filetye to handle Elixir source files.
The module uses the sourceror package
to provide an extended AST
representation of an Elixir file.
Ex extends the source by the key :quoted.
Examples
iex> source = Source.Ex.from_string("Enum.reverse(list)")
iex> Source.get(source, :quoted)
{{:., [trailing_comments: [], line: 1, column: 5],
[
{:__aliases__,
[
trailing_comments: [],
leading_comments: [],
last: [line: 1, column: 1],
line: 1,
column: 1
], [:Enum]},
:reverse
]},
[
trailing_comments: [],
leading_comments: [],
closing: [line: 1, column: 18],
line: 1,
column: 6
], [{:list, [trailing_comments: [], leading_comments: [], line: 1, column: 14], nil}]}
iex> quoted = quote(do: :foo)
iex> source = Source.update(source, :quoted, quoted)
iex> Source.updated?(source)
true
iex> Source.get(source, :content)
"""
:foo
"""
Summary
Functions
Formats the given source, code or quoted into code.
Returns a %Rewrite.Source{} with an added :filetype.
Merges the formatter_opts for a source.
Returns the current modules for the given source.
Returns the modules of a source for the given version.
Puts the formatter_opts to the source.
Returns a %Rewrite.Source{} with an added :filetype.
Types
Functions
@spec format( Rewrite.Source.t() | String.t() | Macro.t(), formatter_opts :: keyword() | nil ) :: String.t()
Formats the given source, code or quoted into code.
Returns an updated source when input is a source.
iex> code = """
...> defmodule Foo do
...> def foo, do: :foo
...> end
...> """
iex> Source.Ex.format(code)
"""
defmodule Foo do
def foo, do: :foo
end
"""
iex> Source.Ex.format(code, force_do_end_blocks: true)
"""
defmodule Foo do
def foo do
:foo
end
end
"""
iex> source = Source.Ex.from_string("""
...> defmodule Foo do
...> def foo, do: :foo
...> end
...> """)
iex> Source.Ex.format(source, force_do_end_blocks: true)
"""
defmodule Foo do
def foo do
:foo
end
end
"""
Returns a %Rewrite.Source{} with an added :filetype.
@spec merge_formatter_opts( Rewrite.Source.t(), keyword() ) :: Rewrite.Source.t()
Merges the formatter_opts for a source.
@spec modules(Rewrite.Source.t()) :: [module()]
Returns the current modules for the given source.
@spec modules(Rewrite.Source.t(), Rewrite.Source.version()) :: [module()]
Returns the modules of a source for the given version.
Examples
iex> bar =
...> """
...> defmodule Bar do
...> def bar, do: :bar
...> end
...> """
iex> foo =
...> """
...> defmodule Baz.Foo do
...> def foo, do: :foo
...> end
...> """
iex> source = Source.Ex.from_string(bar)
iex> source = Source.update(source, :content, bar <> foo)
iex> Source.Ex.modules(source)
[Baz.Foo, Bar]
iex> Source.Ex.modules(source, 2)
[Baz.Foo, Bar]
iex> Source.Ex.modules(source, 1)
[Bar]
@spec put_formatter_opts( Rewrite.Source.t(), keyword() ) :: Rewrite.Source.t()
Puts the formatter_opts to the source.
The formatter options are in use during updating and formatting.
Returns a %Rewrite.Source{} with an added :filetype.
The content is reading from the file under the given path.