Skip to content

Commit ca40d15

Browse files
wojtekmachmichalmuskala
authored andcommitted
Show docs for callback with multiple clauses (#8553)
* Merge callback's multiple clauses in elixir_erl.erl * Simpler callbacks deduplication
1 parent ae6ae8c commit ca40d15

File tree

4 files changed

+35
-5
lines changed

4 files changed

+35
-5
lines changed

lib/elixir/lib/kernel/typespec.ex

+5-2
Original file line numberDiff line numberDiff line change
@@ -108,8 +108,11 @@ defmodule Kernel.Typespec do
108108

109109
case spec_to_signature(expr) do
110110
{name, arity} ->
111-
{line, doc} = get_doc_info(set, :doc, line)
112-
store_doc(set, kind, name, arity, line, :doc, doc, %{})
111+
# store doc only once in case callback has multiple clauses
112+
unless :ets.member(set, {kind, name, arity}) do
113+
{line, doc} = get_doc_info(set, :doc, line)
114+
store_doc(set, kind, name, arity, line, :doc, doc, %{})
115+
end
113116

114117
:error ->
115118
:error

lib/elixir/src/elixir_erl.erl

+7-3
Original file line numberDiff line numberDiff line change
@@ -320,7 +320,12 @@ typespecs_form(Map, TranslatedTypespecs, MacroNames) ->
320320
Forms1 = types_form(Types, Forms0),
321321
Forms2 = callspecs_form(spec, Specs, [], MacroNames, Forms1, Map),
322322
Forms3 = callspecs_form(callback, AllCallbacks, OptionalCallbacks, MacroCallbackNames, Forms2, Map),
323-
{Types, AllCallbacks, Forms3}.
323+
324+
AllCallbacksWithoutSpecs = lists:usort([
325+
{Kind, Name, Arity} || {Kind, {Name, Arity}, _Line, _Spec} <- AllCallbacks
326+
]),
327+
328+
{Types, AllCallbacksWithoutSpecs, Forms3}.
324329

325330
%% Types
326331

@@ -518,8 +523,7 @@ get_callback_docs(Set, Callbacks) ->
518523
[],
519524
doc_value(Doc),
520525
Meta
521-
} || {Kind, {Name, Arity}, _, _} <- Callbacks,
522-
{Key, Line, Doc, Meta} <- ets:lookup(Set, {Kind, Name, Arity})].
526+
} || Callback <- Callbacks, {Key, Line, Doc, Meta} <- ets:lookup(Set, Callback)].
523527

524528
get_type_docs(Set, Types) ->
525529
[{Key,

lib/elixir/test/elixir/kernel/docs_test.exs

+16
Original file line numberDiff line numberDiff line change
@@ -164,9 +164,17 @@ defmodule Kernel.DocsTest do
164164
@callback bar() :: term
165165
@callback baz(any, term) :: any
166166

167+
@doc "Callback with multiple clauses"
168+
@callback callback_multi(integer) :: integer
169+
@callback callback_multi(atom) :: atom
170+
167171
@doc "Macrocallback doc"
168172
@macrocallback qux(any) :: any
169173

174+
@doc "Macrocallback with multiple clauses"
175+
@macrocallback macrocallback_multi(integer) :: integer
176+
@macrocallback macrocallback_multi(atom) :: atom
177+
170178
@doc "Function doc"
171179
@doc since: "1.2.3", color: :red
172180
@doc color: :blue, stable: true
@@ -210,6 +218,7 @@ defmodule Kernel.DocsTest do
210218
[
211219
callback_bar,
212220
callback_baz,
221+
callback_multi,
213222
callback_foo,
214223
function_struct_0,
215224
function_struct_1,
@@ -219,6 +228,7 @@ defmodule Kernel.DocsTest do
219228
function_nullary,
220229
function_qux,
221230
guard_is_zero,
231+
macrocallback_multi,
222232
macrocallback_qux,
223233
type_bar,
224234
type_foo
@@ -231,6 +241,9 @@ defmodule Kernel.DocsTest do
231241
%{since: "1.2.3", deprecated: "use baz/2 instead", color: :blue, stable: true}} =
232242
callback_foo
233243

244+
assert {{:callback, :callback_multi, 1}, _, [], %{"en" => "Callback with multiple clauses"},
245+
%{}} = callback_multi
246+
234247
assert {{:function, :__struct__, 0}, _, ["%Kernel.DocsTest.SampleDocs{}"],
235248
%{"en" => "My struct"}, %{}} = function_struct_0
236249

@@ -259,6 +272,9 @@ defmodule Kernel.DocsTest do
259272
assert {{:macro, :is_zero, 1}, _, ["is_zero(v)"], %{"en" => "A guard"}, %{guard: true}} =
260273
guard_is_zero
261274

275+
assert {{:macrocallback, :macrocallback_multi, 1}, _, [],
276+
%{"en" => "Macrocallback with multiple clauses"}, %{}} = macrocallback_multi
277+
262278
assert {{:macrocallback, :qux, 1}, _, [], %{"en" => "Macrocallback doc"}, %{}} =
263279
macrocallback_qux
264280

lib/iex/test/iex/helpers_test.exs

+7
Original file line numberDiff line numberDiff line change
@@ -677,6 +677,13 @@ defmodule IEx.HelpersTest do
677677
@callback test(:foo) :: integer()
678678
@callback test(:bar) :: [integer()]
679679
"""
680+
681+
assert capture_io(fn -> b(MultipleClauseCallback.test()) end) =~ """
682+
@callback test(:foo) :: integer()
683+
@callback test(:bar) :: [integer()]
684+
685+
callback
686+
"""
680687
end)
681688
after
682689
cleanup_modules([MultipleClauseCallback])

0 commit comments

Comments
 (0)