Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
lukaszsamson committed Aug 29, 2024
1 parent 441de76 commit cc5e8a0
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 63 deletions.
77 changes: 26 additions & 51 deletions lib/elixir_sense/core/compiler.ex
Original file line number Diff line number Diff line change
Expand Up @@ -1454,7 +1454,7 @@ defmodule ElixirSense.Core.Compiler do
state,
env
) do
%{vars: vars, unused: unused} = state
state_orig = state
original_env = env

{expanded, _state, _env} = expand(alias, state, env)
Expand Down Expand Up @@ -1534,16 +1534,10 @@ defmodule ElixirSense.Core.Compiler do
{remove_func_vars_scope(state, state_orig), env}
end)

state =
state
|> apply_optional_callbacks(%{env | module: full})

# restore vars from outer scope
# TODO magic with ElixirEnv instead of new_vars_scope?
# unused probably shouldnt be restored
state =
%{state | vars: vars, unused: unused}
|> remove_vars_scope
state = state
|> apply_optional_callbacks(%{env | module: full})
|> remove_vars_scope(state_orig)
|> remove_attributes_scope
|> remove_module

Expand Down Expand Up @@ -1629,6 +1623,7 @@ defmodule ElixirSense.Core.Compiler do
state
| caller: def_kind in [:defmacro, :defmacrop, :defguard, :defguardp]
}
|> new_vars_scope()
end

# no need to reset versioned_vars - we never update it
Expand Down Expand Up @@ -1662,9 +1657,6 @@ defmodule ElixirSense.Core.Compiler do
def_kind
)

# TODO not sure vars scope is needed
state = state |> new_vars_scope

expr =
case expr do
nil ->
Expand Down Expand Up @@ -1694,15 +1686,14 @@ defmodule ElixirSense.Core.Compiler do
# restore vars from outer scope
state =
%{state | caller: false}
|> remove_vars_scope

state =
unless has_unquotes do
# restore module vars
remove_func_vars_scope(state, state_orig)
else
# no need to do anything
state
# remove scope
remove_vars_scope(state, state_orig)
end

# result of def expansion is fa tuple
Expand Down Expand Up @@ -2138,7 +2129,7 @@ defmodule ElixirSense.Core.Compiler do
{do_expr, do_opts}
end

{e_opts, so, eo} = expand(opts, __MODULE__.Env.reset_vars(s), e)
{e_opts, so, eo} = expand(opts, new_vars_scope(s), e)
{e_cases, sc, ec} = map_fold(&expand_for_generator/3, so, eo, cases)
# elixir raises here for_generator_start on invalid start generator

Expand All @@ -2148,10 +2139,10 @@ defmodule ElixirSense.Core.Compiler do
{maybe_reduce, normalized_opts} =
sanitize_for_options(e_opts, false, false, false, return, meta, e, [])

{e_expr, se, ee} = expand_for_do_block(expr, sc, ec, maybe_reduce)
{e_expr, se, _ee} = expand_for_do_block(expr, sc, ec, maybe_reduce)

{{:for, meta, e_cases ++ [[{:do, e_expr} | normalized_opts]]},
__MODULE__.Env.merge_vars(se, s, ee), e}
remove_vars_scope(se, s), e}
end

defp expand_for_do_block([{:->, _, _} | _] = clauses, s, e, false) do
Expand Down Expand Up @@ -2182,13 +2173,13 @@ defmodule ElixirSense.Core.Compiler do
{_ast, sa, _e} = expand(discarded_args, sa, e)

clause = {:->, clause_meta, [args, right]}
s_reset = __MODULE__.Env.reset_vars(sa)
s_reset = new_vars_scope(sa)

# no point in doing type inference here, we are only certain of the initial value of the accumulator
{e_clause, s_acc, e_acc} =
{e_clause, s_acc, _e_acc} =
__MODULE__.Clauses.clause(&__MODULE__.Clauses.head/3, clause, s_reset, e)

{e_clause, __MODULE__.Env.merge_vars(s_acc, sa, e_acc)}
{e_clause, remove_vars_scope(s_acc, sa)}
end

