Skip to content

Commit ef73fb8

Browse files
liquidzbbatsov
authored andcommitted
Add ns-aliases op to the ns middleware
1 parent 8bc1931 commit ef73fb8

File tree

5 files changed

+44
-3
lines changed

5 files changed

+44
-3
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
* [#546](https://github.com/clojure-emacs/cider-nrepl/pull/546): Added support for matcher-combinators to the test middleware.
88
* [#556](https://github.com/clojure-emacs/cider-nrepl/pull/556): Added configuration option for cljfmt to the format middleware.
9+
* [#558](https://github.com/clojure-emacs/cider-nrepl/pull/558): Added the `ns-aliases` op to the ns middleware.
910

1011
### Changes
1112

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,7 @@ Middleware | Op(s) | Description
267267
`wrap-info` | `info/eldoc` | File/line, arglists, docstrings and other metadata for vars.
268268
`wrap-inspect` |`inspect-(start/refresh/pop/push/reset/get-path)` | Inspect a Clojure expression.
269269
`wrap-macroexpand`| `macroexpand/macroexpand-1/macroexpand-all/macroexpand-step` | Macroexpand a Clojure form.
270-
`wrap-ns` | `ns-list/ns-vars/ns-path/ns-load-all` | Namespace browsing & loading.
270+
`wrap-ns` | `ns-list/ns-vars/ns-path/ns-load-all/ns-aliases` | Namespace browsing & loading.
271271
`wrap-spec` | `spec-list/spec-form/spec-example` | Spec browsing.
272272
`wrap-pprint` | | Adds pretty-printing support to code evaluation. It also installs a dummy `pprint-middleware` op. Thus `wrap-pprint` is discoverable through the `describe` op.
273273
`wrap-pprint-fn` | | Provides a common pretty-printing interface for other middlewares that need to perform customisable pretty-printing.

src/cider/nrepl.clj

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -333,7 +333,11 @@
333333
:return {"status" "done" "path" "The path to the file containing ns."}}
334334
"ns-load-all"
335335
{:doc "Loads all project namespaces."
336-
:return {"status" "done" "loaded-ns" "The list of ns that were loaded."}}}}))
336+
:return {"status" "done" "loaded-ns" "The list of ns that were loaded."}}
337+
"ns-aliases"
338+
{:doc "Returns a map of [ns-alias] to [ns-name] in a namespace."
339+
:requires {"ns" "The namespace to use."}
340+
:return {"status" "done" "ns-aliases" "The map of [ns-alias] to [ns-name] in a namespace."}}}}))
337341

338342
(def-wrapper wrap-out cider.nrepl.middleware.out/handle-out
339343
(cljs/expects-piggieback

src/cider/nrepl/middleware/ns.clj

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
(ns cider.nrepl.middleware.ns
2+
(:refer-clojure :exclude [ns-aliases])
23
(:require
34
[cider.nrepl.middleware.util.cljs :as cljs]
45
[cider.nrepl.middleware.util.error-handling :refer [with-safe-transport]]
@@ -96,11 +97,32 @@
9697
[msg]
9798
{:loaded-ns (ns/load-project-namespaces)})
9899

100+
(defn- ns-aliases-clj [ns]
101+
(->> (symbol ns)
102+
clojure.core/ns-aliases
103+
(u/update-vals ns-name)
104+
u/transform-value))
105+
106+
(defn- ns-aliases-cljs [env ns]
107+
(->> (cljs-analysis/ns-aliases env ns)
108+
(remove (fn [[k v]] (= k v)))
109+
(into {})
110+
u/transform-value))
111+
112+
(defn ns-aliases [{:keys [ns] :as msg}]
113+
(if-let [cljs-env (cljs/grab-cljs-env msg)]
114+
(ns-aliases-cljs cljs-env ns)
115+
(ns-aliases-clj ns)))
116+
117+
(defn- ns-aliases-reply [msg]
118+
{:ns-aliases (ns-aliases msg)})
119+
99120
(defn handle-ns [handler msg]
100121
(with-safe-transport handler msg
101122
"ns-list" ns-list-reply
102123
"ns-list-vars-by-name" ns-list-vars-by-name-reply
103124
"ns-vars" ns-vars-reply
104125
"ns-vars-with-meta" ns-vars-with-meta-reply
105126
"ns-path" ns-path-reply
106-
"ns-load-all" ns-load-all-reply))
127+
"ns-load-all" ns-load-all-reply
128+
"ns-aliases" ns-aliases-reply))

test/clj/cider/nrepl/middleware/ns_test.clj

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,12 @@
8181
(is (= (count (ns-list-vars-by-name 'ns-list-vars-by-name-test)) 1))
8282
(is (not (seq (ns-list-vars-by-name 'all-your-base-are-belong-to-us)))))
8383

84+
(deftest ns-aliases-integration-test
85+
(let [aliases (:ns-aliases (session/message {:op "ns-aliases"
86+
:ns "cider.nrepl.middleware.ns-test"}))]
87+
(is (map? aliases))
88+
(is (= (:cider-ns aliases) "cider.nrepl.middleware.ns"))))
89+
8490
(deftest error-handling-test
8591
(testing "ns-list op error handling"
8692
(with-redefs [cider-ns/ns-list (fn [& _] (throw (Exception. "ns-list error")))]
@@ -115,4 +121,12 @@
115121
(is (.startsWith (:err response) "java.lang.Exception: ns-path error"))
116122
(is (= (:ex response) "class java.lang.Exception"))
117123
(is (= (:status response) #{"ns-path-error" "done"}))
124+
(is (:pp-stacktrace response)))))
125+
126+
(testing "ns-aliases op error handling"
127+
(with-redefs [cider-ns/ns-aliases (fn [& _] (throw (Exception. "ns-aliases error")))]
128+
(let [response (session/message {:op "ns-aliases" :name "testing-function"})]
129+
(is (.startsWith (:err response) "java.lang.Exception: ns-aliases error"))
130+
(is (= (:ex response) "class java.lang.Exception"))
131+
(is (= (:status response) #{"ns-aliases-error" "done"}))
118132
(is (:pp-stacktrace response))))))

0 commit comments

Comments
 (0)