rewrite-clj

orestis 2022-01-26T07:15:26.008900Z

Thanks @lee, I ended up with this:

(defn add-namespace [zloc new-ns insert-loc-fn]
  (let [require-loc (-> zloc
                        (z/find-value z/next 'ns)
                        (z/find-value z/next :require)
                        (z/up))
        insert-loc (insert-loc-fn require-loc)
        n-spaces (-> insert-loc z/node meta :col dec)]
    (-> insert-loc
        (z/insert-right* (n/newline-node "\n"))
        (z/right*)
        (z/insert-right* (n/spaces n-spaces))
        (z/right*)
        (z/insert-right* new-ns))))

orestis 2022-01-26T07:16:44.010200Z

In my case, I wanted the new form to not be inserted at the end, but rather next to a similar ns, hence the insert-loc-fn that operates on the (:require ...) node. I also had to dec the spaces since :col is the start of the first character of the form.

orestis 2022-01-26T07:17:25.010700Z

I also decided to move the cursor instead of doing the insertions in reverse order.

lread 2022-01-26T14:32:43.012Z

@orestis, nice!