Repository: surface-ui/surface_formatter (elixir)

Issue:
Formatter Plugin crashing with Elixir 1.13.0
## Versions

* Elixir: `1.13.0`
* Erlang: `24.1.6`
* surface: `0.6.1`
* surface_formatter: `0.7.1`

## Error

```
mix format failed for file: lib/hygeia_web/live/statistics_live/export.sface
** (FunctionClauseError) no function clause matching in Inspect.Algebra.format/2    
    
    The following arguments were given to Inspect.Algebra.format/2:
    
        # 1
        {:doc_group, {:doc_cons, {:doc_string, "gettext", 7}, {:doc_group, {:doc_cons, {:doc_cons, {:doc_cons, {:doc_nest, {:doc_cons, "(", {:doc_break, "", :strict}}, 2, :break}, {:doc_cons, {:doc_nest, {:doc_cons, {:doc_cons, "\"", {:doc_string, "From", 4}}, "\""}, 2, :break}, {:doc_break, "", :strict}}}, ")"}, :doc_nil}, :self}}, :self}
    
        # 2
        nil
    
    Attempted function clauses (showing 1 out of 1):
    
        def format(doc, width) when is_binary(doc) or doc === :doc_nil or doc === :doc_line or is_tuple(doc) and elem(doc, 0) === :doc_break or elem(doc, 0) === :doc_collapse or elem(doc, 0) === :doc_color or elem(doc, 0) === :doc_cons or elem(doc, 0) === :doc_fits or elem(doc, 0) === :doc_force or elem(doc, 0) === :doc_group or elem(doc, 0) === :doc_nest or elem(doc, 0) === :doc_string and width == :infinity or is_integer(width) and width >= 0
    
    (elixir 1.13.0) lib/inspect/algebra.ex:958: Inspect.Algebra.format/2
    lib/surface/formatter/phases/render.ex:38: Surface.Formatter.Phases.Render.render_node/2
    (elixir 1.13.0) lib/enum.ex:1593: Enum."-map/2-lists^map/1-0-"/2
    lib/surface/formatter/phases/render.ex:183: Surface.Formatter.Phases.Render.render_node/2
    (elixir 1.13.0) lib/enum.ex:1593: Enum."-map/2-lists^map/1-0-"/2
    (elixir 1.13.0) lib/enum.ex:1593: Enum."-map/2-lists^map/1-0-"/2
    lib/surface/formatter/phases/render.ex:183: Surface.Formatter.Phases.Render.render_node/2
    (elixir 1.13.0) lib/enum.ex:1593: Enum."-map/2-lists^map/1-0-"/2
```

See in CI: https://github.com/jshmrtn/hygeia/runs/4412768200?check_suite_focus=true

## Config

```
[
  import_deps: [:phoenix, :phoenix_live_view, :surface, :ecto, :ecto_enum],
  inputs: [
    ".dialyzer_ignore.exs",
    ".formatter.exs",
    "*.{ex,exs}",
    "priv/*/seeds.exs",
    "priv/repo/seeds/*.exs",
    "{config,lib,test}/**/*.{ex,exs,sface}"
  ],
  subdirectories: ["priv/*/migrations"],
  plugins: [Surface.Formatter.Plugin]
]
```


Pull request:
Fix crash when ran as Formatter Plugin without line_length or surface_line_length
Fixes #52.

Patch:
diff --git a/lib/surface/formatter/plugin.ex b/lib/surface/formatter/plugin.ex
index 70d85d0..06d9273 100644
--- a/lib/surface/formatter/plugin.ex
+++ b/lib/surface/formatter/plugin.ex
@@ -56,7 +56,8 @@ defmodule Surface.Formatter.Plugin do
   end
 
   def format(contents, opts) do
-    opts = Keyword.put(opts, :line_length, opts[:surface_line_length] || opts[:line_length])
+    line_length = opts[:surface_line_length] || opts[:line_length]
+    opts = if line_length, do: Keyword.put(opts, :line_length, line_length), else: opts
     Surface.Formatter.format_string!(contents, opts)
   end
 end


Test patch:
diff --git a/test/surface/formatter/plugin_test.exs b/test/surface/formatter/plugin_test.exs
index e46da78..8c63f31 100644
--- a/test/surface/formatter/plugin_test.exs
+++ b/test/surface/formatter/plugin_test.exs
@@ -65,6 +65,7 @@ defmodule Surface.Formatter.PluginTest do
   test ":surface_line_length overrides :line_length" do
     assert_formatter_output(
       "surface_line_length.ex",
+      [line_length: 200, surface_line_length: 50],
       """
       defmodule Foo do
         def render(assigns) do
@@ -91,4 +92,18 @@ defmodule Surface.Formatter.PluginTest do
       """
     )
   end
+
+  test "omitting :line_length and :surface_line_length defaults to default line_length" do
+    # reproducing a bug that occurred when both were omitted, with an Elixir expression
+    assert_formatter_output(
+      "config.sface",
+      [],
+      """
+        {@foo}
+      """,
+      """
+      {@foo}
+      """
+    )
+  end
 end