rewrite-clj

2023-03-04T14:54:45.690159Z

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

borkdude 2023-03-04T15:12:39.868619Z

@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

borkdude 2023-03-04T15:15:11.894259Z

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})

borkdude 2023-03-04T15:17:12.909789Z

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

borkdude 2023-03-04T15:18:31.963379Z

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

😂 1
borkdude 2023-03-04T15:18:48.767879Z

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

2023-03-04T15:34:51.322499Z

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.

2023-03-04T15:36:05.667029Z

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

borkdude 2023-03-04T15:37:49.293609Z

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

👍 1
2023-03-04T15:47:05.295409Z

That’s kind and helpful of you, thanks

borkdude 2023-03-04T16:55:17.442149Z

@nbtheduke Here is how I would approach it: https://github.com/borkdude/ns-parser/blob/main/test/borkdude/ns_parser_test.cljc

borkdude 2023-03-04T16:56:53.533839Z

Similar approach works for edamame

borkdude 2023-03-04T16:57:13.073369Z

Feel free to take it for a spin :)