rewrite-clj

Joel 2024-09-27T04:59:32.255719Z

I’d like to convert (my-function x) to simply x. However, I’d like to reasonably preserve the formatting of x even if it’s a map that spans multiple lines. I’ve located the outer expr and inner x, but that barfs when I do (z/replace outer-loc (z/node inner-loc))

lread 2024-09-27T12:43:25.151829Z

Hiya @joel380, I wonder if rewrite-clj.paredit/raise might work for you.

(require '[rewrite-clj.zip :as z]
         '[rewrite-clj.paredit :as pe])

(-> "{:z (my-function x)}"
    z/of-string
    z/down
    z/right
    z/down
    z/right ;; now at x node
    pe/raise 
    z/root-string)
;; => "{:z x}"

(-> "{:z (my-function {:a 1
 :b 2})}"
    z/of-string
    z/down
    z/right
    z/down
    z/right ;; now at {:a 1 :b 2} node
    pe/raise 
    z/root-string)
;; => "{:z {:a 1\n :b 2}}"
It won't adjust indentation, but it might do the trick.

Joel 2024-09-27T14:22:02.880409Z

I was wondering if there was such a thing, ill give it a try.

lread 2024-09-27T21:24:01.529219Z

Let us know if it works for you!

Joel 2024-09-27T22:21:54.597139Z

yes, it works reasonably well, thanks!

🎉 1