25
25
26
26
; ;; Code:
27
27
28
+ (require 'map )
29
+ (require 'seq )
30
+ (require 'subr-x )
31
+
32
+ (require 'clojure-mode )
28
33
(require 'spinner )
29
- (require 'nrepl-client )
34
+
35
+ (require 'cider-compat )
30
36
(require 'cider-connection )
31
37
(require 'cider-common )
32
38
(require 'cider-util )
33
- (require 'clojure-mode )
34
-
35
- (require 'subr-x )
36
- (require 'cider-compat )
37
- (require 'seq )
39
+ (require 'nrepl-client )
38
40
39
41
40
42
; ;; Eval spinner
@@ -195,71 +197,71 @@ buffer, defaults to (cider-current-repl)."
195
197
If NS is non-nil, include it in the eval request."
196
198
(nrepl-sync-request:eval input (or connection (cider-current-repl)) ns))
197
199
198
- (defcustom cider-pprint -fn 'pprint
199
- " Sets the function to use when pretty- printing evaluation results .
200
+ (defcustom cider-print -fn 'pprint
201
+ " Sets the function to use for printing.
200
202
201
- The value must be one of the following symbols:
203
+ nil – to defer to nREPL to choose the printing function. This will use
204
+ the bound value of \\= `nrepl.middleware.print/*print-fn*\\= `, which
205
+ defaults to the equivalent of \\= `clojure.core/pr\\= `.
202
206
203
- `pprint' - to use \\= `clojure.pprint/pprint \\= `
207
+ `pr' – to use the equivalent of \\= `clojure.core/pr \\= `.
204
208
205
- `fipp' - to use the Fast Idiomatic Pretty Printer, approximately 5-10x
206
- faster than \\= `clojure.core/pprint\\= ` (this is the default)
209
+ `pprint' – to use \\= `clojure.pprint/pprint\\= ` (this is the default).
207
210
208
- `puget' - to use Puget, which provides canonical serialization of data on
209
- top of fipp, but at a slight performance cost
211
+ `fipp' – to use the Fast Idiomatic Pretty Printer, approximately 5-10x
212
+ faster than \\= `clojure.core/pprint \\= `.
210
213
211
- `zprint' - to use zprint, a fast and flexible alternative to the libraries
214
+ `puget' – to use Puget, which provides canonical serialization of data on
215
+ top of fipp, but at a slight performance cost.
216
+
217
+ `zprint' – to use zprint, a fast and flexible alternative to the libraries
212
218
mentioned above.
213
219
214
- Alternatively, can be the namespace-qualified name of a Clojure function of
215
- two arguments - an object to print and an options map. The options map will
216
- have string keys. If the function
217
- cannot be resolved, an exception will be thrown.
218
-
219
- The function should ideally have a two-arity variant that accepts the
220
- object to print and a map of configuration options for the printer. See
221
- `cider-pprint-options' for details.
222
-
223
- The function is also assumed to respect the contract of
224
- \\= `clojure.pprint/pprint\\= ` with respect to the bound values of
225
- \\= `*print-length*\\= `, \\= `*print-level*\\= `, \\= `*print-meta*\\= `, and
226
- \\= `clojure.pprint/*print-right-margin*\\= `. Those would normally serve as
227
- fallback values when a map of print options is not supplied explicitly."
228
- :type '(choice (const pprint)
220
+ Alternatively can be the namespace-qualified name of a Clojure var whose
221
+ function takes three arguments: the object to print, the
222
+ \\= `java.io.PrintWriter\\= ` to print on, and a (possibly nil) map of
223
+ options. If the function cannot be resolved, will behave as if set to
224
+ nil."
225
+ :type '(choice (const nil )
226
+ (const pr)
227
+ (const pprint)
229
228
(const fipp)
230
229
(const puget)
231
230
(const zprint)
232
231
string)
233
232
:group 'cider
234
- :package-version '(cider . " 0.11 .0" ))
233
+ :package-version '(cider . " 0.21 .0" ))
235
234
236
- (defcustom cider-pprint-options nil
237
- " A list of options for the pretty-printer that will be converted to a map.
238
- Note that map can only have string keys, so the printer functions should be
239
- able to handle those. Here's an example for `pprint' :
235
+ (defcustom cider-print-options nil
236
+ " A map of options that will be passed to `cider-print-fn' .
237
+ Here's an example for `pprint' :
240
238
241
- '(dict \" length\" 50 \" right-margin\" 70)"
239
+ '(( \" length\" 50) ( \" right-margin\" 70) )"
242
240
:type 'list
243
241
:group 'cider
244
- :package-version '(cider . " 0.20.0" ))
242
+ :package-version '(cider . " 0.21.0" ))
243
+
244
+ (make-obsolete-variable 'cider-pprint-fn 'cider-print-fn " 0.21" )
245
+ (make-obsolete-variable 'cider-pprint-options 'cider-print-options " 0.21" )
245
246
246
- (defun cider--pprint-fn ()
247
- " Return the value to send in the pprint-fn slot of messages."
248
- (pcase cider-pprint-fn
249
247
(defcustom cider-print-quota (* 1024 1024 )
250
248
" A hard limit on the number of bytes to return from any printing operation.
251
249
Set to nil for no limit."
252
250
:type 'integer
253
251
:group 'cider
254
252
:package-version '(cider . " 0.21.0" ))
255
253
254
+ (defun cider--print-fn ()
255
+ " Return the value to send in the nrepl.middleware.print/print slot."
256
+ (pcase cider-print-fn
257
+ (`pr " cider.nrepl.pprint/pr" )
256
258
(`pprint " cider.nrepl.pprint/pprint" )
257
- (`fipp " cider.nrepl.pprint/fipp-pprint" )
258
- (`puget " cider.nrepl.pprint/puget-pprint" )
259
+ (`fipp " cider.nrepl.pprint/fipp-pprint" )
260
+ (`puget " cider.nrepl.pprint/puget-pprint" )
259
261
(`zprint " zprint.core/zprint-str" )
260
- (_ cider-pprint -fn)))
262
+ (_ cider-print -fn)))
261
263
262
- (defvar cider--pprint -options-mapping
264
+ (defvar cider--print -options-mapping
263
265
'((right-margin
264
266
((fipp . width) (puget . width) (zprint . width)))
265
267
(length
@@ -268,35 +270,49 @@ Set to nil for no limit."
268
270
((fipp . print-level) (puget . print-level) (zprint . max-depth))))
269
271
" A mapping of print option for the various supported print engines." )
270
272
271
- (defun cider--pprint -option (name printer )
272
- " Covert the generic NAME to its PRINTER specific variant.
273
+ (defun cider--print -option (name printer )
274
+ " Convert the generic NAME to its PRINTER specific variant.
273
275
E.g. pprint's right-margin would become width for fipp.
274
276
The function is useful when you want to generate dynamically
275
277
print options.
276
278
277
279
NAME can be a string or a symbol. PRINTER has to be a symbol.
278
280
The result will be a string."
279
281
(let* ((name (cider-maybe-intern name))
280
- (result (cdr (assoc printer (cadr (assoc name cider--pprint -options-mapping))))))
282
+ (result (cdr (assoc printer (cadr (assoc name cider--print -options-mapping))))))
281
283
(symbol-name (or result name))))
282
284
283
- (defun cider--nrepl-pprint-request-plist (right-margin &optional pprint-fn )
284
- " Plist to be appended to an eval request to make it use pprint.
285
- PPRINT-FN is the name of the Clojure function to use.
286
- RIGHT-MARGIN specifies the maximum column-width of the pretty-printed
287
- result, and is included in the request if non-nil."
288
- (let* ((print-options (or cider-pprint-options (nrepl-dict))))
289
- (when right-margin
290
- (setq print-options (nrepl-dict-put print-options (cider--pprint-option " right-margin" cider-pprint-fn) right-margin)))
291
- (append `(" nrepl.middleware.print/print" ,(or pprint-fn (cider--pprint-fn))
292
- " nrepl.middleware.print/stream?" " 1" )
293
- (when cider-print-quota
294
- `(" nrepl.middleware.print/quota" , cider-print-quota ))
295
- (and (not (nrepl-dict-empty-p print-options)) `(" nrepl.middleware.print/options" , print-options )))))
296
-
297
- (defun cider--nrepl-content-type-plist ()
298
- " Plist to be appended to an eval request to make it use content-types."
299
- '(" content-type" " true" ))
285
+ (defun cider--nrepl-print-request-map (&optional right-margin )
286
+ " Map to merge into requests that require pretty-printing.
287
+ RIGHT-MARGIN specifies the maximum column-width of the printed result, and
288
+ is included in the request if non-nil."
289
+ (let* ((width-option (cider--print-option " right-margin" cider-print-fn))
290
+ (print-options (thread-last
291
+ (map-merge 'hash-table
292
+ `((, width-option , right-margin ))
293
+ cider-print-options)
294
+ (map-pairs )
295
+ (seq-mapcat #'identity )
296
+ (apply #'nrepl-dict ))))
297
+ (map-merge 'list
298
+ `((" nrepl.middleware.print/print" ,(cider--print-fn))
299
+ (" nrepl.middleware.print/stream?" " 1" ))
300
+ (when cider-print-quota
301
+ `((" nrepl.middleware.print/quota" , cider-print-quota )))
302
+ (unless (nrepl-dict-empty-p print-options)
303
+ `((" nrepl.middleware.print/options" , print-options ))))))
304
+
305
+ (defun cider--nrepl-pr-request-map ()
306
+ " Map to merge into requests that do not require pretty printing."
307
+ (map-merge 'list
308
+ `((" nrepl.middleware.print/print" " cider.nrepl.pprint/pr"
309
+ " nrepl.middleware.print/stream?" nil ))
310
+ (when cider-print-quota
311
+ `((" nrepl.middleware.print/quota" , cider-print-quota )))))
312
+
313
+ (defun cider--nrepl-content-type-map ()
314
+ " Map to be merged into an eval request to make it use content-types."
315
+ '((" content-type" " true" )))
300
316
301
317
(defun cider-tooling-eval (input callback &optional ns connection )
302
318
" Send the request INPUT to CONNECTION and register the CALLBACK.
@@ -595,10 +611,13 @@ The result entries are relative to the classpath."
595
611
596
612
(defun cider-sync-request:format-edn (edn right-margin )
597
613
" Perform \" format-edn\" op with EDN and RIGHT-MARGIN."
598
- (let* ((response (thread-first `(" op" " format-edn"
599
- " edn" , edn )
600
- (append (cider--nrepl-pprint-request-plist right-margin))
601
- (cider-nrepl-send-sync-request)))
614
+ (let* ((request (thread-last
615
+ (map-merge 'list
616
+ `((" op" " format-edn" )
617
+ (" edn" , edn ))
618
+ (cider--nrepl-print-request-map right-margin))
619
+ (seq-mapcat #'identity )))
620
+ (response (cider-nrepl-send-sync-request request))
602
621
(err (nrepl-dict-get response " err" )))
603
622
(when err
604
623
; ; err will be a stacktrace with a first line that looks like:
0 commit comments