{do_expr, sa} = Enum.map_reduce(clauses, s, transformer)
Expand Down Expand Up @@ -2415,10 +2406,6 @@ defmodule ElixirSense.Core.Compiler do
defmodule Env do
alias ElixirSense.Core.Compiler.Utils, as: ElixirUtils

def reset_vars(s) do
s |> new_vars_scope
end

def reset_read(%{vars: {_, write}} = s, %{vars: {read, _}}) do
%{s | vars: {read, write}}
end
Expand Down Expand Up @@ -2450,18 +2437,6 @@ defmodule ElixirSense.Core.Compiler do
)
end

def merge_vars(s, %{vars: {read, write}}, _e) do
# dbg(s.vars_info)
# dbg({read, write})
s =
%{s | vars: {read, write}}
|> remove_vars_scope

# dbg(s.vars_info)
# dbg(s.vars_info_per_scope_id)
s
end

def calculate_span(meta, name) do
case Keyword.fetch(meta, :column) do
{:ok, column} ->
Expand Down Expand Up @@ -2800,7 +2775,7 @@ defmodule ElixirSense.Core.Compiler do
{_ast, s, _e} = ElixirExpand.expand(opts0 |> Keyword.drop(@valid_with_opts), s, e)

opts0 = sanitize_opts(opts0, @valid_with_opts)
s0 = ElixirEnv.reset_vars(s)
s0 = new_vars_scope(s)
{e_exprs, {s1, e1}} = Enum.map_reduce(exprs, {s0, e}, &expand_with/2)
{e_do, opts1, s2} = expand_with_do(meta, opts0, s, s1, e1)
{e_opts, _opts2, s3} = expand_with_else(opts1, s2, e)
Expand Down Expand Up @@ -2832,9 +2807,9 @@ defmodule ElixirSense.Core.Compiler do
# we return empty expression
expr = expr || []

{e_expr, s_acc, e_acc} = ElixirExpand.expand(expr, acc, e)
{e_expr, s_acc, _e_acc} = ElixirExpand.expand(expr, acc, e)

{e_expr, rest_opts, ElixirEnv.merge_vars(s_acc, s, e_acc)}
{e_expr, rest_opts, remove_vars_scope(s_acc, s)}
end

defp expand_with_else(opts, s, e) do
Expand Down Expand Up @@ -2882,13 +2857,13 @@ defmodule ElixirSense.Core.Compiler do
end

defp expand_try({:do, expr}, s, e) do
{e_expr, se, ee} = ElixirExpand.expand(expr, ElixirEnv.reset_vars(s), e)
{{:do, e_expr}, ElixirEnv.merge_vars(se, s, ee)}
{e_expr, se, _ee} = ElixirExpand.expand(expr, new_vars_scope(s), e)
{{:do, e_expr}, remove_vars_scope(se, s)}
end

defp expand_try({:after, expr}, s, e) do
{e_expr, se, ee} = ElixirExpand.expand(expr, ElixirEnv.reset_vars(s), e)
{{:after, e_expr}, ElixirEnv.merge_vars(se, s, ee)}
{e_expr, se, _ee} = ElixirExpand.expand(expr, new_vars_scope(s), e)
{{:after, e_expr}, remove_vars_scope(se, s)}
end

defp expand_try({:else, _} = else_clause, s, e) do
Expand Down Expand Up @@ -3047,10 +3022,10 @@ defmodule ElixirSense.Core.Compiler do

defp expand_clauses(fun, {key, [_ | _] = clauses}, s, e) do
transformer = fn clause, sa ->
{e_clause, s_acc, e_acc} =
clause(fun, clause, ElixirEnv.reset_vars(sa), e)
{e_clause, s_acc, _e_acc} =
clause(fun, clause, new_vars_scope(sa), e)

{e_clause, ElixirEnv.merge_vars(s_acc, sa, e_acc)}
{e_clause, remove_vars_scope(s_acc, sa)}
end

{values, se} = Enum.map_reduce(clauses, s, transformer)
Expand Down Expand Up @@ -3516,13 +3491,13 @@ defmodule ElixirSense.Core.Compiler do
transformer = fn
{:->, _, [_left, _right]} = clause, sa ->
# elixir raises defaults_in_args
s_reset = ElixirEnv.reset_vars(sa)
s_reset = new_vars_scope(sa)

