Skip to content

Commit

Permalink
Only remove -spec/-type from first line (#1894)
Browse files Browse the repository at this point in the history
Before this change a spec looking like this:

```
-spec foo(integer()) -> ok;
         (atom()) -> ok.
```

would be transformed to:

```
-spec foo(integer()) -> ok;
    (atom()) -> ok.
```

that is the second clause would be indented incorrectly.
  • Loading branch information
garazdawi authored Apr 17, 2024
1 parent 7f8e971 commit 72bb758
Showing 1 changed file with 5 additions and 12 deletions.
17 changes: 5 additions & 12 deletions lib/ex_doc/language/erlang.ex
Original file line number Diff line number Diff line change
Expand Up @@ -709,7 +709,11 @@ defmodule ExDoc.Language.Erlang do
offset = byte_size(Atom.to_string(type)) + 2

options = [linewidth: 98 + offset]
:erl_pp.attribute(ast, options) |> IO.iodata_to_binary() |> trim_offset(offset)

:erl_pp.attribute(ast, options)
|> IO.chardata_to_string()
|> String.trim()
|> String.trim_leading("-#{Atom.to_string(type)} ")
end

## Helpers
Expand All @@ -723,15 +727,4 @@ defmodule ExDoc.Language.Erlang do
:module
end
end

# `-type t() :: atom()` becomes `t() :: atom().`
defp trim_offset(binary, offset) do
binary
|> String.trim()
|> String.split("\n")
|> Enum.map(fn line ->
binary_part(line, offset, byte_size(line) - offset)
end)
|> Enum.join("\n")
end
end

0 comments on commit 72bb758

Please sign in to comment.