@@ -10,12 +10,12 @@ defmodule IEx.Autocomplete do
10
10
h === ?. and t != [ ] ->
11
11
expand_dot ( reduce ( t ) )
12
12
h === ?: ->
13
- expand_erlang_modules
13
+ expand_erlang_modules ( )
14
14
identifier? ( h ) ->
15
15
expand_expr ( reduce ( expr ) )
16
16
( h == ?/ ) and t != [ ] and identifier? ( hd ( t ) ) ->
17
17
expand_expr ( reduce ( t ) )
18
- h in '(+[ ' ->
18
+ h in '([{ ' ->
19
19
expand ( '' )
20
20
true ->
21
21
no ( )
@@ -40,32 +40,26 @@ defmodule IEx.Autocomplete do
40
40
defp expand_expr ( expr ) do
41
41
case Code . string_to_quoted expr do
42
42
{ :ok , atom } when is_atom ( atom ) ->
43
- expand_erlang_modules Atom . to_string ( atom )
43
+ expand_erlang_modules ( Atom . to_string ( atom ) )
44
44
{ :ok , { atom , _ , nil } } when is_atom ( atom ) ->
45
- expand_import Atom . to_string ( atom )
45
+ expand_import ( Atom . to_string ( atom ) )
46
46
{ :ok , { :__aliases__ , _ , [ root ] } } ->
47
- expand_elixir_modules [ ] , Atom . to_string ( root )
47
+ expand_elixir_modules ( [ ] , Atom . to_string ( root ) )
48
48
{ :ok , { :__aliases__ , _ , [ h | _ ] = list } } when is_atom ( h ) ->
49
49
hint = Atom . to_string ( List . last ( list ) )
50
50
list = Enum . take ( list , length ( list ) - 1 )
51
- expand_elixir_modules list , hint
51
+ expand_elixir_modules ( list , hint )
52
52
{ :ok , { { :. , _ , [ mod , fun ] } , _ , [ ] } } when is_atom ( fun ) ->
53
- expand_call mod , Atom . to_string ( fun )
53
+ expand_call ( mod , Atom . to_string ( fun ) )
54
54
_ ->
55
55
no ( )
56
56
end
57
57
end
58
58
59
59
defp reduce ( expr ) do
60
- last_token ( Enum . reverse ( expr ) , [ ' ' , '(' , '[' , '+' , '-' ] )
61
- end
62
-
63
- defp last_token ( s , [ ] ) do
64
- s
65
- end
66
-
67
- defp last_token ( s , [ h | t ] ) do
68
- last_token ( List . last ( :string . tokens ( s , h ) ) , t )
60
+ Enum . reverse Enum . reduce [ ' ' , '(' , '[' , '{' ] , expr , fn token , acc ->
61
+ hd ( :string . tokens ( acc , token ) )
62
+ end
69
63
end
70
64
71
65
defp yes ( hint , entries ) do
@@ -100,35 +94,16 @@ defmodule IEx.Autocomplete do
100
94
end
101
95
end
102
96
103
- ## Root Modules
104
-
105
- defp root_modules do
106
- Enum . reduce :code . all_loaded , [ ] , fn { m , _ } , acc ->
107
- mod = Atom . to_string ( m )
108
- case mod do
109
- "Elixir" <> _ ->
110
- tokens = String . split ( mod , "." )
111
- if length ( tokens ) == 2 do
112
- [ % { kind: :module , name: List . last ( tokens ) , type: :elixir } | acc ]
113
- else
114
- acc
115
- end
116
- _ ->
117
- [ % { kind: :module , name: mod , type: :erlang } | acc ]
118
- end
119
- end
120
- end
121
-
122
97
## Expand calls
123
98
124
99
# :atom.fun
125
100
defp expand_call ( mod , hint ) when is_atom ( mod ) do
126
- expand_require mod , hint
101
+ expand_require ( mod , hint )
127
102
end
128
103
129
104
# Elixir.fun
130
105
defp expand_call ( { :__aliases__ , _ , list } , hint ) do
131
- expand_require Module . concat ( list ) , hint
106
+ expand_require ( Module . concat ( list ) , hint )
132
107
end
133
108
134
109
defp expand_call ( _ , _ ) do
@@ -152,22 +127,23 @@ defmodule IEx.Autocomplete do
152
127
format_expansion match_erlang_modules ( hint ) , hint
153
128
end
154
129
155
- defp match_erlang_modules ( "" ) do
156
- Enum . filter root_modules , fn m -> m . type === :erlang end
157
- end
158
-
159
130
defp match_erlang_modules ( hint ) do
160
- Enum . filter root_modules , fn m -> String . starts_with? ( m . name , hint ) end
131
+ for { mod , _ } <- :code . all_loaded ,
132
+ mod = Atom . to_string ( mod ) ,
133
+ not match? ( "Elixir." <> _ , mod ) ,
134
+ String . starts_with? ( mod , hint ) do
135
+ % { kind: :module , name: mod , type: :erlang }
136
+ end
161
137
end
162
138
163
139
## Elixir modules
164
140
165
141
defp expand_elixir_modules ( list , hint ) do
166
142
mod = Module . concat ( list )
167
- format_expansion elixir_submodules ( mod , hint , list == [ ] ) ++ module_funs ( mod , hint ) , hint
143
+ format_expansion elixir_aliases ( mod , hint , list == [ ] ) ++ module_funs ( mod , hint ) , hint
168
144
end
169
145
170
- defp elixir_submodules ( mod , hint , root ) do
146
+ defp elixir_aliases ( mod , hint , root ) do
171
147
modname = Atom . to_string ( mod )
172
148
depth = length ( String . split ( modname , "." ) ) + 1
173
149
base = modname <> "." <> hint
@@ -225,7 +201,7 @@ defmodule IEx.Autocomplete do
225
201
226
202
for { fun , arities } <- list ,
227
203
name = Atom . to_string ( fun ) ,
228
- hint == "" or String . starts_with? ( name , hint ) do
204
+ String . starts_with? ( name , hint ) do
229
205
% { kind: :function , name: name , arities: arities }
230
206
end
231
207
_ ->
0 commit comments