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

adds support for renaming to import-vars #47

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
41 changes: 24 additions & 17 deletions src/potemkin/namespaces.clj
Original file line number Diff line number Diff line change
Expand Up @@ -74,29 +74,36 @@
(link-vars ~vr (var ~n))
~vr))))

(defn- unravel* [ns x]
(let [[sym new-name] (if (sequential? x)
x
[x x])]
[(symbol
(str ns
(when-let [n (namespace sym)]
(str "." n)))
(name sym))
new-name]))

(defn- unravel [x]
(if (sequential? x)
(->> x
rest
(map #(unravel* (first x) %)))
[[x x]]))

(defmacro import-vars
"Imports a list of vars from other namespaces."
[& syms]
(let [unravel (fn unravel [x]
(if (sequential? x)
(->> x
rest
(mapcat unravel)
(map
#(symbol
(str (first x)
(when-let [n (namespace %)]
(str "." n)))
(name %))))
[x]))
syms (mapcat unravel syms)]
(let [syms (mapcat unravel syms)]
`(do
~@(map
(fn [sym]
(fn [[sym new-name]]
(let [vr (resolve sym)
m (meta vr)]
(cond
(:macro m) `(import-macro ~sym)
(:arglists m) `(import-fn ~sym)
:else `(import-def ~sym))))
(:macro m) `(import-macro ~sym ~new-name)
(:arglists m) `(import-fn ~sym ~new-name)
:else `(import-def ~sym ~new-name))))
syms))))