Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

stdlib: prevent shell from crashing when completing Var/ and Var( #9078

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 25 additions & 14 deletions lib/stdlib/src/edlin_context.erl
Original file line number Diff line number Diff line change
Expand Up @@ -116,20 +116,24 @@ get_context([$(|Bef], CR) ->
[] -> {term}; % parenthesis
"fun" -> {fun_};
_ ->
{_, Mod} = over_module(Bef1, Fun),
case Mod of
"shell" -> {term};
"shell_default" -> {term};
case erl_scan:string(Fun) of
{ok, [{var, _, _}], _} -> {term};
_ ->
case CR#context.parameter_count+1 == length(CR#context.arguments) of
true ->
%% N arguments N-1 commas, this means that we have an argument
%% still being worked on.
{function, Mod, Fun, lists:droplast(CR#context.arguments),
lists:last(CR#context.arguments),CR#context.nestings};
{_, Mod} = over_module(Bef1, Fun),
case Mod of
"shell" -> {term};
"shell_default" -> {term};
_ ->
{function, Mod, Fun, CR#context.arguments,
[], CR#context.nestings}
case CR#context.parameter_count+1 == length(CR#context.arguments) of
true ->
%% N arguments N-1 commas, this means that we have an argument
%% still being worked on.
{function, Mod, Fun, lists:droplast(CR#context.arguments),
lists:last(CR#context.arguments),CR#context.nestings};
_ ->
{function, Mod, Fun, CR#context.arguments,
[], CR#context.nestings}
end
end
end
end;
Expand Down Expand Up @@ -228,8 +232,15 @@ get_context([$:|Bef2], _) ->
end;
get_context([$/|Bef1], _) ->
{Bef2, Fun} = edlin_expand:over_word(Bef1),
{_, Mod} = over_module(Bef2, Fun),
{fun_, Mod, Fun};
case Fun of
[] -> {term};
_ ->
{_, Mod} = over_module(Bef2, Fun),
case Mod of
[] -> {term};
_ -> {fun_, Mod, Fun}
end
end;
get_context([$>,$-|_Bef2], #context{arguments = Args} = CR) ->
%% Inside a function
case CR#context.parameter_count+1 == length(Args) of
Expand Down
12 changes: 8 additions & 4 deletions lib/stdlib/src/edlin_expand.erl
Original file line number Diff line number Diff line change
Expand Up @@ -738,11 +738,15 @@ shell(Fun) ->

-doc false.
shell_default_or_bif(Fun) ->
{ok, [{atom, _, Fun1}], _} = erl_scan:string(Fun),
case lists:member(Fun1, [E || {E,_}<-get_exports(shell_default)]) of
true -> "shell_default";
_ -> bif(Fun)
case erl_scan:string(Fun) of
{ok, [{var, _, _}], _} -> [];
{ok, [{atom, _, Fun1}], _} ->
case lists:member(Fun1, [E || {E,_}<-get_exports(shell_default)]) of
true -> "shell_default";
_ -> bif(Fun)
end
end.

-doc false.
bif(Fun) ->
{ok, [{atom, _, Fun1}], _} = erl_scan:string(Fun),
Expand Down
4 changes: 4 additions & 0 deletions lib/stdlib/test/edlin_context_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -190,4 +190,8 @@ get_context(_Config) ->
{term,[],[]} = edlin_context:get_context(lists:reverse("begin {hej, svej} = {")),
{fun_} = edlin_context:get_context(lists:reverse("fun(")),
{fun_} = edlin_context:get_context(lists:reverse("maps:map(fun(")),
{term} = edlin_context:get_context(lists:reverse("/")),
{term} = edlin_context:get_context(lists:reverse("Foo/")),
{fun_, "user_defined", "a"} = edlin_context:get_context(lists:reverse("a/")),
{term} = edlin_context:get_context(lists:reverse("Foo(")),
ok.
Loading