rewrite-clj 2021-03-13

I’m trying to edit map literals in a string and print the string back out, and I’m confounded that the maps are returned as a single line rather than the line-by-line form that were originally in the string. I thought the whitespace would have been preserved.

Ah… maybe… I see? So z/edit performs an sexpr on the node and the resulting form (which has lost whitespace) is coerced back to a node.

So I should use something other than z/edit?

@lee I have some assoc like functions here https://github.com/borkdude/rewrite-edn which work on rewrite-clj nodes

sorry I meant to tag @reefersleep

I guess you can just use rewrite-edn as an add-on lib

it also has assoc-in, update, update-in

@reefersleep, you could also look at z/assoc.

Yes, I certainly understand that. Even if you assume a standard, you’re increasing the lexical scope that assoc needs to know about. And if you don’t assume a standard, well,

¯\(ツ)

Anyway, after automatically formatting with cljfmt or whatever these problems might go away?

That might also be an option

That yield a weird result like this:

{:a :b
 :c :d :some-key 10}

ah right, I think rewrite-edn does try to do better here

but in general it's hard to take into account a general indentation style/context

Or find :some-key and edit or replace the token to the right. Many ways to roam around a zipper.

Cheers both 🙂

@borkdude do I have to check rewrite-edn out and build it locally because it’s a proof of concept?

let me just cut a 0.0.1 release now

🙌 1

v0.0.1 released

@lee Are there any codox-like docs for rewrite-clj?

ah yes, I think in your case z/assoc would also have worked

I thinks so too, but @reefersleep did not have success.

@reefersleep, if you are still stuck after your next attempts, share a small sample of what you are parsing along with your code and we’ll help.

Here’s a tiny repro of the z/assoc problem:

(-> 
"{:a :b
 :c :d}"
    z/of-string
    (z/assoc :e :f)
    z/print-root)

Yields:

{:a :b
 :c :d :e :f}

And you are looking to format differently with whitespace?

(what would you like the yields to be?)

rewrite-edn puts it on a new line, indented like the previous k/v pair

Like what borkdude says 🙂

it's not the only right answer though, just a different answer that I happened to like

Oh yeah. Any choice is an assumption.

so maybe rewrite-edn is your friend here!

Turns out it is!

I’m trying to use r/assoc, but it assumes a node, and I’m in zipper-mode. Is there a way to go from zipper to node and back?

(I see I can go one way)

@reefersleep from the top of my head: maybe (z/replace zloc (r/assoc (z/node zloc) .. ..))

Brilliant! That works great!

afk now, cya tomorrow

Thanks, sleep tight!

Awesome @vemv ! Much thanks for the report!

✌️ 1