rewrite-clj

https://github.com/clj-commons/rewrite-clj
vemv 2021-03-13T02:36:57.134800Z

alpha for me, I could upgrade a (WIP) project with fairly minimal changes https://github.com/threatgrid/formatting-stack.alias-rewriter/commit/e58dbd529fac31c4f5f55788d6b489c3912d9bd2

lread 2021-03-13T02:59:02.136900Z

Awesome @vemv ! Much thanks for the report!

✌️ 1
reefersleep 2021-03-13T22:53:28.138900Z

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.

reefersleep 2021-03-13T22:55:20.139500Z

lread 2021-03-13T23:01:10.142400Z

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.

reefersleep 2021-03-13T23:01:37.142800Z

So I should use something other than z/edit?

borkdude 2021-03-13T23:02:13.143600Z

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

borkdude 2021-03-13T23:03:07.144600Z

sorry I meant to tag @reefersleep

lread 2021-03-13T23:04:24.144900Z

@borkdude, https://github.com/clj-commons/rewrite-clj/pull/65`assoc-in`

borkdude 2021-03-13T23:05:00.145700Z

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

borkdude 2021-03-13T23:05:56.146300Z

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

lread 2021-03-13T23:07:07.146700Z

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

reefersleep 2021-03-15T11:01:06.159Z

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,

reefersleep 2021-03-15T11:01:08.159200Z

¯\(ツ)

borkdude 2021-03-15T11:02:26.160500Z

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

reefersleep 2021-03-15T11:14:45.175Z

That might also be an option

lread 2021-03-13T23:08:28.148Z

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

reefersleep 2021-03-13T23:09:27.148200Z

Cheers both 🙂

reefersleep 2021-03-13T23:09:56.148800Z

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

reefersleep 2021-03-13T23:11:11.149100Z

That yield a weird result like this:

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

borkdude 2021-03-13T23:11:18.149400Z

let me just cut a 0.0.1 release now

🙌 1
borkdude 2021-03-13T23:14:14.149700Z

v0.0.1 released

reefersleep 2021-03-13T23:15:11.150Z

Cool!

borkdude 2021-03-13T23:15:44.150400Z

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

lread 2021-03-13T23:16:58.151Z

Yup, https://cljdoc.org/d/rewrite-clj/rewrite-clj.

borkdude 2021-03-13T23:17:37.152Z

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

lread 2021-03-13T23:17:55.152300Z

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

lread 2021-03-13T23:18:51.153200Z

@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.

borkdude 2021-03-13T23:20:44.153300Z

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

borkdude 2021-03-13T23:21:13.153500Z

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

reefersleep 2021-03-13T23:27:59.153700Z

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

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

reefersleep 2021-03-13T23:28:19.153900Z

Yields:

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

lread 2021-03-13T23:30:07.154100Z

And you are looking to format differently with whitespace?

lread 2021-03-13T23:30:43.154300Z

(what would you like the yields to be?)

borkdude 2021-03-13T23:37:30.154500Z

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

reefersleep 2021-03-13T23:38:59.154700Z

Like what borkdude says 🙂

borkdude 2021-03-13T23:39:51.155200Z

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

reefersleep 2021-03-13T23:40:09.155400Z

Oh yeah. Any choice is an assumption.

reefersleep 2021-03-13T23:40:54.156100Z

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?

reefersleep 2021-03-13T23:41:00.156300Z

(I see I can go one way)

borkdude 2021-03-13T23:43:25.157200Z

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

borkdude 2021-03-13T23:44:40.157400Z

afk now, cya tomorrow

lread 2021-03-13T23:53:08.157500Z

so maybe rewrite-edn is your friend here!

reefersleep 2021-03-13T23:57:24.157700Z

Brilliant! That works great!

reefersleep 2021-03-13T23:57:33.158Z

Thanks, sleep tight!