View Source Rewrite.Source.Ex (rewrite v0.10.5)

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.

Updating and syncing :quoted

When :quoted becomes updated, content becomes formatted to the Elixir source code. To keep the code in :content in sync with the AST in :quoted, the new code is parsed to a new :quoted. That means that Source.update(source, :quoted, quoted) also updates the AST.

The syncing of :quoted can be suppressed with the option sync_quoted: false.

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 = Code.string_to_quoted!("""
...> defmodule MyApp.New do
...>   def      foo do
...>   :foo
...> end
...> end
...> """)
iex> source = Source.update(source, :quoted, quoted)
iex> Source.updated?(source)
true
iex> Source.get(source, :content)
"""
defmodule MyApp.New do
  def foo do
    :foo
  end
end
"""
iex> Source.get(source, :quoted) == quoted
false

Without syncing :quoted:

iex> project = Rewrite.new([{Source.Ex, sync_quoted: false}])
iex> path = "test/fixtures/source/simple.ex"
iex> project = Rewrite.read!(project, path)
iex> source = Rewrite.source!(project, path)
iex> quoted = Code.string_to_quoted!("""
...> defmodule MyApp.New do
...>   def      foo do
...>   :foo
...> end
...> end
...> """)
iex> source = Source.update(source, :quoted, quoted)
iex> Source.get(source, :quoted) == quoted
true

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

@type t() :: %Rewrite.Source.Ex{
  formatter: (Macro.t() -> String.t()),
  opts: nil | keyword(),
  quoted: Macro.t()
}

Functions

Link to this function

format(input, formatter_opts \\ nil)

View Source
@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
"""
Link to this function

from_string(string, path \\ nil)

View Source

Returns a %Rewrite.Source{} with an added :filetype.

Link to this function

merge_formatter_opts(source, formatter_opts)

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

Link to this function

modules(source, version)

View 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]
Link to this function

put_formatter_opts(source, formatter_opts)

View Source
@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 reads from the file under the given path.

Options

  • :formatter_opts, default: [] - additional formatter options.

  • sync_quoted, default: true - forcing the re-parsing when the source field quoted is updated.