Skip to content

Commit 3839db7

Browse files
committed
Fix clean-ns with namespaced keywords
If a keyword like ::prefix/kw occurred in the file the namespace aliased as `prefix` would be pruned. This closes clojure-emacs/clj-refactor.el#330
1 parent 2e26d7f commit 3839db7

File tree

5 files changed

+27
-6
lines changed

5 files changed

+27
-6
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
### Bugs fixed
66

7+
* [clojure-emacs/clj-refactor.el#330](https://github.com/clojure-emacs/clj-refactor.el/issues/330) `clean-ns` ignores namespaced keywords.
78
* [#160](https://github.com/clojure-emacs/refactor-nrepl/issues/160) Make `resolve-missing` find newly defined vars and types (clj). Because of a stale cache, newly added vars or types would not be found. This fix takes into account vars/types added by eval-ing code (rescan affected namespace), and by hotloading dependencies (reset the cache).
89

910
### New features

src/refactor_nrepl/core.clj

+8-2
Original file line numberDiff line numberDiff line change
@@ -203,10 +203,11 @@
203203
(defn prefix
204204
"java.util.Date -> java.util
205205
206-
clojure.walk/walk -> clojure.walk"
206+
clojure.walk/walk -> clojure.walk
207+
:clojure.core/kw -> kw"
207208
[fully-qualified-name]
208209
(if(re-find #"/" (str fully-qualified-name))
209-
(-> fully-qualified-name str (.split "/") first)
210+
(-> fully-qualified-name str (.split "/") first (str/replace #"^:" ""))
210211
(let [parts (-> fully-qualified-name str (.split "\\.") butlast)]
211212
(when (seq parts)
212213
(str/join "." parts)))))
@@ -320,3 +321,8 @@
320321
(defn normalize-var-name
321322
[sym-str]
322323
(str/replace sym-str #"#'.*/" ""))
324+
325+
(defn fully-qualified?
326+
[symbol-or-keyword]
327+
(when (prefix symbol-or-keyword)
328+
symbol-or-keyword))

src/refactor_nrepl/find/symbols_in_file.clj

+13-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
(ns refactor-nrepl.find.symbols-in-file
2-
(:require [clojure.tools.reader :as reader]
2+
(:require [clojure
3+
[walk :as walk]]
4+
[clojure.tools.reader :as reader]
35
[clojure.tools.reader.reader-types :as readers]
4-
[clojure.walk :as walk]
56
[refactor-nrepl
67
[core :as core]
78
[util :as util]]))
@@ -37,6 +38,11 @@
3738
This includes all regular symbols like foo, but also ctor calls like
3839
Foo. (returned as Foo), and classes used in typehints.
3940
41+
Note: Because it was convenient at the time this function also
42+
returns fully qualified keywords mapped to symbol. This means that
43+
if the file contains `:prefix/kw` the symbol
44+
`fully.resolved.prefix/kw` is included in the set.
45+
4046
Dialect defaults to :clj."
4147
([path parsed-ns] (symbols-in-file path parsed-ns :clj))
4248
([path parsed-ns dialect]
@@ -55,6 +61,11 @@
5561
;; Classes used in typehints
5662
(when-let [t (:tag (meta form))]
5763
(swap! syms conj t))
64+
(when (and (keyword? form)
65+
(core/fully-qualified? form))
66+
(swap! syms conj
67+
(symbol (core/prefix form)
68+
(core/suffix form))))
5869
form)]
5970
(loop [form (reader/read rdr-opts rdr)]
6071
(when (not= form :eof)

src/refactor_nrepl/ns/prune_dependencies.clj

+4-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44
[core :as core]
55
[util :as util]]
66
[refactor-nrepl.find.symbols-in-file :as symbols-in-file]
7-
[refactor-nrepl.config :as config]))
7+
[refactor-nrepl.config :as config]
8+
[refactor-nrepl.s-expressions :as sexp]))
89

910
(defn- lookup-symbol-ns
1011
([ns symbol]
@@ -166,3 +167,5 @@
166167
(prune-cljc-dependencies parsed-ns path)
167168
(prune-clj-or-cljs-dependencies parsed-ns path dialect))
168169
{:source-dialect dialect})))
170+
171+
::sexp/foo

test/resources/ns1.clj

+1-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@
4242
(compose-fixtures)
4343
(clojure.test.junit/with-junit-output "")
4444
(escape)
45-
(inst/read-instant-date)
45+
::inst/namespaced-keyword-prevents-pruning
4646
(clojure.data/diff)
4747
(clojure.edn/read-string)
4848
(clojure.xml/emit "")

0 commit comments

Comments
 (0)