-
Notifications
You must be signed in to change notification settings - Fork 178
/
Copy pathinfo.clj
277 lines (248 loc) · 8.76 KB
/
info.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
(ns cider.nrepl.middleware.info
(:require [clojure.string :as str]
[clojure.java.io :as io]
[cider.nrepl.middleware.util.cljs :as cljs]
[cider.nrepl.middleware.util.java :as java]
[cider.nrepl.middleware.util.misc :as u]
[clojure.repl :as repl]
[cljs-tooling.info :as cljs-info]
[clojure.tools.nrepl.transport :as transport]
[clojure.tools.nrepl.middleware :refer [set-descriptor!]]
[clojure.tools.nrepl.misc :refer [response-for]]))
(defn maybe-protocol
[info]
(if-let [prot-meta (meta (:protocol info))]
(merge info {:file (:file prot-meta)
:line (:line prot-meta)})
info))
(def var-meta-whitelist
[:ns :name :doc :file :arglists :forms :macro :protocol :line :column :static :added :deprecated :resource])
(defn- map-seq [x]
(if (seq x)
x
nil))
(defn var-meta
[v]
(-> v meta maybe-protocol (select-keys var-meta-whitelist) map-seq))
(defn ns-meta
[ns]
(merge
(meta ns)
{:ns ns
:file (-> (ns-publics ns)
first
second
var-meta
:file)
:line 1}))
(defn resolve-var
[ns sym]
(if-let [ns (find-ns ns)]
(try (ns-resolve ns sym)
;; Impl might try to resolve it as a class, which may fail
(catch ClassNotFoundException _
nil)
;; TODO: Preserve and display the exception info
(catch Exception _
nil))))
(defn resolve-aliases
[ns]
(if-let [ns (find-ns ns)]
(ns-aliases ns)))
;; This reproduces the behavior of the canonical `clojure.repl/doc` using its
;; internals, but returns the metadata rather than just printing. Oddly, the
;; only place in the Clojure API that special form metadata is available *as
;; data* is a private function. Lame. Just call through the var.
(defn resolve-special
"Return info for the symbol if it's a special form, or nil otherwise. Adds
`:url` unless that value is explicitly set to `nil` -- the same behavior
used by `clojure.repl/doc`."
[sym]
(try
(let [sym (get '{& fn, catch try, finally try} sym sym)
v (meta (ns-resolve (find-ns 'clojure.core) sym))]
(when-let [m (cond (special-symbol? sym) (#'repl/special-doc sym)
(:special-form v) v)]
(assoc m
:url (if (contains? m :url)
(when (:url m)
(str "http://clojure.org/" (:url m)))
(str "http://clojure.org/special_forms#" (:name m))))))
(catch Exception _)))
(defn find-cljx-source
"If file was cross-compiled using CLJX, return path to original file."
[filename]
(let [file (if-let [rsrc (io/resource filename)]
(when (= "file" (.getProtocol rsrc))
(io/as-file rsrc))
(io/as-file filename))]
(when (and file (.exists file))
(with-open [in (io/input-stream file)]
(.skip in (max 0 (- (.length file) 1024)))
(->> (slurp in)
(re-find #";+ This file autogenerated from (.*)$" )
second)))))
(defn handle-cljx-sources
"If file in info is a result of crosscompilation, replace file
with .cljx source."
[info]
(if (:file info)
(update-in info [:file] #(or (find-cljx-source %) %))
info))
(defn info-clj
[ns sym]
(cond
;; it's a special (special-symbol? or :special-form)
(resolve-special sym) (resolve-special sym)
;; it's a var
(var-meta (resolve-var ns sym)) (var-meta (resolve-var ns sym))
;; sym is an alias for another ns
(get (resolve-aliases ns) sym) (ns-meta (get (resolve-aliases ns) sym))
;; it's simply a full ns
(find-ns sym) (ns-meta (find-ns sym))
;; it's a Java class/member symbol...or nil
:else (java/resolve-symbol ns sym)))
(defn info-cljs
[env symbol ns]
(let [x (cljs-info/info env symbol ns)]
(select-keys x [:file :line :ns :doc :column :name :arglists])))
(defn info-java
[class member]
(java/member-info class member))
(defn info
[{:keys [ns symbol class member] :as msg}]
(let [[ns symbol class member] (map u/as-sym [ns symbol class member])]
(if-let [cljs-env (cljs/grab-cljs-env msg)]
(handle-cljx-sources (info-cljs cljs-env symbol ns))
(if ns
(handle-cljx-sources (info-clj ns symbol))
(info-java class member)))))
(defn resource-path
"If it's a resource, return a tuple of the relative path and the full resource path."
[x]
(or (if-let [full (io/resource x)]
[x full])
(if-let [[_ relative] (re-find #".*jar!/(.*)" x)]
(if-let [full (io/resource relative)]
[relative full]))
;; handles load-file on jar resources from a cider buffer
(if-let [[_ relative] (re-find #".*jar:(.*)" x)]
(if-let [full (io/resource relative)]
[relative full]))))
(defn file-path
"For a file path, return a URL to the file if it exists and does not
represent a form evaluated at the REPL."
[x]
(when (seq x)
(let [f (io/file x)]
(when (and (.exists f)
(not (-> f .getName (.startsWith "form-init"))))
(io/as-url f)))))
(defn file-info
[path]
(let [[resource-relative resource-full] (resource-path path)]
(merge {:file (or (file-path path) resource-full path)}
;; Classpath-relative path if possible
(if resource-relative
{:resource resource-relative}))))
(defn javadoc-info
"Resolve a relative javadoc path to a URL and return as a map. Prefer javadoc
resources on the classpath; then use online javadoc content for core API
classes. If no source is available, return the relative path as is."
[path]
{:javadoc
(or (io/resource path)
(when (re-find #"^(java|javax|org.omg|org.w3c.dom|org.xml.sax)/" path)
(format "http://docs.oracle.com/javase/%s/docs/api/%s"
u/java-api-version path))
path)})
(declare format-response)
(defn format-nested
"Apply response formatting to nested `:candidates` info for Java members."
[info]
(if-let [candidates (:candidates info)]
(assoc info :candidates
(into {} (for [[k v] candidates]
[k (format-response v)])))
info))
(defn blacklist
"Remove anything that might contain arbitrary EDN, metadata can hold anything"
[info]
(let [blacklisted #{:arglists :forms}]
(apply dissoc info blacklisted)))
(defn format-response
[info]
(when info
(-> info
(merge (when-let [ns (:ns info)]
(:ns (str ns)))
(when-let [args (:arglists info)]
{:arglists-str (pr-str args)})
(when-let [forms (:forms info)]
{:forms-str (->> (map #(str " " (pr-str %)) forms)
(str/join \newline))})
(when-let [file (:file info)]
(file-info file))
(when-let [path (:javadoc info)]
(javadoc-info path)))
format-nested
blacklist
u/transform-value)))
(defn info-reply
[{:keys [transport] :as msg}]
(try
(if-let [var-info (format-response (info msg))]
(transport/send
transport (response-for msg var-info {:status :done}))
(transport/send
transport (response-for msg {:status #{:no-info :done}})))
(catch Exception e
(transport/send
transport (response-for msg (u/err-info e :info-error))))))
(defn extract-eldoc [info]
(if (contains? info :candidates)
(->> (:candidates info)
vals
(mapcat :arglists)
distinct
(sort-by count))
(:arglists info)))
(defn format-eldoc [raw-eldoc]
(map #(mapv str %) raw-eldoc))
(defn eldoc
[msg]
(if-let [raw-eldoc (extract-eldoc (info msg))]
(format-eldoc raw-eldoc)))
(defn eldoc-reply
[{:keys [transport] :as msg}]
(try
(if-let [var-eldoc (eldoc msg)]
(transport/send
transport (response-for msg {:eldoc var-eldoc :status :done}))
(transport/send
transport (response-for msg {:status #{:no-eldoc :done}})))
(catch Exception e
(transport/send
transport (response-for msg (u/err-info e :eldoc-error))))))
(defn wrap-info
"Middleware that looks up info for a symbol within the context of a particular namespace."
[handler]
(fn [{:keys [op] :as msg}]
(cond
(= "info" op) (info-reply msg)
(= "eldoc" op) (eldoc-reply msg)
:else (handler msg))))
(set-descriptor!
#'wrap-info
(cljs/maybe-piggieback
{:handles
{"info"
{:doc "Return a map of information about the specified symbol."
:requires {"symbol" "The symbol to lookup"
"ns" "The current namespace"}
:returns {"status" "done"}}
"eldoc"
{:doc "Return a map of information about the specified symbol."
:requires {"symbol" "The symbol to lookup"
"ns" "The current namespace"}
:returns {"status" "done"}}}}))