Skip to content

Commit

Permalink
Fix displaying auto-linked callbacks and types
Browse files Browse the repository at this point in the history
  • Loading branch information
wojtekmach committed May 11, 2020
1 parent 2a156ae commit 36ec685
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 16 deletions.
11 changes: 8 additions & 3 deletions lib/ex_doc/autolink.ex
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,10 @@ defmodule ExDoc.Autolink do
end
end

defp walk({:code, _, [code]} = ast, config) do
defp walk({:code, attrs, [code]} = ast, config) do
if url = url(code, :regular, config) do
{:a, [href: url], [ast]}
code = remove_prefix(code)
{:a, [href: url], [{:code, attrs, [code]}]}
else
ast
end
Expand Down Expand Up @@ -204,10 +205,14 @@ defmodule ExDoc.Autolink do
end
end

defp kind("t:" <> rest), do: {:type, rest}
defp kind("c:" <> rest), do: {:callback, rest}
defp kind("t:" <> rest), do: {:type, rest}
defp kind(rest), do: {:function, rest}

defp remove_prefix("c:" <> rest), do: rest
defp remove_prefix("t:" <> rest), do: rest
defp remove_prefix(rest), do: rest

defp parse_arity(string) do
case Integer.parse(string) do
{arity, ""} -> {:ok, arity}
Expand Down
18 changes: 7 additions & 11 deletions test/ex_doc/autolink_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -87,37 +87,33 @@ defmodule ExDoc.AutolinkTest do

test "elixir callback" do
assert autolink("c:GenServer.handle_call/3") ==
~m"[`c:GenServer.handle_call/3`](https://hexdocs.pm/elixir/GenServer.html#c:handle_call/3)"

# TODO: there should be no `c:` in the link _text_!
# assert autolink("c:GenServer.handle_call/3") ==
# ~m"[`GenServer.handle_call/3`](https://hexdocs.pm/elixir/GenServer.html#c:handle_call/3)"
~m"[`GenServer.handle_call/3`](https://hexdocs.pm/elixir/GenServer.html#c:handle_call/3)"
end

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

test "elixir type" do
assert autolink("t:Calendar.date/0") ==
~m"[`t:Calendar.date/0`](https://hexdocs.pm/elixir/Calendar.html#t:date/0)"
~m"[`Calendar.date/0`](https://hexdocs.pm/elixir/Calendar.html#t:date/0)"
end

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

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

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

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

test "special forms" do
Expand Down
2 changes: 1 addition & 1 deletion test/ex_doc/formatter/html/templates_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -378,7 +378,7 @@ defmodule ExDoc.Formatter.HTML.TemplatesTest do
refute content =~ ~s[minus(#{integer}, #{integer}) :: #{integer}]

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

assert content =~ ~r{opaque/0.*<span class="note">\(opaque\)</span>}ms
end
Expand Down
2 changes: 1 addition & 1 deletion test/ex_doc/formatter/html_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -319,7 +319,7 @@ defmodule ExDoc.Formatter.HTMLTest do
~r{<a href="TypesAndSpecs.Sub.html"><code(\sclass="inline")?>TypesAndSpecs.Sub</code></a>}

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

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

0 comments on commit 36ec685

Please sign in to comment.