Skip to content

Commit 36ec685

Browse files
committed
Fix displaying auto-linked callbacks and types
1 parent 2a156ae commit 36ec685

File tree

4 files changed

+17
-16
lines changed

4 files changed

+17
-16
lines changed

lib/ex_doc/autolink.ex

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,9 +72,10 @@ defmodule ExDoc.Autolink do
7272
end
7373
end
7474

75-
defp walk({:code, _, [code]} = ast, config) do
75+
defp walk({:code, attrs, [code]} = ast, config) do
7676
if url = url(code, :regular, config) do
77-
{:a, [href: url], [ast]}
77+
code = remove_prefix(code)
78+
{:a, [href: url], [{:code, attrs, [code]}]}
7879
else
7980
ast
8081
end
@@ -204,10 +205,14 @@ defmodule ExDoc.Autolink do
204205
end
205206
end
206207

207-
defp kind("t:" <> rest), do: {:type, rest}
208208
defp kind("c:" <> rest), do: {:callback, rest}
209+
defp kind("t:" <> rest), do: {:type, rest}
209210
defp kind(rest), do: {:function, rest}
210211

212+
defp remove_prefix("c:" <> rest), do: rest
213+
defp remove_prefix("t:" <> rest), do: rest
214+
defp remove_prefix(rest), do: rest
215+
211216
defp parse_arity(string) do
212217
case Integer.parse(string) do
213218
{arity, ""} -> {:ok, arity}

test/ex_doc/autolink_test.exs

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -87,37 +87,33 @@ defmodule ExDoc.AutolinkTest do
8787

8888
test "elixir callback" do
8989
assert autolink("c:GenServer.handle_call/3") ==
90-
~m"[`c:GenServer.handle_call/3`](https://hexdocs.pm/elixir/GenServer.html#c:handle_call/3)"
91-
92-
# TODO: there should be no `c:` in the link _text_!
93-
# assert autolink("c:GenServer.handle_call/3") ==
94-
# ~m"[`GenServer.handle_call/3`](https://hexdocs.pm/elixir/GenServer.html#c:handle_call/3)"
90+
~m"[`GenServer.handle_call/3`](https://hexdocs.pm/elixir/GenServer.html#c:handle_call/3)"
9591
end
9692

9793
test "erlang callback" do
9894
assert autolink("c::gen_server.handle_call/3") ==
99-
~m"[`c::gen_server.handle_call/3`](http://www.erlang.org/doc/man/gen_server.html#Module:handle_call-3)"
95+
~m"[`:gen_server.handle_call/3`](http://www.erlang.org/doc/man/gen_server.html#Module:handle_call-3)"
10096
end
10197

10298
test "elixir type" do
10399
assert autolink("t:Calendar.date/0") ==
104-
~m"[`t:Calendar.date/0`](https://hexdocs.pm/elixir/Calendar.html#t:date/0)"
100+
~m"[`Calendar.date/0`](https://hexdocs.pm/elixir/Calendar.html#t:date/0)"
105101
end
106102

107103
test "elixir basic & built-in types" do
108104
assert autolink("t:atom/0") ==
109-
~m"[`t:atom/0`](https://hexdocs.pm/elixir/typespecs.html#basic-types)"
105+
~m"[`atom/0`](https://hexdocs.pm/elixir/typespecs.html#basic-types)"
110106

111107
assert autolink("t:keyword/0") ==
112-
~m"[`t:keyword/0`](https://hexdocs.pm/elixir/typespecs.html#built-in-types)"
108+
~m"[`keyword/0`](https://hexdocs.pm/elixir/typespecs.html#built-in-types)"
113109

114110
assert autolink("t:keyword/0", app: :elixir) ==
115-
~m"[`t:keyword/0`](typespecs.html#built-in-types)"
111+
~m"[`keyword/0`](typespecs.html#built-in-types)"
116112
end
117113

118114
test "erlang type" do
119115
assert autolink("t::array.array/0") ==
120-
~m"[`t::array.array/0`](http://www.erlang.org/doc/man/array.html#type-array)"
116+
~m"[`:array.array/0`](http://www.erlang.org/doc/man/array.html#type-array)"
121117
end
122118

123119
test "special forms" do

test/ex_doc/formatter/html/templates_test.exs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -378,7 +378,7 @@ defmodule ExDoc.Formatter.HTML.TemplatesTest do
378378
refute content =~ ~s[minus(#{integer}, #{integer}) :: #{integer}]
379379

380380
assert content =~
381-
~s[Basic type: <a href=\"https://hexdocs.pm/elixir/typespecs.html#basic-types\"><code class=\"inline\">t:atom/0</code></a>.]
381+
~s[Basic type: <a href=\"https://hexdocs.pm/elixir/typespecs.html#basic-types\"><code class=\"inline\">atom/0</code></a>.]
382382

383383
assert content =~ ~r{opaque/0.*<span class="note">\(opaque\)</span>}ms
384384
end

test/ex_doc/formatter/html_test.exs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -319,7 +319,7 @@ defmodule ExDoc.Formatter.HTMLTest do
319319
~r{<a href="TypesAndSpecs.Sub.html"><code(\sclass="inline")?>TypesAndSpecs.Sub</code></a>}
320320

321321
assert content =~
322-
~r{<a href="typespecs.html#basic-types"><code(\sclass="inline")?>t:atom/0</code></a>}
322+
~r{<a href="typespecs.html#basic-types"><code(\sclass="inline")?>atom/0</code></a>}
323323

324324
assert content =~
325325
~r{<a href="https://hexdocs.pm/mix/Mix.Tasks.Compile.Elixir.html"><code(\sclass="inline")?>mix compile.elixir</code></a>}

0 commit comments

Comments
 (0)