Fork me on GitHub
#rewrite-clj
<
2023-03-04
>
Noah Bogart14:03:45

Is there a common pattern for tracking namespaces to handle auto-resolving keywords, symbols, and prefixed maps?

borkdude15:03:39

@nbtheduke There are options you can provide to sexpr how to render those, but there isn't any logic to parse ns forms and track ns state. In edamame that would be done like this:

(require '[clojure.test :as t])
(e/parse-string "::t/foo" {:auto-resolve (fn [x] (if (= :current x) *ns* (get (ns-aliases *ns*) x)))})
:clojure.test/foo

borkdude15:03:11

It could maybe be useful to have something like:

(def x (rewrite.namespace/parse-ns-form ns-form-node)) ;; static ns form parsing, no evaluation in the host
whose result you could pass to (sexpr ... {:... x})

borkdude15:03:12

@nbtheduke Actually, in grasp, I abuse SCI to emulate the ns state

borkdude15:03:31

So you create a context with SCI, then evaluate those namespace forms in the SCI context, so it won't pollute the host

😂 2
borkdude15:03:48

and then you can query the SCI context what aliases there are in the "current" namespace, etc

Noah Bogart15:03:51

That’s clever. I’m using edamame right now with the rudimentary ns handling you suggested a while back but thinking of switching back to rewrite-clj cuz I need the zipper api for a feature I’m planning.

Noah Bogart15:03:05

I’ll look at how grasp does it, maybe I can rely on sci too

borkdude15:03:49

I'll see if I can put something together which is both useful for rewrite-clj and edamame since this is a problem that comes up now and then

👍 2
Noah Bogart15:03:05

That’s kind and helpful of you, thanks

borkdude16:03:53

Similar approach works for edamame

borkdude16:03:13

Feel free to take it for a spin :)