Skip to content

Commit

Permalink
Fix crash in callback suggestions
Browse files Browse the repository at this point in the history
Fix formatting of types matching locals without parens
Fixes #235
  • Loading branch information
lukaszsamson committed Jul 12, 2023
1 parent 3aea5e2 commit 9d9f674
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 5 deletions.
27 changes: 24 additions & 3 deletions lib/elixir_sense/core/introspection.ex
Original file line number Diff line number Diff line change
Expand Up @@ -813,15 +813,36 @@ defmodule ElixirSense.Core.Introspection do
defp drop_macro_env(other), do: other

defp get_typespec_signature({:when, _, [{:"::", _, [{name, meta, args}, _]}, _]}, arity) do
Macro.to_string({name, meta, strip_types(args, arity)})
to_string_with_parens({name, meta, strip_types(args, arity)})
end

defp get_typespec_signature({:"::", _, [{name, meta, args}, _]}, arity) do
Macro.to_string({name, meta, strip_types(args, arity)})
to_string_with_parens({name, meta, strip_types(args, arity)})
end

defp get_typespec_signature({name, meta, args}, arity) do
Macro.to_string({name, meta, strip_types(args, arity)})
to_string_with_parens({name, meta, strip_types(args, arity)})
end

def to_string_with_parens({name, meta, args}) when is_atom(name) do
if Code.Formatter.local_without_parens?(
name,
length(args),
Code.Formatter.locals_without_parens()
) do
# Macro.to_string formats some locals without parens
# notable case is Phoenix.Endpoint.config/2 callback
replacement = :__elixir_sense_replace_me__

Macro.to_string({replacement, meta, args})
|> String.replace(Atom.to_string(replacement), Atom.to_string(name))
else
Macro.to_string({name, meta, args})
end
end

def to_string_with_parens(ast) do
Macro.to_string(ast)
end

defp strip_types(args, arity) do
Expand Down
2 changes: 1 addition & 1 deletion lib/elixir_sense/core/type_info.ex
Original file line number Diff line number Diff line change
Expand Up @@ -456,7 +456,7 @@ defmodule ElixirSense.Core.TypeInfo do
end

def type_str(type) do
typespec_to_quoted(type) |> Macro.to_string()
typespec_to_quoted(type) |> Introspection.to_string_with_parens()
end

defp extract_list_type_spec_options(list_type_specs) do
Expand Down
2 changes: 1 addition & 1 deletion lib/elixir_sense/providers/suggestion/reducers/struct.ex
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ defmodule ElixirSense.Providers.Suggestion.Reducers.Struct do
end

some ->
Macro.to_string(some)
Introspection.to_string_with_parens(some)
end

%{
Expand Down

0 comments on commit 9d9f674

Please sign in to comment.