Igniter.Code.Common.move_to_cursor returns error

I’m trying to get Igniter.Code.Common.move_to_cursor to work for some code patching and I’m having trouble. I keep getting an :error returned. I then tried to run the demo code from the docs and that is also returning :error. Is there any work-arounds or maybe I’m just doing something wrong (probably :slight_smile: )?

Igniter version: igniter 0.5.50
Elixir version: Elixir 1.18.1 (compiled with Erlang/OTP 27)
OS: OSX

zipper =
  """
  if true do
    10
  end
  """
  |> Sourceror.Zipper.zip()

pattern =
  """
  if __ do
    __cursor__()
  end
  """

Igniter.Code.Common.move_to_cursor(zipper, pattern)
# returns :error

Many thanks for any help.

Cheers,

Dan

It appears to maybe just be an error in the documentation, I was able to get the example to work by parsing the string first.

zipper =
  """
  if true do
    10
  end
  """
  |> Sourceror.parse_string!()
  |> Sourceror.Zipper.zip()

Yep! Error in the docs. Could you open an issue or a PR to address?

Will do! :+1: And thanks for Igniter! @zachdaniel

On a separate note, I’m struggling a bit finding the right way to add a function above another and skipping the documentation. Do you have a good recipe for this? I am able to add code before the function, but it’s still after the function docs.

For example:

defmodule MyModule do
  .... stuff here

  @doc """
  Some docs
  
  blah, blah, blah
  """
  def my_function() do
    do_things
  end
end

Thanks for any guidance.

Cheers,

Dan

It’s a bit tough TBH. Probably what you’d need to do is move to the function, and then move up past any known module attributes like @doc, @spec etc. We could internalize that complexity into a helper like move_to_function_and_attrs into igniter.

Thanks for the tips. I’m just going to put it below a know function for now. :+1:

1 Like