Skip to content

Commit

Permalink
Make document symbols more resilient (#820)
Browse files Browse the repository at this point in the history
Do not crash on incomplete typespecs
  • Loading branch information
lukaszsamson committed Mar 4, 2023
1 parent 6a0a55b commit 3f01e21
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -169,14 +169,18 @@ defmodule ElixirLS.LanguageServer.Providers.DocumentSymbols do

# Types
defp extract_symbol(_current_module, {:@, location, [{type_kind, _, type_expression}]})
when type_kind in [:type, :typep, :opaque, :callback, :macrocallback] do
when type_kind in [:type, :typep, :opaque, :callback, :macrocallback] and
not is_nil(type_expression) do
{type_name, type_head_location} =
case type_expression do
[{:"::", _, [{_, type_head_location, _} = type_head | _]}] ->
{Macro.to_string(type_head), type_head_location}

[{:when, _, [{:"::", _, [{_, type_head_location, _} = type_head, _]}, _]}] ->
{Macro.to_string(type_head), type_head_location}

[{_, type_head_location, _} = type_head | _] ->
{Macro.to_string(type_head), type_head_location}
end

type_name =
Expand Down
24 changes: 24 additions & 0 deletions apps/language_server/test/providers/document_symbols_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -1172,6 +1172,8 @@ defmodule ElixirLS.LanguageServer.Providers.DocumentSymbolsTest do
@opaque my_simple_opaque :: integer
@type my_with_args(key, value) :: [{key, value}]
@type my_with_args_when(key, value) :: [{key, value}] when value: integer
@type abc
@type
end
"""

Expand Down Expand Up @@ -1216,6 +1218,16 @@ defmodule ElixirLS.LanguageServer.Providers.DocumentSymbolsTest do
children: [],
kind: 5,
name: "@type my_with_args_when(key, value)"
},
%Protocol.DocumentSymbol{
children: [],
kind: 5,
name: "@type abc"
},
%Protocol.DocumentSymbol{
children: [],
kind: 14,
name: "@type"
}
],
kind: 2,
Expand All @@ -1235,6 +1247,8 @@ defmodule ElixirLS.LanguageServer.Providers.DocumentSymbolsTest do
@opaque my_simple_opaque :: integer
@type my_with_args(key, value) :: [{key, value}]
@type my_with_args_when(key, value) :: [{key, value}] when value: integer
@type abc
@type
end
"""

Expand Down Expand Up @@ -1279,6 +1293,16 @@ defmodule ElixirLS.LanguageServer.Providers.DocumentSymbolsTest do
kind: 5,
name: "@type my_with_args_when(key, value)",
containerName: "MyModule"
},
%Protocol.SymbolInformation{
kind: 5,
name: "@type abc",
containerName: "MyModule"
},
%Protocol.SymbolInformation{
kind: 14,
name: "@type",
containerName: "MyModule"
}
]} = DocumentSymbols.symbols(uri, text, false)
end
Expand Down

0 comments on commit 3f01e21

Please sign in to comment.