rewrite-clj

Ken Huang 2021-08-04T17:13:16.000200Z

lread 2021-08-04T19:21:07.001Z

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

lread 2021-08-04T19:23:39.002800Z

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

lread 2021-08-04T21:12:56.012Z

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.

👍 1