View Source Rewrite.Source.Ex (rewrite v1.0.1)

An implementation of Rewrite.Filetype 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 resyncing :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 resyncing of :quoted can be suppressed with the option resync_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 resyncing :quoted:

iex> project = Rewrite.new(filetypes: [{Source.Ex, resync_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

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

Returns the current modules for the given source.

Returns the modules of a source for the given version.

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

Types

@type t() :: %Rewrite.Source.Ex{opts: keyword(), quoted: Macro.t()}

Functions

Link to this function

format(source, formatter_opts \\ nil)

View Source
This function is deprecated. Use the fromatting functionlity provided by Rewrite.DotFormatter instead..
Link to this function

from_string(string, opts \\ [])

View Source

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

@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, opts)

View Source
This function is deprecated. Use the fromatting functionlity provided by Rewrite.DotFormatter instead..

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

The content reads from the file under the given path.

Options

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