Skip to content

Commit 2c2d033

Browse files
johansjaericmj
authored andcommitted
Run the code formatter on IEx.Autocomplete (#6767)
1 parent e8550bc commit 2c2d033

File tree

1 file changed

+64
-42
lines changed

1 file changed

+64
-42
lines changed

lib/iex/lib/iex/autocomplete.ex

Lines changed: 64 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,19 @@ defmodule IEx.Autocomplete do
1111
cond do
1212
h === ?. and t != [] ->
1313
expand_dot(reduce(t), server)
14+
1415
h === ?: and t == [] ->
1516
expand_erlang_modules()
17+
1618
identifier?(h) ->
1719
expand_expr(reduce(expr), server)
18-
(h == ?/) and t != [] and identifier?(hd(t)) ->
20+
21+
h == ?/ and t != [] and identifier?(hd(t)) ->
1922
expand_expr(reduce(t), server)
23+
2024
h in '([{' ->
2125
expand('')
26+
2227
true ->
2328
no()
2429
end
@@ -34,36 +39,44 @@ defmodule IEx.Autocomplete do
3439
end
3540

3641
defp identifier?(h) do
37-
(h in ?a..?z) or (h in ?A..?Z) or (h in ?0..?9) or h in [?_, ??, ?!]
42+
h in ?a..?z or h in ?A..?Z or h in ?0..?9 or h in [?_, ??, ?!]
3843
end
3944

4045
defp expand_dot(expr, server) do
41-
case Code.string_to_quoted expr do
46+
case Code.string_to_quoted(expr) do
4247
{:ok, atom} when is_atom(atom) ->
4348
expand_call(atom, "", server)
49+
4450
{:ok, {:__aliases__, _, list}} ->
4551
expand_elixir_modules(list, "", server)
52+
4653
{:ok, {_, _, _} = ast_node} ->
4754
expand_call(ast_node, "", server)
55+
4856
_ ->
4957
no()
5058
end
5159
end
5260

5361
defp expand_expr(expr, server) do
54-
case Code.string_to_quoted expr do
62+
case Code.string_to_quoted(expr) do
5563
{:ok, atom} when is_atom(atom) ->
5664
expand_erlang_modules(Atom.to_string(atom))
65+
5766
{:ok, {atom, _, nil}} when is_atom(atom) ->
5867
expand_variable_or_import(Atom.to_string(atom), server)
68+
5969
{:ok, {:__aliases__, _, [root]}} ->
6070
expand_elixir_modules([], Atom.to_string(root), server)
71+
6172
{:ok, {:__aliases__, _, [h | _] = list}} when is_atom(h) ->
6273
hint = Atom.to_string(List.last(list))
6374
list = Enum.take(list, length(list) - 1)
6475
expand_elixir_modules(list, hint, server)
76+
6577
{:ok, {{:., _, [ast_node, fun]}, _, []}} when is_atom(fun) ->
6678
expand_call(ast_node, Atom.to_string(fun), server)
79+
6780
_ ->
6881
no()
6982
end
@@ -78,10 +91,8 @@ defmodule IEx.Autocomplete do
7891
|> trim_leading(?%)
7992
end
8093

81-
defp trim_leading([char | rest], char),
82-
do: rest
83-
defp trim_leading(expr, _char),
84-
do: expr
94+
defp trim_leading([char | rest], char), do: rest
95+
defp trim_leading(expr, _char), do: expr
8596

8697
defp yes(hint, entries) do
8798
{:yes, String.to_charlist(hint), Enum.map(entries, &String.to_charlist/1)}
@@ -99,15 +110,16 @@ defmodule IEx.Autocomplete do
99110

100111
defp format_expansion([uniq], hint) do
101112
case to_hint(uniq, hint) do
102-
"" -> yes("", to_uniq_entries(uniq))
113+
"" -> yes("", to_uniq_entries(uniq))
103114
hint -> yes(hint, [])
104115
end
105116
end
106117

107118
defp format_expansion([first | _] = entries, hint) do
108-
binary = Enum.map(entries, &(&1.name))
119+
binary = Enum.map(entries, & &1.name)
109120
length = byte_size(hint)
110121
prefix = :binary.longest_common_prefix(binary)
122+
111123
if prefix in [0, length] do
112124
yes("", Enum.flat_map(entries, &to_entries/1))
113125
else
@@ -151,12 +163,14 @@ defmodule IEx.Autocomplete do
151163
end
152164

153165
defp expand_require(mod, hint) do
154-
format_expansion match_module_funs(get_module_funs(mod), hint), hint
166+
format_expansion(match_module_funs(get_module_funs(mod), hint), hint)
155167
end
156168

157169
defp expand_variable_or_import(hint, server) do
158170
variables = expand_variable(hint, server)
159-
funs = match_module_funs(imports_from_env(server) ++ get_module_funs(Kernel.SpecialForms), hint)
171+
imports = imports_from_env(server)
172+
module_funs = get_module_funs(Kernel.SpecialForms)
173+
funs = match_module_funs(imports ++ module_funs, hint)
160174
format_expansion(variables ++ funs, hint)
161175
end
162176

@@ -169,12 +183,11 @@ defmodule IEx.Autocomplete do
169183
## Erlang modules
170184

171185
defp expand_erlang_modules(hint \\ "") do
172-
format_expansion match_erlang_modules(hint), hint
186+
format_expansion(match_erlang_modules(hint), hint)
173187
end
174188

175189
defp match_erlang_modules(hint) do
176-
for mod <- match_modules(hint, true),
177-
usable_as_unquoted_module?(mod) do
190+
for mod <- match_modules(hint, true), usable_as_unquoted_module?(mod) do
178191
%{kind: :module, name: mod, type: :erlang}
179192
end
180193
end
@@ -207,6 +220,7 @@ defmodule IEx.Autocomplete do
207220
:error -> {:ok, Module.concat([name | rest])}
208221
end
209222
end
223+
210224
defp expand_alias([_ | _], _) do
211225
:error
212226
end
@@ -220,9 +234,9 @@ defmodule IEx.Autocomplete do
220234
end
221235

222236
defp match_elixir_modules(module, hint) do
223-
name = Atom.to_string(module)
237+
name = Atom.to_string(module)
224238
depth = length(String.split(name, ".")) + 1
225-
base = name <> "." <> hint
239+
base = name <> "." <> hint
226240

227241
for mod <- match_modules(base, module === Elixir),
228242
parts = String.split(mod, "."),
@@ -231,24 +245,23 @@ defmodule IEx.Autocomplete do
231245
valid_alias_piece?("." <> name) do
232246
%{kind: :module, type: :elixir, name: name}
233247
end
234-
|> Enum.uniq
248+
|> Enum.uniq()
235249
end
236250

237251
defp valid_alias_piece?(<<?., char, rest::binary>>) when char in ?A..?Z,
238252
do: valid_alias_rest?(rest)
239-
defp valid_alias_piece?(_),
240-
do: false
253+
254+
defp valid_alias_piece?(_), do: false
241255

242256
defp valid_alias_rest?(<<char, rest::binary>>)
243257
when char in ?A..?Z
244258
when char in ?a..?z
245259
when char in ?0..?9
246260
when char == ?_,
247261
do: valid_alias_rest?(rest)
248-
defp valid_alias_rest?(<<>>),
249-
do: true
250-
defp valid_alias_rest?(rest),
251-
do: valid_alias_piece?(rest)
262+
263+
defp valid_alias_rest?(<<>>), do: true
264+
defp valid_alias_rest?(rest), do: valid_alias_piece?(rest)
252265

253266
## Helpers
254267

@@ -261,8 +274,8 @@ defmodule IEx.Autocomplete do
261274
defp match_modules(hint, root) do
262275
get_modules(root)
263276
|> :lists.usort()
264-
|> Enum.drop_while(& not String.starts_with?(&1, hint))
265-
|> Enum.take_while(& String.starts_with?(&1, hint))
277+
|> Enum.drop_while(&not String.starts_with?(&1, hint))
278+
|> Enum.take_while(&String.starts_with?(&1, hint))
266279
end
267280

268281
defp get_modules(true) do
@@ -271,6 +284,7 @@ defmodule IEx.Autocomplete do
271284

272285
defp get_modules(false) do
273286
modules = Enum.map(:code.all_loaded(), &Atom.to_string(elem(&1, 0)))
287+
274288
case :code.get_mode() do
275289
:interactive -> modules ++ get_modules_from_applications()
276290
_otherwise -> modules
@@ -295,29 +309,35 @@ defmodule IEx.Autocomplete do
295309
end
296310

297311
defp match_module_funs(funs, hint) do
298-
for({fun, arity} <- funs,
299-
name = Atom.to_string(fun),
300-
String.starts_with?(name, hint),
301-
do: %{kind: :function, name: name, arity: arity})
312+
for {fun, arity} <- funs, name = Atom.to_string(fun), String.starts_with?(name, hint) do
313+
%{
314+
kind: :function,
315+
name: name,
316+
arity: arity
317+
}
318+
end
302319
|> Enum.sort_by(&{&1.name, &1.arity})
303320
end
304321

305322
defp match_map_fields(map, hint) do
306-
for({key, value} when is_atom(key) <- Map.to_list(map),
323+
for {key, value} when is_atom(key) <- Map.to_list(map),
307324
key = Atom.to_string(key),
308-
String.starts_with?(key, hint),
309-
do: %{kind: :map_key, name: key, value_is_map: is_map(value)})
325+
String.starts_with?(key, hint) do
326+
%{kind: :map_key, name: key, value_is_map: is_map(value)}
327+
end
310328
|> Enum.sort_by(& &1.name)
311329
end
312330

313331
defp get_module_funs(mod) do
314332
cond do
315333
not ensure_loaded?(mod) ->
316334
[]
335+
317336
docs = Code.get_docs(mod, :docs) ->
318337
exports(mod)
319338
|> Kernel.--(default_arg_functions_with_doc_false(docs))
320339
|> Enum.reject(&hidden_fun?(&1, docs))
340+
321341
true ->
322342
exports(mod)
323343
end
@@ -327,7 +347,7 @@ defmodule IEx.Autocomplete do
327347
for {{fun_name, arity}, _, _, args, false} <- docs,
328348
count = count_defaults(args),
329349
count > 0,
330-
new_arity <- (arity-count)..arity,
350+
new_arity <- (arity - count)..arity,
331351
do: {fun_name, new_arity}
332352
end
333353

@@ -339,34 +359,36 @@ defmodule IEx.Autocomplete do
339359
case List.keyfind(docs, fun, 0) do
340360
nil ->
341361
underscored_fun?(fun)
362+
342363
{_, _, _, _, false} ->
343364
true
365+
344366
{fun, _, _, _, nil} ->
345367
underscored_fun?(fun)
368+
346369
{_, _, _, _, _} ->
347370
false
348371
end
349372
end
350373

351-
defp underscored_fun?({name, _}),
352-
do: hd(Atom.to_charlist(name)) == ?_
374+
defp underscored_fun?({name, _}), do: hd(Atom.to_charlist(name)) == ?_
353375

354376
defp ensure_loaded?(Elixir), do: false
355377
defp ensure_loaded?(mod), do: Code.ensure_loaded?(mod)
356378

357379
## Ad-hoc conversions
358380

359-
defp to_entries(%{kind: kind, name: name}) when
360-
kind in [:map_key, :module, :variable] do
381+
defp to_entries(%{kind: kind, name: name})
382+
when kind in [:map_key, :module, :variable] do
361383
[name]
362384
end
363385

364386
defp to_entries(%{kind: :function, name: name, arity: arity}) do
365387
["#{name}/#{arity}"]
366388
end
367389

368-
defp to_uniq_entries(%{kind: kind}) when
369-
kind in [:map_key, :module, :variable] do
390+
defp to_uniq_entries(%{kind: kind})
391+
when kind in [:map_key, :module, :variable] do
370392
[]
371393
end
372394

@@ -382,8 +404,8 @@ defmodule IEx.Autocomplete do
382404
format_hint(name, hint) <> "."
383405
end
384406

385-
defp to_hint(%{kind: kind, name: name}, hint) when
386-
kind in [:function, :map_key, :module, :variable] do
407+
defp to_hint(%{kind: kind, name: name}, hint)
408+
when kind in [:function, :map_key, :module, :variable] do
387409
format_hint(name, hint)
388410
end
389411

0 commit comments

Comments
 (0)