Skip to content

Commit

Permalink
fiddle with private apis to ease iex playing
Browse files Browse the repository at this point in the history
  • Loading branch information
novaugust committed Nov 27, 2024
1 parent 5b56613 commit d35a28d
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 26 deletions.
13 changes: 6 additions & 7 deletions lib/styler.ex
Original file line number Diff line number Diff line change
Expand Up @@ -63,21 +63,21 @@ defmodule Styler do
def features(_opts), do: [sigils: [], extensions: [".ex", ".exs"]]

@impl Format
def format(input, formatter_opts) do
def format(input, formatter_opts \\ []) do
file = formatter_opts[:file]
styler_opts = formatter_opts[:styler] || []

{ast, comments} =
input
|> string_to_quoted_with_comments(to_string(file))
|> string_to_ast(to_string(file))
|> style(file, styler_opts)

quoted_to_string(ast, comments, formatter_opts)
ast_to_string(ast, comments, formatter_opts)
end

@doc false
# Wrap `Code.string_to_quoted_with_comments` with our desired options
def string_to_quoted_with_comments(code, file \\ "nofile") when is_binary(code) do
def string_to_ast(code, file \\ "nofile") when is_binary(code) do
Code.string_to_quoted_with_comments!(code,
literal_encoder: &__MODULE__.literal_encoder/2,
token_metadata: true,
Expand All @@ -89,9 +89,8 @@ defmodule Styler do
@doc false
def literal_encoder(literal, meta), do: {:ok, {:__block__, meta, [literal]}}

@doc false
# Turns an ast and comments back into code, formatting it along the way.
def quoted_to_string(ast, comments, formatter_opts \\ []) do
@doc "Turns an ast and comments back into code, formatting it along the way."
def ast_to_string(ast, comments \\ [], formatter_opts \\ []) do
opts = [{:comments, comments}, {:escape, false} | formatter_opts]
{line_length, opts} = Keyword.pop(opts, :line_length, 122)

Expand Down
24 changes: 12 additions & 12 deletions lib/zipper.ex
Original file line number Diff line number Diff line change
Expand Up @@ -317,8 +317,11 @@ defmodule Styler.Zipper do
if next = next(zipper), do: do_traverse(next, acc, fun), else: {top(zipper), acc}
end

# Same as `traverse/3`, but doesn't waste cycles going back to the top of the tree when traversal is finished
@doc false
@doc """
Same as `traverse/3`, but doesn't waste cycles going back to the top of the tree when traversal is finished
Useful when only the accumulator is of interest, and no updates to the zipper are.
"""
@spec reduce(zipper, term, (zipper, term -> {zipper, term})) :: term
def reduce({_, nil} = zipper, acc, fun) do
do_reduce(zipper, acc, fun)
Expand Down Expand Up @@ -390,17 +393,14 @@ defmodule Styler.Zipper do
end
end

@doc false
# Similar to traverse_while/3, but returns the `acc` directly, skipping the return to the top of the zipper.
# For that reason the :halt tuple is instead just a 2-ple of `{:halt, acc}`
@spec reduce_while(zipper, term, (zipper, term -> {command, zipper, term})) :: {zipper, term}
def reduce_while({_tree, nil} = zipper, acc, fun) do
do_reduce_while(zipper, acc, fun)
end
@doc """
Same as `traverse_while/3` except it only returns the acc, saving the work of returning to the top of the zipper.
def reduce_while({tree, meta}, acc, fun) do
{{updated, _meta}, acc} = do_reduce_while({tree, nil}, acc, fun)
{{updated, meta}, acc}
For that reason the `:halt` tuple is instead just a 2-ple of `{:halt, acc}`
"""
@spec reduce_while(zipper, term, (zipper, term -> {:cont | :skip, zipper, term} | {:halt, term})) :: term
def reduce_while({tree, _meta}, acc, fun) do
do_reduce_while({tree, nil}, acc, fun)
end

defp do_reduce_while(zipper, acc, fun) do
Expand Down
2 changes: 1 addition & 1 deletion test/style/configs_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ defmodule Styler.Style.ConfigsTest do
alias Styler.Style.Configs

test "only runs on exs files in config folders" do
{ast, _} = Styler.string_to_quoted_with_comments("import Config\n\nconfig :bar, boop: :baz")
{ast, _} = Styler.string_to_ast("import Config\n\nconfig :bar, boop: :baz")
zipper = Styler.Zipper.zip(ast)

for file <- ~w(dev.exs my_app.exs config.exs) do
Expand Down
2 changes: 1 addition & 1 deletion test/style_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ defmodule Styler.StyleTest do
# After module
"""

@comments @code |> Styler.string_to_quoted_with_comments() |> elem(1)
@comments @code |> Styler.string_to_ast() |> elem(1)

describe "displace_comments/2" do
test "Doesn't lose any comments" do
Expand Down
10 changes: 5 additions & 5 deletions test/support/style_case.ex
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,11 @@ defmodule Styler.StyleCase do
if styled != expected and ExUnit.configuration()[:trace] do
IO.puts("\n======Given=============\n")
IO.puts(before)
{before_ast, before_comments} = Styler.string_to_quoted_with_comments(before)
{before_ast, before_comments} = Styler.string_to_ast(before)
dbg(before_ast)
dbg(before_comments)
IO.puts("======Expected AST==========\n")
{expected_ast, expected_comments} = Styler.string_to_quoted_with_comments(expected)
{expected_ast, expected_comments} = Styler.string_to_ast(expected)
dbg(expected_ast)
dbg(expected_comments)
IO.puts("======Got AST===============\n")
Expand Down Expand Up @@ -107,7 +107,7 @@ defmodule Styler.StyleCase do
dbg(styled_ast)

IO.puts("expected:")
dbg(elem(Styler.string_to_quoted_with_comments(expected), 0))
dbg(elem(Styler.string_to_ast(expected), 0))

IO.puts("code:\n#{styled}")
flunk("")
Expand All @@ -133,11 +133,11 @@ defmodule Styler.StyleCase do
end

def style(code, filename \\ "testfile") do
{ast, comments} = Styler.string_to_quoted_with_comments(code)
{ast, comments} = Styler.string_to_ast(code)
{styled_ast, comments} = Styler.style({ast, comments}, filename, on_error: :raise)

try do
styled_code = styled_ast |> Styler.quoted_to_string(comments) |> String.trim_trailing("\n")
styled_code = styled_ast |> Styler.ast_to_string(comments) |> String.trim_trailing("\n")
{styled_ast, styled_code, comments}
rescue
exception ->
Expand Down

0 comments on commit d35a28d

Please sign in to comment.