clj-commons / rewrite-clj

Rewrite Clojure code and edn
https://cljdoc.org/d/rewrite-clj
MIT License
586 stars 53 forks source link

Add `remove` with a movement function #258

Open mrkam2 opened 3 months ago

mrkam2 commented 3 months ago

Problem/Opportunity Sometimes it is hard to use remove because it always selects the previous node in a depth-first traversal. So after removing a selection may move arbitrarily deep in the tree structure. I propose to have an alternative version that applies a given traversal function so that you have better control over the selected node after the deletion.

Proposed Solution (z/remove zloc z/left) selects the left sibling node after removing the current node. (z/remove zloc z/right) selects the right sibling node after removing the current node. (z/remove zloc z/prev) matches the current behavior.

Similar changes for z/remove* function.

lread commented 3 months ago

Thanks for raising this issue @mrkam2!

I agree remove can be tricky to use for the reason you described.

Your idea is interesting!

For (z/remove zloc z/left), I assume we'd throw when there is no left node? And the same idea for (z/remove zloc z/right) when there is no right node?

mrkam2 commented 3 months ago

I guess it could simply return nil just as movement functions do. Throwing is also an option.

On Fri, Mar 15, 2024, 5:17 PM Lee Read @.***> wrote:

Thanks for raising this issue @mrkam2 https://github.com/mrkam2!

I agree remove can be tricky to use for the reason you described.

Your idea is interesting!

For (z/remove zloc z/left), I assume we'd throw when there is no left node? And the same idea for (z/remove zloc z/right) when there is no right node?

— Reply to this email directly, view it on GitHub https://github.com/clj-commons/rewrite-clj/issues/258#issuecomment-2000748470, or unsubscribe https://github.com/notifications/unsubscribe-auth/AGXVI5QG4B632ZAZVEK3SPLYYOFR5AVCNFSM6AAAAABEY2G23KVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDAMBQG42DQNBXGA . You are receiving this because you were mentioned.Message ID: @.***>