Hi, it's me again with another sanity check. It seems like a barfed node didn't have its position updated. Check this out...
;123456789012
(-> "[1 [2 3] 4]"
z/of-string
(z/find-value z/next 3)
z/node
meta) ;; => {:row 1, :col 7, :end-row 1, :end-col 8}
All good with the position of the token node "3".
But when I barf forward it...
;123456789012
(-> "[1 [2 3] 4]"
z/of-string
(z/find-value z/next 3)
paredit/barf-forward
((fn [zloc] [(z/root-string zloc) (z/node zloc) (meta (z/node zloc))])))
;; 12345678901
;; => ["[1 [2] 3 4]" <token: 3> {:row 1, :col 7, :end-row 1, :end-col 8}]
The token node 3 moved to :col 8 but it keeps showing :col 7 . WDYT?Hmmm I see. From what I could understand you need to :track-position? true AND retrieve the position using z/position (the meta in the nodes are kept untouched).
;123456789012
(-> "[1 [2 3] 4]"
(z/of-string {:track-position? true})
(z/find-value z/next 3)
paredit/barf-forward
((fn [zloc] [(z/root-string zloc)
(z/node zloc)
(z/position-span zloc)
(meta (z/node zloc))])))
;; 12345678901
;; => ["[1 [2] 3 4]" <token: 3> [[1 8] [1 9]] {:row 1, :col 7, :end-row 1, :end-col 8}]
I've tried :track-position? before but as I was retrieving the position using the meta, it didn't work.
Thanks @lee, appreciate your help 🙇
Out of curiosity: why does your z/node returns a map and mine something between brackets? 🤔Glad you are sorted out @andreribeirocamargo! The position tracking zipper was added after the initial implementation (all before my time), and is optional because it incurs a performance hit. The difference in node printing is probably due to how our particular REPLs choose to print out the nodes. Rewrite-clj nodes override the default print method, which is questionable, but I guess the idea was to have something easy to read (probably along the lines of what folks typically do in Java with toString). Me playing in my REPL:
(-> "foo"
z/of-string
z/node)
;; => {:value foo, :string-value "foo", :map-qualifier nil}
(-> "foo"
z/of-string
z/node
pr-str)
;; => "<token: foo>"Thank you again @lee for tracking down the issue and solving it ❤️ Your bump of tools.reader fixed my issue.
Thanks for following up and trying it out @andreribeirocamargo!
Hiya @andreribeirocamargo! The metadata is only populated at initial parse. Any updates do not affect metadata. If you want a zipper that tracks positions, you need to use a https://cljdoc.org/d/rewrite-clj/rewrite-clj/1.1.49/doc/user-guide#position-tracking.
;123456789012
(-> "[1 [2 3] 4]"
(z/of-string {:track-position? true})
(z/find-value z/next 3)
pe/barf-forward
((fn [zloc] [(z/root-string zloc) (z/node zloc) (z/position-span zloc)])))
;; 12345678901
;; => ["[1 [2] 3 4]" {:value 3, :string-value "3"} [[1 8] [1 9]]]