Skip to content

Commit 6fe5b4b

Browse files
authored
Merge pull request #174 from mainej/root-node-span
Fix end position on root node of untracked zipper
2 parents 01dc410 + 2b9ef28 commit 6fe5b4b

File tree

3 files changed

+29
-7
lines changed

3 files changed

+29
-7
lines changed

src/rewrite_clj/parser.cljc

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,11 @@
2323
"Parse all forms from the given reader."
2424
[#?(:cljs ^not-native reader :default reader)]
2525
(let [nodes (->> (repeatedly #(parse reader))
26-
(take-while identity)
27-
(doall))]
28-
(with-meta
29-
(nforms/forms-node nodes)
30-
(meta (first nodes)))))
26+
(take-while identity))
27+
position-meta (merge (meta (first nodes))
28+
(select-keys (meta (last nodes))
29+
[:end-row :end-col]))]
30+
(with-meta (nforms/forms-node nodes) position-meta)))
3131

3232
;; ## Specialized Parsers
3333

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)