Skip to content

Commit bdfa5d7

Browse files
committed
Declare wrap-pprint, wrap-pprint-fn and wrap-version along other middleware
1 parent efb1b00 commit bdfa5d7

File tree

6 files changed

+128
-131
lines changed

6 files changed

+128
-131
lines changed

project.clj

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@
4242
cider.nrepl/wrap-macroexpand
4343
cider.nrepl/wrap-ns
4444
cider.nrepl/wrap-out
45+
cider.nrepl/wrap-pprint
46+
cider.nrepl/wrap-pprint-fn
4547
cider.nrepl/wrap-refresh
4648
cider.nrepl/wrap-resource
4749
cider.nrepl/wrap-spec
@@ -50,9 +52,7 @@
5052
cider.nrepl/wrap-trace
5153
cider.nrepl/wrap-tracker
5254
cider.nrepl/wrap-undef
53-
cider.nrepl.middleware.pprint/wrap-pprint
54-
cider.nrepl.middleware.pprint/wrap-pprint-fn
55-
cider.nrepl.middleware.version/wrap-version]}
55+
cider.nrepl/wrap-version]}
5656
:dependencies [[org.clojure/tools.nrepl "0.2.13"]
5757
;; For developing the Leiningen plugin.
5858
[leiningen-core "2.7.1"]

src/cider/nrepl.clj

Lines changed: 100 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33
[clojure.tools.nrepl.middleware.session :refer [session]]
44
[clojure.tools.nrepl.middleware.pr-values :refer [pr-values]]
55
[clojure.tools.nrepl.server :as nrepl-server]
6+
[cider.nrepl.version :as version]
67
[cider.nrepl.middleware.util.cljs :as cljs]
78
[cider.nrepl.middleware.pprint :as pprint]
8-
[cider.nrepl.middleware.version]
99
[cider.nrepl.print-method]))
1010

1111
(def DELAYS (atom nil))
@@ -60,35 +60,52 @@
6060

6161
;;; Wrappers
6262

63-
(def-wrapper wrap-debug cider.nrepl.middleware.debug/handle-debug
64-
#{"eval"}
65-
(cljs/requires-piggieback
66-
{:doc "Provide instrumentation and debugging functionality."
67-
:expects #{"eval"}
68-
:requires #{#'pprint/wrap-pprint-fn #'session}
69-
:handles {"debug-input"
70-
{:doc "Read client input on debug action."
71-
:requires {"input" "The user's reply to the input request."}
72-
:returns {"status" "done"}}
73-
"init-debugger"
74-
{:doc "Initialize the debugger so that `breakpoint` works correctly. This usually does not respond immediately. It sends a response when a breakpoint is reached or when the message is discarded."
75-
:requires {"id" "A message id that will be responded to when a breakpoint is reached."}}
76-
"debug-instrumented-defs"
77-
{:doc "Return an alist of definitions currently thought to be instrumented on each namespace. Due to Clojure's versatility, this could include false postives, but there will not be false negatives. Instrumentations inside protocols are not listed."
78-
:returns {"status" "done"
79-
"list" "The alist of (NAMESPACE . VARS) that are thought to be instrumented."}}
80-
"debug-middleware"
81-
{:doc "Debug a code form or fall back on regular eval."
82-
:requires {"id" "A message id that will be responded to when a breakpoint is reached."
83-
"code" "Code to debug, there must be a #dbg or a #break reader macro in it, or nothing will happen."
84-
"file" "File where the code is located."
85-
"ns" "Passed to \"eval\"."
86-
"point" "Position in the file where the provided code begins."}
87-
:returns {"status" "\"done\" if the message will no longer be used, or \"need-debug-input\" during debugging sessions"}}}}))
63+
(def wrap-pprint-fn-optional-arguments
64+
"Common pprint arguments for CIDER's middleware."
65+
{"pprint-fn" "The namespace-qualified name of a single-arity function to use for pretty-printing. Defaults to `clojure.pprint/pprint`."
66+
"print-length" "Value to bind to `*print-length*` when pretty-printing. Defaults to the value bound in the current REPL session."
67+
"print-level" "Value to bind to `*print-level*` when pretty-printing. Defaults to the value bound in the current REPL session."
68+
"print-meta" "Value to bind to `*print-meta*` when pretty-printing. Defaults to the value bound in the current REPL session."
69+
"print-right-margin" "Value to bind to `clojure.pprint/*print-right-margin*` when pretty-printing. Defaults to the value bound in the current REPL session."})
8870

