Skip to content

Commit

Permalink
simplify var location
Browse files Browse the repository at this point in the history
  • Loading branch information
lukaszsamson committed Sep 18, 2024
1 parent b1df447 commit dd572f4
Show file tree
Hide file tree
Showing 6 changed files with 11 additions and 80 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ defmodule ElixirLS.LanguageServer.Providers.Completion.Suggestion do
alias ElixirSense.Core.Parser
alias ElixirSense.Core.Source
alias ElixirLS.LanguageServer.Providers.Completion.Reducers
alias ElixirSense.Core.Normalized.Code, as: NormalizedCode

@type generic :: %{
type: :generic,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ defmodule ElixirLS.LanguageServer.Providers.Definition.Locator do
alias ElixirSense.Core.State
alias ElixirSense.Core.State.ModFunInfo
alias ElixirSense.Core.State.TypeInfo
alias ElixirSense.Core.State.VarInfo
alias ElixirSense.Core.Source
alias ElixirSense.Core.SurroundContext
alias ElixirLS.LanguageServer.Location
Expand All @@ -42,8 +41,7 @@ defmodule ElixirLS.LanguageServer.Providers.Definition.Locator do

find(
context,
env
|> Metadata.add_scope_vars(metadata, {line, column}),
env,
metadata
)
end
Expand All @@ -61,7 +59,6 @@ defmodule ElixirLS.LanguageServer.Providers.Definition.Locator do
context,
%State.Env{
module: module,
vars: vars,
attributes: attributes
} = env,
metadata
Expand All @@ -78,13 +75,7 @@ defmodule ElixirLS.LanguageServer.Providers.Definition.Locator do
nil

{:variable, variable, version} ->
var_info =
vars
|> Enum.find(fn
%VarInfo{} = info ->
info.name == variable and (info.version == version or version == :any) and
context.begin in info.positions
end)
var_info = Metadata.find_var(metadata, variable, version, context.begin)

if var_info != nil do
{definition_line, definition_column} = Enum.min(var_info.positions)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ defmodule ElixirLS.LanguageServer.Providers.DocumentSymbols do
alias ElixirLS.LanguageServer.Providers.SymbolUtils
alias ElixirLS.LanguageServer.{SourceFile, Parser}
require ElixirLS.LanguageServer.Protocol, as: Protocol
alias ElixirSense.Core.Normalized.Module, as: NormalizedModule

defmodule Info do
defstruct [:type, :name, :detail, :location, :children, :selection_location, :symbol]
Expand Down Expand Up @@ -173,13 +172,13 @@ defmodule ElixirLS.LanguageServer.Providers.DocumentSymbols do
not is_nil(type_expression) do
type_name_location =
case type_expression do
[{:"::", _, [{name, type_head_location, args} = type_head | _]}] ->
[{:"::", _, [{name, type_head_location, args} = _type_head | _]}] ->
{{name, args}, type_head_location}

[{:when, _, [{:"::", _, [{name, type_head_location, args} = type_head, _]}, _]}] ->
[{:when, _, [{:"::", _, [{name, type_head_location, args} = _type_head, _]}, _]}] ->
{{name, args}, type_head_location}

[{name, type_head_location, args} = type_head | _] ->
[{name, type_head_location, args} = _type_head | _] ->
{{name, args}, type_head_location}

_ ->
Expand Down Expand Up @@ -218,16 +217,9 @@ defmodule ElixirLS.LanguageServer.Providers.DocumentSymbols do
# Function, macro, guard with when
defp extract_symbol(
_current_module,
{defname, location, [{:when, _, [{name, head_location, args} = fn_head, _]} | _]}
{defname, location, [{:when, _, [{name, head_location, args} = _fn_head, _]} | _]}
)
when defname in @defs do
head =
Macro.to_string(fn_head)
|> String.replace(~r/,*\n\s*/u, fn
"," <> _ -> ", "
_ -> ""
end)

%Info{
type: if(defname in @macro_defs, do: :constant, else: :function),
symbol: to_string(name),
Expand All @@ -242,16 +234,9 @@ defmodule ElixirLS.LanguageServer.Providers.DocumentSymbols do
# Function, macro, delegate
defp extract_symbol(
_current_module,
{defname, location, [{name, head_location, args} = fn_head | _]}
{defname, location, [{name, head_location, args} = _fn_head | _]}
)
when defname in @defs do
head =
Macro.to_string(fn_head)
|> String.replace(~r/,*\n\s*/u, fn
"," <> _ -> ", "
_ -> ""
end)

%Info{
type: if(defname in @macro_defs, do: :constant, else: :function),
symbol: to_string(name),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ defmodule ElixirLS.LanguageServer.Providers.ExecuteCommand.ExpandMacro do
"""

alias ElixirLS.LanguageServer.Server
alias ElixirSense.Core.Ast
alias ElixirSense.Core.State
alias ElixirSense.Core.Parser
alias ElixirSense.Core.Metadata
Expand Down
15 changes: 2 additions & 13 deletions apps/language_server/lib/language_server/providers/hover/docs.ex
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ defmodule ElixirLS.LanguageServer.Providers.Hover.Docs do
alias ElixirSense.Core.State
alias ElixirSense.Core.SurroundContext
alias ElixirSense.Core.State.ModFunInfo
alias ElixirSense.Core.State.VarInfo
alias ElixirSense.Core.TypeInfo
alias ElixirSense.Core.Parser

Expand Down Expand Up @@ -83,7 +82,6 @@ defmodule ElixirLS.LanguageServer.Providers.Hover.Docs do

env =
Metadata.get_cursor_env(metadata, {line, column}, {begin_pos, end_pos})
|> Metadata.add_scope_vars(metadata, {line, column})

case all(context, env, metadata) do
[] ->
Expand All @@ -104,8 +102,7 @@ defmodule ElixirLS.LanguageServer.Providers.Hover.Docs do
defp all(
context,
%State.Env{
module: module,
vars: vars
module: module
} = env,
metadata
) do
Expand Down Expand Up @@ -136,15 +133,7 @@ defmodule ElixirLS.LanguageServer.Providers.Hover.Docs do
}

{:variable, variable, version} ->
{line, column} = context.begin

var_info =
vars
|> Enum.find(fn
%VarInfo{} = info ->
info.name == variable and (info.version == version or version == :any) and
{line, column} in info.positions
end)
var_info = Metadata.find_var(metadata, variable, version, context.begin)

if var_info != nil do
%{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,7 @@ defmodule ElixirLS.LanguageServer.Providers.References.Locator do
:none ->
[]

%{
begin: {begin_line, begin_col}
} = context ->
context ->
metadata =
Keyword.get_lazy(options, :metadata, fn ->
Parser.parse_string(code, true, true, {line, column})
Expand All @@ -40,34 +38,13 @@ defmodule ElixirLS.LanguageServer.Providers.References.Locator do
module: module
} =
Metadata.get_cursor_env(metadata, {line, column}, {context.begin, context.end})
|> Metadata.add_scope_vars(metadata, {line, column})

# find last env of current module
attributes = get_attributes(metadata, module)

# one line can contain variables from many scopes
# if the cursor is over variable take variables from the scope as it will
# be more correct than the env scope
vars =
case Enum.find(env.vars, fn %VarInfo{positions: positions} ->
{begin_line, begin_col} in positions
end) do
%VarInfo{scope_id: scope_id} ->
# in (h|l)?eex templates vars_info_per_scope_id[scope_id] is nil
if metadata.vars_info_per_scope_id[scope_id] do
metadata.vars_info_per_scope_id[scope_id]
else
[]
end

nil ->
[]
end

find(
context,
env,
vars,
attributes,
metadata,
trace
Expand Down Expand Up @@ -101,7 +78,6 @@ defmodule ElixirLS.LanguageServer.Providers.References.Locator do
aliases: aliases,
module: module
} = env,
vars,
attributes,
%Metadata{
mods_funs_to_positions: mods_funs,
Expand Down Expand Up @@ -196,15 +172,7 @@ defmodule ElixirLS.LanguageServer.Providers.References.Locator do
[]

{:variable, variable, version} ->
{line, column} = context.begin

var_info =
Enum.find_value(vars, fn {{_name, _version}, %VarInfo{} = info} ->
if info.name == variable and (info.version == version or version == :any) and
{line, column} in info.positions do
info
end
end)
var_info = Metadata.find_var(metadata, variable, version, context.begin)

if var_info != nil do
%VarInfo{positions: positions} = var_info
Expand Down

0 comments on commit dd572f4

Please sign in to comment.