Skip to content

Commit 2f84895

Browse files
author
José Valim
committed
Merge pull request #918 from alco/iex-h-fix
Make h() in IEx look at Kernel.SpecialForms too
2 parents 538a29e + 24a4929 commit 2f84895

File tree

1 file changed

+39
-12
lines changed

1 file changed

+39
-12
lines changed

lib/iex/lib/iex/helpers.ex

Lines changed: 39 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -123,8 +123,17 @@ defmodule IEx.Helpers do
123123

124124
defmacro h({ name, _, args }) when args == [] or is_atom(args) do
125125
quote do
126-
h(unquote(__MODULE__), unquote(name))
127-
h(Kernel, unquote(name))
126+
candidates = [unquote(__MODULE__), Kernel, Kernel.SpecialForms]
127+
128+
# If we got at least one :ok, final result will be :ok
129+
Enum.reduce candidates, :not_found, fn(mod, flag) ->
130+
ret = h(mod, unquote(name))
131+
if flag == :ok do
132+
:ok
133+
else
134+
ret
135+
end
136+
end
128137
end
129138
end
130139

@@ -134,17 +143,31 @@ defmodule IEx.Helpers do
134143
end
135144
end
136145

146+
defmacrop mfa_exported?(module, function, arity) do
147+
quote do
148+
function_exported?(unquote(module), unquote(function), unquote(arity)) or
149+
macro_exported?(unquote(module), unquote(function), unquote(arity))
150+
end
151+
end
152+
153+
defp h_kernel(function, arity) do
154+
if mfa_exported?(Kernel, function, arity) do
155+
h(Kernel, function, arity)
156+
else
157+
h(Kernel.SpecialForms, function, arity)
158+
end
159+
end
160+
137161
@doc false
138162
def h(:h, 1) do
139163
h(__MODULE__, :h, 1)
140164
end
141165

142166
def h(function, arity) when is_atom(function) and is_integer(arity) do
143-
if function_exported?(__MODULE__, function, arity) or
144-
macro_exported?(__MODULE__, function, arity) do
167+
if mfa_exported?(__MODULE__, function, arity) do
145168
h(__MODULE__, function, arity)
146169
else
147-
h(Kernel, function, arity)
170+
h_kernel(function, arity)
148171
end
149172
end
150173

@@ -166,12 +189,16 @@ defmodule IEx.Helpers do
166189
end
167190

168191
def h(module, function) when is_atom(module) and is_atom(function) do
169-
lc {{f, arity}, _line, _type, _args, doc } inlist module.__info__(:docs),
192+
result = lc {{f, arity}, _line, _type, _args, doc } inlist module.__info__(:docs),
170193
f == function and doc != false do
171194
h(module, function, arity)
172195
IO.puts ""
173196
end
174-
:ok
197+
if result != [] do
198+
:ok
199+
else
200+
:not_found
201+
end
175202
end
176203

177204
def h(_, _) do
@@ -235,7 +262,7 @@ defmodule IEx.Helpers do
235262
end
236263

237264
@doc """
238-
Prints all types for the given module or prints out a specified type's
265+
Prints all types for the given module or prints out a specified type's
239266
specification
240267
241268
## Examples
@@ -265,7 +292,7 @@ defmodule IEx.Helpers do
265292

266293
@doc false
267294
def t(module, type) when is_atom(type) do
268-
types = lc {_, {t, _, _args}} = typespec inlist Kernel.Typespec.beam_types(module),
295+
types = lc {_, {t, _, _args}} = typespec inlist Kernel.Typespec.beam_types(module),
269296
t == type do
270297
print_type(typespec)
271298
typespec
@@ -290,7 +317,7 @@ defmodule IEx.Helpers do
290317

291318
@doc false
292319
def t(module, type, arity) do
293-
types = lc {_, {t, _, args}} = typespec inlist Kernel.Typespec.beam_types(module),
320+
types = lc {_, {t, _, args}} = typespec inlist Kernel.Typespec.beam_types(module),
294321
length(args) == arity and t == type, do: typespec
295322

296323
case types do
@@ -336,7 +363,7 @@ defmodule IEx.Helpers do
336363
defmacro s({ :/, _, [{ fun, _, args }, arity] }) when args == [] or is_atom(args) do
337364
quote do
338365
s(Kernel, unquote(fun), unquote(arity))
339-
end
366+
end
340367
end
341368

342369
defmacro s(module) do
@@ -461,7 +488,7 @@ defmodule IEx.Helpers do
461488
flush
462489
after
463490
0 -> :ok
464-
end
491+
end
465492
end
466493

467494
defp iex_reloaded do

0 commit comments

Comments
 (0)