fix: properly move to arguments of Module.fun calls

improvement: add `Igniter.Code.Common.expand_literal/1`
This commit is contained in:
Zach Daniel 2024-08-26 16:48:03 -04:00
parent aefb8df79a
commit a5bdf2b7a0
2 changed files with 55 additions and 6 deletions

View file

@ -85,6 +85,23 @@ defmodule Igniter.Code.Common do
end
end
@doc """
Expands a literal value using the env at the cursor, if possible
"""
def expand_literal(zipper) do
if Macro.quoted_literal?(zipper.node) do
case current_env(zipper) do
{:ok, env} ->
{:ok, Macro.expand_literals(zipper.node, env)}
_ ->
:error
end
else
:error
end
end
@doc """
Adds the provided code to the zipper.
@ -691,15 +708,38 @@ defmodule Igniter.Code.Common do
equal_vals?(l, r) || equal_modules?(l, r)
end
defp equal_vals?({:__block__, _, [value]} = block, value) do
!extendable_block?(block)
defp equal_vals?({:__block__, _, [value]}, value) do
true
end
defp equal_vals?(value, {:__block__, _, [value]} = block) do
!extendable_block?(block)
defp equal_vals?(value, {:__block__, _, [value]}) do
true
end
defp equal_vals?(_, _), do: false
defp equal_vals?(left, right) do
cond do
extendable_block?(left) ->
case left do
{:__block__, _, [left]} ->
equal_vals?(left, right)
_ ->
false
end
extendable_block?(right) ->
case right do
{:__block__, _, [right]} ->
equal_vals?(left, right)
_ ->
false
end
true ->
false
end
end
@spec expand_alias(Zipper.t()) :: Zipper.t()
def expand_alias(zipper) do

View file

@ -422,6 +422,15 @@ defmodule Igniter.Code.Function do
end
end
else
offset =
case zipper.node do
{{:., _, _}, _, _args} ->
1
_ ->
0
end
zipper
|> Zipper.down()
|> case do
@ -430,7 +439,7 @@ defmodule Igniter.Code.Function do
zipper ->
zipper
|> Common.nth_right(index)
|> Common.nth_right(index + offset)
|> case do
:error ->
:error