Skip to content

Commit 5d89df8

Browse files
committed
Use nrepl.middleware.print
1 parent e8f857b commit 5d89df8

File tree

15 files changed

+131
-162
lines changed

15 files changed

+131
-162
lines changed

README.md

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -268,8 +268,6 @@ Middleware | Op(s) | Description
268268
`wrap-macroexpand`| `macroexpand/macroexpand-1/macroexpand-all/macroexpand-step` | Macroexpand a Clojure form.
269269
`wrap-ns` | `ns-list/ns-vars/ns-path/ns-load-all/ns-aliases` | Namespace browsing & loading.
270270
`wrap-spec` | `spec-list/spec-form/spec-example` | Spec browsing.
271-
`wrap-pprint` | - | **(DEPRECATED)** Adds pretty-printing support to code evaluation. It also installs a dummy `pprint-middleware` op. Thus `wrap-pprint` is discoverable through the `describe` op.
272-
`wrap-pprint-fn` | - | **(DEPRECATED)** Provides a common pretty-printing interface for other middlewares that need to perform customisable pretty-printing.
273271
`wrap-profile` | `toggle-profile/toggle-profile-ns/is-var-profiled/profile-summary/profile-var-summary/clear-profile/get-max-samples/set-max-samples` | Provides profiling support based on the [profile](https://github.com/thunknyc/profile) library.
274272
`wrap-refresh` | `refresh/refresh-all/refresh-clear` | Code reloading.
275273
`wrap-resource` | `resource` | Return resource path.

project.clj

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,6 @@
9595
cider.nrepl/wrap-macroexpand
9696
cider.nrepl/wrap-ns
9797
cider.nrepl/wrap-out
98-
cider.nrepl/wrap-pprint-fn
9998
cider.nrepl/wrap-profile
10099
cider.nrepl/wrap-refresh
101100
cider.nrepl/wrap-resource

src/cider/nrepl.clj

Lines changed: 17 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,11 @@
22
(:require
33
[cider.nrepl.version :as version]
44
[cider.nrepl.middleware.util.cljs :as cljs]
5-
[cider.nrepl.middleware.pprint :as pprint]
65
[cider.nrepl.print-method]
76
[nrepl.middleware :refer [set-descriptor!]]
87
[nrepl.middleware.caught :refer [wrap-caught]]
8+
[nrepl.middleware.print :refer [wrap-print wrap-print-optional-arguments]]
99
[nrepl.middleware.session :refer [session]]
10-
[nrepl.middleware.pr-values :refer [pr-values]]
1110
[nrepl.server :as nrepl-server]))
1211

