-
Notifications
You must be signed in to change notification settings - Fork 175
/
undef.clj
54 lines (49 loc) · 1.91 KB
/
undef.clj
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
(ns cider.nrepl.middleware.undef
"Middleware for undefining symbols.
Fully qualified symbols are interpreted as a var to be unmapped in its
original namespace, whereas unqualified symbols are interpreted as both a var
and ns alias to be unmapped from the current namespace."
(:require
[cider.nrepl.middleware.util.error-handling :refer [with-safe-transport]]
[orchard.misc :as misc]))
(defn undef
"Undefines a symbol.
When `sym` is unqualified, it is interpreted as both an alias and var to be
unmapped from the namespace `ns`.
When qualified (eg. `foo/bar`), it is interpreted as a var to be unmapped in
the namespace `foo`, which may be an alias in `ns`."
[{:keys [ns sym symbol]}]
(let [ns (misc/as-sym ns)
sym (or sym symbol) ;; for backwards compatibility
[sym-ns sym-name] ((juxt (comp misc/as-sym namespace) misc/name-sym)
(misc/as-sym sym))]
(if sym-ns
;; fully qualified => var in other namespace
(let [other-ns (get (ns-aliases ns) sym-ns sym-ns)]
(ns-unmap other-ns sym-name))
;; unqualified => alias or var in current ns
(do (ns-unalias ns sym-name)
(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)]
;; Do not remove the default java.lang imports, as they are not relinked on the next load
;; see https://github.com/clojure-emacs/cider/issues/3194
(doseq [[sym ref] (ns-map ns)]
(when-not (identical? ref (get clojure.lang.RT/DEFAULT_IMPORTS sym))
(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-all" undef-all-reply))