-
Notifications
You must be signed in to change notification settings - Fork 351
Autolink mix tasks #911
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Autolink mix tasks #911
Changes from all commits
19a641c
7948f6f
93a5e87
c113d85
5e00c08
2010e9f
feaf03a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,7 +5,7 @@ defmodule ExDoc.Formatter.HTML.Autolink do | |
import ExDoc.Formatter.HTML.Templates, only: [h: 1, enc_h: 1] | ||
|
||
@type language :: :elixir | :erlang | :markdown | ||
@type kind :: :function | :module | ||
@type kind :: :function | :module | :mix_task | ||
@type link_type :: :normal | :custom | ||
|
||
@backtick_token "<B706848484895T>" | ||
|
@@ -106,7 +106,7 @@ defmodule ExDoc.Formatter.HTML.Autolink do | |
language <- [:elixir, :erlang], | ||
kind <- [:module, :function] do | ||
{kind, language, link_type} | ||
end) | ||
end) ++ [{:mix_task, :elixir, :normal}] | ||
|
||
@doc """ | ||
Autolinks any documentation in the project. | ||
|
@@ -325,7 +325,7 @@ defmodule ExDoc.Formatter.HTML.Autolink do | |
# | ||
# `language` can be: `:elixir`, `:erlang` or `:markdown`. | ||
# | ||
# `kind` is either `:function` or `:module`. | ||
# `kind` is either `:function`, `:module`, or `:mix_task`. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm OK with :mix_task, but I'm wondering. Is there any other kind of task? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. there are Rebar tasks which we might similarly support one day but not sure how hard that would be. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. beacuse I'm seeing we are calling My only concern is that there are other tools other than Mix and Rebar for creating tasks that we may support There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 for |
||
# | ||
# It accepts a list of `options` used in the replacement functions. | ||
# - `:aliases | ||
|
@@ -447,6 +447,37 @@ defmodule ExDoc.Formatter.HTML.Autolink do | |
end | ||
end | ||
|
||
defp replace_fun(:mix_task, :elixir, :normal, options) do | ||
extension = options[:extension] || ".html" | ||
lib_dirs = options[:lib_dirs] || default_lib_dirs(:elixir) | ||
module_id = options[:module_id] || nil | ||
modules_refs = options[:modules_refs] || [] | ||
|
||
fn all, text, "mix " <> task_name -> | ||
task_module = | ||
task_name | ||
|> String.split(".") | ||
|> Enum.map(&Macro.camelize/1) | ||
|> Enum.join(".") | ||
|
||
match = "Mix.Tasks." <> task_module | ||
|
||
cond do | ||
match == module_id -> | ||
"[#{text}](#{match}#{extension}#content)" | ||
|
||
match in modules_refs -> | ||
"[#{text}](#{match}#{extension})" | ||
|
||
doc = module_docs(:elixir, match, lib_dirs) -> | ||
"[#{text}](#{doc}#{match}.html)" | ||
|
||
true -> | ||
all | ||
end | ||
end | ||
end | ||
|
||
## Helpers | ||
|
||
defp default_text(_, :custom, _match, _pmfa, text), | ||
|
@@ -721,6 +752,12 @@ defmodule ExDoc.Formatter.HTML.Autolink do | |
}x | ||
end | ||
|
||
defp re_kind_language(:mix_task, :elixir) do | ||
~r{ | ||
mix\ ([a-z][a-z0-9\._]*) | ||
}x | ||
end | ||
|
||
defp re_kind_language_link_type(kind, language, link_type) do | ||
source = Regex.source(re_kind_language(kind, language)) | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -327,6 +327,45 @@ defmodule ExDoc.Formatter.HTML.AutolinkTest do | |
end | ||
end | ||
|
||
describe "Mix tasks" do | ||
test "autolinks built-in tasks" do | ||
assert project_doc("`mix test`", %{}) == | ||
"[`mix test`](#{@elixir_docs}mix/Mix.Tasks.Test.html)" | ||
end | ||
|
||
test "autolinks custom tasks" do | ||
assert project_doc("`mix foo.bar.baz`", %{modules_refs: ["Mix.Tasks.Foo.Bar.Baz"]}) == | ||
"[`mix foo.bar.baz`](Mix.Tasks.Foo.Bar.Baz.html)" | ||
end | ||
|
||
test "autolinks task in the same module" do | ||
assert project_doc("`mix foo`", %{ | ||
modules_refs: ["Mix.Tasks.Foo"], | ||
module_id: "Mix.Tasks.Foo" | ||
}) == "[`mix foo`](Mix.Tasks.Foo.html#content)" | ||
end | ||
|
||
test "autolinks tasks from dependencies" do | ||
# Using Hex (archive) as we don't depend on any packages with Mix tasks | ||
lib_dirs = [{Application.app_dir(:hex), "#{@elixir_docs}hex/"}] | ||
|
||
assert project_doc("`mix hex.publish`", %{ | ||
docs_refs: ["MyModule"], | ||
module_id: "MyModule", | ||
extension: ".html", | ||
lib_dirs: lib_dirs | ||
}) == "[`mix hex.publish`](#{@elixir_docs}hex/Mix.Tasks.Hex.Publish.html)" | ||
end | ||
|
||
test "does not autolink task with arguments" do | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. as the 1st step I skipped it but autolinking these too can be nice, see screenshot. |
||
assert project_doc("`mix test test/`", %{}) == "`mix test test/`" | ||
end | ||
|
||
test "does not autolink unknown task" do | ||
assert project_doc("`mix foo`", %{}) == "`mix foo`" | ||
end | ||
end | ||
|
||
describe "Erlang modules" do | ||
test "autolinks to Erlang modules" do | ||
assert project_doc("`:erlang`", %{}) == "[`:erlang`](#{@erlang_docs}erlang.html)" | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,6 +5,7 @@ | |
`==/2` | ||
[`===`](`Kernel.===/2`) | ||
`t:atom/0` | ||
`mix compile.elixir` | ||
|
||
## `Header` sample | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I guess we could also support
:custom
links.such as we do with the rest of the funtions.
ex_doc/test/ex_doc/formatter/html/autolink_test.exs
Lines 433 to 434 in 5e00c08
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@fertapric convinced me that custom links can sometimes hurt IEx experience so how about we hold off on this change for now?
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is that something that we could improve on the IEx side?