-
Notifications
You must be signed in to change notification settings - Fork 3.5k
Improve IEx autocompletion to handle variable names. #5504
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
Improve IEx autocompletion to handle variable names. #5504
Conversation
lib/iex/lib/iex/autocomplete.ex
Outdated
|
|
||
| defp to_entries(%{kind: :module, name: name}) do | ||
| defp to_entries(%{kind: kind, name: name}) | ||
| when kind in [:map_key, :module, :var_name] do |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please indent this as follows:
defp to_entries(%{kind: kind, name: name}) when
kind in [:map_key, :module, :var_name] do
Same below. :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@josevalim is this the new preferred way of breaking up guards? leaving when at the end of the first line?
| end | ||
|
|
||
| defp find_matched_variables(binding, var_prefix) do | ||
| for {var_name, _value} <- binding, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please make sure that variable names in bindings are atoms, they are not always atoms in bindings because of hygiene, and then use Atom.to_string/1.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good to know -- I had no idea! Is it worth adding test coverage for this since it's a simple mistake to make? What's an example of an expression that would result in a variable name not being an atom?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Any expression that defines a variable inside a quote:
iex(1)> Code.eval_quoted (quote do var = 1 end), []
{1, [{{:var, Elixir}, 1}]}One suggestion is to define a macro inside the test module and invoke that macro on @tag previous_line: "Mod.macro".
- Format multi-line guard clauses as requested. - Do not assume that binding keys are always atoms.
This aligns with the existing kind values. We use `:function`, `:map_key` and `:module`, not `:fun_name`, `:map_key_name` and `:mod_name`.
|
@josevalim I can't get your suggestion to work. I tried this: defmacro define_var do
quote do: my_var_1 = 1
end
@tag previous_line: "require #{__MODULE__}; #{__MODULE__}.define_var(); my_var_2 = 2"
test "ignores quoted variables when performing variable completion" do
assert expand('my_var') == {:yes, '_2', []}
endThe test passes (which is good) but still passes if I undo my "filter to only atom variables" fix. I added some debug printing and found that the binding is just Thoughts? |
|
@myronmarston I will investigate. |
Great, thanks. BTW, I've got the custom import auto-completion ready to go now, too. Do you want me to push that commit to this PR so it is included or should I wait until this is merged and then open a new PR? It builds on top of this one so I can't open another PR without also including the commits from this one. |
|
@myronmarston using |
|
Let's wait until this is merged since I think there should be nothing more blocking it. |
Also, fix the way we filter out non-atom variables. For some reason, the guard clause did not work.
|
Thanks, the for {var_name, _value} when is_atom(var_name) <- binding,
# ...Instead, I had to use a filter clause: for {var_name, _value} <- binding,
is_atom(var_name),
# ...
Is that expected? It certainly surprised me. |
|
That's a bug. Or we should fail to compile or it should work. Please open up an issue. |
|
@myronmarston fwiw, i cannot reproduce it: iex(1)> for {k, v} when is_atom(v) <- [foo: :bar, baz: 1], do: v
[:bar] |
|
Thanks for fixing the |
|
❤️ 💚 💙 💛 💜 |
- Variable name autocompletion was added in elixir-lang#5504. - Autocompletion of imported `:only` functions was added in elixir-lang#5518.
Follow up to #5488.