Skip to content

Commit d35a28d

Browse files
committed
fiddle with private apis to ease iex playing
1 parent 5b56613 commit d35a28d

File tree

5 files changed

+25
-26
lines changed

5 files changed

+25
-26
lines changed

lib/styler.ex

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -63,21 +63,21 @@ defmodule Styler do
6363
def features(_opts), do: [sigils: [], extensions: [".ex", ".exs"]]
6464

6565
@impl Format
66-
def format(input, formatter_opts) do
66+
def format(input, formatter_opts \\ []) do
6767
file = formatter_opts[:file]
6868
styler_opts = formatter_opts[:styler] || []
6969

7070
{ast, comments} =
7171
input
72-
|> string_to_quoted_with_comments(to_string(file))
72+
|> string_to_ast(to_string(file))
7373
|> style(file, styler_opts)
7474

75-
quoted_to_string(ast, comments, formatter_opts)
75+
ast_to_string(ast, comments, formatter_opts)
7676
end
7777

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

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

lib/zipper.ex

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -317,8 +317,11 @@ defmodule Styler.Zipper do
317317
if next = next(zipper), do: do_traverse(next, acc, fun), else: {top(zipper), acc}
318318
end
319319

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

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

406406
defp do_reduce_while(zipper, acc, fun) do

test/style/configs_test.exs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ defmodule Styler.Style.ConfigsTest do
1515
alias Styler.Style.Configs
1616

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

2121
for file <- ~w(dev.exs my_app.exs config.exs) do

test/style_test.exs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ defmodule Styler.StyleTest do
3535
# After module
3636
"""
3737

38-
@comments @code |> Styler.string_to_quoted_with_comments() |> elem(1)
38+
@comments @code |> Styler.string_to_ast() |> elem(1)
3939

4040
describe "displace_comments/2" do
4141
test "Doesn't lose any comments" do

test/support/style_case.ex

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,11 @@ defmodule Styler.StyleCase do
4040
if styled != expected and ExUnit.configuration()[:trace] do
4141
IO.puts("\n======Given=============\n")
4242
IO.puts(before)
43-
{before_ast, before_comments} = Styler.string_to_quoted_with_comments(before)
43+
{before_ast, before_comments} = Styler.string_to_ast(before)
4444
dbg(before_ast)
4545
dbg(before_comments)
4646
IO.puts("======Expected AST==========\n")
47-
{expected_ast, expected_comments} = Styler.string_to_quoted_with_comments(expected)
47+
{expected_ast, expected_comments} = Styler.string_to_ast(expected)
4848
dbg(expected_ast)
4949
dbg(expected_comments)
5050
IO.puts("======Got AST===============\n")
@@ -107,7 +107,7 @@ defmodule Styler.StyleCase do
107107
dbg(styled_ast)
108108

109109
IO.puts("expected:")
110-
dbg(elem(Styler.string_to_quoted_with_comments(expected), 0))
110+
dbg(elem(Styler.string_to_ast(expected), 0))
111111

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

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

139139
try do
140-
styled_code = styled_ast |> Styler.quoted_to_string(comments) |> String.trim_trailing("\n")
140+
styled_code = styled_ast |> Styler.ast_to_string(comments) |> String.trim_trailing("\n")
141141
{styled_ast, styled_code, comments}
142142
rescue
143143
exception ->

0 commit comments

Comments
 (0)