# no point in doing type inference here, we have no idea what the fn will be called with
{e_clause, s_acc, e_acc} =
{e_clause, s_acc, _e_acc} =
ElixirClauses.clause(&ElixirClauses.head/3, clause, s_reset, e)

{e_clause, ElixirEnv.merge_vars(s_acc, sa, e_acc)}
{e_clause, remove_vars_scope(s_acc, sa)}
end

{e_clauses, se} = Enum.map_reduce(clauses, s, transformer)
Expand Down
2 changes: 1 addition & 1 deletion lib/elixir_sense/core/metadata_builder.ex
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ defmodule ElixirSense.Core.MetadataBuilder do

state
|> remove_attributes_scope
|> remove_vars_scope
|> remove_vars_scope(%{vars: {%{}, false}})
|> remove_module
end

Expand Down
7 changes: 5 additions & 2 deletions lib/elixir_sense/core/state.ex
Original file line number Diff line number Diff line change
Expand Up @@ -819,16 +819,19 @@ defmodule ElixirSense.Core.State do
%__MODULE__{state | attributes: [[] | state.attributes], scope_attributes: [[]]}
end

def remove_vars_scope(%__MODULE__{} = state) do
def remove_vars_scope(%__MODULE__{} = state, %{vars: vars}) do
state = maybe_move_vars_to_outer_scope(state)
%__MODULE__{
state
| scope_ids: tl(state.scope_ids),
vars_info: tl(state.vars_info),
vars_info_per_scope_id: update_vars_info_per_scope_id(state)
vars_info_per_scope_id: update_vars_info_per_scope_id(state),
# restore elixir_ex fields
vars: vars
}
end

# TODO should we restore unused?
def remove_func_vars_scope(%__MODULE__{} = state, %{vars: vars, unused: unused}) do
%__MODULE__{
state
Expand Down
16 changes: 7 additions & 9 deletions test/elixir_sense/core/metadata_builder_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -2660,16 +2660,15 @@ defmodule ElixirSense.Core.MetadataBuilderTest do
"""
|> string_to_state

assert ([
assert [
%VarInfo{name: :_par5, positions: [{3, 57}], scope_id: scope_id_1},
%VarInfo{name: :par1, positions: [{3, 20}], scope_id: scope_id_1},
%VarInfo{name: :par2, positions: [{3, 33}], scope_id: scope_id_1},
%VarInfo{name: :par3, positions: [{3, 39}], scope_id: scope_id_1},
%VarInfo{name: :par4, positions: [{3, 51}], scope_id: scope_id_1},
%VarInfo{name: :var_in1, positions: [{4, 5}], scope_id: scope_id_2},
%VarInfo{name: :var_in2, positions: [{5, 5}], scope_id: scope_id_2}
]
when scope_id_2 > scope_id_1) = state |> get_line_vars(6)
%VarInfo{name: :var_in1, positions: [{4, 5}], scope_id: scope_id_1},
%VarInfo{name: :var_in2, positions: [{5, 5}], scope_id: scope_id_1}
] = state |> get_line_vars(6)

assert [
%VarInfo{name: :arg, positions: [{8, 14}, {8, 24}]}
Expand Down Expand Up @@ -3449,7 +3448,7 @@ defmodule ElixirSense.Core.MetadataBuilderTest do
{:x, nil}
]

assert ([
assert [
%VarInfo{
name: :_my_other,
positions: [{2, 24}],
Expand All @@ -3458,7 +3457,7 @@ defmodule ElixirSense.Core.MetadataBuilderTest do
%VarInfo{
name: :abc,
positions: [{3, 6}],
scope_id: scope_id_2
scope_id: scope_id_1
},
%VarInfo{
name: :my_var,
Expand All @@ -3470,8 +3469,7 @@ defmodule ElixirSense.Core.MetadataBuilderTest do
positions: [{2, 43}, {3, 14}],
scope_id: scope_id_1
}
]
when scope_id_2 > scope_id_1) = state |> get_line_vars(4)
] = state |> get_line_vars(4)
end
end

Expand Down

0 comments on commit cc5e8a0

Please sign in to comment.