Skip to content

Commit

Permalink
simplify scope handling
Browse files Browse the repository at this point in the history
  • Loading branch information
lukaszsamson committed Apr 21, 2024
1 parent aee6316 commit ec46edb
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 53 deletions.
36 changes: 9 additions & 27 deletions lib/elixir_sense/core/metadata_builder.ex
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,7 @@ defmodule ElixirSense.Core.MetadataBuilder do
state
|> remove_attributes_scope
|> remove_behaviours_scope
|> remove_alias_scope
|> remove_import_scope
|> remove_require_scope
|> remove_lexical_scope
|> remove_vars_scope
|> remove_namespace
|> remove_protocol_implementation
Expand Down Expand Up @@ -155,11 +153,9 @@ defmodule ElixirSense.Core.MetadataBuilder do
|> add_namespace(module)
|> add_current_module_to_index(position, end_position, generated: state.generated)
|> alias_submodule(module)
|> new_alias_scope
|> new_lexical_scope
|> new_attributes_scope
|> new_behaviours_scope
|> new_import_scope
|> new_require_scope
|> new_vars_scope
|> maybe_add_protocol_behaviour(module)

Expand Down Expand Up @@ -195,9 +191,7 @@ defmodule ElixirSense.Core.MetadataBuilder do
|> apply_optional_callbacks
|> remove_attributes_scope
|> remove_behaviours_scope
|> remove_alias_scope
|> remove_import_scope
|> remove_require_scope
|> remove_lexical_scope
|> remove_vars_scope
|> remove_namespace
|> remove_protocol_implementation
Expand Down Expand Up @@ -307,9 +301,7 @@ defmodule ElixirSense.Core.MetadataBuilder do
state
|> new_named_func(name, length(params || []))
|> add_func_to_index(name, params || [], position, end_position, type, options)
|> new_alias_scope
|> new_import_scope
|> new_require_scope
|> new_lexical_scope
|> new_func_vars_scope
|> add_vars(vars, true)
|> add_current_env_to_line(Keyword.fetch!(meta, :line))
Expand Down Expand Up @@ -349,9 +341,7 @@ defmodule ElixirSense.Core.MetadataBuilder do
defp post_func(ast, state) do
# dbg(ast)
state
|> remove_alias_scope
|> remove_import_scope
|> remove_require_scope
|> remove_lexical_scope
|> remove_func_vars_scope
|> remove_last_scope_from_scopes
|> result(ast)
Expand Down Expand Up @@ -400,9 +390,7 @@ defmodule ElixirSense.Core.MetadataBuilder do
end

state
|> new_alias_scope
|> new_import_scope
|> new_require_scope
|> new_lexical_scope
|> new_vars_scope
|> result(ast)
end
Expand All @@ -418,9 +406,7 @@ defmodule ElixirSense.Core.MetadataBuilder do
end

state
|> remove_alias_scope
|> remove_import_scope
|> remove_require_scope
|> remove_lexical_scope
|> maybe_move_vars_to_outer_scope
|> remove_vars_scope
|> result(ast)
Expand All @@ -435,9 +421,7 @@ defmodule ElixirSense.Core.MetadataBuilder do
|> merge_same_name_vars()

state
|> new_alias_scope
|> new_import_scope
|> new_require_scope
|> new_lexical_scope
|> new_vars_scope
|> add_vars(vars, true)
|> add_current_env_to_line(line)
Expand All @@ -446,9 +430,7 @@ defmodule ElixirSense.Core.MetadataBuilder do

defp post_clause(ast, state) do
state
|> remove_alias_scope
|> remove_import_scope
|> remove_require_scope
|> remove_lexical_scope
|> maybe_move_vars_to_outer_scope
|> remove_vars_scope
|> result(ast)
Expand Down
34 changes: 8 additions & 26 deletions lib/elixir_sense/core/state.ex
Original file line number Diff line number Diff line change
Expand Up @@ -731,12 +731,6 @@ defmodule ElixirSense.Core.State do
%{state | scopes: [[{:typespec, name, arity} | hd(state.scopes)] | state.scopes]}
end

# def remove_typespec_namespace(%__MODULE__{} = state) do
# outer_scopes = state.scopes |> tl

# %{state | scopes: outer_scopes}
# end

def register_optional_callbacks(%__MODULE__{} = state, list) do
[_ | rest] = state.optional_callbacks_context
%{state | optional_callbacks_context: [list | rest]}
Expand Down Expand Up @@ -942,14 +936,6 @@ defmodule ElixirSense.Core.State do
end)
end

def new_alias_scope(%__MODULE__{} = state) do
%__MODULE__{state | aliases: [[] | state.aliases]}
end

def remove_alias_scope(%__MODULE__{} = state) do
%__MODULE__{state | aliases: tl(state.aliases)}
end

def new_vars_scope(%__MODULE__{} = state) do
scope_id = state.scope_id_count + 1

Expand Down Expand Up @@ -1153,30 +1139,26 @@ defmodule ElixirSense.Core.State do
Enum.reduce(aliases_tuples, state, fn tuple, state -> add_alias(state, tuple) end)
end

def new_import_scope(%__MODULE__{} = state) do
def new_lexical_scope(%__MODULE__{} = state) do
%__MODULE__{
state
| functions: [hd(state.functions) | state.functions],
macros: [hd(state.macros) | state.macros]
macros: [hd(state.macros) | state.macros],
requires: [[] | state.requires],
aliases: [[] | state.aliases]
}
end

def new_require_scope(%__MODULE__{} = state) do
%__MODULE__{state | requires: [[] | state.requires]}
end

def remove_import_scope(%__MODULE__{} = state) do
def remove_lexical_scope(%__MODULE__{} = state) do
%__MODULE__{
state
| functions: tl(state.functions),
macros: tl(state.macros)
macros: tl(state.macros),
requires: tl(state.requires),
aliases: tl(state.aliases)
}
end

def remove_require_scope(%__MODULE__{} = state) do
%__MODULE__{state | requires: tl(state.requires)}
end

def add_import(%__MODULE__{} = state, module, opts) when is_atom(module) or is_list(module) do
module = expand_alias(state, module)

Expand Down

0 comments on commit ec46edb

Please sign in to comment.