Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support ns-aliases and ns-refers #886

Merged
merged 2 commits into from
Mar 16, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ All notable changes to this project will be documented in this file. This change
### Added
- Add `planck.core/with-in-str` ([#873](https://github.com/planck-repl/planck/issues/873))
- Document `planck.io/resource` ([#487](https://github.com/planck-repl/planck/issues/487))`
- Support `ns-aliases` and `ns-refers` ([#505](https://github.com/planck-repl/planck/issues/505))

### Fixed
- Execution error instead of syntax for `08` ([#857](https://github.com/planck-repl/planck/issues/857))
Expand Down
18 changes: 18 additions & 0 deletions planck-cljs/src/planck/core.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -433,6 +433,24 @@
:name symbol?
:val (s/? any?)))

(defn ns-aliases
"Returns a map of the aliases for the namespace."
[ns]
(#'repl/ns-aliases ns))

(s/fdef ns-aliases
:args (s/cat :ns (s/or :sym symbol? :ns #(instance? Namespace %)))
:ret (s/map-of simple-symbol? #(instance? Namespace %)))

(defn ns-refers
"Returns a map of the refer mappings for the namespace."
[ns]
(#'repl/ns-refers ns))

(s/fdef ns-refers
:args (s/cat :ns (s/or :sym symbol? :ns #(instance? Namespace %)))
:ret (s/map-of simple-symbol? var?))

(defn- transfer-ns
[state ns]
(-> state
Expand Down
21 changes: 21 additions & 0 deletions planck-cljs/src/planck/repl.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -2202,6 +2202,27 @@
(when-let [the-ns (find-ns (cond-> ns (instance? Namespace ns) ns-name))]
(eval `(def ~name (quote ~val)) (ns-name the-ns)))))

(defn- ns-aliases
[ns]
(if-some [the-ns (find-ns (cond-> ns (instance? Namespace ns) ns-name))]
(->> (get-in @st [::ana/namespaces (ns-name the-ns) :requires])
(keep (fn [[k v]]
(when (not= k v)
[k (find-ns v)])))
(into {}))
(throw (ex-info (str "No namespace " ns " found.") {}))))

(defn- ns-refers
[ns]
(if-some [the-ns (find-ns (cond-> ns (instance? Namespace ns) ns-name))]
(merge
(apply dissoc (ns-publics 'cljs.core)
(get-in @st [::ana/namespaces (ns-name the-ns) :excludes]))
(->> (get-in @st [::ana/namespaces (ns-name the-ns) :uses])
(map (fn [[k v]] [k (ns-resolve v k)]))
(into {})))
(throw (ex-info (str "No namespace " ns " found.") {}))))

(defn- ^:export wrap-color-err
[]
(let [orig-print-err-fn js/PLANCK_PRINT_ERR_FN]
Expand Down
8 changes: 6 additions & 2 deletions planck-cljs/test/foo/core.cljs
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
(ns foo.core)
(ns foo.core
(:refer-clojure :exclude [map])
(:require
[clojure.string :as string]
[clojure.set :as set :refer [union intersection]]))

;; A test namespace for testing interns, ns-resolve
;; A test namespace for testing interns, ns-resolve, ns-aliases, ns-refers

(def h 3)
12 changes: 12 additions & 0 deletions planck-cljs/test/planck/core_test.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,18 @@
(is (= 'bar @(planck.core/ns-resolve 'foo.core 'd)))
(is (= '[bar] @(planck.core/ns-resolve 'foo.core 'e))))

(deftest test-ns-aliases
(is (= '{string clojure.string, set clojure.set}
(into {} (map (fn [[k v]] [k (ns-name v)])) (planck.core/ns-aliases 'foo.core))))
(is (thrown? js/Error (planck.core/ns-aliases 'unknown.namespace))))

(deftest test-ns-refers
(let [refers (planck.core/ns-refers 'foo.core)]
(is (= ['union #'clojure.set/union] (find refers 'union)))
(is (= ['intersection #'clojure.set/intersection] (find refers 'intersection)))
(is (= ['reduce #'cljs.core/reduce] (find refers 'reduce)))
(is (not (contains? refers 'map)))))

(defn spit-slurp [file-name content]
(planck.core/spit file-name content)
(planck.core/slurp file-name))
Expand Down
20 changes: 20 additions & 0 deletions site/src/planck-core.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ _Vars_
[line-seq](#line-seq)<br/>
[load-reader](#load-reader)<br/>
[load-string](#load-string)<br/>
[ns-aliases](#ns-aliases)<br/>
[ns-refers](#ns-refers)<br/>
[ns-resolve](#ns-resolve)<br/>
[read](#read)<br/>
[read-line](#read-line)<br/>
Expand Down Expand Up @@ -209,6 +211,24 @@ Spec<br/>
_args_: `(cat :s string?)`<br/>
_ret_: `any?`

### <a name="ns-aliases"></a>ns-aliases
`([ns])`

Returns a map of the aliases for the namespace.

Spec<br/>
_args_: `(cat :ns (or :sym symbol? :ns #(instance? Namespace %)))`<br/>
_ret_: `(map-of simple-symbol? (instance? Namespace %))`<br/>

### <a name="ns-refers"></a>ns-refers
`([ns])`

Returns a map of the refer mappings for the namespace.

Spec<br/>
_args_: `(cat :ns (or :sym symbol? :ns #(instance? Namespace %)))`<br/>
_ret_: `(map-of simple-symbol? var?)`<br/>

### <a name="ns-resolve"></a>ns-resolve
`([ns sym])`

Expand Down