|
| 1 | +(ns refactor-nrepl.find.find-macros |
| 2 | + (:import clojure.lang.LineNumberingPushbackReader |
| 3 | + [java.io BufferedReader File FileReader StringReader]) |
| 4 | + (:require [clojure.string :as str] |
| 5 | + [refactor-nrepl.ns |
| 6 | + [helpers :as ns-helpers] |
| 7 | + [ns-parser :as ns-parser] |
| 8 | + [tracker :as tracker]] |
| 9 | + [refactor-nrepl.util :as util] |
| 10 | + [rewrite-clj.zip :as zip] |
| 11 | + [refactor-nrepl.find.bindings :as bindings])) |
| 12 | + |
| 13 | +(defn- keep-lines |
| 14 | + "Keep the first n lines of s." |
| 15 | + [s n] |
| 16 | + (->> s |
| 17 | + StringReader. |
| 18 | + java.io.BufferedReader. |
| 19 | + line-seq |
| 20 | + (take n) |
| 21 | + doall |
| 22 | + (str/join "\n"))) |
| 23 | + |
| 24 | +(defn- build-macro-meta [form line-end path] |
| 25 | + (let [file-content (slurp path) |
| 26 | + file-ns (util/ns-from-string file-content) |
| 27 | + content (keep-lines file-content line-end) |
| 28 | + sexp (util/get-last-sexp content) |
| 29 | + macro-name (name (second form)) |
| 30 | + col-beg (.indexOf sexp macro-name)] |
| 31 | + {:name (str file-ns "/" macro-name) |
| 32 | + :col-beg col-beg |
| 33 | + :col-end (+ col-beg (.length macro-name)) |
| 34 | + :line-end line-end |
| 35 | + :line-beg (inc (- line-end (count (str/split-lines sexp)))) |
| 36 | + :file (.getAbsolutePath path) |
| 37 | + :match sexp})) |
| 38 | + |
| 39 | +(defn- find-macro-definitions-in-file |
| 40 | + [path] |
| 41 | + (let [rdr (LineNumberingPushbackReader. (FileReader. path))] |
| 42 | + (loop [macros [], form (read rdr nil :eof)] |
| 43 | + (cond |
| 44 | + (= form :eof) macros |
| 45 | + (= (first form) 'defmacro) |
| 46 | + (recur (conj macros (build-macro-meta form (.getLineNumber rdr) path)) |
| 47 | + (read rdr nil :eof)) |
| 48 | + :else (recur macros (read rdr nil :eof)))))) |
| 49 | + |
| 50 | +(defn- find-macro-definitions-in-project |
| 51 | + "Finds all macros that are defined in the project." |
| 52 | + [] |
| 53 | + (->> (util/find-clojure-sources-in-project) |
| 54 | + (mapcat find-macro-definitions-in-file))) |
| 55 | + |
| 56 | +(defn- get-ns-aliases |
| 57 | + "Create a map of ns-aliases to namespaces." |
| 58 | + [libspecs] |
| 59 | + (->> libspecs |
| 60 | + (map #((juxt (comp str :ns) (comp str :as)) %)) |
| 61 | + (into {}))) |
| 62 | + |
| 63 | +(defn- referred-from-require? |
| 64 | + [libspec macro-name] |
| 65 | + (some->> (when (sequential? (:refer libspec)) (:refer libspec)) |
| 66 | + (map str) |
| 67 | + (some #(= % (ns-helpers/suffix macro-name))))) |
| 68 | + |
| 69 | +(defn- referred-from-use? |
| 70 | + [libspec macro-name] |
| 71 | + (some->> libspec |
| 72 | + :use |
| 73 | + (map str) |
| 74 | + (filter #(= % (ns-helpers/suffix macro-name))) |
| 75 | + first)) |
| 76 | + |
| 77 | +(defn- macro-referred? [libspecs macro-name] |
| 78 | + (let [libspec (some->> libspecs |
| 79 | + (filter #(= (str (:ns %)) (ns-helpers/prefix macro-name))) |
| 80 | + first)] |
| 81 | + (or (referred-from-require? libspec macro-name) |
| 82 | + (referred-from-use? libspec macro-name)))) |
| 83 | + |
| 84 | +(defn- node->occurrence |
| 85 | + "line-offset is the offset in the file where we start searching. |
| 86 | + This is after the ns but clj-rewrite keeps tracking of any following |
| 87 | + whitespace." |
| 88 | + [path macro-name line-offset zip-node] |
| 89 | + (let [node (zip/node zip-node) |
| 90 | + val (:string-value node) |
| 91 | + {:keys [row col]} (meta node)] |
| 92 | + {:match (zip/string (zip/up zip-node)) |
| 93 | + :name macro-name |
| 94 | + :line-beg (dec (+ row line-offset)) |
| 95 | + :line-end (dec (+ row line-offset)) |
| 96 | + :col-beg (dec col) |
| 97 | + :col-end (dec (+ col (.length val))) |
| 98 | + :file path})) |
| 99 | + |
| 100 | +(defn- token-node? [node] |
| 101 | + (= (zip/tag node) :token)) |
| 102 | + |
| 103 | +(defn- macro-found? |
| 104 | + [sym macro-name libspecs current-ns] |
| 105 | + (let [macro-prefix (ns-helpers/prefix macro-name) |
| 106 | + macro-suffix (ns-helpers/suffix macro-name) |
| 107 | + alias? ((get-ns-aliases libspecs) macro-prefix)] |
| 108 | + (or |
| 109 | + ;; locally defined macro |
| 110 | + (and (= current-ns macro-prefix) |
| 111 | + (= sym macro-suffix)) |
| 112 | + ;; fully qualified |
| 113 | + (= sym macro-name) |
| 114 | + ;; aliased |
| 115 | + (when alias? (= sym (str alias? "/" macro-suffix))) |
| 116 | + ;; referred |
| 117 | + (when (macro-referred? libspecs macro-name) |
| 118 | + (= sym macro-suffix)) |
| 119 | + ;; I used to have a clause here for (:use .. :rename {...}) |
| 120 | + ;; but :use is ;; basically deprecated and nobody used :rename to |
| 121 | + ;; begin with so I dropped it when the test failed. |
| 122 | + ))) |
| 123 | + |
| 124 | +(defn- active-bindings |
| 125 | + "Find all the bindings above the current zip-node." |
| 126 | + [zip-node] |
| 127 | + (->> (zip/leftmost zip-node) |
| 128 | + (iterate #(zip/up %)) |
| 129 | + (take-while identity) |
| 130 | + (mapcat #(-> % zip/sexpr bindings/extract-local-bindings)) |
| 131 | + (into #{}))) |
| 132 | + |
| 133 | +(defn- macro-shadowed? [macro-sym zip-node] |
| 134 | + ((active-bindings zip-node) macro-sym)) |
| 135 | + |
| 136 | +(defn- content-offset [path] |
| 137 | + (-> path slurp util/get-next-sexp str/split-lines count)) |
| 138 | + |
| 139 | +(defn- collect-occurrences |
| 140 | + [occurrences macro ^File path zip-node] |
| 141 | + (let [node (zip/node zip-node) |
| 142 | + macro-name (str (:name macro)) |
| 143 | + sym (:string-value node) |
| 144 | + path (.getAbsolutePath path) |
| 145 | + ns-form (ns-helpers/read-ns-form path) |
| 146 | + libspecs (ns-parser/get-libspecs ns-form) |
| 147 | + current-ns (str (second ns-form)) |
| 148 | + offset (content-offset path)] |
| 149 | + (when (and (macro-found? sym macro-name libspecs current-ns) |
| 150 | + (not (macro-shadowed? sym zip-node))) |
| 151 | + (swap! occurrences conj |
| 152 | + (node->occurrence path macro-name offset zip-node)))) |
| 153 | + zip-node) |
| 154 | + |
| 155 | +(defn- find-usages-in-file [macro ^File path] |
| 156 | + (let [zipper (-> path slurp ns-helpers/file-content-sans-ns zip/of-string) |
| 157 | + occurrences (atom [])] |
| 158 | + (zip/postwalk zipper |
| 159 | + token-node? |
| 160 | + (partial collect-occurrences occurrences macro path)) |
| 161 | + (loop [zipper (zip/right zipper)] |
| 162 | + (when zipper |
| 163 | + (zip/postwalk zipper |
| 164 | + token-node? |
| 165 | + (partial collect-occurrences occurrences macro path)) |
| 166 | + (recur (zip/right zipper)))) |
| 167 | + @occurrences)) |
| 168 | + |
| 169 | + |
| 170 | +(defn- fully-qualified-name? [fully-qualified-name] |
| 171 | + (when (ns-helpers/prefix fully-qualified-name) |
| 172 | + fully-qualified-name)) |
| 173 | + |
| 174 | +(defn find-macro |
| 175 | + "Finds all occurrences of the macro, including the definition, in |
| 176 | + the project." |
| 177 | + [fully-qualified-name] |
| 178 | + (when (fully-qualified-name? fully-qualified-name) |
| 179 | + (let [all-defs (find-macro-definitions-in-project) |
| 180 | + macro-def (first (filter #(= (:name %) fully-qualified-name) all-defs)) |
| 181 | + tracker (tracker/build-tracker) |
| 182 | + origin-ns (symbol (ns-helpers/prefix fully-qualified-name)) |
| 183 | + dependents (tracker/get-dependents tracker origin-ns)] |
| 184 | + (some->> macro-def |
| 185 | + :file |
| 186 | + File. |
| 187 | + (conj dependents) |
| 188 | + (mapcat (partial find-usages-in-file macro-def)) |
| 189 | + (into #{}) |
| 190 | + (remove nil?))))) |
0 commit comments