Skip to content

Commit 3ae0007

Browse files
author
José Valim
committed
Remove unecessary protocol in IEx.Autocomplete
1 parent b4b9ecc commit 3ae0007

File tree

1 file changed

+29
-42
lines changed

1 file changed

+29
-42
lines changed

lib/iex/lib/iex/autocomplete.ex

Lines changed: 29 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -4,45 +4,6 @@ defmodule IEx.Autocomplete do
44
defrecord Mod, name: nil, type: nil
55
defrecord Fun, name: nil, arities: []
66

7-
defprotocol Entry do
8-
@moduledoc false
9-
def to_entries(entry)
10-
def to_uniq_entries(entry)
11-
def to_hint(entry, hint)
12-
end
13-
14-
defimpl Entry, for: Mod do
15-
@moduledoc false
16-
17-
def to_entries(mod) do
18-
[mod.name]
19-
end
20-
21-
def to_uniq_entries(_fun) do
22-
[]
23-
end
24-
25-
def to_hint(Mod[name: name], hint) do
26-
:binary.part(name, size(hint), size(name)-size(hint)) <> "."
27-
end
28-
end
29-
30-
defimpl Entry, for: Fun do
31-
@moduledoc false
32-
33-
def to_entries(fun) do
34-
lc a inlist fun.arities, do: "#{fun.name}/#{a}"
35-
end
36-
37-
def to_uniq_entries(fun) do
38-
to_entries(fun)
39-
end
40-
41-
def to_hint(Fun[name: name], hint) do
42-
:binary.part(name, size(hint), size(name)-size(hint))
43-
end
44-
end
45-
467
def expand([]) do
478
funs = module_funs(IEx.Helpers) ++ module_funs(Kernel)
489
mods = [Mod[name: "Elixir", type: :elixir]]
@@ -129,8 +90,8 @@ defmodule IEx.Autocomplete do
12990
end
13091

13192
defp format_expansion([uniq], hint) do
132-
hint = Entry.to_hint(uniq, hint)
133-
uniq = if hint == "", do: Entry.to_uniq_entries(uniq), else: []
93+
hint = to_hint(uniq, hint)
94+
uniq = if hint == "", do: to_uniq_entries(uniq), else: []
13495
yes(hint, uniq)
13596
end
13697

@@ -139,7 +100,7 @@ defmodule IEx.Autocomplete do
139100
length = byte_size(hint)
140101
prefix = :binary.longest_common_prefix(binary)
141102
if prefix in [0, length] do
142-
entries = Enum.reduce(entries, [], fn e, acc -> Entry.to_entries(e) ++ acc end)
103+
entries = Enum.reduce(entries, [], fn e, acc -> to_entries(e) ++ acc end)
143104
yes("", entries)
144105
else
145106
yes(:binary.part(first.name, prefix, length-prefix), [])
@@ -270,4 +231,30 @@ defmodule IEx.Autocomplete do
270231

271232
defp ensure_loaded(Elixir), do: { :error, :nofile }
272233
defp ensure_loaded(mod), do: Code.ensure_compiled(mod)
234+
235+
## Ad-hoc conversions
236+
237+
defp to_entries(Mod[name: name]) do
238+
[name]
239+
end
240+
241+
defp to_entries(Fun[name: name, arities: arities]) do
242+
lc a inlist arities, do: "#{name}/#{a}"
243+
end
244+
245+
defp to_uniq_entries(Mod[]) do
246+
[]
247+
end
248+
249+
defp to_uniq_entries(Fun[] = fun) do
250+
to_entries(fun)
251+
end
252+
253+
defp to_hint(Mod[name: name], hint) do
254+
:binary.part(name, size(hint), size(name)-size(hint)) <> "."
255+
end
256+
257+
defp to_hint(Fun[name: name], hint) do
258+
:binary.part(name, size(hint), size(name)-size(hint))
259+
end
273260
end

0 commit comments

Comments
 (0)