Fork me on GitHub

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)
        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/insert-right* (n/spaces n-spaces))
        (z/insert-right* new-ns))))


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.


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