-
Notifications
You must be signed in to change notification settings - Fork 19
/
util.cljc
290 lines (225 loc) · 6.92 KB
/
util.cljc
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
279
280
281
282
283
284
285
286
287
288
289
290
(ns debux.common.util
"Utilities common for clojure and clojurescript"
(:require [clojure.string :as str]
[clojure.pprint :as pp]
[clojure.zip :as z]
[clojure.walk :as walk]
[cljs.analyzer.api :as ana] ))
;;; For internal debugging
(defmacro d
"The internal macro to debug dbg macro.
<form any> a form to be evaluated"
[form]
`(let [return# ~form]
(println ">> dbg_:" (pr-str '~form) "=>\n" (pr-str return#) "<<")
return#))
;;; config
(def config*
(atom {:debug-mode true
:ns-blacklist nil
:ns-whitelist nil
:print-seq-length 100
:indent-level 1} ))
(defn set-print-seq-length! [num]
(swap! config* assoc :print-seq-length num)
nil)
(defn set-debug-mode! [val]
(swap! config* assoc :debug-mode val)
nil)
(defn set-ns-blacklist! [blacklist]
(swap! config* assoc :ns-blacklist blacklist)
nil)
(defn set-ns-whitelist! [whitelist]
(swap! config* assoc :ns-whitelist whitelist)
nil)
(defn ns-match? [current-ns target-ns]
(-> (re-pattern (str/escape target-ns {\. "\\." \* ".*"}))
(re-matches current-ns)))
(defn in-ns-list? [current-ns ns-list]
(some #(ns-match? current-ns %) ns-list))
(defn debug-enabled? [current-ns]
(let [{:keys [debug-mode ns-whitelist ns-blacklist]} @config*]
(when debug-mode
(cond
(and (empty? ns-whitelist)
(empty? ns-blacklist))
true
(empty? ns-whitelist)
(not (in-ns-list? current-ns ns-blacklist))
(empty? ns-blacklist)
(in-ns-list? current-ns ns-whitelist)
:else
(and (in-ns-list? current-ns ns-whitelist)
(not (in-ns-list? current-ns ns-blacklist)) )) )))
;;; general
(defn cljs-env? [env]
(boolean (:ns env)))
(defn lazy-seq? [coll]
(instance? clojure.lang.IPending coll))
(defn replace-& [v]
(walk/postwalk-replace {'& ''&} v))
(defn vec->map [v]
(apply hash-map v))
;;; zipper
(defn sequential-zip [root]
(z/zipper sequential?
identity
(fn [x children]
(if (vector? x) (vec children) children))
root))
(defn right-or-next [loc]
(if-let [right (z/right loc)]
;; in case of (... (+ a b) c) or (... a b)
right
(if (sequential? (z/node loc))
(let [rightmost (-> loc z/down z/rightmost)]
(if (sequential? (z/node rightmost))
;; in case of (... (+ a (* b c)))
(recur rightmost)
;; in case of (... (+ a b))
(-> rightmost z/next) ))
;; in case of (... a)
(-> loc z/next) )))
;;; symbol with namespace
#?(:clj
(defn- var->symbol [v]
(let [m (meta v)
ns (str (ns-name (:ns m)))
name (str (:name m))]
(symbol ns name) )))
#?(:clj
(defn- ns-symbol-for-clj [sym]
(if-let [v (resolve sym)]
(var->symbol v)
sym) ))
#?(:clj
(defn- ns-symbol-for-cljs [sym env]
(if-let [meta (ana/resolve env sym)]
;; normal symbol
(let [[ns name] (str/split (str (:name meta)) #"/")]
;; The special symbol `.` must be handled in the following special symbol part.
;; However, the special symbol `.` returns meta {:name / :ns nil}, which may be a bug.
(if (nil? ns)
sym
(symbol ns name)))
;; special symbols except for `.`
sym) ))
#?(:clj
(defn ns-symbol [sym & [env]]
(if (symbol? sym)
(if (cljs-env? env)
(ns-symbol-for-cljs sym env)
(ns-symbol-for-clj sym))
sym) ))
;;; :dup option
(defn eval-changed?
[evals form return]
;; init
(when-not (contains? @evals form)
(swap! evals assoc form ""))
;; update
(and (not= return (get @evals form))
(swap! evals assoc form return) ))
;;; print
(defn take-n-if-seq [n result]
(if (seq? result)
(take (or n (:print-seq-length @config*)) result)
result))
(defn truncate [s]
(if (> (count s) 70)
(str (.substring s 0 70) " ...")
s))
(defn- make-bars-
[times]
(apply str (repeat times "|")))
(def make-bars (memoize make-bars-))
(defn prepend-bars
[line indent-level]
(let [indent-level' (if (> indent-level 1)
(dec indent-level)
indent-level)]
(str (make-bars indent-level') " " line)))
(defn print-form-with-indent
[form indent-level]
(println (prepend-bars form indent-level))
(flush))
(defn form-header [form & [msg]]
(str (truncate (pr-str form))
(and msg (str " <" msg ">"))
" =>"))
(defn prepend-blanks
[lines]
(mapv #(str " " %) lines))
(defn pprint-result-with-indent
[result indent-level]
(let [pprint (str/trim (with-out-str (pp/pprint result)))]
(println (->> (str/split pprint #"\n")
prepend-blanks
(mapv #(prepend-bars % indent-level))
(str/join "\n")))
(flush) ))
(defn insert-blank-line []
#?(:clj (do (println " ") (flush))
:cljs (.log js/console " ")))
;;; parse options
(defn parse-opts
[opts]
(loop [opts opts
acc {}]
(let [f (first opts)
s (second opts)]
(cond
(empty? opts)
(assoc acc :evals '(atom {}))
(number? f)
(recur (next opts) (assoc acc :n f))
(string? f)
(recur (next opts) (assoc acc :msg f))
(= f :if)
(recur (nnext opts) (assoc acc :condition s))
(= f :dup)
(recur (next opts) (assoc acc :dup true))
;;; for clojurescript
(= f :js)
(recur (next opts) (assoc acc :js true))
(#{:once :o} f)
(recur (next opts) (assoc acc :once true))
(#{:style :s} f)
(recur (nnext opts) (assoc acc :style s))
(= f :clog)
(recur (next opts) (assoc acc :clog true)) ))))
;;; quote the value parts of a map
(defn quote-val [[k v]]
`[~k '~v])
(defn quote-vals [m]
(->> (map quote-val m)
(into {})))
;;; for recur processing
(defn include-recur? [form]
(((comp set flatten) form) 'recur))
#?(:clj
(defn final-target? [sym targets env]
(let [ns-sym (ns-symbol sym env)]
(or (get targets ns-sym)
(some #(= % ns-sym)
'[clojure.core/defn clojure.core/defn- clojure.core/fn
cljs.core/defn cljs.core/defn- cljs.core/fn] )))))
(defn o-skip? [sym]
(= 'debux.common.macro-specs/o-skip sym))
;;; spy functions
(def spy-first
(fn [result quoted-form {:keys [n] :as opts}]
(print-form-with-indent (form-header quoted-form) 1)
(pprint-result-with-indent (take-n-if-seq n result) 1)
result))
(def spy-last
(fn [quoted-form {:keys [n] :as opts} result]
(print-form-with-indent (form-header quoted-form) 1)
(pprint-result-with-indent (take-n-if-seq n result) 1)
result))
(defn spy-comp [quoted-form form {:keys [n] :as opts}]
(fn [& arg]
(let [result (apply form arg)]
(print-form-with-indent (form-header quoted-form) 1)
(pprint-result-with-indent (take-n-if-seq n result) 1)
result) ))