|
3 | 3 | [refactor-nrepl.core :as core] |
4 | 4 | [refactor-nrepl.ns.ns-parser :as ns-parser] |
5 | 5 | [refactor-nrepl.ns.suggest-aliases :as suggest-aliases] |
6 | | - [refactor-nrepl.util :as util]) |
| 6 | + [refactor-nrepl.util :as util] |
| 7 | + [refactor-nrepl.util.meta :as meta]) |
7 | 8 | (:import |
8 | 9 | (java.io File))) |
9 | 10 |
|
10 | 11 | ;; The structure here is {path {lang [timestamp value]}} |
11 | 12 | ;; where lang is either :clj or :cljs |
12 | 13 | (def ^:private cache (atom {})) |
13 | 14 |
|
| 15 | +(defn vec-distinct-into [x y] |
| 16 | + (into [] |
| 17 | + (comp cat |
| 18 | + (distinct)) |
| 19 | + [x y])) |
| 20 | + |
| 21 | +(defn merge-libspecs-meta [a b] |
| 22 | + (let [{:keys [used-from files]} (meta b)] |
| 23 | + (cond-> a |
| 24 | + (seq used-from) (vary-meta update :used-from vec-distinct-into used-from) |
| 25 | + (seq files) (vary-meta update :files vec-distinct-into files)))) |
| 26 | + |
14 | 27 | (defn- aliases [libspecs] |
15 | | - (->> libspecs |
16 | | - (map (juxt :as :ns)) |
17 | | - (remove #(nil? (first %))) |
18 | | - distinct)) |
| 28 | + (meta/distinct merge-libspecs-meta |
| 29 | + (into [] |
| 30 | + (comp (map (juxt :as :ns)) |
| 31 | + (filter first)) |
| 32 | + libspecs))) |
19 | 33 |
|
20 | 34 | (defn- aliases-by-frequencies [libspecs] |
21 | | - (let [grouped (->> libspecs |
22 | | - (mapcat aliases) ; => [[str clojure.string] ...] |
| 35 | + (let [grouped (->> (into [] |
| 36 | + (mapcat aliases) ; => [[str clojure.string] ...] |
| 37 | + libspecs) |
23 | 38 | (sort-by (comp str second)) |
24 | 39 | (group-by first) ; => {str [[str clojure.string] [str clojure.string]] ...} |
25 | 40 | )] |
|
37 | 52 | (when (= ts (.lastModified f)) |
38 | 53 | v))) |
39 | 54 |
|
| 55 | +(defn add-used-from-meta [libspecs ^File f] |
| 56 | + (let [extension (case (re-find #"\.clj[cs]?$" (-> f .getAbsolutePath)) |
| 57 | + ".clj" [:clj] ;; these are expressed as vectors, so that `#'merge-libspecs-meta` can operate upon them |
| 58 | + ".cljs" [:cljs] |
| 59 | + ".cljc" [:cljc] |
| 60 | + nil)] |
| 61 | + (if-not extension |
| 62 | + libspecs |
| 63 | + (into [] |
| 64 | + (map (fn [libspec] |
| 65 | + (cond-> libspec |
| 66 | + (not (-> libspec :ns string?)) |
| 67 | + (update :ns vary-meta assoc :used-from extension)))) |
| 68 | + libspecs)))) |
| 69 | + |
40 | 70 | (defn- put-cached-ns-info! [^File f lang] |
41 | 71 | (binding [;; briefly memoize this function to avoid repeating its IO cost while `f` is being cached: |
42 | 72 | ns-parser/*read-ns-form-with-meta* (memoize core/read-ns-form-with-meta)] |
43 | 73 | (let [libspecs (ns-parser/get-libspecs-from-file lang f) |
44 | 74 | [_ namespace-name] (ns-parser/*read-ns-form-with-meta* lang f) |
45 | 75 | suggested-aliases (suggest-aliases/suggested-aliases namespace-name) |
46 | | - v {:libspecs libspecs |
| 76 | + v {:libspecs (add-used-from-meta libspecs f) |
47 | 77 | :namespace-name namespace-name |
48 | 78 | :suggested-aliases suggested-aliases |
49 | 79 | :test-like-ns-name? (suggest-aliases/test-like-ns-name? namespace-name)}] |
|
0 commit comments