Skip to content

Commit

Permalink
Treat uneval nodes as comments
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
tobias committed Feb 18, 2021
1 parent 98fcfc3 commit a323c9a
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 3 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

## Unreleased

* [link TBD]: Properly skip uneval nodes when looking for the first/last sexp

## 2.5.1 (2021-02-16)

### Bugs fixed
Expand Down
7 changes: 4 additions & 3 deletions src/refactor_nrepl/s_expressions.clj
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@
[zipper]
(take-while (complement zip/end?) (iterate zip/next zipper)))

(defn- comment-or-string-or-nil? [zloc]
(defn- comment-or-string-or-uneval-or-nil? [zloc]
(or (nil? zloc)
(= :uneval (zip/tag zloc))
(not (zip/sexpr zloc)) ; comment node
(string? (zip/sexpr zloc))))

Expand All @@ -18,15 +19,15 @@
(let [reader (zip-reader/string-reader file-content)]
(loop [sexp (zip-parser/parse reader)]
(let [zloc (zip/edn sexp)]
(if (and zloc (not (comment-or-string-or-nil? zloc)))
(if (and zloc (not (comment-or-string-or-uneval-or-nil? zloc)))
(zip/string zloc)
(when (.peek-char reader)
(recur (zip-parser/parse reader))))))))

(defn get-last-sexp
^String [file-content]
(let [zloc (->> file-content zip/of-string zip/rightmost)]
(some (fn [zloc] (when-not (comment-or-string-or-nil? zloc)
(some (fn [zloc] (when-not (comment-or-string-or-uneval-or-nil? zloc)
(zip/string zloc)))
(take-while (complement nil?) (iterate zip/left zloc)))))

Expand Down
9 changes: 9 additions & 0 deletions test/refactor_nrepl/s_expressions_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@
#{foo bar baz}
;; some other stuff
(foobar baz)")
(def file-content-with-uneval "#_ foo
(foobar baz)")

(def binding-location [3 8])
(def set-location [7 35])
(def map-location [7 28])
Expand All @@ -33,3 +36,9 @@
(apply sut/get-enclosing-sexp file-content when-not-location)))
(t/is (= nil (sut/get-first-sexp weird-file-content)))
(t/is (= "#{foo bar baz}" (sut/get-first-sexp file-content-with-set))))

(t/deftest get-first-sexp
(t/is (= "(ns resources.testproject.src.com.example.sexp-test)"
(sut/get-first-sexp file-content)))
(t/is (= "(foobar baz)"
(sut/get-first-sexp file-content-with-uneval))))

0 comments on commit a323c9a

Please sign in to comment.