rewrite-clj

Joel 2024-06-25T04:47:43.138389Z

I’m replacing some vectors with maps. I’m invoking (z/replace zloc m) , which puts comma separators between the entries in m . Is there a way to avoid the commas? (Personally, I’m fine with them, but coworkers seem to look askance at the commas). I guess I need another pass to remove them?

👍 1
borkdude 2024-06-25T14:32:17.776729Z

maybe @lee has some ideas on this, but here's how I "uncomma"'d some EDN in jet: https://github.com/borkdude/jet/blob/1b126c051fb858708f30eca6d57eaded172d79b2/src/jet/formats.clj#L66-L74

👍🏼 1
lread 2024-06-25T14:59:18.196739Z

That seems like a decent approach to me @borkdude! @joel380, in case you didn't know, the commas are coming from Clojure itself:

{:a 1 :b 2 :c 3}
;; => {:a 1, :b 2, :c 3}
A repro of your situation:
(require '[rewrite-clj.zip :as z])

(-> "some code"
    z/of-string
    z/right
    (z/replace {:a 1 :b 2 :c 3})
    z/root-string)
;; => "some {:a 1, :b 2, :c 3}"
I don't know where your m map is coming from, so this may not work for you, but you could express your map as rewrite-clj nodes:
(require
  '[rewrite-clj.node :as n]
  '[rewrite-clj.zip :as z])

(-> "some code"
    z/of-string
    z/right
    (z/replace (n/map-node [(n/keyword-node :a)
                            (n/spaces 1)
                            (n/token-node 1)
                            (n/spaces 1)
                            (n/keyword-node :b)
                            (n/spaces 1)
                            (n/token-node 2)
                            (n/spaces 1)
                            (n/keyword-node :c)
                            (n/spaces 1)
                            (n/token-node 3)]))
    z/root-string)
;; => "some {:a 1 :b 2 :c 3}"
Or express your map as nodes from a parsed string:
(require
  '[rewrite-clj.parser :as p]
  '[rewrite-clj.zip :as z])

(-> "some code"
    z/of-string
    z/right
    (z/replace (p/parse-string "{:a 1 :b 2 :c 3}"))
    z/root-string)
;; => "some {:a 1 :b 2 :c 3}"

Joel 2024-06-26T15:46:43.162419Z

I like the borkdude’s solution above as then I can theoretically call it on an arbitrary expression. Except I see that depends on z/end? which I think would be the end of the string/file? Is there a way to change that so that it’s applied to the “current expression” only?

borkdude 2024-06-26T15:50:41.347119Z

I'm not sure if there's another predicate to check the current expression's end, have you scanned the rest of the API?

Joel 2024-06-26T15:51:23.502219Z

I see a z/of-node not sure what that does though.

borkdude 2024-06-26T15:52:34.033779Z

maybe this is what subzip is for

Joel 2024-06-26T15:54:52.595499Z

That looks promising. So I z/replace my map in, and still should be able to use the same zloc to subzip the commas out with pretty much your code.

lread 2024-06-26T15:58:22.508739Z

Yep, that's the intent of sub editing.

lread 2024-06-25T14:13:58.244549Z

For interested lurkers, a rewrite-clj discussion in #grasp https://clojurians.slack.com/archives/C01EF12T49W/p1719242956880299