Skip to content

Commit

Permalink
Add a option for including private vars to the ns-vars middleware
Browse files Browse the repository at this point in the history
  • Loading branch information
liquidz committed Mar 27, 2019
1 parent 2249b55 commit 3608c07
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 34 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,14 @@

## master (unreleased)

### New features

* FIXME: Added a option for including private vars to the ns-vars middleware.

### Bugs fixed

* FIXME: Fix `ns-vars-with-meta` to return public vars.

## 0.21.1 (2019-02-15)

### New features
Expand Down
6 changes: 4 additions & 2 deletions src/cider/nrepl.clj
Original file line number Diff line number Diff line change
Expand Up @@ -268,10 +268,12 @@
"ns-vars"
{:doc "Returns a sorted list of all vars in a namespace."
:requires {"ns" "The namespace to browse."}
:returns {"status" "done" "ns-vars" "The sorted list of all vars in a namespace."}}
:optional {"include-privates?" "If logical true, the result list will include private vars in a namespace."}
:returns {"status" "done" "ns-vars" "The sorted list of public vars in a namespace."}}
"ns-vars-with-meta"
{:doc "Returns a map of [var-name] to [var-metadata] for all vars in a namespace."
{:doc "Returns a map of [var-name] to [var-metadata] for public vars in a namespace."
:requires {"ns" "The namespace to use."}
:optional {"include-privates?" "If logical true, the result map will include [var-name] and [var-metadata] pairs for private vars in a namespace."}
:returns {"status" "done" "ns-vars-with-meta" "The map of [var-name] to [var-metadata] for all vars in a namespace."}}
"ns-path"
{:doc "Returns the path to the file containing ns."
Expand Down
76 changes: 44 additions & 32 deletions src/cider/nrepl/middleware/ns.clj
Original file line number Diff line number Diff line change
Expand Up @@ -17,39 +17,51 @@
(filter #(= (first %) name))
(map second)))

(defn ns-vars-clj [ns]
(->> (symbol ns)
ns-publics
keys
(map name)
sort))

(defn ns-vars-with-meta-clj [ns]
(->> (symbol ns)
ns-interns
(u/update-vals (comp um/relevant-meta meta))
(u/update-keys name)
(into (sorted-map))))
(defn ns-vars-clj [ns & [include-privates?]]
(let [fetch-vars (if include-privates?
ns-interns
ns-publics)]
(->> (symbol ns)
fetch-vars
keys
(map name)
sort)))

(defn ns-vars-with-meta-clj [ns & [include-privates?]]
(let [fetch-vars (if include-privates?
ns-interns
ns-publics)]
(->> (symbol ns)
fetch-vars
(u/update-vals (comp um/relevant-meta meta))
(u/update-keys name)
(into (sorted-map)))))

(defn ns-list-cljs [env]
(->> (cljs-analysis/all-ns env)
keys
(map name)
sort))

(defn ns-vars-cljs [env ns]
(->> (symbol ns)
(cljs-analysis/public-vars env)
keys
(map name)
sort))

(defn ns-vars-with-meta-cljs [env ns]
(->> (symbol ns)
(cljs-analysis/public-vars env)
(u/update-vals (comp um/relevant-meta :meta))
(u/update-keys name)
(into (sorted-map))))
(defn ns-vars-cljs [env ns & [include-privates?]]
(let [fetch-vars (if include-privates?
(partial cljs-analysis/ns-interns-from-env env)
(partial cljs-analysis/public-vars env))]
(->> (symbol ns)
fetch-vars
keys
(map name)
sort)))

(defn ns-vars-with-meta-cljs [env ns & [include-privates?]]
(let [fetch-vars (if include-privates?
(partial cljs-analysis/ns-interns-from-env env)
(partial cljs-analysis/public-vars env))]
(->> (symbol ns)
fetch-vars
(u/update-vals (comp um/relevant-meta :meta))
(u/update-keys name)
(into (sorted-map)))))

(defn ns-path-cljs [env ns]
(->> (symbol ns)
Expand All @@ -61,15 +73,15 @@
(ns-list-cljs cljs-env)
(ns/loaded-namespaces filter-regexps)))

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

(defn ns-vars-with-meta [{:keys [ns] :as msg}]
(defn ns-vars-with-meta [{:keys [ns include-privates?] :as msg}]
(if-let [cljs-env (cljs/grab-cljs-env msg)]
(ns-vars-with-meta-cljs cljs-env ns)
(ns-vars-with-meta-clj ns)))
(ns-vars-with-meta-cljs cljs-env ns include-privates?)
(ns-vars-with-meta-clj ns include-privates?)))

(defn ns-path [{:keys [ns] :as msg}]
(if-let [cljs-env (cljs/grab-cljs-env msg)]
Expand Down
33 changes: 33 additions & 0 deletions test/clj/cider/nrepl/middleware/ns_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,22 @@
(is (sequential? ns-vars))
(is (every? string? ns-vars))))

(deftest ns-vars-including-privates-test
(testing "Without private vars"
(let [ns-vars (:ns-vars (session/message {:op "ns-vars"
:ns "clojure.core"}))]
(is (sequential? ns-vars))
(is (every? string? ns-vars))
(is (nil? (some #(= "is-annotation?" %) ns-vars)))))

(testing "Including private vars"
(let [ns-vars (:ns-vars (session/message {:op "ns-vars"
:ns "clojure.core"
:include-privates? 1}))]
(is (sequential? ns-vars))
(is (every? string? ns-vars))
(is (some #(= "is-annotation?" %) ns-vars)))))

(deftest ns-vars-with-meta-integration-test
(let [ns-vars-with-meta (:ns-vars-with-meta
(session/message {:op "ns-vars-with-meta"
Expand All @@ -62,6 +78,23 @@
(is (= (:*ns* ns-vars-with-meta)
{:doc "\"A clojure.lang.Namespace object representing the current namespace.\""}))))

(deftest ns-vars-with-meta-including-privates-test
(testing "Without private vars"
(let [ns-vars-with-meta (:ns-vars-with-meta
(session/message {:op "ns-vars-with-meta"
:ns "clojure.core"}))]
(is (every? (comp map? second) ns-vars-with-meta))
(is (nil? (:is-annotation? ns-vars-with-meta)))))

(testing "Including private vars"
(let [ns-vars-with-meta (:ns-vars-with-meta
(session/message {:op "ns-vars-with-meta"
:ns "clojure.core"
:include-privates? 1}))]
(is (every? (comp map? second) ns-vars-with-meta))
(is (= (:is-annotation? ns-vars-with-meta)
{:arglists "([c])"})))))

(deftest ns-path-integration-test
(let [ns-path (:path (session/message {:op "ns-path"
:ns "cider.nrepl.middleware.ns"}))
Expand Down

0 comments on commit 3608c07

Please sign in to comment.