|
| 1 | +(ns rewrite-clj.potemkin) |
| 2 | + |
| 3 | +;; --- copied from ztellman/potemkin |
| 4 | +;; |
| 5 | +;; Copyright (c) 2013 Zachary Tellman |
| 6 | +;; |
| 7 | +;; Permission is hereby granted, free of charge, to any person obtaining a copy |
| 8 | +;; of this software and associated documentation files (the "Software"), to |
| 9 | +;; deal in the Software without restriction, including without limitation the |
| 10 | +;; rights to use, copy, modify, merge, publish, distribute, sublicense, and/or |
| 11 | +;; sell copies of the Software, and to permit persons to whom the Software is |
| 12 | +;; furnished to do so, subject to the following conditions: |
| 13 | +;; |
| 14 | +;; The above copyright notice and this permission notice shall be included in |
| 15 | +;; all copies or substantial portions of the Software. |
| 16 | +;; |
| 17 | +;; THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 18 | +;; IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 19 | +;; FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 20 | +;; AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 21 | +;; LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
| 22 | +;; FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS |
| 23 | +;; IN THE SOFTWARE. |
| 24 | +;; |
| 25 | +;; --- |
| 26 | + |
| 27 | +;; --- potemkin.namespaces |
| 28 | + |
| 29 | +(defn link-vars |
| 30 | + "Makes sure that all changes to `src` are reflected in `dst`." |
| 31 | + [src dst] |
| 32 | + (add-watch src dst |
| 33 | + (fn [_ src old new] |
| 34 | + (alter-var-root dst (constantly @src)) |
| 35 | + (alter-meta! dst merge (dissoc (meta src) :name))))) |
| 36 | + |
| 37 | +(defmacro import-fn |
| 38 | + "Given a function in another namespace, defines a function with the |
| 39 | + same name in the current namespace. Argument lists, doc-strings, |
| 40 | + and original line-numbers are preserved." |
| 41 | + ([sym] |
| 42 | + `(import-fn ~sym nil)) |
| 43 | + ([sym name] |
| 44 | + (let [vr (resolve sym) |
| 45 | + m (meta vr) |
| 46 | + n (or name (:name m)) |
| 47 | + arglists (:arglists m) |
| 48 | + protocol (:protocol m)] |
| 49 | + (when-not vr |
| 50 | + (throw (IllegalArgumentException. (str "Don't recognize " sym)))) |
| 51 | + (when (:macro m) |
| 52 | + (throw (IllegalArgumentException. |
| 53 | + (str "Calling import-fn on a macro: " sym)))) |
| 54 | + |
| 55 | + `(do |
| 56 | + (def ~(with-meta n {:protocol protocol}) (deref ~vr)) |
| 57 | + (alter-meta! (var ~n) merge (dissoc (meta ~vr) :name)) |
| 58 | + (link-vars ~vr (var ~n)) |
| 59 | + ~vr)))) |
| 60 | + |
| 61 | +(defmacro import-macro |
| 62 | + "Given a macro in another namespace, defines a macro with the same |
| 63 | + name in the current namespace. Argument lists, doc-strings, and |
| 64 | + original line-numbers are preserved." |
| 65 | + ([sym] |
| 66 | + `(import-macro ~sym nil)) |
| 67 | + ([sym name] |
| 68 | + (let [vr (resolve sym) |
| 69 | + m (meta vr) |
| 70 | + n (or name (with-meta (:name m) {})) |
| 71 | + arglists (:arglists m)] |
| 72 | + (when-not vr |
| 73 | + (throw (IllegalArgumentException. (str "Don't recognize " sym)))) |
| 74 | + (when-not (:macro m) |
| 75 | + (throw (IllegalArgumentException. |
| 76 | + (str "Calling import-macro on a non-macro: " sym)))) |
| 77 | + `(do |
| 78 | + (def ~n ~(resolve sym)) |
| 79 | + (alter-meta! (var ~n) merge (dissoc (meta ~vr) :name)) |
| 80 | + (.setMacro (var ~n)) |
| 81 | + (link-vars ~vr (var ~n)) |
| 82 | + ~vr)))) |
| 83 | + |
| 84 | +(defmacro import-def |
| 85 | + "Given a regular def'd var from another namespace, defined a new var with the |
| 86 | + same name in the current namespace." |
| 87 | + ([sym] |
| 88 | + `(import-def ~sym nil)) |
| 89 | + ([sym name] |
| 90 | + (let [vr (resolve sym) |
| 91 | + m (meta vr) |
| 92 | + n (or name (:name m)) |
| 93 | + n (with-meta n (if (:dynamic m) {:dynamic true} {})) |
| 94 | + nspace (:ns m)] |
| 95 | + (when-not vr |
| 96 | + (throw (IllegalArgumentException. (str "Don't recognize " sym)))) |
| 97 | + `(do |
| 98 | + (def ~n @~vr) |
| 99 | + (alter-meta! (var ~n) merge (dissoc (meta ~vr) :name)) |
| 100 | + (link-vars ~vr (var ~n)) |
| 101 | + ~vr)))) |
| 102 | + |
| 103 | +(defmacro import-vars |
| 104 | + "Imports a list of vars from other namespaces." |
| 105 | + [& syms] |
| 106 | + (let [unravel (fn unravel [x] |
| 107 | + (if (sequential? x) |
| 108 | + (->> x |
| 109 | + rest |
| 110 | + (mapcat unravel) |
| 111 | + (map |
| 112 | + #(symbol |
| 113 | + (str (first x) |
| 114 | + (when-let [n (namespace %)] |
| 115 | + (str "." n))) |
| 116 | + (name %)))) |
| 117 | + [x])) |
| 118 | + syms (mapcat unravel syms)] |
| 119 | + `(do |
| 120 | + ~@(map |
| 121 | + (fn [sym] |
| 122 | + (let [vr (resolve sym) |
| 123 | + m (meta vr)] |
| 124 | + (cond |
| 125 | + (:macro m) `(import-macro ~sym) |
| 126 | + (:arglists m) `(import-fn ~sym) |
| 127 | + :else `(import-def ~sym)))) |
| 128 | + syms)))) |
| 129 | + |
| 130 | +;; --- potemkin.types |
| 131 | + |
| 132 | +(defmacro defprotocol+ |
| 133 | + "A simpler version of 'potemkin.types/defprotocol+'." |
| 134 | + [name & body] |
| 135 | + (let [prev-body (-> name resolve meta :potemkin/body)] |
| 136 | + (when-not (= prev-body body) |
| 137 | + `(let [p# (defprotocol ~name ~@body)] |
| 138 | + (alter-meta! (resolve p#) assoc :potemkin/body '~body) |
| 139 | + p#)))) |
0 commit comments