Skip to content

Commit 1193e18

Browse files
committed
make macro expand more resilient from cursor nodes
1 parent 7b063cf commit 1193e18

File tree

2 files changed

+32
-1
lines changed

2 files changed

+32
-1
lines changed

lib/elixir_sense/core/compiler.ex

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1965,6 +1965,8 @@ defmodule ElixirSense.Core.Compiler do
19651965

19661966
defp expand_macro_callback(meta, module, fun, args, callback, state, env) do
19671967
# dbg({module, fun, args})
1968+
{args, state, env} = expand_macro_arg_cursor(args, state, env)
1969+
19681970
try do
19691971
callback.(meta, args)
19701972
catch
@@ -1993,11 +1995,40 @@ defmodule ElixirSense.Core.Compiler do
19931995
end
19941996

19951997
defp expand_macro_callback!(meta, _module, _fun, args, callback, state, env) do
1998+
{args, state, env} = expand_macro_arg_cursor(args, state, env)
1999+
19962000
ast = callback.(meta, args)
2001+
2002+
state =
2003+
if __MODULE__.Utils.has_cursor?(args) and not __MODULE__.Utils.has_cursor?(ast) do
2004+
# in case there was cursor in the original args but it's not present in macro result
2005+
# expand a fake node
2006+
{_ast, state, _env} = expand({:__cursor__, [], []}, state, env)
2007+
state
2008+
else
2009+
state
2010+
end
2011+
19972012
{ast, state, env} = expand(ast, state, env)
19982013
{ast, state, env}
19992014
end
20002015

2016+
defp expand_macro_arg_cursor(args, state, env) when is_list(args) do
2017+
{args, state} =
2018+
Enum.reduce(args, {[], state}, fn arg, {acc, state} ->
2019+
{arg, state, _env} = expand_macro_arg_cursor(arg, state, env)
2020+
{[arg | acc], state}
2021+
end)
2022+
2023+
{Enum.reverse(args), state, env}
2024+
end
2025+
2026+
defp expand_macro_arg_cursor({:__cursor__, _, list} = ast, state, env) when is_list(list) do
2027+
expand(ast, state, env)
2028+
end
2029+
2030+
defp expand_macro_arg_cursor(arg, state, env), do: {arg, state, env}
2031+
20012032
defp ex_unit_test_name(state, name) do
20022033
case state.ex_unit_describe do
20032034
nil -> "test #{name}"

lib/elixir_sense/core/type_info.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,7 @@ defmodule ElixirSense.Core.TypeInfo do
229229
|> Code.format_string!(line_length: line_length)
230230
|> to_string()
231231
rescue
232-
e ->
232+
_ ->
233233
if Version.match?(System.version(), ">= 1.18.0-dev") do
234234
Logger.warning(
235235
"Macro.to_string(#{inspect(sanitized)}) returned invalid code. If you believe this to be an error please report that to elixir project."

0 commit comments

Comments
 (0)