From 72bb75889d0eef8df71cbf40bfd9d7bb8ed16120 Mon Sep 17 00:00:00 2001 From: Lukas Larsson Date: Wed, 17 Apr 2024 14:59:36 +0200 Subject: [PATCH] Only remove -spec/-type from first line (#1894) 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. --- lib/ex_doc/language/erlang.ex | 17 +++++------------ 1 file changed, 5 insertions(+), 12 deletions(-) diff --git a/lib/ex_doc/language/erlang.ex b/lib/ex_doc/language/erlang.ex index d4d5f5a89..ca9b16985 100644 --- a/lib/ex_doc/language/erlang.ex +++ b/lib/ex_doc/language/erlang.ex @@ -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 @@ -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