Skip to content

Commit

Permalink
Do a runtime check for Macro.classify_atom/1
Browse files Browse the repository at this point in the history
  • Loading branch information
zachallaun committed Jul 30, 2024
1 parent f0be0d8 commit cba3fe2
Showing 1 changed file with 12 additions and 13 deletions.
25 changes: 12 additions & 13 deletions lib/elixir_sense/providers/completion/completion_engine.ex
Original file line number Diff line number Diff line change
Expand Up @@ -850,19 +850,18 @@ defmodule ElixirSense.Providers.Completion.CompletionEngine do

## Helpers

# Version.match? is slow, we need to avoid it in a hot loop
if Version.match?(System.version(), ">= 1.14.0-dev") do
defp usable_as_unquoted_module?(name) do
# Conversion to atom is not a problem because
# it is only called with existing modules names.
# credo:disable-for-lines:7
Macro.classify_atom(String.to_atom(name)) in [:identifier, :unquoted] and
not String.starts_with?(name, "Elixir.")
end
else
defp usable_as_unquoted_module?(name) do
Code.Identifier.classify(String.to_atom(name)) != :other and
not String.starts_with?(name, "Elixir.")
defp usable_as_unquoted_module?(name) do
unquoted_atom_or_identifier?(String.to_atom(name)) and
not String.starts_with?(name, "Elixir.")
end

defp unquoted_atom_or_identifier?(atom) when is_atom(atom) do
# Macro.classify_atom/1 was introduced in 1.14.0. If it's not available,
# assume we're on an older version and fall back to a private API.
if function_exported?(Macro, :classify_atom, 1) do
apply(Macro, :classify_atom, [atom]) in [:identifier, :unquoted]
else
apply(Code.Identifier, :classify, [atom]) != :other
end
end

Expand Down

0 comments on commit cba3fe2

Please sign in to comment.