This repository has been archived by the owner on Oct 21, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 47
/
Copy pathdoc.clj
176 lines (155 loc) · 6.78 KB
/
doc.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
(ns lighttable.nrepl.doc
(:require [lighttable.nrepl.core :as core]
[lighttable.nrepl.eval :as eval]
[lighttable.nrepl.cljs :as cljs]
[clojure.java.io :as io]
[clojure.string :as string]
[cljs.env :as env]
[cljs.compiler :as comp]
[cljs.analyzer :as ana])
(:import java.net.URL java.io.File))
(defn- next-row
"Computes the value of the next row of the distance matrix, based on the
values from the previous row."
[pre-row char1 str2]
(let [init-val [(inc (first pre-row))]
row (fn [crow [diagonal above char2]]
(let [dist (if (= char2 char1) diagonal
(inc (min diagonal above (peek crow))))]
(conj crow dist)))]
(reduce row init-val (map vector pre-row (rest pre-row) str2))))
;; Author: PLIQUE Guillaume (Yomguithereal)
;; Source: https://gist.github.com/vishnuvyas/958488
(defn levenshtein
"Compute the levenshtein distance (a.k.a edit distance) between two strings.
Informally, the Levenshtein distance between two words is the minimum number
of single-character edits (i.e. insertions, deletions or substitutions)
required to change one word into the other"
[str1 str2]
(let [first-row (range (inc (count str2)))]
(peek (reduce #(next-row %1 %2 str2) first-row str1))))
(defn clean-meta
"returns a hash-map with only these keys: :ns :name :doc :arglists
:file :line :macro and updates :ns to a string"
[m]
(when m
(-> m
(select-keys [:ns :name :doc :arglists :file :line :macro])
(update-in [:ns] str)
)))
(defn str-contains? [orig search]
(> (.indexOf orig search) -1))
(defn format-result
"Returns a hash-map with `:args` as the original `:arglists` value. `:args` is converted to
a string and dissociated from `:arglists`."
[m]
(when m
(-> m
(assoc :args (-> m :arglists str))
(dissoc :arglists))))
(defn format-cljs-result [m]
(when m
(-> m
(assoc :name (-> m :name name)
:ns (-> m :name namespace)
:args (-> m :arglists second str))
(dissoc :arglists)
)))
(defn find-doc
"Look for a var with name `search` among all namespaces. Only public
vars with docstring are returned.
Returns a list of metadata (max 30) hash-maps sorted by their levenshtein
distance with the `search` input."
[search]
(let [with-dist #(hash-map :dist (levenshtein search (str (:name %)))
:meta %)
all-vars (vals (apply merge (map ns-interns (all-ns))))
dist-metas (into [] (comp (map meta) (filter :doc) (remove :private)
(map clean-meta) (map format-result)
(map with-dist))
all-vars)]
(take 30 (map :meta (sort-by :dist dist-metas)))))
(def jar-temp-files
"Maps jar-url paths to temp files"
(atom {}))
(defn jar-url->file
"Given a jar-url, retrieve its cached tempfile or copy its contents to a
tempfile and return it."
[^URL jar-url]
(or
(get @jar-temp-files (.getPath jar-url))
(let [[_ relative-name ext] (re-find #"!/(.*)(\.[^.]+)$" (.getPath jar-url))
new-file (.getPath (File/createTempFile relative-name ext))
body (-> jar-url .getContent slurp)]
(swap! jar-temp-files assoc (.getPath jar-url) new-file)
(spit new-file body)
new-file)))
(defn resolve-file
"Resolves a file to its full path. Jar paths are unpacked to a tempfile."
[file]
(when file
(let [url (or (io/resource file)
(URL.
;; cljs files don't have jar:file: or file: prefix off of metadata
(if (.contains file ".jar!/")
(string/replace-first file #"^file:" "jar:file:")
(string/replace-first file #"^/" "file:/"))))]
(if (= "jar" (.getProtocol url))
(jar-url->file url)
(-> url io/file .getPath)))))
(defn dmeta [nsp sym]
(-> (ns-resolve (symbol nsp) sym)
(meta)
(clean-meta)))
(defn get-doc [nsp sym]
(let [sym (symbol sym)
x (try (dmeta nsp sym)
(catch Exception e))]
(format-result x)))
(defn cljs-find-doc [search]
(env/with-compiler-env cljs/compiler-env
(let [ms (mapcat #(->> (:defs %)
(vals)
(sort-by :name)
(map clean-meta)) (-> @env/*compiler* :cljs.analyzer/namespaces vals))]
(for [m ms
:when (and (:doc m)
(not (:private m))
(or (str-contains? (:doc m) search)
(str-contains? (str (:ns m)) search)
(str-contains? (str (:name m)) search)))]
(format-cljs-result m)))))
(defn get-cljs-doc [nsp sym]
(env/with-compiler-env cljs/compiler-env
(->
(ana/resolve-var {:ns ((-> @env/*compiler* :cljs.analyzer/namespaces) (symbol nsp))} (symbol sym))
(clean-meta)
(format-cljs-result))))
(defmethod core/handle "editor.clj.doc" [{:keys [ns sym loc session path result-type] :as msg}]
(let [ns (eval/normalize-ns ns path)
_ (eval/require|create-ns (symbol ns))
res (some-> (get-doc ns sym)
(assoc :loc loc :result-type result-type)
(update-in [:file] resolve-file))]
(core/respond msg "editor.clj.doc" res))
@session)
(defmethod core/handle "editor.cljs.doc" [{:keys [ns sym loc session path result-type] :as msg}]
(env/with-compiler-env cljs/compiler-env
(let [ns (eval/normalize-ns ns path)]
(eval/require|create-ns (symbol ns))
(cljs/init-cljs (symbol ns) path)
(let [clj (get-doc 'clojure.core sym)
res (if (:macro clj)
clj
(get-cljs-doc ns sym))
res (some-> res
(assoc :loc loc :result-type result-type)
(update-in [:file] resolve-file))]
(core/respond msg "editor.cljs.doc" res))))
@session)
(defmethod core/handle "docs.clj.search" [{:keys [search session] :as msg}]
(core/respond msg "doc.search.results" (find-doc search))
@session)
(defmethod core/handle "docs.cljs.search" [{:keys [search session] :as msg}]
(core/respond msg "doc.search.results" (cljs-find-doc search))
@session)