Skip to content
This repository has been archived by the owner on Jun 4, 2022. It is now read-only.

Commit

Permalink
Add ns-aliases and ns-map, refer them at REPL startup (#479)
Browse files Browse the repository at this point in the history
  • Loading branch information
anmonteiro committed Jul 10, 2019
1 parent 0177d96 commit 8e919fa
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 3 deletions.
59 changes: 58 additions & 1 deletion src/cljs/snapshot/lumo/repl.cljs
Expand Up @@ -536,7 +536,7 @@
[sym]
(symbol (str (name sym) MACROS_SUFFIX)))

(defn- all-ns
(defn all-ns
"Returns a sequence of all namespaces."
[]
(keys (::ana/namespaces @st)))
Expand Down Expand Up @@ -588,6 +588,7 @@
(into {}
(merge (get-in @st [::ana/namespaces cur-ns :requires])
(get-in @st [::ana/namespaces cur-ns :require-macros])))))

(defn- ns-syms
[nsname pred]
(into []
Expand Down Expand Up @@ -858,13 +859,69 @@
(vreset! result value))))
@result)))

(defn- ns-resolve
[ns sym]
(let [result (atom nil)]
(binding [ana/*cljs-warnings* (zipmap (keys ana/*cljs-warnings*) (repeat false))]
(cljs/eval st `(~'var ~sym)
{:ns ns
:context :expr}
(fn [{:keys [value error]}]
(when-not error
(reset! result value)))))
@result))

(defn- intern
([ns name]
(intern ns name nil))
([ns name val]
(when-let [the-ns (find-ns (cond-> ns (instance? Namespace ns) ns-name))]
(eval `(def ~name ~val) (ns-name the-ns)))))

(defn ns-aliases
"Returns a map of the aliases for the namespace."
[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*
[the-ns]
(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 {}))))

(defn ns-refers
"Returns a map of the refer mappings for the namespace."
[ns]
(if-some [the-ns (find-ns (cond-> ns (instance? Namespace ns) ns-name))]
(ns-refers* the-ns)
(throw (ex-info (str "No namespace " ns " found.") {}))))

(defn ns-map
"Returns a map of the refer mappings for the namespace."
[ns]
(if-some [the-ns (find-ns (cond-> ns (instance? Namespace ns) ns-name))]
(let [ns-name (ns-name the-ns)]
(merge
(ns-refers* the-ns)
(into {}
(comp
(filter #(not (:private (val %))))
(remove #(:anonymous (val %)))
(map (fn [[k v]] [k (ns-resolve ns-name k)])))
(apply merge
((juxt :defs :macros)
(get-in @st [::ana/namespaces ns-name]))))))
(throw (ex-info (str "No namespace " ns " found.") {}))))

;; --------------------
;; Code evaluation

Expand Down
4 changes: 3 additions & 1 deletion src/js/cljs.js
Expand Up @@ -551,7 +551,9 @@ async function startClojureScriptEngine(opts: CLIOptsType): Promise<void> {
}

execute(
"(require '[lumo.repl :refer [apropos find-doc] :refer-macros [dir doc source]])",
`(require '[lumo.repl
:refer [apropos find-doc all-ns ns-aliases ns-refers ns-map]
:refer-macros [dir doc source]])`,
'text',
true,
false,
Expand Down
1 change: 0 additions & 1 deletion vendor/nexe/exe.js
Expand Up @@ -181,7 +181,6 @@ exports.compile = function(options, complete) {
const source = fs.readFileSync(options.input, 'utf8');
const thirdPartyMain = `
if (!process.send) {
console.log('toine', global.process.argv);
process.argv.splice(1, 0, 'nexe.js');
}
Expand Down

0 comments on commit 8e919fa

Please sign in to comment.