Skip to content

Commit

Permalink
improvements to document symbols provider
Browse files Browse the repository at this point in the history
  • Loading branch information
lukaszsamson committed May 18, 2022
1 parent 7f37d59 commit 1e38db4
Show file tree
Hide file tree
Showing 3 changed files with 138 additions and 153 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,13 @@ defmodule ElixirLS.LanguageServer.Providers.DocumentSymbols do

@defs [:def, :defp, :defmacro, :defmacrop, :defguard, :defguardp, :defdelegate]

@docs [
@supplementing_attributes [
:doc,
:moduledoc,
:typedoc
:typedoc,
:spec,
:impl,
:deprecated
]

@max_parser_errors 6
Expand Down Expand Up @@ -118,14 +121,8 @@ defmodule ElixirLS.LanguageServer.Providers.DocumentSymbols do
end

# Struct and exception
defp extract_symbol(_module_name, {defname, location, [properties | _]})
defp extract_symbol(module_name, {defname, location, [properties | _]})
when defname in [:defstruct, :defexception] do
name =
case defname do
:defstruct -> "struct"
:defexception -> "exception"
end

children =
if is_list(properties) do
properties
Expand All @@ -135,15 +132,20 @@ defmodule ElixirLS.LanguageServer.Providers.DocumentSymbols do
[]
end

%Info{type: :struct, name: name, location: location, children: children}
%Info{
type: :struct,
name: "#{defname} #{module_name}",
location: location,
children: children
}
end

# Docs
defp extract_symbol(_, {:@, _, [{kind, _, _}]}) when kind in @docs, do: nil
# We skip attributes only supplementoing the symbol
defp extract_symbol(_, {:@, _, [{kind, _, _}]}) when kind in @supplementing_attributes, do: nil

# Types
defp extract_symbol(_current_module, {:@, _, [{type_kind, location, type_expression}]})
when type_kind in [:type, :typep, :opaque, :spec, :callback, :macrocallback] do
when type_kind in [:type, :typep, :opaque, :callback, :macrocallback] do
type_name =
case type_expression do
[{:"::", _, [{_, _, _} = type_head | _]}] ->
Expand All @@ -158,7 +160,7 @@ defmodule ElixirLS.LanguageServer.Providers.DocumentSymbols do

%Info{
type: type,
name: type_name,
name: "@#{type_kind} #{type_name}",
location: location,
children: []
}
Expand All @@ -168,19 +170,7 @@ defmodule ElixirLS.LanguageServer.Providers.DocumentSymbols do
defp extract_symbol(_current_module, {:@, _, [{:behaviour, location, [behaviour_expression]}]}) do
module_name = extract_module_name(behaviour_expression)

%Info{type: :constant, name: "@behaviour #{module_name}", location: location, children: []}
end

# @impl true
defp extract_symbol(_current_module, {:@, _, [{:impl, location, [true]}]}) do
%Info{type: :constant, name: "@impl true", location: location, children: []}
end

# @impl BehaviourModule
defp extract_symbol(_current_module, {:@, _, [{:impl, location, [impl_expression]}]}) do
module_name = extract_module_name(impl_expression)

%Info{type: :constant, name: "@impl #{module_name}", location: location, children: []}
%Info{type: :interface, name: "@behaviour #{module_name}", location: location, children: []}
end

# Other attributes
Expand Down Expand Up @@ -217,6 +207,20 @@ defmodule ElixirLS.LanguageServer.Providers.DocumentSymbols do
}
end

defp extract_symbol(
_current_module,
{{:., location, [{:__aliases__, _, [:Record]}, :defrecord]}, _, [record_name, _]}
) do
name = Macro.to_string(record_name) |> String.replace("\n", "")

%Info{
type: :class,
name: "defrecord #{name}",
location: location,
children: []
}
end

# ExUnit test
defp extract_symbol(_current_module, {:test, location, [name | _]}) do
%Info{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ defmodule ElixirLS.LanguageServer.Providers.Hover do
""
else
s = root_mod_name |> source()

cond do
third_dep?(s, project_dir) -> third_dep_name(s, project_dir)
builtin?(s) -> builtin_dep_name(s)
Expand Down
Loading

0 comments on commit 1e38db4

Please sign in to comment.