Skip to content

Commit 505a50f

Browse files
committed
add support for OTP 27 ex_unit autolinking
add cheatmd
1 parent 1f1f88b commit 505a50f

File tree

3 files changed

+97
-16
lines changed

3 files changed

+97
-16
lines changed

apps/language_server/lib/language_server/dialyzer/supervisor.ex

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
defmodule ElixirLS.LanguageServer.Dialyzer.Supervisor do
2-
alias ElixirLS.LanguageServer.{Dialyzer, DialyzerIncremental}
32
use Supervisor
43

54
def start_link(parent \\ self(), name \\ nil, root_path, dialyzer_module) do

apps/language_server/lib/language_server/markdown_utils.ex

Lines changed: 58 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -95,10 +95,6 @@ defmodule ElixirLS.LanguageServer.MarkdownUtils do
9595
"**Since** #{text}"
9696
end
9797

98-
defp get_metadata_entry_md({:group, text}) when is_binary(text) do
99-
"**Group** #{text}"
100-
end
101-
10298
defp get_metadata_entry_md({:guard, true}) do
10399
"**Guard**"
104100
end
@@ -249,13 +245,21 @@ defmodule ElixirLS.LanguageServer.MarkdownUtils do
249245
{key, url}
250246
end)
251247

248+
@erlang_ex_doc? System.otp_release() |> String.to_integer() >= 27
249+
252250
def transform_ex_doc_link("t:" <> rest, current_module) do
253251
case @builtin_type_url[rest] do
254252
nil ->
255253
case get_module_fun_arity(rest) do
256254
{module, type, arity} ->
257255
if match?(":" <> _, rest) do
258-
"https://www.erlang.org/doc/man/#{module}.html#type-#{type}"
256+
if @erlang_ex_doc? do
257+
# TODO not sure hos the docs will handle versions app/vsn does not work as of June 2024
258+
{app, _vsn} = DocLinks.get_app(module)
259+
"https://www.erlang.org/doc/apps/#{app}/#{module}.html#t:#{type}/#{arity}"
260+
else
261+
"https://www.erlang.org/doc/man/#{module}.html#type-#{type}"
262+
end
259263
else
260264
DocLinks.hex_docs_type_link(module || current_module, type, arity)
261265
end
@@ -270,7 +274,13 @@ defmodule ElixirLS.LanguageServer.MarkdownUtils do
270274
case get_module_fun_arity(rest) do
271275
{module, callback, arity} ->
272276
if match?(":" <> _, rest) do
273-
"https://www.erlang.org/doc/man/#{module}.html#Module:#{callback}-#{arity}"
277+
if @erlang_ex_doc? do
278+
# TODO not sure hos the docs will handle versions app/vsn does not work as of June 2024
279+
{app, _vsn} = DocLinks.get_app(module)
280+
"https://www.erlang.org/doc/apps/#{app}/#{module}.html#c:#{callback}/#{arity}"
281+
else
282+
"https://www.erlang.org/doc/man/#{module}.html#Module:#{callback}-#{arity}"
283+
end
274284
else
275285
DocLinks.hex_docs_callback_link(module || current_module, callback, arity)
276286
end
@@ -280,6 +290,32 @@ defmodule ElixirLS.LanguageServer.MarkdownUtils do
280290
def transform_ex_doc_link("e:http://" <> rest, _current_module), do: "http://" <> rest
281291
def transform_ex_doc_link("e:https://" <> rest, _current_module), do: "https://" <> rest
282292

293+
otp_apps_dir =
294+
:code.get_object_code(:erlang) |> elem(2) |> Path.join("../../..") |> Path.expand()
295+
296+
@all_otp_apps otp_apps_dir
297+
|> File.ls!()
298+
|> Enum.map(&(&1 |> String.split("-") |> hd() |> String.to_atom()))
299+
300+
if @erlang_ex_doc? do
301+
def transform_ex_doc_link("e:system:" <> rest, _current_module) do
302+
{page, anchor} = split(rest)
303+
304+
page =
305+
page
306+
|> String.replace(~r/\.(md|livemd|cheatmd|txt)$/, ".html")
307+
|> String.replace(" ", "-")
308+
|> String.downcase()
309+
310+
"https://www.erlang.org/doc/system/#{page}" <>
311+
if anchor do
312+
"#" <> anchor
313+
else
314+
""
315+
end
316+
end
317+
end
318+
283319
def transform_ex_doc_link("e:" <> rest, current_module) do
284320
{page, anchor} = split(rest)
285321

@@ -291,7 +327,7 @@ defmodule ElixirLS.LanguageServer.MarkdownUtils do
291327

292328
page =
293329
page
294-
|> String.replace(~r/\.(md|livemd|txt)$/, ".html")
330+
|> String.replace(~r/\.(md|livemd|cheatmd|txt)$/, ".html")
295331
|> String.replace(" ", "-")
296332
|> String.downcase()
297333

@@ -321,7 +357,14 @@ defmodule ElixirLS.LanguageServer.MarkdownUtils do
321357
end
322358