1312
(def delayed-handlers
@@ -80,34 +79,10 @@
8079
;; makes the code a bit more complex, but improves a lot the startup time
8180
;; as almost nothing gets loaded during REPL boot time.
8281

83-
(def wrap-pprint-fn-optional-arguments
84-
"Common pprint arguments for CIDER's middleware."
85-
{"pprint-fn" "The namespace-qualified name of a 1 or 2-arity function to use for pretty-printing. Defaults to `cider.nrepl.pprint/pprint`."
86-
"print-options" "A map of configuration entries that the pprint-fn will understand. Those would typically be specific to the pprint-fn in question."})
87-
88-
(def-wrapper wrap-pprint-fn cider.nrepl.middleware.pprint/handle-pprint-fn
89-
(fn [msg] true)
90-
{:doc "Middleware that provides a common interface for other middlewares that
91-
need to perform customisable pretty-printing.
92-
93-
A namespace-qualified name of the function to be used for printing can
94-
be optionally passed in the `:pprint-fn` slot, the default value being
95-
`cider.nrepl.pprint/pprint`. The function name can be passed as
96-
a string or symbol. Note that function should take 1 or two params - the
97-
object to print and the an optional map of print params. The params should
98-
be passed as `:print-options` - a map of key/value params.
99-
100-
The `:pprint-fn` slot will be replaced with the var that maps to the name
101-
that was initially passed.
102-
103-
Middlewares further down the stack can then look up the `:pprint-fn`
104-
slot, call it where necessary, and pass it the value of the `:print-options` slot."
105-
:requires #{#'session}
106-
:expects #{"eval" "load-file"}})
107-
10882
(def-wrapper wrap-content-type cider.nrepl.middleware.content-type/handle-content-type
10983
#{"eval"}
11084
{:doc "Middleware that adds `content-type` annotations to the result of the the eval op."
85+
:requires #{#'wrap-print}
11186
:expects #{"eval" "load-file"}
11287
:returns {"content-type" "A MIME type for the response, if one can be detected."
11388
"content-transfer-encoding" "The encoding (if any) of the content."
@@ -163,7 +138,7 @@
163138
(cljs/requires-piggieback
164139
{:doc "Provide instrumentation and debugging functionality."
165140
:expects #{"eval"}
166-
:requires #{#'wrap-pprint-fn #'session}
141+
:requires #{#'wrap-print #'session}
167142
:handles {"debug-input"
168143
{:doc "Read client input on debug action."
169144
:requires {"input" "The user's reply to the input request."}
@@ -190,7 +165,7 @@
190165

191166
(def-wrapper wrap-format cider.nrepl.middleware.format/handle-format
192167
{:doc "Middleware providing support for formatting Clojure code and EDN data."
193-
:requires #{#'wrap-pprint-fn}
168+
:requires #{#'wrap-print}
194169
:handles {"format-code"
195170
{:doc "Reformats the given Clojure code, returning the result as a string."
196171
:requires {"code" "The code to format."}
@@ -199,8 +174,7 @@
199174
"format-edn"
200175
{:doc "Reformats the given EDN data, returning the result as a string."
201176
:requires {"edn" "The data to format."}
202-
:optional {"print-options" "A map of params for the print function."
203-
"pprint-fn" "Fully qualified name of the print function to be used."}
177+
:optional wrap-print-optional-arguments
204178
:returns {"formatted-edn" "The formatted data."}}}})
205179

206180
(def-wrapper wrap-info cider.nrepl.middleware.info/handle-info
@@ -229,7 +203,7 @@
229203
in the `:inspect` slot will cause the last value returned by eval to
230204
be inspected. Returns a string representation of the resulting
231205
inspector's state in the `:value` slot."
232-
:requires #{"clone" #'pr-values #'wrap-caught}
206+
:requires #{"clone" #'wrap-caught #'wrap-print}
233207
:expects #{"eval"}
234208
:handles {"inspect-pop"
235209
{:doc "Moves one level up in the inspector stack."
@@ -359,10 +333,10 @@
359333

360334
(def-wrapper wrap-refresh cider.nrepl.middleware.refresh/handle-refresh
361335
{:doc "Refresh middleware."
362-
:requires #{"clone" #'wrap-pprint-fn}
336+
:requires #{"clone" #'wrap-print}
363337
:handles {"refresh"
364338
{:doc "Reloads all changed files in dependency order."
365-
:optional (merge wrap-pprint-fn-optional-arguments
339+
:optional (merge wrap-print-optional-arguments
366340
{"dirs" "List of directories to scan. If no directories given, defaults to all directories on the classpath."
367341
"before" "The namespace-qualified name of a zero-arity function to call before reloading."
368342
"after" "The namespace-qualified name of a zero-arity function to call after reloading."})
@@ -372,7 +346,7 @@
372346
"error-ns" "The namespace that caused reloading to fail when `status` is `:error`."}}
373347
"refresh-all"
374348
{:doc "Reloads all files in dependency order."
375-
:optional (merge wrap-pprint-fn-optional-arguments
349+
:optional (merge wrap-print-optional-arguments
376350
{"dirs" "List of directories to scan. If no directories given, defaults to all directories on the classpath."
377351
"before" "The namespace-qualified name of a zero-arity function to call before reloading."
378352
"after" "The namespace-qualified name of a zero-arity function to call after reloading."})
@@ -415,31 +389,30 @@
415389
(cljs/requires-piggieback
416390
{:doc "Middleware that handles stacktrace requests, sending
417391
cause and stack frame info for the most recent exception."
418-
:requires #{#'session #'wrap-pprint-fn}
392+
:requires #{#'session #'wrap-print}
419393
:expects #{}
420394
:handles {"stacktrace" {:doc "Return messages describing each cause and stack frame of the most recent exception."
421-
:optional wrap-pprint-fn-optional-arguments
395+
:optional wrap-print-optional-arguments
422396
:returns {"status" "\"done\", or \"no-error\" if `*e` is nil"}}}}))
423397

424398
(def-wrapper wrap-test cider.nrepl.middleware.test/handle-test
425399
{:doc "Middleware that handles testing requests."
426-
:requires #{#'session #'wrap-pprint-fn}
427-
:expects #{#'pr-values}
400+
:requires #{#'session #'wrap-print}
428401
:handles {"test-var-query"
429402
{:doc "Run tests specified by the `var-query` and return results. Results are cached for exception retrieval and to enable re-running of failed/erring tests."
430-
:optional wrap-pprint-fn-optional-arguments}
403+
:optional wrap-print-optional-arguments}
431404
"test"
432405
{:doc "[DEPRECATED] Run tests in the specified namespace and return results. This accepts a set of `tests` to be run; if nil, runs all tests. Results are cached for exception retrieval and to enable re-running of failed/erring tests."
433-
:optional wrap-pprint-fn-optional-arguments}
406+
:optional wrap-print-optional-arguments}
434407
"test-all"
435408
{:doc "[DEPRECATED] Run all tests in the project. If `load?` is truthy, all project namespaces are loaded; otherwise, only tests in presently loaded namespaces are run. Results are cached for exception retrieval and to enable re-running of failed/erring tests."
436-
:optional wrap-pprint-fn-optional-arguments}
409+
:optional wrap-print-optional-arguments}
437410
"test-stacktrace"
438411
{:doc "Rerun all tests that did not pass when last run. Results are cached for exception retrieval and to enable re-running of failed/erring tests."
439-
:optional wrap-pprint-fn-optional-arguments}
412+
:optional wrap-print-optional-arguments}
440413
"retest"
441414
{:doc "Return exception cause and stack frame info for an erring test via the `stacktrace` middleware. The error to be retrieved is referenced by namespace, var name, and assertion index within the var."
442-
:optional wrap-pprint-fn-optional-arguments}}})
415+
:optional wrap-print-optional-arguments}}})
443416

444417
(def-wrapper wrap-trace cider.nrepl.middleware.trace/handle-trace
445418
{:doc "Toggle tracing of a given var."
@@ -505,7 +478,6 @@
505478
wrap-out
506479
wrap-content-type
507480
wrap-slurp
508-
wrap-pprint-fn
509481
wrap-profile
510482
wrap-refresh
511483
wrap-resource

src/cider/nrepl/middleware/debug.clj

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
[cider.nrepl.middleware.util.instrument :as ins]
99
[cider.nrepl.middleware.util.nrepl :refer [notify-client]]
1010
[nrepl.middleware.interruptible-eval :refer [*msg*]]
11+
[nrepl.middleware.print :as print]
1112
[nrepl.middleware.session :as session]
1213
[nrepl.misc :refer [response-for]]
1314
[nrepl.transport :as transport]
@@ -216,7 +217,7 @@ this map (identified by a key), and will `dissoc` it afterwards."}
216217
(when-not (instance? ThreadDeath root-ex#)
217218
(debugger-send
218219
{:status :eval-error
219-
:causes [(let [causes# (stacktrace/analyze-causes e# (:pprint-fn *msg*) (:print-options *msg*))]
220+
:causes [(let [causes# (stacktrace/analyze-causes e# (::print/print-fn *msg*))]
220221
(when (coll? causes#) (last causes#)))]})))
221222
error#))]
222223
(if (= error# ~sym)
@@ -290,7 +291,7 @@ this map (identified by a key), and will `dissoc` it afterwards."}
290291
:causes [{:class "StackTrace"
291292
:message "Harmless user-requested stacktrace"
292293
:stacktrace (-> (Exception. "Dummy")
293-
(stacktrace/analyze-causes (:pprint-fn *msg*) (:print-options *msg*))
294+
(stacktrace/analyze-causes (::print/print-fn *msg*))
294295
last :stacktrace)}]}))
295296

296297
(def debug-commands

src/cider/nrepl/middleware/format.clj

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,10 @@
77
[clojure.string :as str]
88
[clojure.tools.reader.edn :as edn]
99
[clojure.tools.reader.reader-types :as readers]
10-
[clojure.walk :as walk]))
10+
[clojure.walk :as walk]
11+
[nrepl.middleware.print :as print])
12+
(:import
13+
(java.io StringWriter)))
1114

1215
;;; Code formatting
1316
(defn- keyword->symbol [kw]
@@ -43,15 +46,18 @@
4346
(recur (conj forms form)))))))
4447

4548
(defn- format-edn
46-
[edn pprint-fn print-options]
49+
[edn print-fn]
4750
(->> (read-edn edn)
48-
(map #(pprint-fn % print-options))
51+
(map (fn [value]
52+
(let [writer (StringWriter.)]
53+
(print-fn value writer)
54+
(str writer))))
4955
(str/join "\n")
50-
str/trim))
56+
(str/trim)))
5157

5258
(defn format-edn-reply
53-
[{:keys [edn pprint-fn print-options] :as msg}]
54-
{:formatted-edn (format-edn edn pprint-fn print-options)})
59+
[{:keys [edn ::print/print-fn] :as msg}]
60+
{:formatted-edn (format-edn edn print-fn)})
5561

5662
;;; Middleware op handling
5763
(defn handle-format [handler msg]

src/cider/nrepl/middleware/inspect.clj

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,18 +16,21 @@
1616
(alter-meta! update ::inspector #(apply f % args))
1717
(get ::inspector)))
1818

19+
(defn- inspector-response
20+
([msg inspector]
21+
(inspector-response msg inspector {:status :done}))
22+
([msg {:keys [rendered] :as inspector} resp]
23+
(let [value (binding [*print-length* nil]
24+
(pr-str rendered))]
25+
(response-for msg resp {:value value}))))
26+
1927
(defn inspect-reply
2028
[{:keys [page-size transport] :as msg} eval-response]
2129
(let [value (cljs/response-value msg eval-response)
2230
page-size (or page-size 32)
2331
inspector (swap-inspector! msg #(-> (assoc % :page-size page-size)
2432
(inspect/start value)))]
25-
(binding [*print-length* nil]
26-
;; Remove print-length limit because it breaks the output in the middle of
27-
;; the page when inspecting long sequences.
28-
(transport/send
29-
transport
30-
(response-for msg :value (:rendered inspector))))))
33+
(transport/send transport (inspector-response msg inspector {}))))
3134

3235
(defn inspector-transport
3336
[{:keys [^Transport transport, session] :as msg}]
@@ -60,9 +63,6 @@
6063
[handler msg]
6164
(handler (eval-msg msg)))
6265

63-
(defn- inspector-response [msg inspector]
64-
(response-for msg :value (:rendered inspector) :status :done))
65-
6666
(defn pop-reply [msg]
6767
(inspector-response msg (swap-inspector! msg inspect/up)))
6868

src/cider/nrepl/middleware/pprint.clj

Lines changed: 0 additions & 29 deletions
This file was deleted.

src/cider/nrepl/middleware/refresh.clj

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
[clojure.tools.namespace.reload :as reload]
1313
[clojure.tools.namespace.track :as track]
1414
[nrepl.middleware.interruptible-eval :refer [*msg*]]
15+
[nrepl.middleware.print :as print]
1516
[nrepl.misc :refer [response-for]]
1617
[nrepl.transport :as transport]
1718
[orchard.misc :as u]))
@@ -74,8 +75,8 @@
7475
(format "%s is not a function of no arguments" sym))))
7576

7677
(binding [*msg* msg
77-
*out* (get @session #'*out*)
78-
*err* (get @session #'*err*)]
78+
*out* (print/replying-PrintWriter :out msg {})
79+
*err* (print/replying-PrintWriter :err msg {})]
7980
(do
8081
(when (var? the-var)
8182
(@the-var))
@@ -90,16 +91,16 @@
9091

9192
(defn- error-reply
9293
[{:keys [error error-ns]}
93-
{:keys [pprint-fn print-options session transport] :as msg}]
94+
{:keys [::print/print-fn session transport] :as msg}]
9495

9596
(transport/send
9697
transport
9798
(response-for msg (cond-> {:status :error}
98-
error (assoc :error (analyze-causes error pprint-fn print-options))
99+
error (assoc :error (analyze-causes error print-fn))
99100
error-ns (assoc :error-ns error-ns))))
100101

101102
(binding [*msg* msg
102-
*err* (get @session #'*err*)]
103+
*err* (print/replying-PrintWriter :err msg {})]
103104
(repl-caught error)))
104105

105106
(defn- result-reply

0 commit comments

Comments
 (0)