Skip to content

Commit 0c98e65

Browse files
authored
Do a runtime check for Macro.classify_atom/1 (#294)
1 parent f0be0d8 commit 0c98e65

File tree

1 file changed

+12
-13
lines changed

1 file changed

+12
-13
lines changed

lib/elixir_sense/providers/completion/completion_engine.ex

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -850,19 +850,18 @@ defmodule ElixirSense.Providers.Completion.CompletionEngine do
850850

851851
## Helpers
852852

853-
# Version.match? is slow, we need to avoid it in a hot loop
854-
if Version.match?(System.version(), ">= 1.14.0-dev") do
855-
defp usable_as_unquoted_module?(name) do
856-
# Conversion to atom is not a problem because
857-
# it is only called with existing modules names.
858-
# credo:disable-for-lines:7
859-
Macro.classify_atom(String.to_atom(name)) in [:identifier, :unquoted] and
860-
not String.starts_with?(name, "Elixir.")
861-
end
862-
else
863-
defp usable_as_unquoted_module?(name) do
864-
Code.Identifier.classify(String.to_atom(name)) != :other and
865-
not String.starts_with?(name, "Elixir.")
853+
defp usable_as_unquoted_module?(name) do
854+
unquoted_atom_or_identifier?(String.to_atom(name)) and
855+
not String.starts_with?(name, "Elixir.")
856+
end
857+
858+
defp unquoted_atom_or_identifier?(atom) when is_atom(atom) do
859+
# Macro.classify_atom/1 was introduced in 1.14.0. If it's not available,
860+
# assume we're on an older version and fall back to a private API.
861+
if function_exported?(Macro, :classify_atom, 1) do
862+
apply(Macro, :classify_atom, [atom]) in [:identifier, :unquoted]
863+
else
864+
apply(Code.Identifier, :classify, [atom]) != :other
866865
end
867866
end
868867

0 commit comments

Comments
 (0)