323359
if app_vsn do
324-
DocLinks.hex_docs_extra_link(app_vsn, page) <>
360+
{app, _vsn} = app_vsn
361+
362+
if app in @all_otp_apps and @erlang_ex_doc? do
363+
# TODO not sure hos the docs will handle versions app/vsn does not work as of June 2024
364+
"https://www.erlang.org/doc/apps/#{app}/#{page}"
365+
else
366+
DocLinks.hex_docs_extra_link(app_vsn, page)
367+
end <>
325368
if anchor do
326369
"#" <> anchor
327370
else
@@ -344,7 +387,13 @@ defmodule ElixirLS.LanguageServer.MarkdownUtils do
344387

345388
{module, function, arity} ->
346389
if match?(":" <> _, prefix) and module != Kernel.SpecialForms do
347-
"https://www.erlang.org/doc/man/#{module}.html##{function}-#{arity}"
390+
if @erlang_ex_doc? do
391+
# TODO not sure hos the docs will handle versions app/vsn does not work as of June 2024
392+
{app, _vsn} = DocLinks.get_app(module)
393+
"https://www.erlang.org/doc/apps/#{app}/#{module}.html##{function}/#{arity}"
394+
else
395+
"https://www.erlang.org/doc/man/#{module}.html##{function}-#{arity}"
396+
end
348397
else
349398
DocLinks.hex_docs_function_link(module || current_module, function, arity)
350399
end

apps/language_server/test/markdown_utils_test.exs

Lines changed: 39 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -167,8 +167,14 @@ defmodule ElixirLS.LanguageServer.MarkdownUtilsTest do
167167
end
168168

169169
test "erlang type" do
170-
assert MarkdownUtils.transform_ex_doc_links("`t::array.array/0`") ==
171-
"[`:array.array/0`](https://www.erlang.org/doc/man/array.html#type-array)"
170+
expected =
171+
if System.otp_release() |> String.to_integer() >= 27 do
172+
"[`:array.array/0`](https://www.erlang.org/doc/apps/stdlib/array.html#t:array/0)"
173+
else
174+
"[`:array.array/0`](https://www.erlang.org/doc/man/array.html#type-array)"
175+
end
176+
177+
assert MarkdownUtils.transform_ex_doc_links("`t::array.array/0`") == expected
172178
end
173179

174180
test "elixir callback link with prefix" do
@@ -177,8 +183,14 @@ defmodule ElixirLS.LanguageServer.MarkdownUtilsTest do
177183
end
178184

179185
test "erlang callback" do
180-
assert MarkdownUtils.transform_ex_doc_links("`c::gen_server.handle_call/3`") ==
181-
"[`:gen_server.handle_call/3`](https://www.erlang.org/doc/man/gen_server.html#Module:handle_call-3)"
186+
expected =
187+
if System.otp_release() |> String.to_integer() >= 27 do
188+
"[`:gen_server.handle_call/3`](https://www.erlang.org/doc/apps/stdlib/gen_server.html#c:handle_call/3)"
189+
else
190+
"[`:gen_server.handle_call/3`](https://www.erlang.org/doc/man/gen_server.html#Module:handle_call-3)"
191+
end
192+
193+
assert MarkdownUtils.transform_ex_doc_links("`c::gen_server.handle_call/3`") == expected
182194
end
183195

184196
test "elixir callback link without module" do
@@ -237,8 +249,14 @@ defmodule ElixirLS.LanguageServer.MarkdownUtilsTest do
237249
end
238250

239251
test "erlang stdlib function link" do
240-
assert MarkdownUtils.transform_ex_doc_links("`:lists.all/2`") ==
241-
"[`:lists.all/2`](https://www.erlang.org/doc/man/lists.html#all-2)"
252+
expected =
253+
if System.otp_release() |> String.to_integer() >= 27 do
254+
"[`:lists.all/2`](https://www.erlang.org/doc/apps/stdlib/lists.html#all/2)"
255+
else
256+
"[`:lists.all/2`](https://www.erlang.org/doc/man/lists.html#all-2)"
257+
end
258+
259+
assert MarkdownUtils.transform_ex_doc_links("`:lists.all/2`") == expected
242260
end
243261

244262
test "extra page" do
@@ -276,6 +294,21 @@ defmodule ElixirLS.LanguageServer.MarkdownUtilsTest do
276294
) == "[Up and running](http://example.com/foo.md)"
277295
end
278296

297+
test "erlang extra page" do
298+
assert MarkdownUtils.transform_ex_doc_links(
299+
"[Up and running](e:erts_alloc.md)",
300+
:erlang
301+
) == "[Up and running](https://www.erlang.org/doc/apps/erts/erts_alloc.html)"
302+
end
303+
304+
test "erlang extra page with app" do
305+
assert MarkdownUtils.transform_ex_doc_links(
306+
"[Up and running](e:system:expressions.md#term-comparisons)",
307+
:lists
308+
) ==
309+
"[Up and running](https://www.erlang.org/doc/system/expressions.html#term-comparisons)"
310+
end
311+
279312
test "expression" do
280313
assert MarkdownUtils.transform_ex_doc_links("`1 + 2`") == "`1 + 2`"
281314
end

0 commit comments

Comments
 (0)