Skip to content

Commit 3f01e21

Browse files
authored
Make document symbols more resilient (#820)
Do not crash on incomplete typespecs
1 parent 6a0a55b commit 3f01e21

File tree

2 files changed

+29
-1
lines changed

2 files changed

+29
-1
lines changed

apps/language_server/lib/language_server/providers/document_symbols.ex

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,14 +169,18 @@ defmodule ElixirLS.LanguageServer.Providers.DocumentSymbols do
169169

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

178179
[{:when, _, [{:"::", _, [{_, type_head_location, _} = type_head, _]}, _]}] ->
179180
{Macro.to_string(type_head), type_head_location}
181+
182+
[{_, type_head_location, _} = type_head | _] ->
183+
{Macro.to_string(type_head), type_head_location}
180184
end
181185

182186
type_name =

apps/language_server/test/providers/document_symbols_test.exs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1172,6 +1172,8 @@ defmodule ElixirLS.LanguageServer.Providers.DocumentSymbolsTest do
11721172
@opaque my_simple_opaque :: integer
11731173
@type my_with_args(key, value) :: [{key, value}]
11741174
@type my_with_args_when(key, value) :: [{key, value}] when value: integer
1175+
@type abc
1176+
@type
11751177
end
11761178
"""
11771179

@@ -1216,6 +1218,16 @@ defmodule ElixirLS.LanguageServer.Providers.DocumentSymbolsTest do
12161218
children: [],
12171219
kind: 5,
12181220
name: "@type my_with_args_when(key, value)"
1221+
},
1222+
%Protocol.DocumentSymbol{
1223+
children: [],
1224+
kind: 5,
1225+
name: "@type abc"
1226+
},
1227+
%Protocol.DocumentSymbol{
1228+
children: [],
1229+
kind: 14,
1230+
name: "@type"
12191231
}
12201232
],
12211233
kind: 2,
@@ -1235,6 +1247,8 @@ defmodule ElixirLS.LanguageServer.Providers.DocumentSymbolsTest do
12351247
@opaque my_simple_opaque :: integer
12361248
@type my_with_args(key, value) :: [{key, value}]
12371249
@type my_with_args_when(key, value) :: [{key, value}] when value: integer
1250+
@type abc
1251+
@type
12381252
end
12391253
"""
12401254

@@ -1279,6 +1293,16 @@ defmodule ElixirLS.LanguageServer.Providers.DocumentSymbolsTest do
12791293
kind: 5,
12801294
name: "@type my_with_args_when(key, value)",
12811295
containerName: "MyModule"
1296+
},
1297+
%Protocol.SymbolInformation{
1298+
kind: 5,
1299+
name: "@type abc",
1300+
containerName: "MyModule"
1301+
},
1302+
%Protocol.SymbolInformation{
1303+
kind: 14,
1304+
name: "@type",
1305+
containerName: "MyModule"
12821306
}
12831307
]} = DocumentSymbols.symbols(uri, text, false)
12841308
end

0 commit comments

Comments
 (0)