diff --git a/lib/ex_format.ex b/lib/ex_format.ex index 9d74bbf..5bcc811 100644 --- a/lib/ex_format.ex +++ b/lib/ex_format.ex @@ -75,6 +75,7 @@ defmodule ExFormat do state = %{ parenless_calls: MapSet.new(@parenless_calls), parenless_zero_arity?: false, + in_list?: false, in_spec: nil, } for {line, i} <- Enum.with_index(lines) do @@ -428,7 +429,14 @@ defmodule ExFormat do # Tuple containers def to_string({:{}, _, args} = ast, fun, state) do - tuple = "{" <> tuple_to_string(args, fun, state) <> "}" + tuple = + # Enforce keyword syntax for tuples of size 2 inside list + if state.in_list? and length(args) == 2 do + [k, v] = args + kw_list_to_string([{k, v}], fun, state) + else + "{" <> tuple_to_string(args, fun, state) <> "}" + end fun.(ast, tuple) end @@ -972,6 +980,7 @@ defmodule ExFormat do end defp list_to_string(list, fun, state) do + state = %{state | in_list?: true} list_string = Enum.map_join(list, ", ", &to_string(&1, fun, state)) if not fits?(" " <> list_string <> " ") or line_breaks?(list) do list_to_multiline_string(list, fun, state) @@ -988,12 +997,22 @@ defmodule ExFormat do "\n " <> list_string <> ",\n" end - defp kw_list_to_string(list, fun, state) do - list_string = Enum.map_join(list, ", ", fn {key, value} -> - atom_name = case Inspect.Atom.inspect(key) do + defp kw_list_to_string([{key, value}], fun, state) when not is_atom(key) do + atom_name = + case to_string(key, fun, state) do ":" <> rest -> rest other -> other end + atom_name <> ": " <> to_string(value, fn(_ast, string) -> string end, state) + end + + defp kw_list_to_string(list, fun, state) do + list_string = Enum.map_join(list, ", ", fn {key, value} -> + atom_name = + case Inspect.Atom.inspect(key) do + ":" <> rest -> rest + other -> other + end atom_name <> ": " <> to_string(value, fn(_ast, string) -> string end, state) end) if not fits?(" " <> list_string <> " ") or line_breaks?(list) do diff --git a/test/unit/indentation_test.exs b/test/unit/indentation_test.exs index 912c38f..a8a023d 100644 --- a/test/unit/indentation_test.exs +++ b/test/unit/indentation_test.exs @@ -67,10 +67,9 @@ defmodule ExFormat.Unit.IndentationTest do [{:prefix_newline, get_prefix_newline(curr_lineno-1, prev_lineno)}] ++ curr_ctx """ good = """ - [{:prev, prev_lineno}] ++ - [{:prefix_comments, get_prefix_comments(curr_lineno - 1, prev_lineno)}] ++ - [{:prefix_newline, get_prefix_newline(curr_lineno - 1, prev_lineno)}] ++ - curr_ctx + [prev: prev_lineno] ++ + [prefix_comments: get_prefix_comments(curr_lineno - 1, prev_lineno)] ++ + [prefix_newline: get_prefix_newline(curr_lineno - 1, prev_lineno)] ++ curr_ctx """ assert_format_string(bad, good) end diff --git a/test/unit/kw_list_syntax_test.exs b/test/unit/kw_list_syntax_test.exs index e7edf45..0878410 100644 --- a/test/unit/kw_list_syntax_test.exs +++ b/test/unit/kw_list_syntax_test.exs @@ -44,4 +44,20 @@ defmodule ExFormat.Unit.KwListSyntaxTest do """ end end + + describe "keyword syntax for tuples inside list" do + test "maintain keyword sugar for tuples of size 2" do + assert_format_string("[:foo, bar: :baz]\n") + assert_format_string("[bar: :baz]\n") + end + + test "enforce keyword sugar for tuples of size 2" do + assert_format_string("[{:bar, :baz}]", "[bar: :baz]\n") + assert_format_string("[:foo, {:bar, :baz}]", "[:foo, bar: :baz]\n") + end + + test "do not enforce keyword sugar for tuples of size more than 2" do + assert_format_string("[{:foo, :bar, :baz}]\n") + end + end end diff --git a/test/unit/kw_list_test.exs b/test/unit/kw_list_test.exs index 47e54a7..d80cb2f 100644 --- a/test/unit/kw_list_test.exs +++ b/test/unit/kw_list_test.exs @@ -147,7 +147,7 @@ defmodule ExFormat.Unit.KeywordListTest do package: package(), aliases: aliases(), test_coverage: [tool: ExCoveralls], - preferred_cli_env: [{:"test.local", :test}, {:coveralls, :test}, {:"coveralls.travis", :test}], + preferred_cli_env: ["test.local": :test, coveralls: :test, "coveralls.travis": :test], name: "Mssqlex", source_url: "https://github.com/findmypast-oss/mssqlex", docs: [main: "readme", extras: ["README.md"]], diff --git a/test/unit/list_test.exs b/test/unit/list_test.exs index c855c50..e58bfe4 100644 --- a/test/unit/list_test.exs +++ b/test/unit/list_test.exs @@ -92,13 +92,13 @@ defmodule ExFormat.Unit.ListTest do defp deps() do [ # Web server - {:cowboy, "~> 1.0"}, + cowboy: "~> 1.0", # Web framework - {:phoenix, "~> 1.3.0-rc"}, + phoenix: "~> 1.3.0-rc", # XML parser helper - {:sweet_xml, "~> 0.6"}, + sweet_xml: "~> 0.6", # Statsd metrics sink client - {:statix, "~> 1.0"}, + statix: "~> 1.0", ] end """