diff --git a/src/cider/nrepl/middleware/ns.clj b/src/cider/nrepl/middleware/ns.clj index 347a1ae87..c9e0dcede 100644 --- a/src/cider/nrepl/middleware/ns.clj +++ b/src/cider/nrepl/middleware/ns.clj @@ -10,23 +10,26 @@ [misc :refer [response-for]] [transport :as transport]])) +(defn inlined-dependency-name? [^String ns-name] + (or + ;; rewritten by mranderson + (.startsWith ns-name "deps.") + (.startsWith ns-name "mranderson") + ;; rewritten by dolly + (.startsWith ns-name "eastwood.copieddeps"))) + (defn inlined-dependency? "Returns true if the namespace matches one of our, or eastwood's, inlined dependencies." [namespace] (let [ns-name (str (ns-name namespace))] - (or - ;; rewritten by mranderson - (.startsWith ns-name "deps.") - (.startsWith ns-name "mranderson") - ;; rewritten by dolly - (.startsWith ns-name "eastwood.copieddeps")))) + (inlined-dependency-name? ns-name))) (defn ns-list-clj [] (->> (all-ns) - (remove inlined-dependency?) (map ns-name) (map name) + (remove inlined-dependency-name?) (sort))) (defn ns-list-vars-by-name diff --git a/src/cider/nrepl/middleware/track_state.clj b/src/cider/nrepl/middleware/track_state.clj index f7aa1025f..3c2cacb76 100644 --- a/src/cider/nrepl/middleware/track_state.clj +++ b/src/cider/nrepl/middleware/track_state.clj @@ -1,12 +1,16 @@ (ns cider.nrepl.middleware.track-state "State tracker for client sessions." {:author "Artur Malabarba"} - (:require [cider.nrepl.middleware.util.cljs :as cljs] + (:require [cider.nrepl.middleware.ns :as ns] + [cider.nrepl.middleware.util.cljs :as cljs] [cider.nrepl.middleware.util.misc :as misc] [cljs-tooling.util.analysis :as cljs-ana] [clojure.tools.nrepl.middleware :refer [set-descriptor!]]) (:import clojure.tools.nrepl.transport.Transport)) +(def clojure-core (try (find-ns 'clojure.core) + (catch Exception e nil))) + ;;; Auxiliary (defn update-vals "Update the keys of map `m` via the function `f`." @@ -18,12 +22,11 @@ (defn filter-core "Remove keys whose values are vars in the core namespace." [refers] - (let [core (find-ns 'clojure.core)] - (reduce (fn [acc [sym var]] - (if (identical? (:ns (meta var)) core) - acc - (assoc acc sym var))) - {} refers))) + (reduce (fn [acc [sym var]] + (if (identical? (:ns (meta var)) clojure-core) + acc + (assoc acc sym var))) + {} refers)) (def relevant-meta-keys "Metadata keys that are useful to us. @@ -43,21 +46,29 @@ "Return a map of useful information about ns." class) +(defn track-ns? [ns-name-symbol] + (let [^String name-string (name ns-name-symbol)] + (not (or (ns/inlined-dependency-name? name-string) + (.startsWith name-string "cider."))))) + ;; Clojure Namespaces (defmethod ns-as-map clojure.lang.Namespace [ns] - {:name (ns-name ns) - :interns (update-vals relevant-meta (ns-interns ns)) - :aliases (update-vals ns-name (ns-aliases ns)) - :refers (filter-core (ns-refers ns))}) + (let [the-name (ns-name ns)] + (when (track-ns? the-name) + {:name the-name + :interns (update-vals relevant-meta (ns-interns ns)) + :aliases (update-vals ns-name (ns-aliases ns)) + :refers (filter-core (ns-refers ns))}))) ;; ClojureScript Namespaces -(defmethod ns-as-map clojure.lang.Associative [ns] - (let [{:keys [use-macros require-macros uses requires defs]} ns] - ;; For some reason, cljs (or piggieback) adds a :test key to the - ;; var metadata stored in the namespace. - {:name (:name ns) - :interns (update-vals #(dissoc (relevant-meta %) :test) defs) - :aliases (merge require-macros requires) - :refers (merge uses use-macros)})) +(defmethod ns-as-map clojure.lang.Associative [{the-name :name, :as ns}] + (when (track-ns? the-name) + (let [{:keys [use-macros require-macros uses requires defs]} ns] + {:name the-name + ;; For some reason, cljs (or piggieback) adds a :test key to the + ;; var metadata stored in the namespace. + :interns (update-vals #(dissoc (relevant-meta %) :test) defs) + :aliases (merge require-macros requires) + :refers (merge uses use-macros)}))) (def ns-cache "Cache of the namespace info that has been sent to each session. @@ -82,7 +93,7 @@ acc (assoc acc name ns)))) {} - (map ns-as-map new))) + (remove not (map ns-as-map new)))) (defn assoc-state "Return response with a :state entry assoc'ed. diff --git a/test/clj/cider/nrepl/middleware/track_state_test.clj b/test/clj/cider/nrepl/middleware/track_state_test.clj index a4061fc63..e56bd0454 100644 --- a/test/clj/cider/nrepl/middleware/track_state_test.clj +++ b/test/clj/cider/nrepl/middleware/track_state_test.clj @@ -13,8 +13,17 @@ nil (catch Exception e true)))) +(deftest track-ns? + (is (s/track-ns? 'me.ok)) + (is (s/track-ns? 'batman.not-bruce)) + (is (not (s/track-ns? 'mranderson045.a))) + (is (not (s/track-ns? 'cider.nrepl.middleware.track-state))) + (is (not (s/track-ns? 'deps.pl))) + (is (not (s/track-ns? 'eastwood.copieddeps)))) + (deftest assoc-state - (with-redefs [s/ns-cache (atom {})] + (with-redefs [s/ns-cache (atom {}) + s/track-ns? (constantly true)] (let [{:keys [repl-type changed-namespaces]} (:state (s/assoc-state {} msg))] (is (= repl-type :clj)) (is (map? changed-namespaces)) @@ -64,11 +73,16 @@ (deftest ns-as-map (alter-meta! #'update-vals merge {:indent 1 :cider-instrumented 2 :something-else 3}) - (let [{:keys [interns aliases] :as ns} (s/ns-as-map (find-ns 'cider.nrepl.middleware.track-state-test))] - (is (> (count ns) 3)) - (is (> (count interns) 4)) - (is (= (into #{} (keys (interns 'update-vals))) - #{:cider-instrumented :indent :test})) - (is (> (count aliases) 2)) - (is (= (aliases 's) - 'cider.nrepl.middleware.track-state)))) + (with-redefs [s/track-ns? (constantly true)] + (let [{:keys [interns aliases] :as ns} (s/ns-as-map (find-ns 'cider.nrepl.middleware.track-state-test))] + (is (> (count ns) 3)) + (is (> (count interns) 4)) + (is (= (into #{} (keys (interns 'update-vals))) + #{:cider-instrumented :indent :test})) + (is (> (count aliases) 2)) + (is (= (aliases 's) + 'cider.nrepl.middleware.track-state)))) + (with-redefs [s/track-ns? (constantly nil)] + (let [{:keys [interns aliases] :as ns} + (s/ns-as-map (find-ns 'cider.nrepl.middleware.track-state-test))] + (is (not ns)))))