rewrite-clj

https://github.com/clj-commons/rewrite-clj
NoahTheDuke 2020-10-19T15:30:32.039800Z

hey friends

NoahTheDuke 2020-10-19T15:33:25.042Z

i'm working on a card game, and i have files with the card effect definitions. i want to fundamentally change how the effect definitions are structured by code-walking and making the changes and then saving the file at the end, but i'm struggling to figure out how to traverse the nodes rewrite-clj gives back

NoahTheDuke 2020-10-19T15:33:36.042300Z

for example, here's a simple card definition:

(defcard "15 Minutes"
  {:abilities [{:cost [:click 1]
                :msg "shuffle 15 Minutes into R&D"
                :label "Shuffle 15 Minutes into R&D"
                :effect (effect (move :corp card :deck nil)
                                (shuffle! :corp :deck)
                                (update-all-agenda-points))}]
   :flags {:has-abilities-when-stolen true}})

NoahTheDuke 2020-10-19T15:35:27.043800Z

i parse the file with (def agendas (z/of-file "src/clj/game/cards/agendas.clj")), and then call (z/find-next-value agendas z/next 'defcard) to move to the defcard node.

NoahTheDuke 2020-10-19T16:54:45.046300Z

is there a better way of saying (-> (z/find-next-value agendas z/next 'defcard) z/next z/next (z/find-value z/next :abilities) z/right) to process the map inside the :abilities vector?

lread 2020-10-19T17:28:56.047800Z

Hi @nbtheduke. I guess you could skip the first find, that is if you don’t need it for some other reason.

(-> agendas
    (z/find-next-value z/next :abilities)
    z/right)
Would bring you to your :abilities vector.

NoahTheDuke 2020-10-19T17:56:30.048200Z

thank you!

NoahTheDuke 2020-10-19T18:19:55.048800Z

what if i wanted to look at every map literal in the whole file?

borkdude 2020-10-19T19:03:55.049400Z

@nbtheduke You could iterate through every form in the file using z/next and check every time if it's a map or not

NoahTheDuke 2020-10-19T19:05:26.049700Z

interesting, okay

borkdude 2020-10-19T19:13:50.050200Z

@nbtheduke This is also what I do in carve where I remove nodes by looking at the location given a list of locations: https://github.com/borkdude/carve/blob/8e9a29b9b985a89ce0aa0b7f4117e072a07bed44/src/carve/impl.clj#L88