This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2023-11-27
Channels
- # announcements (4)
- # beginners (41)
- # biff (8)
- # cider (14)
- # clj-kondo (5)
- # clojure (45)
- # clojure-brasil (1)
- # clojure-europe (20)
- # clojure-nl (1)
- # clojure-norway (30)
- # clojure-uk (10)
- # clojurescript (8)
- # cursive (25)
- # datomic (20)
- # emacs (11)
- # events (1)
- # hoplon (9)
- # humbleui (7)
- # hyperfiddle (6)
- # lsp (63)
- # matrix (1)
- # observability (20)
- # off-topic (36)
- # polylith (11)
- # re-frame (2)
- # releases (1)
- # rewrite-clj (6)
- # scittle (42)
- # sql (6)
- # squint (86)
- # tools-deps (9)
Hi 👋 With the zipper API, how would I find the last item in a form? E.g. in this
(-> zloc
(z/find-value z/next 'ns)
(z/find-value z/next :require)
z/up
to-end) ;; I found to-end in the docs as an example but it seems to be doing something else :)
Maybe z/rightmost
would help you?
(require '[rewrite-clj.zip :as z])
(def zloc (z/of-string "[[1 2 3] [4 5 6] [7 8 9]]"))
(-> zloc
z/rightmost
z/string)
;; => "[[1 2 3] [4 5 6] [7 8 9]]"
(-> zloc
z/down
z/rightmost
z/string)
;; => "[7 8 9]"
(-> zloc
z/down
z/rightmost
z/down
z/rightmost
z/string)
;; => "9"
Also is there a way to use append-child
to append a linebreak? 🙂
Maybe insert-newline-right
or insert-newline-left
would work for you.
(require '[rewrite-clj.zip :as z])
(def zloc (z/of-string "[[1 2 3] [4 5 6] [7 8 9]]"))
(-> zloc
z/down
z/right
z/insert-newline-right
z/print-root)
Prints:
[[1 2 3] [4 5 6]
[7 8 9]]
But if you feel you need to append a child, that's fine. But use append-child*
instead so that extra whitespace won't be added.
(require '[rewrite-clj.node :as n]
'[rewrite-clj.zip :as z])
(def zloc (z/of-string "[[1 2 3] [4 5 6] [7 8 9]]"))
(-> zloc
z/down
(z/append-child* (n/newlines 1))
z/print-root)
Prints:
[[1 2 3
] [4 5 6] [7 8 9]]