Skip to content

Commit

Permalink
[Fix clojure-emacs/cider#1889] Ensure that ns cache has clojure(scrip…
Browse files Browse the repository at this point in the history
…t) core (#391)
  • Loading branch information
dpsutton authored and bbatsov committed Jan 6, 2017
1 parent 65e6a11 commit fbc326d
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 8 deletions.
33 changes: 25 additions & 8 deletions src/cider/nrepl/middleware/track_state.clj
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,27 @@
[f coll]
(persistent! (reduce f (transient {}) coll)))

(defn ensure-clojure-core-present
"Check if `old-ns-map` has clojure.core, else add it to
current-ns-map. If `cljs` we inject cljs.core instead. `cljs` is the
cljs environment grabbed from the message (if present)."
[old-ns-map project-ns-map cljs]
(cond
(and cljs (not (contains? old-ns-map 'cljs.core)))
(assoc project-ns-map 'cljs.core
(ns-as-map (cljs-ana/find-ns cljs "cljs.core")))

;; we have cljs and the cljs core, nothing to do
cljs
project-ns-map

;; we've got core in old or new
(some #{clojure-core} (mapcat keys [old-ns-map project-ns-map]))
project-ns-map

:else
(assoc project-ns-map clojure-core clojure-core-map)))

(defn update-and-send-cache
"Send a reply to msg with state information assoc'ed.
old-data is the ns-cache that needs to be updated (the one
Expand Down Expand Up @@ -139,17 +160,13 @@
(if cljs
(vals (cljs-ana/all-ns cljs))
(all-ns)))
;; When a cljs-repl is created, the ns-cache is not empty, so
;; we can't use the technique we use for `clojure.core` below.
project-ns-map (if (and cljs (not (contains? old-data 'cljs.core)))
(assoc project-ns-map 'cljs.core
(ns-as-map (cljs-ana/find-ns cljs "cljs.core")))
project-ns-map)
project-ns-map (ensure-clojure-core-present old-data
project-ns-map
cljs)
changed-ns-map (-> project-ns-map
;; Add back namespaces that the project depends on.
(merge-used-aliases (or old-data {}) find-ns-fn)
;; If this is a new REPL, add `clojure.core` manually.
(calculate-changed-ns-map (or old-data {'clojure.core clojure-core-map})))]
(calculate-changed-ns-map old-data))]
(try (->> (response-for
msg :status :state
:repl-type (if cljs :cljs :clj)
Expand Down
21 changes: 21 additions & 0 deletions test/clj/cider/nrepl/middleware/track_state_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -113,3 +113,24 @@
'cider.nrepl.middleware.track-state))
(is (contains? (st/merge-used-aliases (assoc some-ns-map 'cider.nrepl.middleware.track-state nil) nil ns-name)
'cider.nrepl.middleware.track-state)))

(deftest ensure-clojure-core-present
(testing "if clojurescript doesn't add clojure"
;; note that the {:msg :stuff} object is much more complex in
;; actual use and in fact the msg is much more comlicated
(is (-> (st/ensure-clojure-core-present {}
{'cljs.core :present}
{:msg :stuff})
keys
#{st/clojure-core}
not)))
(testing "if core already present doesn't overwrite or add"
(is (= :present
(-> (st/ensure-clojure-core-present {}
{st/clojure-core :present}
nil)
(get st/clojure-core)))))
(testing "if core missing and not cljs, it adds it"
(is (= st/clojure-core-map
(-> (st/ensure-clojure-core-present {} {} nil)
(get st/clojure-core))))))

0 comments on commit fbc326d

Please sign in to comment.