Skip to content

Commit 48a7041

Browse files
committed
Treat uneval nodes as comments
Uneval nodes throw an UnsupportedOperationException if node/sexpr is called on them, which will break get-first-sexp if it is given file-content that starts with `#_ whatever`. This includes uneval nodes in the set of nodes we should skip when looking for first & last sexps.
1 parent 98fcfc3 commit 48a7041

File tree

3 files changed

+15
-3
lines changed

3 files changed

+15
-3
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
## Unreleased
44

5+
* [#294](https://github.com/clojure-emacs/refactor-nrepl/pull/294): Properly skip uneval nodes when looking for the first/last sexp
6+
57
## 2.5.1 (2021-02-16)
68

79
### Bugs fixed

src/refactor_nrepl/s_expressions.clj

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,9 @@
88
[zipper]
99
(take-while (complement zip/end?) (iterate zip/next zipper)))
1010

11-
(defn- comment-or-string-or-nil? [zloc]
11+
(defn- comment-or-string-or-uneval-or-nil? [zloc]
1212
(or (nil? zloc)
13+
(= :uneval (zip/tag zloc))
1314
(not (zip/sexpr zloc)) ; comment node
1415
(string? (zip/sexpr zloc))))
1516

@@ -18,15 +19,15 @@
1819
(let [reader (zip-reader/string-reader file-content)]
1920
(loop [sexp (zip-parser/parse reader)]
2021
(let [zloc (zip/edn sexp)]
21-
(if (and zloc (not (comment-or-string-or-nil? zloc)))
22+
(if (and zloc (not (comment-or-string-or-uneval-or-nil? zloc)))
2223
(zip/string zloc)
2324
(when (.peek-char reader)
2425
(recur (zip-parser/parse reader))))))))
2526

2627
(defn get-last-sexp
2728
^String [file-content]
2829
(let [zloc (->> file-content zip/of-string zip/rightmost)]
29-
(some (fn [zloc] (when-not (comment-or-string-or-nil? zloc)
30+
(some (fn [zloc] (when-not (comment-or-string-or-uneval-or-nil? zloc)
3031
(zip/string zloc)))
3132
(take-while (complement nil?) (iterate zip/left zloc)))))
3233

test/refactor_nrepl/s_expressions_test.clj

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@
1010
#{foo bar baz}
1111
;; some other stuff
1212
(foobar baz)")
13+
(def file-content-with-uneval "#_ foo
14+
(foobar baz)")
15+
1316
(def binding-location [3 8])
1417
(def set-location [7 35])
1518
(def map-location [7 28])
@@ -33,3 +36,9 @@
3336
(apply sut/get-enclosing-sexp file-content when-not-location)))
3437
(t/is (= nil (sut/get-first-sexp weird-file-content)))
3538
(t/is (= "#{foo bar baz}" (sut/get-first-sexp file-content-with-set))))
39+
40+
(t/deftest get-first-sexp
41+
(t/is (= "(ns resources.testproject.src.com.example.sexp-test)"
42+
(sut/get-first-sexp file-content)))
43+
(t/is (= "(foobar baz)"
44+
(sut/get-first-sexp file-content-with-uneval))))

0 commit comments

Comments
 (0)