Skip to content

Commit 8d69169

Browse files
committed
Support types and callbacks in mix help
1 parent 6f2e24c commit 8d69169

2 files changed

Lines changed: 53 additions & 21 deletions

File tree

lib/mix/lib/mix/tasks/help.ex

Lines changed: 31 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,11 @@ defmodule Mix.Tasks.Help do
2525
2626
But also for modules, functions, and applications:
2727
28-
$ mix help MODULE - prints the definition for the given module
29-
$ mix help MODULE.FUN - prints the definition for the given module+function
28+
$ mix help MODULE - prints the documentation for the given module
29+
$ mix help MODULE.FUN - prints the documentation for the given module+function
3030
$ mix help app:APP - prints a summary of all public modules in application
31+
$ mix help c:MODULE.NAME - prints the documentation for the given callback
32+
$ mix help t:MODULE.NAME - prints the documentation for the given type
3133
3234
## Colors
3335
@@ -149,27 +151,14 @@ defmodule Mix.Tasks.Help do
149151
end
150152
end
151153

152-
def run([module = <<first, _::binary>>]) when first in ?A..?Z or first == ?: do
153-
loadpaths!()
154-
155-
iex_colors = Application.get_env(:iex, :colors, [])
156-
mix_colors = Application.get_env(:mix, :colors, [])
154+
def run(["t:" <> type]),
155+
do: run_iex_doc(:t, type)
157156

158-
try do
159-
Application.put_env(:iex, :colors, mix_colors)
157+
def run(["c:" <> callback]),
158+
do: run_iex_doc(:b, callback)
160159

161-
module
162-
|> Code.string_to_quoted!()
163-
|> IEx.Introspection.decompose(__ENV__)
164-
|> case do
165-
:error -> Mix.raise("Invalid expression: #{module}")
166-
decomposition -> decomposition
167-
end
168-
|> IEx.Introspection.h()
169-
after
170-
Application.put_env(:iex, :colors, iex_colors)
171-
end
172-
end
160+
def run([module = <<first, _::binary>>]) when first in ?A..?Z or first == ?:,
161+
do: run_iex_doc(:h, module)
173162

174163
def run([task]) do
175164
loadpaths!()
@@ -186,6 +175,27 @@ defmodule Mix.Tasks.Help do
186175
Mix.raise("Unexpected arguments, expected \"mix help\" or \"mix help TASK\"")
187176
end
188177

178+
defp run_iex_doc(type, string) do
179+
loadpaths!()
180+
181+
iex_colors = Application.get_env(:iex, :colors, [])
182+
mix_colors = Application.get_env(:mix, :colors, [])
183+
184+
try do
185+
Application.put_env(:iex, :colors, mix_colors)
186+
187+
string
188+
|> Code.string_to_quoted!()
189+
|> IEx.Introspection.decompose(__ENV__)
190+
|> case do
191+
:error -> Mix.raise("Invalid expression: #{string}")
192+
decomposition -> apply(IEx.Introspection, type, [decomposition])
193+
end
194+
after
195+
Application.put_env(:iex, :colors, iex_colors)
196+
end
197+
end
198+
189199
defp ansi_opts do
190200
opts = Application.get_env(:mix, :colors)
191201
[width: width(), enabled: ansi_docs?(opts)] ++ opts

lib/mix/test/mix/tasks/help_test.exs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -322,6 +322,28 @@ defmodule Mix.Tasks.HelpTest do
322322
end)
323323
end
324324

325+
test "help t:MODULE", context do
326+
in_tmp(context.test, fn ->
327+
output =
328+
capture_io(fn ->
329+
Mix.Tasks.Help.run(["t:Enum.t/0"])
330+
end)
331+
332+
assert output =~ "@type t()"
333+
end)
334+
end
335+
336+
test "help c:MODULE", context do
337+
in_tmp(context.test, fn ->
338+
output =
339+
capture_io(fn ->
340+
Mix.Tasks.Help.run(["c:GenServer.init/1"])
341+
end)
342+
343+
assert output =~ "@callback init"
344+
end)
345+
end
346+
325347
test "help ERROR" do
326348
assert_raise Mix.Error, "Invalid expression: Foo.bar(~s[baz])", fn ->
327349
Mix.Tasks.Help.run(["Foo.bar(~s[baz])"])

0 commit comments

Comments
 (0)