Skip to content

Commit ba3ea89

Browse files
committed
Fix end position on root node of untracked zipper
The end position of the root node of a parsed string was inherited from the first child. This meant that the root node looked like it spanned only one child, instead of all of its children. This fix makes the root node take its end position from its last child. After this fix, the end position of an untracked zipper (which uses the parser's data) matches the end position of a tracked zipper (which uses node extents).
1 parent e20cbf0 commit ba3ea89

File tree

3 files changed

+26
-3
lines changed

3 files changed

+26
-3
lines changed

src/rewrite_clj/parser.cljc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@
2727
(doall))]
2828
(with-meta
2929
(nforms/forms-node nodes)
30-
(meta (first nodes)))))
30+
(merge (meta (first nodes))
31+
(select-keys (meta (last nodes)) [:end-row :end-col])))))
3132

3233
;; ## Specialized Parsers
3334

test/rewrite_clj/parser_test.cljc

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -541,7 +541,18 @@
541541
[2 4] [2 5] :token "x" 'x
542542
[3 3] [3 14] :list "(println x)" '(println x)
543543
[3 4] [3 11] :token "println" 'println
544-
[3 12] [3 13] :token "x" 'x)))
544+
[3 12] [3 13] :token "x" 'x))
545+
;; root node
546+
(let [s (str
547+
;1234567890
548+
"(def a 1)\n"
549+
"(def b\n"
550+
" 2)")
551+
n (p/parse-string-all s)
552+
start-pos ((juxt :row :col) (meta n))
553+
end-pos ((juxt :end-row :end-col) (meta n))]
554+
(is (= [1 1] start-pos))
555+
(is (= [3 5] end-pos))))
545556

546557

547558
(deftest t-os-specific-line-endings

test/rewrite_clj/zip_test.cljc

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,18 @@
5858
[2 4] [2 5] :token "x" 'x
5959
[3 3] [3 14] :list "(println x)" '(println x)
6060
[3 4] [3 11] :token "println" 'println
61-
[3 12] [3 13] :token "x" 'x)))
61+
[3 12] [3 13] :token "x" 'x))
62+
;; root node
63+
(let [s (str
64+
;1234567890
65+
"(def a 1)\n"
66+
"(def b\n"
67+
" 2)")
68+
[start-pos end-pos] (-> (z/of-string s {:track-position? true})
69+
z/up
70+
z/position-span)]
71+
(is (= [1 1] start-pos))
72+
(is (= [3 5] end-pos))))
6273

6374
(deftest namespaced-keywords
6475
(is (= ":dill" (-> ":dill" z/of-string z/root-string)))

0 commit comments

Comments
 (0)