alpha ✅ for me, I could upgrade a (WIP) project with fairly minimal changes https://github.com/threatgrid/formatting-stack.alias-rewriter/commit/e58dbd529fac31c4f5f55788d6b489c3912d9bd2
Awesome @vemv ! Much thanks for the report!
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
@borkdude, https://github.com/clj-commons/rewrite-clj/pull/65`assoc-in`
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
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?
That yield a weird result like this:
{:a :b
:c :d :some-key 10}
let me just cut a 0.0.1 release now
v0.0.1 released
Cool!
@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.
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
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.
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) .. ..))
afk now, cya tomorrow
so maybe rewrite-edn is your friend here!
Brilliant! That works great!
Thanks, sleep tight!