Skip to content

Commit

Permalink
Fix doesn't respect line_length when formatting the whole file (#29)
Browse files Browse the repository at this point in the history
* Add failing test

* Read line_length from opts when formatting the whole file

* Add comment with explanation for test

* Update CHANGELOG
  • Loading branch information
angelikatyborska authored Nov 21, 2024
1 parent fad970a commit b91ffaa
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 1 deletion.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

## 0.3.1 (2024-11-24)

- Respect `line_length` option when formatting the whole `.ex` file.

## 0.3.0 (2024-04-01)

- Support opaque types in doctest results (e.g. `#User<name: "", ...>`).
Expand Down
4 changes: 3 additions & 1 deletion lib/doctest_formatter/formatter.ex
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,11 @@ defmodule DoctestFormatter.Formatter do
end
end)

desired_line_length = Keyword.get(opts, :line_length, default_elixir_line_length())

forms
|> Code.quoted_to_algebra(to_algebra_opts)
|> Inspect.Algebra.format(default_elixir_line_length())
|> Inspect.Algebra.format(desired_line_length)
|> Kernel.++(["\n"])
|> IO.iodata_to_binary()
end
Expand Down
35 changes: 35 additions & 0 deletions test/doctest_formatter/formatter_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -1049,6 +1049,41 @@ defmodule DoctestFormatter.FormatterTest do
output = format(input, opts)
assert output == desired_output
end

test "uses the desired line length for the non-doctest code too" do
# 300 is much longer than the default 98 chars, if the formatter doesn't respect the option of 300 chars,
# it would try to split the long lines into multiple lines
opts = [line_length: 300]

input =
"""
defmodule Foo do
@doc \"""
iex> "aaa"
"a" <> "a" <> "a"
\"""
def my_long_function(argument1, argument2, argument3, argument4, argument5, argument6, argument7, argument8) do
(1000 + argument1 + argument2 + argument3 + argument4 + argument5 + argument6 + argument7 + argument8) * 2
end
end
"""

desired_output =
"""
defmodule Foo do
@doc \"""
iex> "aaa"
"a" <> "a" <> "a"
\"""
def my_long_function(argument1, argument2, argument3, argument4, argument5, argument6, argument7, argument8) do
(1000 + argument1 + argument2 + argument3 + argument4 + argument5 + argument6 + argument7 + argument8) * 2
end
end
"""

output = format(input, opts)
assert output == desired_output
end
end

describe "format/2 on exceptions" do
Expand Down

0 comments on commit b91ffaa

Please sign in to comment.