Skip to content

Commit c91d065

Browse files
authored
Add undef-all op (#698)
1 parent e6f18dd commit c91d065

File tree

5 files changed

+67
-4
lines changed

5 files changed

+67
-4
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
## master (unreleased)
44

5+
### New features
6+
7+
* [#698](https://github.com/clojure-emacs/cider-nrepl/pull/698): Add `undef-all` op to undefine all symbols and aliases in namespace
8+
59
## 0.26.0 (2021-04-22)
610

711
### New features

doc/modules/ROOT/pages/nrepl-api/ops.adoc

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -969,7 +969,7 @@ Required parameters::
969969
{blank}
970970

971971
Optional parameters::
972-
* `:filter-regex` Only the specs that matches filter prefix regex will be returned
972+
* `:filter-regex` Only the specs that matches filter prefix regex will be returned
973973

974974

975975
Returns::
@@ -1179,3 +1179,20 @@ Optional parameters::
11791179

11801180
Returns::
11811181
* `:status` done
1182+
1183+
1184+
1185+
=== `undef-all`
1186+
1187+
Undefine all aliases and symbols in a namespace
1188+
1189+
Required parameters::
1190+
* `:ns` The namespace to operate on
1191+
1192+
1193+
Optional parameters::
1194+
{blank}
1195+
1196+
Returns::
1197+
* `:status` done
1198+

src/cider/nrepl.clj

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -473,7 +473,7 @@
473473
(def ops-that-can-eval
474474
"Set of nREPL ops that can lead to code being evaluated."
475475
#{"eval" "load-file" "refresh" "refresh-all" "refresh-clear"
476-
"toggle-trace-var" "toggle-trace-ns" "undef"})
476+
"toggle-trace-var" "toggle-trace-ns" "undef" "undef-all"})
477477

478478
(def-wrapper wrap-tracker cider.nrepl.middleware.track-state/handle-tracker
479479
ops-that-can-eval
@@ -492,7 +492,10 @@
492492
{"undef" {:doc "Undefine a symbol"
493493
:requires {"sym" "The symbol to undefine"
494494
"ns" "The namespace is which to resolve sym (falls back to *ns* if not specified)"}
495-
:returns {"status" "done"}}}})
495+
:returns {"status" "done"}}
496+
"undef-all" {:doc "Undefine all aliases and symbols in a namespace"
497+
:requires {"ns" "The namespace to operate on"}
498+
:returns {"status" "done"}}}})
496499

497500
(def-wrapper wrap-version cider.nrepl.middleware.version/handle-version
498501
{:doc "Provides CIDER-nREPL version information."

src/cider/nrepl/middleware/undef.clj

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,25 @@
2727
(ns-unmap ns sym-name)))
2828
sym))
2929

30+
(defn undef-all
31+
"Undefines all symbol mappings and aliases in the namespace."
32+
[{:keys [ns]}]
33+
(let [ns (misc/as-sym ns)]
34+
(doseq [[sym _] (ns-map ns)]
35+
(ns-unmap ns sym))
36+
(doseq [[sym _] (ns-aliases ns)]
37+
(ns-unalias ns sym))
38+
ns))
39+
3040
(defn undef-reply
3141
[msg]
3242
{:undef (undef msg)})
3343

44+
(defn undef-all-reply
45+
[msg]
46+
{:undef-all (undef-all msg)})
47+
3448
(defn handle-undef [handler msg]
3549
(with-safe-transport handler msg
36-
"undef" undef-reply))
50+
"undef" undef-reply
51+
"undef-all" undef-all-reply))

test/clj/cider/nrepl/middleware/undef_test.clj

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,3 +101,27 @@
101101
(is (:pp-stacktrace response))
102102
(is (:err response))
103103
(is (:ex response)))))
104+
105+
(deftest undef-all-test
106+
(testing "undef-all undefines all vars in namespace"
107+
(is (= #{"done"}
108+
(:status (session/message {:op "eval"
109+
:code "(do (ns other.ns (:require [clojure.walk :as walk :refer [postwalk]])))"}))))
110+
(is (= ["#'clojure.core/assoc"]
111+
(:value (session/message {:op "eval"
112+
:code "(do (in-ns 'user) (ns-resolve 'other.ns 'assoc))"}))))
113+
(is (= ["#'clojure.walk/postwalk"]
114+
(:value (session/message {:op "eval"
115+
:code "(ns-resolve 'other.ns 'postwalk)"}))))
116+
(is (= #{"done"}
117+
(:status (session/message {:op "undef-all"
118+
:ns "other.ns"}))))
119+
(is (= ["nil"]
120+
(:value (session/message {:op "eval"
121+
:code "(ns-resolve 'other.ns 'assoc)"}))))
122+
(is (= ["nil"]
123+
(:value (session/message {:op "eval"
124+
:code "(ns-resolve 'other.ns 'postwalk)"}))))
125+
(is (= ["{}"]
126+
(:value (session/message {:op "eval"
127+
:code "(ns-aliases 'other.ns)"}))))))

0 commit comments

Comments
 (0)