Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

improvement: add move_to_cursor/2 #147

Merged
merged 3 commits into from
Jun 25, 2024
Merged

improvement: add move_to_cursor/2 #147

merged 3 commits into from
Jun 25, 2024

Conversation

zachdaniel
Copy link
Contributor

So, I'm sure that there will need to be lots of workshopping or even that the implementation should be fully changed, but this at a minimum starts the conversation :)

Its relatively simple, but does technically break if someone is using the variables/functions ___cursor___ or ___skip___ (note the triple underscores).

@NickNeck
Copy link
Contributor

NickNeck commented Jun 7, 2024

That looks like a great idea. I was asking myself if we could use Code.Fragment. container_cursor_to_quoted/2 (available since Elixir 1.17) here.

iex>  Code.Fragment.container_cursor_to_quoted("if ... do")
{:ok, {:if, [line: 1], [{:..., [line: 1], []}, [do: {:__cursor__, [line: 1], []}]]}}
iex>  Code.Fragment.container_cursor_to_quoted("... |> Enum.filter(")
{:ok,
 {:|>, [line: 1],
  [
    {:..., [line: 1], []},
    {{:., [line: 1], [{:__aliases__, [line: 1], [:Enum]}, :filter]}, [line: 1],
     [{:__cursor__, [line: 1], []}]}
  ]}}

The ... for skipping would be nice. The function move_to_cursor could accept opts to set the skip marker.

Zipper.move_to_cursor(zipper, "if ... do")
Zipper.move_to_cursor(zipper, "if :ignore do", skip: :ignore)

@zachdaniel
Copy link
Contributor Author

Hmm....that is pretty nice. I like that better than ___skip___ 😆

@zachdaniel
Copy link
Contributor Author

curious to see if it will play out

@zachdaniel
Copy link
Contributor Author

So, I've used __cursor__ again to line up with that function, and ... for skipping, but I did just realize that IIRC ... is going to be an operator used by Elixir in 1.18. So we probably can't use that. I think it will have to be something else. Maybe __?

@zachdaniel
Copy link
Contributor Author

I think a variation using container_cursor_to_quoted would make sense, but it would be a different function since it only has to match on what "leads up to" the cursor, whereas this has to match up before and after.

@NickNeck
Copy link
Contributor

NickNeck commented Jun 7, 2024

Yes, with container_cursor_to_quoted it would be a different function but a variant that could make sense. It's a shame that we can't use .... I think __ could also fit there.

When move_to_cursor works we could try something like Regex.named_caputres 😄

{zipper, caputres} = Zipper.move_to_coursor_and_capture(zipper, "__cursor__ |> Enum.map(__map) |> Enum.join(__join) ")
captures.map #=> AST for Enum.map args
captures.join #=> AST ...

But, one step at a time.

@doorgan
Copy link
Owner

doorgan commented Jun 7, 2024

Maybe __?

When I first took a stab at this, I went with __ because it felt like a really weird thing to type in most elixir code, so I'm good with using this to ignore values!

@zachdaniel
Copy link
Contributor Author

__ is especially good, because you get this:

iex(1)> defmodule Thing do
...(1)> def thing(__), do: 10
...(1)> end
warning: unknown compiler variable "__" (expected one of __MODULE__, __ENV__, __DIR__, __CALLER__, __STACKTRACE__)
└─ iex:2: Thing.thing/1

Copy link
Owner

@doorgan doorgan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

After thinking a lot about this, this implementation looks reasonable and is an improvement over the existing apis, so I feel good about merging it
We can always adjust it as we see people use it more and we discover more about its shortcomings

@doorgan doorgan merged commit b353d74 into doorgan:main Jun 25, 2024
6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants