Skip to content

Commit 34ef5b0

Browse files
committed
[Fix #289] Namespaced keywords vs find (local) symbol
The edge-case handling for local symbols in opt-maps wasn't using a reader that was configured correctly and choked on a namespaced keyword.
1 parent 7cefbaf commit 34ef5b0

File tree

4 files changed

+32
-6
lines changed

4 files changed

+32
-6
lines changed

Diff for: CHANGELOG.md

+2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
## Unreleased
44

5+
### Bugs fixed
6+
* [#289](https://github.com/clojure-emacs/refactor-nrepl/issues/289): Fix an edge-case with involving keywords that caused find-symbol to crash.
57
* [#305](https://github.com/clojure-emacs/refactor-nrepl/issues/305): Don't put `:as` or `:refer` on their own lines in the ns form, when the libspec is so long it causes the line to wrap.
68
* [clojure-emacs/clj-refactor.el#459](https://github.com/clojure-emacs/clj-refactor.el/issues/459): `clean-ns` should conform to the style guide: `(:require` in the ns form should be followed by a newline.
79
* You can opt out via the new `:insert-newline-after-require` configuration option.

Diff for: src/refactor_nrepl/find/find_symbol.clj

+7-4
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@
1010
[s-expressions :as sexp]]
1111
[refactor-nrepl.find.find-macros :refer [find-macro]]
1212
[refactor-nrepl.find.util :as find-util]
13-
[refactor-nrepl.ns.libspecs :as libspecs])
13+
[refactor-nrepl.ns.libspecs :as libspecs]
14+
[clojure.tools.reader :as reader])
1415
(:import (java.io File)))
1516

1617
(def ^:private symbol-regex #"[\w\.:\*\+\-_!\?]+")
@@ -140,12 +141,14 @@
140141

141142
(defn- get&read-enclosing-sexps
142143
[file-content {:keys [^long line-beg ^long col-beg]}]
143-
(binding [*read-eval* false]
144+
(binding [*read-eval* false
145+
clojure.tools.reader/*alias-map*
146+
(ns-aliases (core/ns-from-string file-content))]
144147
(let [line (dec line-beg)
145148
encl-sexp-level1 (or (sexp/get-enclosing-sexp file-content line col-beg) "")
146149
encl-sexp-level2 (or (sexp/get-enclosing-sexp file-content line col-beg 2) "")]
147-
[encl-sexp-level1 (read-string encl-sexp-level1)
148-
encl-sexp-level2 (read-string encl-sexp-level2)])))
150+
[encl-sexp-level1 (reader/read-string encl-sexp-level1)
151+
encl-sexp-level2 (reader/read-string encl-sexp-level2)])))
149152

150153
(defn- optmap-with-default?
151154
[var-name _file-content [_ [_ level1-form _ level2-form]]]

Diff for: test/refactor_nrepl/integration_tests.clj

+14-2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
[clojure.test :refer :all]
44
[nrepl.server :as nrepl]
55
[refactor-nrepl middleware
6+
[analyzer :as analyzer]
67
[client :refer :all]
78
[core :as core]]
89
[clojure.string :as str])
@@ -58,11 +59,11 @@
5859
(defn ns-ast-throw-error-for-five [^String content]
5960
(if (.contains content "com.example.five")
6061
(throw (IllegalThreadStateException. "FAILED!"))
61-
(#'refactor-nrepl.analyzer/cachable-ast content)))
62+
(#'analyzer/cachable-ast content)))
6263

6364
(deftest test-find-two-foo-errors-ignored
6465
(with-testproject-on-classpath
65-
(with-redefs [refactor-nrepl.analyzer/ns-ast ns-ast-throw-error-for-five]
66+
(with-redefs [analyzer/ns-ast ns-ast-throw-error-for-five]
6667
(let [transport (connect :port 7777)
6768
response (find-usages :transport transport :ns 'com.example.two
6869
:file (str test-project-dir "/src/com/example/one.clj")
@@ -206,6 +207,17 @@
206207
result (remove keyword? response)]
207208
(is (= 3 (count result)) (format "expected 3 results but got %d" (count result))))))
208209

210+
(when-let [{:keys [minor major]} *clojure-version*]
211+
(and (>= major 1)
212+
(>= minor 9))
213+
(deftest find-local-in-namespaced-destructuring
214+
(with-testproject-on-classpath
215+
(let [five-file (str test-project-dir "/src/com/example/five.clj")
216+
transport (connect :port 7777)
217+
response (find-usages :transport transport :name "foo" :file five-file :line 68 :column 14)
218+
result (remove keyword? response)]
219+
(is (= 2 (count result)) (format "expected 3 results but got %d" (count result)))))))
220+
209221
(deftest test-find-used-locals
210222
(with-testproject-on-classpath
211223
(let [five-file (str test-project-dir "/src/com/example/five.clj")

Diff for: test/resources/testproject/src/com/example/five.clj

+9
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,15 @@
5858
[:bar :foo]
5959
(count foo)))
6060

61+
(when-let [{:keys [minor major]} *clojure-version*]
62+
(and (>= major 1)
63+
(>= minor 9))
64+
(defn fn-with-let-with-namespaced-keyword-destructuring []
65+
;; https://github.com/clojure-emacs/refactor-nrepl/issues/289
66+
(let [{::str/keys [foo bar]} (hash-map)]
67+
[:bar :foo]
68+
(count foo))))
69+
6170
;; This was causing both find-local-symbol and find-macros to blow up, for
6271
;; different reasons
6372
::str/bar

0 commit comments

Comments
 (0)