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
Functions
Returns a %Rewrite.Source{}
with an added :filetype
.
@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]
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 fieldquoted
is updated.