diff --git a/CHANGELOG.md b/CHANGELOG.md index 391b87a1..6a0f37e4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/src/refactor_nrepl/s_expressions.clj b/src/refactor_nrepl/s_expressions.clj index 11313ad0..71e313cd 100644 --- a/src/refactor_nrepl/s_expressions.clj +++ b/src/refactor_nrepl/s_expressions.clj @@ -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)))) @@ -18,7 +19,7 @@ (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)))))))) @@ -26,7 +27,7 @@ (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))))) diff --git a/test/refactor_nrepl/s_expressions_test.clj b/test/refactor_nrepl/s_expressions_test.clj index 4bf0c71a..2a22407d 100644 --- a/test/refactor_nrepl/s_expressions_test.clj +++ b/test/refactor_nrepl/s_expressions_test.clj @@ -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]) @@ -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))))