Skip to content

Commit d3ffdeb

Browse files
committed
Fix completion for multiple matches
1 parent c749f6b commit d3ffdeb

File tree

4 files changed

+72
-40
lines changed

4 files changed

+72
-40
lines changed

src/ierl_backend_elixir.erl

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -123,23 +123,22 @@ is_complete(Code, _Msg, _State) ->
123123

124124
complete(Code, CursorPos, _Msg, _State) ->
125125
% TODO: Match in the environment
126-
L = lists:sublist(binary_to_list(Code), CursorPos),
127-
Res = case 'Elixir.IEx.Autocomplete':expand(lists:reverse(L)) of
128-
{yes, Expansion, []} ->
129-
[Expansion];
130-
{yes, [], Matches} ->
131-
[
132-
Name ||
133-
{Name, _Arity} <- lists:map(fun split_arity/1, Matches)
134-
];
135-
{no, [], Matches} ->
136-
[
137-
Name ||
138-
{Name, _Arity} <- lists:map(fun split_arity/1, Matches)
139-
]
140-
end,
141-
142-
[list_to_binary(R) || R <- Res].
126+
Code1 = binary_to_list(Code),
127+
L = lists:sublist(Code1, CursorPos),
128+
case 'Elixir.IEx.Autocomplete':expand(lists:reverse(L)) of
129+
{yes, Expansion, []} ->
130+
[Expansion];
131+
{yes, [], Matches} ->
132+
{Start, End} = ierl_util:find_span(Code1, CursorPos),
133+
{Start, End,
134+
[
135+
Name ||
136+
{Name, _Arity} <- lists:map(fun split_arity/1, Matches)
137+
]
138+
};
139+
{no, [], _} ->
140+
[]
141+
end.
143142

144143

145144
inspect(_Code, _CursorPos, _Detail, _Msg, _State) ->

src/ierl_backend_erlang.erl

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -92,17 +92,18 @@ exec_counter(State) ->
9292

9393
complete(Code, CursorPos, _Msg, _State) ->
9494
% TODO: Use the existing bindings as well
95-
L = lists:sublist(binary_to_list(Code), CursorPos),
96-
Res = case edlin_expand:expand(lists:reverse(L)) of
97-
{yes, Expansion, []} ->
98-
[Expansion];
99-
{yes, [], Matches} ->
100-
[Name || {Name, _Arity} <- Matches];
101-
{no, [], Matches} ->
102-
[Name || {Name, _Arity} <- Matches]
103-
end,
104-
105-
[list_to_binary(R) || R <- Res].
95+
Code1 = binary_to_list(Code),
96+
L = lists:sublist(Code1, CursorPos),
97+
98+
case edlin_expand:expand(lists:reverse(L)) of
99+
{yes, Expansion, []} ->
100+
[Expansion];
101+
{yes, [], Matches} ->
102+
{Start, End} = ierl_util:find_span(Code1, CursorPos),
103+
{Start, End, [Name || {Name, _Arity} <- Matches]};
104+
{no, [], _} ->
105+
[]
106+
end.
106107

107108

108109
is_complete(Code, _Msg, _State) ->

src/ierl_backend_lfe.erl

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -114,17 +114,18 @@ is_complete(Code, _Msg, _State) ->
114114
complete(Code, CursorPos, _Msg, State) ->
115115
% TODO: Check in the environment for completables
116116
_Env = State#state.env,
117-
L = lists:sublist(binary_to_list(Code), CursorPos),
118-
Res = case lfe_edlin_expand:expand(lists:reverse(L)) of
119-
{yes, Expansion, []} ->
120-
[Expansion];
121-
{yes, [], Matches} ->
122-
[Name || {Name, _Arity} <- Matches];
123-
{no, [], Matches} ->
124-
[Name || {Name, _Arity} <- Matches]
125-
end,
126-
127-
[list_to_binary(R) || R <- Res].
117+
Code1 = binary_to_list(Code),
118+
L = lists:sublist(Code1, CursorPos),
119+
120+
case lfe_edlin_expand:expand(lists:reverse(L)) of
121+
{yes, Expansion, []} ->
122+
[Expansion];
123+
{yes, [], Matches} ->
124+
{Start, End} = ierl_util:find_span(Code1, CursorPos),
125+
{Start, End, [Name || {Name, _Arity} <- Matches]};
126+
{no, [], _} ->
127+
[]
128+
end.
128129

129130

130131
inspect(_Code, _CursorPos, _Detail, _Msg, _State) ->

src/ierl_util.erl

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
11
-module(ierl_util).
22

3-
-export([get_app_version/1, simplify/1, format_args/1]).
3+
-export(
4+
[
5+
get_app_version/1,
6+
simplify/1,
7+
format_args/1,
8+
find_span/2
9+
]).
10+
411

512
-spec get_app_version(atom()) -> string().
613
get_app_version(Name) ->
@@ -55,3 +62,27 @@ format_args(Map) ->
5562
)
5663
).
5764

65+
66+
% @doc Find the span of characters (and underscores) that surround the given
67+
% cursor
68+
-spec find_span(string(), non_neg_integer()) -> {non_neg_integer(),
69+
non_neg_integer()}.
70+
find_span(String, Cursor) ->
71+
{Left, Right} = lists:split(Cursor, String),
72+
73+
IsChar = fun (X) when X >= $a andalso X =< $z ->
74+
true;
75+
(X) when X >= $A andalso X =< $Z ->
76+
true;
77+
(X) when X >= $0 andalso X =< $9 ->
78+
true;
79+
($_) ->
80+
true;
81+
(_) ->
82+
false
83+
end,
84+
85+
{
86+
Cursor - length(lists:takewhile(IsChar, lists:reverse(Left))),
87+
Cursor + length(lists:takewhile(IsChar, Right))
88+
}.

0 commit comments

Comments
 (0)