Skip to content

Commit

Permalink
Add kmfa to metadata before annotations_for_docs
Browse files Browse the repository at this point in the history
  • Loading branch information
garazdawi authored and josevalim committed Jan 30, 2024
1 parent cf27553 commit c5ffca5
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 16 deletions.
36 changes: 21 additions & 15 deletions lib/ex_doc/retriever.ex
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,12 @@ defmodule ExDoc.Retriever do
doc_file = anno_file(anno, source)
doc_line = anno_line(anno)

metadata =
Map.merge(
%{kind: type, name: name, arity: arity, module: module_data.module},
metadata
)

source_url =
source_link(function_data[:source_file], source, function_data.source_line)

Expand All @@ -255,13 +261,7 @@ defmodule ExDoc.Retriever do
function_data.doc_fallback.()

group =
GroupMatcher.match_function(
groups_for_docs,
Map.merge(
%{kind: type, name: name, arity: arity, module: module_data.module},
metadata
)
)
GroupMatcher.match_function(groups_for_docs, metadata)

%ExDoc.FunctionNode{
id: nil_or_name(name, arity),
Expand Down Expand Up @@ -328,6 +328,12 @@ defmodule ExDoc.Retriever do
source_url =
source_link(callback_data[:source_file], source, callback_data.source_line)

metadata =
Map.merge(
%{kind: kind, name: name, arity: arity, module: module_data.module},
metadata
)

signature = signature(callback_data.signature)
specs = callback_data.specs

Expand All @@ -342,10 +348,7 @@ defmodule ExDoc.Retriever do
group =
GroupMatcher.match_function(
groups_for_docs,
Map.merge(
%{kind: kind, name: name, arity: arity, module: module_data.module},
metadata
)
metadata
)

%ExDoc.FunctionNode{
Expand Down Expand Up @@ -384,6 +387,12 @@ defmodule ExDoc.Retriever do

type_data = module_data.language.type_data(type_entry, module_data)

metadata =
Map.merge(
%{kind: kind, name: name, arity: arity, module: module_data.module},
metadata
)

source_url =
source_link(type_data[:source_file], source, type_data.source_line)

Expand All @@ -401,10 +410,7 @@ defmodule ExDoc.Retriever do
group =
GroupMatcher.match_function(
groups_for_docs,
Map.merge(
%{kind: kind, name: name, arity: arity, module: module_data.module},
metadata
)
metadata
)

%ExDoc.TypeNode{
Expand Down
4 changes: 3 additions & 1 deletion lib/mix/tasks/docs.ex
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,9 @@ defmodule Mix.Tasks.Docs do
be lazily executed.
* `:annotations_for_docs` - a function that receives metadata and returns a list
of annotations to be added to the signature.
of annotations to be added to the signature. The metadata received will also
contain `:module`, `:name`, `:arity` and `:kind` to help identify which entity is
currently being processed.
* `:api_reference` - Whether to generate `api-reference.html`; default: `true`.
If this is set to false, `:main` must also be set.
Expand Down
42 changes: 42 additions & 0 deletions test/ex_doc/retriever_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,48 @@ defmodule ExDoc.RetrieverTest do
assert %{id: "baz/0", group: :"Group 2"} = baz
end

test "function annotations", c do
elixirc(c, ~S"""
defmodule A do
def foo(), do: :ok
end
""")

{[mod], []} =
Retriever.docs_from_modules([A], %ExDoc.Config{
annotations_for_docs: fn metadata ->
[metadata[:module], metadata[:name], metadata[:arity], metadata[:kind]]
end
})

[foo] = mod.docs
assert foo.id == "foo/0"
assert foo.annotations == [A, :foo, 0, :function]
end

test "function annotations override", c do
elixirc(c, ~S"""
defmodule A do
@doc module: B
@doc name: :bar
@doc arity: 1
@doc kind: :type
def foo(), do: :ok
end
""")

{[mod], []} =
Retriever.docs_from_modules([A], %ExDoc.Config{
annotations_for_docs: fn metadata ->
[metadata[:module], metadata[:name], metadata[:arity], metadata[:kind]]
end
})

[foo] = mod.docs
assert foo.id == "foo/0"
assert foo.annotations == [B, :bar, 1, :type]
end

test "custom function annotations", c do
elixirc(c, ~S"""
defmodule A do
Expand Down

0 comments on commit c5ffca5

Please sign in to comment.