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

Add undef-all op #698

Merged
merged 4 commits into from
Apr 23, 2021
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

## master (unreleased)

### New features

* [#698](https://github.com/clojure-emacs/cider-nrepl/pull/698): Add `undef-all` op to undefine all symbols and aliases in namespace

## 0.26.0 (2021-04-22)

### New features
Expand Down
19 changes: 18 additions & 1 deletion doc/modules/ROOT/pages/nrepl-api/ops.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -969,7 +969,7 @@ Required parameters::
{blank}

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


Returns::
Expand Down Expand Up @@ -1179,3 +1179,20 @@ Optional parameters::

Returns::
* `:status` done



=== `undef-all`

Undefine all aliases and symbols in a namespace

Required parameters::
* `:ns` The namespace to operate on


Optional parameters::
{blank}

Returns::
* `:status` done

7 changes: 5 additions & 2 deletions src/cider/nrepl.clj
Original file line number Diff line number Diff line change
Expand Up @@ -473,7 +473,7 @@
(def ops-that-can-eval
"Set of nREPL ops that can lead to code being evaluated."
#{"eval" "load-file" "refresh" "refresh-all" "refresh-clear"
"toggle-trace-var" "toggle-trace-ns" "undef"})
"toggle-trace-var" "toggle-trace-ns" "undef" "undef-all"})

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

(def-wrapper wrap-version cider.nrepl.middleware.version/handle-version
{:doc "Provides CIDER-nREPL version information."
Expand Down
17 changes: 16 additions & 1 deletion src/cider/nrepl/middleware/undef.clj
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,25 @@
(ns-unmap ns sym-name)))
sym))

(defn undef-all
"Undefines all symbol mappings and aliases in the namespace."
[{:keys [ns]}]
(let [ns (misc/as-sym ns)]
(doseq [[sym _] (ns-map ns)]
(ns-unmap ns sym))
(doseq [[sym _] (ns-aliases ns)]
(ns-unalias ns sym))
ns))

(defn undef-reply
[msg]
{:undef (undef msg)})

(defn undef-all-reply
[msg]
{:undef-all (undef-all msg)})

(defn handle-undef [handler msg]
(with-safe-transport handler msg
"undef" undef-reply))
"undef" undef-reply
"undef-all" undef-all-reply))
24 changes: 24 additions & 0 deletions test/clj/cider/nrepl/middleware/undef_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -101,3 +101,27 @@
(is (:pp-stacktrace response))
(is (:err response))
(is (:ex response)))))

(deftest undef-all-test
(testing "undef-all undefines all vars in namespace"
(is (= #{"done"}
(:status (session/message {:op "eval"
:code "(do (ns other.ns (:require [clojure.walk :as walk :refer [postwalk]])))"}))))
(is (= ["#'clojure.core/assoc"]
(:value (session/message {:op "eval"
:code "(do (in-ns 'user) (ns-resolve 'other.ns 'assoc))"}))))
(is (= ["#'clojure.walk/postwalk"]
(:value (session/message {:op "eval"
:code "(ns-resolve 'other.ns 'postwalk)"}))))
(is (= #{"done"}
(:status (session/message {:op "undef-all"
:ns "other.ns"}))))
(is (= ["nil"]
(:value (session/message {:op "eval"
:code "(ns-resolve 'other.ns 'assoc)"}))))
(is (= ["nil"]
(:value (session/message {:op "eval"
:code "(ns-resolve 'other.ns 'postwalk)"}))))
(is (= ["{}"]
(:value (session/message {:op "eval"
:code "(ns-aliases 'other.ns)"}))))))