-
Notifications
You must be signed in to change notification settings - Fork 38
/
utils.clj
278 lines (250 loc) · 10.7 KB
/
utils.clj
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
(ns compliment.utils
"Functions and utilities for source implementations."
(:import java.io.File
java.nio.file.Files
(java.util.function Function Predicate)
java.util.concurrent.locks.ReentrantLock
(java.util.jar JarEntry JarFile)
java.util.stream.Collectors))
(def ^:dynamic *extra-metadata*
"Signals to downstream sources which additional information about completion
candidates they should attach . Should be a set of keywords."
nil)
(defn split-by-leading-literals
"Separate quote/var/deref qualifiers from a var name."
[symbol-str]
(next (re-matches #"(@{0,2}#'|'|@)?(.*)" symbol-str)))
(set! *unchecked-math* true)
(defn fuzzy-matches?
"Tests if symbol matches the prefix when symbol is split into parts on
separator."
[^String prefix, ^String symbol, ^Character separator]
(let [pn (.length prefix), sn (.length symbol)]
(or (= pn 0)
(and (> sn 0)
(= (.charAt prefix 0) (.charAt symbol 0))
(loop [pi 1, si 1, skipping false]
(cond (>= pi pn) true
(>= si sn) false
:else
(let [pc (.charAt prefix pi)
sc (.charAt symbol si)
match (= pc sc)]
(cond skipping (if (= sc separator)
(recur (if match (inc pi) pi) (inc si) false)
(recur pi (inc si) true))
match (recur (inc pi) (inc si) false)
:else (recur pi (inc si) (not (= pc separator)))))))))))
(defn fuzzy-matches-no-skip?
"Tests if symbol matches the prefix where separator? checks whether character
is a separator. Unlike `fuzzy-matches?` requires separator characters to be
present in prefix."
[^String prefix, ^String symbol, separator?]
(let [pn (.length prefix), sn (.length symbol)]
(or (= pn 0)
(and (> sn 0)
(= (.charAt prefix 0) (.charAt symbol 0))
(loop [pi 1, si 1, skipping false]
(cond (>= pi pn) true
(>= si sn) false
:else
(let [pc (.charAt prefix pi)
sc (.charAt symbol si)]
(cond skipping (if (separator? sc)
(recur pi si false)
(recur pi (inc si) true))
(= pc sc) (recur (inc pi) (inc si) false)
:else (recur pi (inc si) true)))))))))
(set! *unchecked-math* false)
(defn resolve-class
"Tries to resolve a classname from the given symbol, or returns nil
if classname can't be resolved."
[ns sym]
(when-let [val (try (ns-resolve ns sym)
(catch ClassNotFoundException ex nil))]
(when (class? val) val)))
(defn resolve-namespace
"Tries to resolve a namespace from the given symbol, either from a
fully qualified name or an alias in the given namespace."
[sym ns]
(or ((ns-aliases ns) sym) (find-ns sym)))
(def primitive-cache (atom {}))
(let [lock (ReentrantLock.)]
(defn cache-last-result* [name key value-fn]
(try (.lock lock)
(let [[cached-key cached-value :as t] (@primitive-cache name)]
(if (and t (= cached-key key))
cached-value
(let [value (value-fn)]
(swap! primitive-cache assoc name [key value])
value)))
(finally (.unlock lock)))))
(defmacro cache-last-result
"If cache for `name` is absent, or `key` doesn't match the key in the cache,
calculate `v` and return it. Else return value from cache."
{:style/indent 2}
[name key value]
`(cache-last-result* ~name ~key (fn [] ~value)))
^{:lite nil}
(defn flush-caches
"Removes all cached values, forcing functions that depend on
`cache-last-result` to recalculate."
[]
(reset! primitive-cache {}))
;; Classpath inspection
(defn- classpath
"Returns a sequence of File objects of the elements on the classpath."
[]
(mapcat #(some-> (System/getProperty %) (.split File/pathSeparator))
["sun.boot.class.path" "java.ext.dirs" "java.class.path"
;; This is where Boot keeps references to dependencies.
"fake.class.path"]))
(defn- file-seq-nonr
"A tree seq on java.io.Files, doesn't resolve symlinked directories to avoid
infinite sequence resulting from recursive symlinked directories."
[dir]
(tree-seq
(fn [^File f] (and (.isDirectory f) (not (Files/isSymbolicLink (.toPath f)))))
(fn [^File d] (seq (.listFiles d)))
dir))
(defn- list-files
"Given a path (either a jar file, directory with classes or directory with
paths) returns all files under that path."
[^String path, scan-jars?]
(cond (.endsWith path "/*")
(for [^File jar (.listFiles (File. path))
:when (.endsWith ^String (.getName jar) ".jar")
file (list-files (.getPath jar) scan-jars?)]
file)
(.endsWith path ".jar")
(if scan-jars?
(try (-> (.stream (JarFile. path))
(.filter (reify Predicate
(test [_ entry]
(not (.isDirectory ^JarEntry entry)))))
(.map (reify Function
(apply [_ entry]
(.getName ^JarEntry entry))))
(.collect (Collectors/toList)))
(catch Exception _))
())
(= path "") ()
(.exists (File. path))
(let [root (File. path)
root-path (.toPath root)]
(for [^File file (file-seq-nonr root)
:when (not (.isDirectory file))]
(let [filename (str (.relativize root-path (.toPath file)))]
(if (.startsWith filename File/separator)
(.substring filename 1) filename))))))
(defmacro list-jdk9-base-classfiles
"Because on JDK9+ the classfiles are stored not in rt.jar on classpath, but in
modules, we have to do extra work to extract them."
[]
(if (resolve-class *ns* 'java.lang.module.ModuleFinder)
`(-> (.findAll (java.lang.module.ModuleFinder/ofSystem))
(.stream)
(.flatMap (reify Function
(apply [_ mref#]
(.list (.open ^java.lang.module.ModuleReference mref#)))))
(.collect (Collectors/toList)))
()))
(defn- all-files-on-classpath
"Given a list of files on the classpath, returns the list of all files,
including those located inside jar files."
[classpath]
(cache-last-result :all-files-on-classpath classpath
(let [seen (java.util.HashMap.)
seen? (fn [x] (.putIfAbsent seen x x))]
(-> []
(into (comp (map #(list-files % true)) cat (remove seen?)) classpath)
(into (remove seen?) (list-jdk9-base-classfiles))))))
(defn classes-on-classpath
"Returns a map of all classes that can be located on the classpath. Key
represent the root package of the class, and value is a list of all classes
for that package."
[]
(let [classpath (classpath)]
(cache-last-result :classes-on-classpath classpath
(let [roots (volatile! #{})
classes
(vec
(for [^String file (all-files-on-classpath classpath)
:when (and (.endsWith file ".class")
(not (.contains file "__"))
(not (.contains file "$"))
(not= file "module-info.class"))
:let [c (-> (subs file 0 (- (.length file) 6)) ;; .class
;; Resource separator is always / on all OSes.
(.replace "/" "."))
_ (vswap! roots conj
(subs c 0 (max (.indexOf ^String c ".") 0)))]]
c))]
(swap! primitive-cache assoc :root-packages-on-classpath
(set (remove empty? @roots)))
classes))))
(defn root-packages-on-classpath
"Return a set of all classname \"TLDs\" on the classpath."
[]
(classes-on-classpath)
(@primitive-cache :root-packages-on-classpath))
(defn namespaces&files-on-classpath
"Returns a collection of maps (e.g. `{:ns-str \"some.ns\", :file
\"some/ns.cljs\"}`) of all clj/cljc/cljs namespaces obtained by classpath
scanning."
[]
(let [classpath (classpath)]
(cache-last-result :namespaces-on-classpath classpath
;; TODO deduplicate these results by ns-str
(for [^String file (all-files-on-classpath classpath)
:when (or (.endsWith file ".clj") (.endsWith file ".cljs")
(.endsWith file ".cljc"))]
(let [ns-str (.. (subs file 0 (.lastIndexOf file "."))
(replace "/" ".") (replace "_" "-"))]
{:ns-str ns-str, :file file})))))
^{:lite nil}
(defn project-resources
"Returns a list of all non-code files in the current project."
[]
(let [classpath (classpath)]
(cache-last-result :project-resources classpath
(for [path classpath
^String file (list-files path false)
:when (not (re-find #"\.(clj[cs]?|jar|class)$" file))]
;; resource pathes always use "/" regardless of platform
(.replace file File/separator "/")))))
^{:lite nil}
(defn var->class
"Given a form that may be a var, returns the class that is associated
to its :tag or its value (in that precedence order)."
[ns form]
(when-let [var-ref (and (symbol? form)
(let [found (ns-resolve ns form)]
(when (var? found)
found)))]
;; let :tag take precedence - maybe the :tag says "this is an IFoo" (interface),
;; and the class says "this is a FooImpl"
;; (concrete class, which maybe has worse documentation
;; or other inheritance intricacies)
(or (-> var-ref meta :tag)
(class (deref var-ref)))))
^{:lite nil}
(defn invocation-form->class
"Given a form that might be an invocation form (i.e. a list),
return the class that is returned, according to the invoked function's var metadata."
[ns form]
(when-let [var-from-invocation (and (seq? form)
(symbol? (first form))
(ns-resolve ns (first form)))]
(when (var? var-from-invocation)
(-> var-from-invocation meta :tag))))
^{:lite nil}
(defn literal->class
"Extracts the class from a literal.
This is meant to support interop on strings and Clojure collections."
[form]
(when (or (string? form)
(and (coll? form)
;; invocations happen to be lists, but that's not relevant here:
(not (seq? form))))
(class form)))