Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: clojure-emacs/cider-nrepl
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: master
Choose a base ref
...
head repository: yuhan0/cider-nrepl
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: undef-all
Choose a head ref
Can’t automatically merge. Don’t worry, you can still create the pull request.
  • 4 commits
  • 5 files changed
  • 1 contributor

Commits on Apr 23, 2021

  1. Add undef-all op

    yuhan0 committed Apr 23, 2021
    Copy the full SHA
    a001267 View commit details
  2. Add tests for undef-all

    yuhan0 committed Apr 23, 2021
    Copy the full SHA
    1a70aaa View commit details
  3. Update changelog

    yuhan0 committed Apr 23, 2021
    Copy the full SHA
    6af746d View commit details
  4. Regenerate docs

    yuhan0 committed Apr 23, 2021
    Copy the full SHA
    cb2c430 View commit details
Showing with 67 additions and 4 deletions.
  1. +4 −0 CHANGELOG.md
  2. +18 −1 doc/modules/ROOT/pages/nrepl-api/ops.adoc
  3. +5 −2 src/cider/nrepl.clj
  4. +16 −1 src/cider/nrepl/middleware/undef.clj
  5. +24 −0 test/clj/cider/nrepl/middleware/undef_test.clj
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
19 changes: 18 additions & 1 deletion doc/modules/ROOT/pages/nrepl-api/ops.adoc
Original file line number Diff line number Diff line change
@@ -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::
@@ -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
@@ -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
@@ -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."
17 changes: 16 additions & 1 deletion src/cider/nrepl/middleware/undef.clj
Original file line number Diff line number Diff line change
@@ -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
@@ -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)"}))))))