Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Treat uneval nodes as comments #294

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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

* [#294](https://github.com/clojure-emacs/refactor-nrepl/pull/294): 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))))