Skip to content

Commit 5f39d87

Browse files
committed
In case function has no docs, use the ones from callback
1 parent aab85cf commit 5f39d87

File tree

3 files changed

+23
-8
lines changed

3 files changed

+23
-8
lines changed

lib/ex_doc/retriever.ex

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -236,14 +236,7 @@ defmodule ExDoc.Retriever do
236236
function = actual_def(name, arity, type)
237237
line = find_actual_line(module_info.abst_code, function, :function) || doc_line
238238

239-
behaviour = Map.get(module_info.impls, {name, arity})
240-
241-
doc =
242-
if is_nil(doc) && behaviour do
243-
"Callback implementation for `c:#{inspect behaviour}.#{name}/#{arity}`."
244-
else
245-
doc
246-
end
239+
doc = docstring(doc, name, arity, Map.fetch(module_info.impls, {name, arity}))
247240

248241
specs = module_info.specs
249242
|> Map.get(function, [])
@@ -272,6 +265,22 @@ defmodule ExDoc.Retriever do
272265
end
273266
end
274267

268+
defp docstring(nil, name, arity, {:ok, behaviour}) do
269+
callback_docs = Code.get_docs(behaviour, :callback_docs) || []
270+
case List.keyfind(callback_docs, {name, arity}, 0) do
271+
{{^name, ^arity}, _, :callback, callback_docs} when is_binary(callback_docs) ->
272+
[header, body] = String.split(callback_docs, "\n", parts: 2)
273+
info = "Documentation for callback `c:#{inspect behaviour}.#{name}/#{arity}`."
274+
"#{header}\n\n#{info}\n\n#{String.trim_leading(body)}"
275+
_ ->
276+
"Callback implementation for `c:#{inspect behaviour}.#{name}/#{arity}`."
277+
end
278+
end
279+
280+
defp docstring(doc, _name, _arity, _behaviour) do
281+
doc
282+
end
283+
275284
defp get_callbacks(%{type: :behaviour, name: name, abst_code: abst_code}, source) do
276285
optional_callbacks = get_optional_callbacks(abst_code)
277286
(Code.get_docs(name, :callback_docs) || [])

test/ex_doc/retriever_test.exs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,10 @@ defmodule ExDoc.RetrieverTest do
196196
"A doc for this so it doesn't use 'Callback implementation for'"
197197
assert Enum.at(docs, 1).doc ==
198198
"Callback implementation for `c:CustomBehaviourOne.greet/1`."
199+
assert Enum.at(docs, 2).doc ==
200+
"This is a sample callback.\n\n" <>
201+
"Documentation for callback `c:CustomBehaviourOne.hello/1`.\n\n" <>
202+
"With description\n"
199203
end
200204

201205
## PROTOCOLS

test/fixtures/behaviour.ex

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ defmodule CustomBehaviourOne do
44

55
@doc """
66
This is a sample callback.
7+
8+
With description
79
"""
810
@callback hello(integer) :: integer
911
@callback greet(integer | String.t) :: integer

0 commit comments

Comments
 (0)