Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 19 additions & 6 deletions lib/mix/lib/mix/tasks/help.ex
Original file line number Diff line number Diff line change
Expand Up @@ -94,13 +94,26 @@ defmodule Mix.Tasks.Help do
Mix.raise("Unexpected arguments, expected \"mix help --search PATTERN\"")
end

def run([task]) do
loadpaths!()
opts = Application.get_env(:mix, :colors)
opts = [width: width(), enabled: ansi_docs?(opts)] ++ opts
def run([task_or_module]) do
if Regex.match?(~r/(:|[A-Z]).*/, task_or_module) do
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of a regex, let's use pattern matching!

case task_or_module do
  <<first, _::binary>> when first in ?A..?Z or first == ?: ->

if Mix.Project.get() do
Mix.Tasks.Compile.run([])
end
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe we can call loadpaths!()instead!


for doc <- verbose_doc(task) do
print_doc(task, doc, opts)
task_or_module
|> Code.string_to_quoted!()
|> IEx.Introspection.decompose(__ENV__)
|> Code.eval_quoted()
|> elem(0)
|> IEx.Introspection.h()
else
loadpaths!()
opts = Application.get_env(:mix, :colors)
opts = [width: width(), enabled: ansi_docs?(opts)] ++ opts
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need to apply this configuration to IEx as well, so maybe we need something like:

iex_colors = Application.get_env(:iex, :colors, [])

try do
  Application.put_env(:iex, :colors, Application.get_env(:mix, :colors))
  IEx.Introspection.h()
after
  Application.put_env(:iex, :colors, iex_colors)
end


for doc <- verbose_doc(task_or_module) do
print_doc(task_or_module, doc, opts)
end
end

:ok
Expand Down
46 changes: 46 additions & 0 deletions lib/mix/test/mix/tasks/help_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,52 @@ defmodule Mix.Tasks.HelpTest do
end)
end

test "help Elixir MODULE", context do
in_tmp(context.test, fn ->
output =
capture_io(fn ->
Mix.Tasks.Help.run(["Mix"])
end)

assert output =~
"Mix is a build tool that provides tasks for creating, compiling, and testing\nElixir projects, managing its dependencies, and more."
end)
end

test "help Erlang MODULE", context do
in_tmp(context.test, fn ->
output =
capture_io(fn ->
Mix.Tasks.Help.run([":math"])
end)

assert output =~ "This module provides an interface to a number of mathematical functions."
end)
end

test "help FUNCTION/0", context do
in_tmp(context.test, fn ->
output =
capture_io(fn ->
Mix.Tasks.Help.run(["DateTime.utc_now()"])
end)

assert output =~ "Returns the current datetime in UTC"
end)
end

test "help FUNCTION/1", context do
in_tmp(context.test, fn ->
output =
capture_io(fn ->
Mix.Tasks.Help.run(["Enum.all?/1"])
end)

assert output =~
"Returns \e[36mtrue\e[0m if all elements in \e[36menumerable\e[0m are truthy"
end)
end

test "help --search PATTERN", context do
in_tmp(context.test, fn ->
Mix.Tasks.Help.run(["--search", "deps"])
Expand Down
Loading