Fork me on GitHub
#rewrite-clj
<
2021-08-04
>
lread19:08:07

Hi @whatacold! I’ll have a peek and get back to you!

lread19:08:39

(Maybe interesting aside: Your sample reminded me of https://github.com/lread/test-doc-blocks, which is not at all what you are looking for or asking about, but will test code snippits found in documentation)

lread21:08:56

So I had a peek @whatacold. The following might (?) be tripping you up: • z/right will return nil when there is no next sibling • z/end? will return true when at (not past) the last node (https://github.com/clj-commons/rewrite-clj/issues/155). It will also return true for a nil input. What do you think of this instead?

(loop [zloc zloc]
  (let [zloc (some-> zloc
                     (z/insert-right* (n/comment-node "; test"))
                     (z/insert-right* (n/newlines 1)))
        next-sib (z/right zloc)]
    (if next-sib
      (recur next-sib)
      (z/print-root zloc))))
Outputs:
(defn my-function [a]
  (* a 3))
;; test

(my-function 7)
;; test
Notes: • I switched to using node creation functions, a personal preference which I find more explicit for this use case. • Switched from insert-right to insert-right*. The * version does no extra automagic whitespace handling which is what you’d want when inserting whitespace/comment nodes, I think.

👍 3