Skip to content

Commit

Permalink
Add ns-aliases op to the ns middleware
Browse files Browse the repository at this point in the history
  • Loading branch information
liquidz committed Oct 30, 2018
1 parent 8bc1931 commit 6add52d
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

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

### Changes

Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@ Middleware | Op(s) | Description
`wrap-info` | `info/eldoc` | File/line, arglists, docstrings and other metadata for vars.
`wrap-inspect` |`inspect-(start/refresh/pop/push/reset/get-path)` | Inspect a Clojure expression.
`wrap-macroexpand`| `macroexpand/macroexpand-1/macroexpand-all/macroexpand-step` | Macroexpand a Clojure form.
`wrap-ns` | `ns-list/ns-vars/ns-path/ns-load-all` | Namespace browsing & loading.
`wrap-ns` | `ns-list/ns-vars/ns-path/ns-load-all/ns-aliases` | Namespace browsing & loading.
`wrap-spec` | `spec-list/spec-form/spec-example` | Spec browsing.
`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.
`wrap-pprint-fn` | | Provides a common pretty-printing interface for other middlewares that need to perform customisable pretty-printing.
Expand Down
6 changes: 5 additions & 1 deletion src/cider/nrepl.clj
Original file line number Diff line number Diff line change
Expand Up @@ -333,7 +333,11 @@
:return {"status" "done" "path" "The path to the file containing ns."}}
"ns-load-all"
{:doc "Loads all project namespaces."
:return {"status" "done" "loaded-ns" "The list of ns that were loaded."}}}}))
:return {"status" "done" "loaded-ns" "The list of ns that were loaded."}}
"ns-aliases"
{:doc "Returns a map of [ns-alias] to [ns-name] in a namespace."
:requires {"ns" "The namespace to use."}
:return {"status" "done" "ns-aliases" "The map of [ns-alias] to [ns-name] in a namespace."}}}}))

(def-wrapper wrap-out cider.nrepl.middleware.out/handle-out
(cljs/expects-piggieback
Expand Down
24 changes: 23 additions & 1 deletion src/cider/nrepl/middleware/ns.clj
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
(ns cider.nrepl.middleware.ns
(:refer-clojure :exclude [ns-aliases])
(:require
[cider.nrepl.middleware.util.cljs :as cljs]
[cider.nrepl.middleware.util.error-handling :refer [with-safe-transport]]
Expand Down Expand Up @@ -96,11 +97,32 @@
[msg]
{:loaded-ns (ns/load-project-namespaces)})

(defn- ns-aliases-clj [ns]
(->> (symbol ns)
clojure.core/ns-aliases
(u/update-vals ns-name)
u/transform-value))

(defn- ns-aliases-cljs [env ns]
(->> (cljs-analysis/ns-aliases env ns)
(remove (fn [[k v]] (= k v)))
(into {})
u/transform-value))

(defn ns-aliases [{:keys [ns] :as msg}]
(if-let [cljs-env (cljs/grab-cljs-env msg)]
(ns-aliases-cljs cljs-env ns)
(ns-aliases-clj ns)))

(defn- ns-aliases-reply [msg]
{:ns-aliases (ns-aliases msg)})

(defn handle-ns [handler msg]
(with-safe-transport handler msg
"ns-list" ns-list-reply
"ns-list-vars-by-name" ns-list-vars-by-name-reply
"ns-vars" ns-vars-reply
"ns-vars-with-meta" ns-vars-with-meta-reply
"ns-path" ns-path-reply
"ns-load-all" ns-load-all-reply))
"ns-load-all" ns-load-all-reply
"ns-aliases" ns-aliases-reply))
14 changes: 14 additions & 0 deletions test/clj/cider/nrepl/middleware/ns_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,12 @@
(is (= (count (ns-list-vars-by-name 'ns-list-vars-by-name-test)) 1))
(is (not (seq (ns-list-vars-by-name 'all-your-base-are-belong-to-us)))))

(deftest ns-aliases-integration-test
(let [aliases (:ns-aliases (session/message {:op "ns-aliases"
:ns "cider.nrepl.middleware.ns-test"}))]
(is (map? aliases))
(is (= (:cider-ns aliases) "cider.nrepl.middleware.ns"))))

(deftest error-handling-test
(testing "ns-list op error handling"
(with-redefs [cider-ns/ns-list (fn [& _] (throw (Exception. "ns-list error")))]
Expand Down Expand Up @@ -115,4 +121,12 @@
(is (.startsWith (:err response) "java.lang.Exception: ns-path error"))
(is (= (:ex response) "class java.lang.Exception"))
(is (= (:status response) #{"ns-path-error" "done"}))
(is (:pp-stacktrace response)))))

(testing "ns-aliases op error handling"
(with-redefs [cider-ns/ns-aliases (fn [& _] (throw (Exception. "ns-aliases error")))]
(let [response (session/message {:op "ns-aliases" :name "testing-function"})]
(is (.startsWith (:err response) "java.lang.Exception: ns-aliases error"))
(is (= (:ex response) "class java.lang.Exception"))
(is (= (:status response) #{"ns-aliases-error" "done"}))
(is (:pp-stacktrace response))))))

0 comments on commit 6add52d

Please sign in to comment.