89-
(def-wrapper wrap-enlighten cider.nrepl.middleware.enlighten/handle-enlighten
90-
:enlighten
91-
{:expects #{"eval" #'wrap-debug}})
71+
(def-wrapper wrap-pprint-fn cider.nrepl.middleware.pprint/handle-pprint-fn
72+
(fn [msg] true)
73+
{:doc "Middleware that provides a common interface for other middlewares that
74+
need to perform customisable pretty-printing.
75+
76+
A namespace-qualified name of the function to be used for printing can
77+
be optionally passed in the `:pprint-fn` slot, the default value being
78+
`clojure.pprint/pprint`.
79+
80+
The `:pprint-fn` slot will be replaced with a closure that calls the
81+
given printing function with `*print-length*`, `*print-level*`,
82+
`*print-meta*`, and `clojure.pprint/*print-right-margin*` bound to the
83+
values of the `:print-length`, `:print-level`, `:print-meta`, and
84+
`:print-right-margin` slots respectively.
85+
86+
Middlewares further down the stack can then look up the `:pprint-fn`
87+
slot and call it where necessary."
88+
:requires #{#'session}})
89+
90+
(def-wrapper wrap-pprint cider.nrepl.middleware.pprint/handle-pprint
91+
#{"eval" "load-file"}
92+
(cljs/expects-piggieback
93+
{:doc "Middleware that adds a pretty-printing option to the eval op.
94+
Passing a non-nil value in the `:pprint` slot will cause eval to call
95+
clojure.pprint/pprint on its result. The `:right-margin` slot can be
96+
used to bind `*clojure.pprint/*print-right-margin*` during the
97+
evaluation. (N.B., the encoding used to transmit the request map
98+
`msg` across the wire will convert presumably falsey values into
99+
truthy values. If you don't want something to be pretty printed,
100+
remove the `:pprint` key entirely from your request map, don't try
101+
and set the value to nil, false, or string representations of the
102+
above)."
103+
:requires #{"clone" #'pr-values #'wrap-pprint-fn}
104+
:expects #{"eval" "load-file"}
105+
:handles {"pprint-middleware"
106+
{:doc "Enhances the `eval` op by adding pretty-printing. Not an op in itself."
107+
:optional (merge wrap-pprint-fn-optional-arguments
108+
{"pprint" "If present and non-nil, pretty-print the result of evaluation."})}}}))
92109

93110
(def-wrapper wrap-apropos cider.nrepl.middleware.apropos/handle-apropos
94111
{:doc "Middleware that handles apropos requests"
@@ -124,9 +141,39 @@
124141
"complete-flush-caches"
125142
{:doc "Forces the completion backend to repopulate all its caches"}}}))
126143

144+
(def-wrapper wrap-debug cider.nrepl.middleware.debug/handle-debug
145+
#{"eval"}
146+
(cljs/requires-piggieback
147+
{:doc "Provide instrumentation and debugging functionality."
148+
:expects #{"eval"}
149+
:requires #{#'wrap-pprint-fn #'session}
150+
:handles {"debug-input"
151+
{:doc "Read client input on debug action."
152+
:requires {"input" "The user's reply to the input request."}
153+
:returns {"status" "done"}}
154+
"init-debugger"
155+
{:doc "Initialize the debugger so that `breakpoint` works correctly. This usually does not respond immediately. It sends a response when a breakpoint is reached or when the message is discarded."
156+
:requires {"id" "A message id that will be responded to when a breakpoint is reached."}}
157+
"debug-instrumented-defs"
158+
{:doc "Return an alist of definitions currently thought to be instrumented on each namespace. Due to Clojure's versatility, this could include false postives, but there will not be false negatives. Instrumentations inside protocols are not listed."
159+
:returns {"status" "done"
160+
"list" "The alist of (NAMESPACE . VARS) that are thought to be instrumented."}}
161+
"debug-middleware"
162+
{:doc "Debug a code form or fall back on regular eval."
163+
:requires {"id" "A message id that will be responded to when a breakpoint is reached."
164+
"code" "Code to debug, there must be a #dbg or a #break reader macro in it, or nothing will happen."
165+
"file" "File where the code is located."
166+
"ns" "Passed to \"eval\"."
167+
"point" "Position in the file where the provided code begins."}
168+
:returns {"status" "\"done\" if the message will no longer be used, or \"need-debug-input\" during debugging sessions"}}}}))
169+
170+
(def-wrapper wrap-enlighten cider.nrepl.middleware.enlighten/handle-enlighten
171+
:enlighten
172+
{:expects #{"eval" #'wrap-debug}})
173+
127174
(def-wrapper wrap-format cider.nrepl.middleware.format/handle-format
128175
{:doc "Middleware providing support for formatting Clojure code and EDN data."
129-
:requires #{#'pprint/wrap-pprint-fn}
176+
:requires #{#'wrap-pprint-fn}
130177
:handles {"format-code"
131178
{:doc "Reformats the given Clojure code, returning the result as a string."
132179
:requires {"code" "The code to format."}
@@ -249,10 +296,10 @@
249296

250297
(def-wrapper wrap-refresh cider.nrepl.middleware.refresh/handle-refresh
251298
{:doc "Refresh middleware."
252-
:requires #{"clone" #'pprint/wrap-pprint-fn}
299+
:requires #{"clone" #'wrap-pprint-fn}
253300
:handles {"refresh"
254301
{:doc "Reloads all changed files in dependency order."
255-
:optional (merge pprint/wrap-pprint-fn-optional-arguments
302+
:optional (merge wrap-pprint-fn-optional-arguments
256303
{"dirs" "List of directories to scan. If no directories given, defaults to all directories on the classpath."
257304
"before" "The namespace-qualified name of a zero-arity function to call before reloading."
258305
"after" "The namespace-qualified name of a zero-arity function to call after reloading."})
@@ -262,7 +309,7 @@
262309
"error-ns" "The namespace that caused reloading to fail when `status` is `:error`."}}
263310
"refresh-all"
264311
{:doc "Reloads all files in dependency order."
265-
:optional (merge pprint/wrap-pprint-fn-optional-arguments
312+
:optional (merge wrap-pprint-fn-optional-arguments
266313
{"dirs" "List of directories to scan. If no directories given, defaults to all directories on the classpath."
267314
"before" "The namespace-qualified name of a zero-arity function to call before reloading."
268315
"after" "The namespace-qualified name of a zero-arity function to call after reloading."})
@@ -305,28 +352,28 @@
305352
(cljs/requires-piggieback
306353
{:doc "Middleware that handles stacktrace requests, sending
307354
cause and stack frame info for the most recent exception."
308-
:requires #{#'session #'pprint/wrap-pprint-fn}
355+
:requires #{#'session #'wrap-pprint-fn}
309356
:expects #{}
310357
:handles {"stacktrace" {:doc "Return messages describing each cause and stack frame of the most recent exception."
311-
:optional pprint/wrap-pprint-fn-optional-arguments
358+
:optional wrap-pprint-fn-optional-arguments
312359
:returns {"status" "\"done\", or \"no-error\" if `*e` is nil"}}}}))
313360

314361
(def-wrapper wrap-test cider.nrepl.middleware.test/handle-test
315362
{:doc "Middleware that handles testing requests."
316-
:requires #{#'session #'pprint/wrap-pprint-fn}
363+
:requires #{#'session #'wrap-pprint-fn}
317364
:expects #{#'pr-values}
318365
:handles {"test"
319366
{:doc "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."
320-
:optional pprint/wrap-pprint-fn-optional-arguments}
367+
:optional wrap-pprint-fn-optional-arguments}
321368
"test-all"
322369
{:doc "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."
323-
:optional pprint/wrap-pprint-fn-optional-arguments}
370+
:optional wrap-pprint-fn-optional-arguments}
324371
"test-stacktrace"
325372
{: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."
326-
:optional pprint/wrap-pprint-fn-optional-arguments}
373+
:optional wrap-pprint-fn-optional-arguments}
327374
"retest"
328375
{: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."
329-
:optional pprint/wrap-pprint-fn-optional-arguments}}})
376+
:optional wrap-pprint-fn-optional-arguments}}})
330377

331378
(def-wrapper wrap-trace cider.nrepl.middleware.trace/handle-trace
332379
{:doc "Toggle tracing of a given var."
@@ -365,6 +412,16 @@
365412
"ns" "The current namespace"}
366413
:returns {"status" "done"}}}})
367414

415+
(def-wrapper wrap-version cider.nrepl.middleware.version/handle-version
416+
{:doc "Provides CIDER-nREPL version information."
417+
:describe-fn #'version/cider-version-reply ;; For the "describe" op. Merged into `:aux`.
418+
:handles
419+
{"cider-version"
420+
{:doc "Returns the version of the CIDER-nREPL middleware."
421+
:requires {}
422+
:returns {"cider-version" "CIDER-nREPL's version map."
423+
"status" "done"}}}})
424+
368425

369426
;;; Cider's Handler
370427

@@ -381,6 +438,8 @@
381438
wrap-macroexpand
382439
wrap-ns
383440
wrap-out
441+
wrap-pprint
442+
wrap-pprint-fn
384443
wrap-refresh
385444
wrap-resource
386445
wrap-spec
@@ -389,9 +448,7 @@
389448
wrap-trace
390449
wrap-tracker
391450
wrap-undef
392-
cider.nrepl.middleware.pprint/wrap-pprint
393-
cider.nrepl.middleware.pprint/wrap-pprint-fn
394-
cider.nrepl.middleware.version/wrap-version])
451+
wrap-version])
395452

396453
(def cider-nrepl-handler
397454
"CIDER's nREPL handler."

src/cider/nrepl/middleware/pprint.clj

Lines changed: 8 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -42,26 +42,11 @@
4242
pp-fn
4343
(throw (IllegalArgumentException. (format "%s is not resolvable to a var" sym)))))
4444

45-
(defn wrap-pprint-fn
46-
"Middleware that provides a common interface for other middlewares that need
47-
to perform customisable pretty-printing.
48-
49-
A namespace-qualified name of the function to be used for printing can be
50-
optionally passed in the `:pprint-fn` slot, the default value being
51-
`clojure.pprint/pprint`.
52-
53-
The `:pprint-fn` slot will be replaced with a closure that calls the given
54-
printing function with `*print-length*`, `*print-level*`, `*print-meta*`, and
55-
`clojure.pprint/*print-right-margin*` bound to the values of the
56-
`:print-length`, `:print-level`, `:print-meta`, and `:print-right-margin`
57-
slots respectively.
58-
59-
Middlewares further down the stack can then look up the `:pprint-fn` slot and
60-
call it where necessary."
61-
[handler]
62-
(fn [{:keys [pprint-fn print-length print-level print-meta print-right-margin session]
63-
:or {pprint-fn 'clojure.pprint/pprint}
64-
:as msg}]
45+
(defn handle-pprint-fn
46+
[handler msg]
47+
(let [{:keys [pprint-fn print-length print-level print-meta print-right-margin session]
48+
:or {pprint-fn 'clojure.pprint/pprint}}
49+
msg]
6550
(handler (assoc msg :pprint-fn (fn [object]
6651
(binding [*print-length* (or print-length (get @session #'*print-length*))
6752
*print-level* (or print-level (get @session #'*print-level*))
@@ -70,17 +55,6 @@
7055
*print-right-margin* (or print-right-margin *print-right-margin*)]
7156
((resolve-pprint-fn pprint-fn) object)))))))
7257

73-
(def wrap-pprint-fn-optional-arguments
74-
{"pprint-fn" "The namespace-qualified name of a single-arity function to use for pretty-printing. Defaults to `clojure.pprint/pprint`."
75-
"print-length" "Value to bind to `*print-length*` when pretty-printing. Defaults to the value bound in the current REPL session."
76-
"print-level" "Value to bind to `*print-level*` when pretty-printing. Defaults to the value bound in the current REPL session."
77-
"print-meta" "Value to bind to `*print-meta*` when pretty-printing. Defaults to the value bound in the current REPL session."
78-
"print-right-margin" "Value to bind to `clojure.pprint/*print-right-margin*` when pretty-printing. Defaults to the value bound in the current REPL session."})
79-
80-
(set-descriptor!
81-
#'wrap-pprint-fn
82-
{:requires #{#'session/session}})
83-
8458
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
8559

8660
(defn- pprint-writer
@@ -110,29 +84,9 @@
11084
(pprint-reply msg response))
11185
(.send transport (dissoc response :value)))))
11286

113-
(defn wrap-pprint
114-
"Middleware that adds a pretty-printing option to the eval op.
115-
Passing a non-nil value in the `:pprint` slot will cause eval to call
116-
clojure.pprint/pprint on its result. The `:right-margin` slot can be used to
117-
bind `*clojure.pprint/*print-right-margin*` during the evaluation. (N.B., the
118-
encoding used to transmit the request map `msg` across the wire will
119-
convert presumably falsey values into truthy values. If you don't
120-
want something to be pretty printed, remove the `:pprint` key
121-
entirely from your request map, don't try and set the value to nil,
122-
false, or string representations of the above)."
123-
[handler]
124-
(fn [{:keys [op pprint] :as msg}]
87+
(defn handle-pprint
88+
[handler msg]
89+
(let [{:keys [op pprint]} msg]
12590
(handler (if (and pprint (#{"eval" "load-file"} op))
12691
(assoc msg :transport (pprint-transport msg))
12792
msg))))
128-
129-
(set-descriptor!
130-
#'wrap-pprint
131-
(cljs/expects-piggieback
132-
{:requires #{"clone" #'pr-values #'wrap-pprint-fn}
133-
:expects #{"eval" "load-file"}
134-
:handles
135-
{"pprint-middleware"
136-
{:doc "Enhances the `eval` op by adding pretty-printing. Not an op in itself."
137-
:optional (merge wrap-pprint-fn-optional-arguments
138-
{"pprint" "If present and non-nil, pretty-print the result of evaluation."})}}}))

0 commit comments

Comments
 (0)