Skip to content

Commit 264da40

Browse files
committed
Add inspect-print-current-value op
This PR adds the `inspect-print-current-value` op. It prints the current value of the inspector with the user provided `::print/print-fn` or a default one. It is used by CIDER in the `cider-inspector-print-current-value` command, bound to "P" in the inspector, to print it's current value.
1 parent 729c172 commit 264da40

File tree

5 files changed

+96
-1
lines changed

5 files changed

+96
-1
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
## master (unreleased)
44

5+
* [#933](https://github.com/clojure-emacs/cider-nrepl/pull/933): Add the `cider-inspector-print-current-value` command to print the current value of the inspector.
6+
57
## 0.55.2 (2025-04-18)
68

79
* Bump `orchard` to [0.34.0](https://github.com/clojure-emacs/orchard/blob/master/CHANGELOG.md#0340-2025-04-18).

doc/modules/ROOT/pages/nrepl-api/ops.adoc

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -576,6 +576,30 @@ Returns::
576576

577577

578578

579+
=== `inspect-print-current-value`
580+
581+
Print the current value of the inspector.
582+
583+
Required parameters::
584+
* `:session` The current session
585+
586+
587+
Optional parameters::
588+
* `:nrepl.middleware.print/buffer-size` The size of the buffer to use when streaming results. Defaults to 1024.
589+
* `:nrepl.middleware.print/keys` A seq of the keys in the response whose values should be printed.
590+
* `:nrepl.middleware.print/options` A map of options to pass to the printing function. Defaults to ``nil``.
591+
* `:nrepl.middleware.print/print` A fully-qualified symbol naming a var whose function to use for printing. Must point to a function with signature [value writer options].
592+
* `:nrepl.middleware.print/quota` A hard limit on the number of bytes printed for each value.
593+
* `:nrepl.middleware.print/stream?` If logical true, the result of printing each value will be streamed to the client over one or more messages.
594+
595+
596+
Returns::
597+
* `:path` Printed representation of current inspector path.
598+
* `:status` "done"
599+
* `:value` The inspector result. Contains a specially-formatted string that can be ``read`` and then rendered client-side.
600+
601+
602+
579603
=== `inspect-push`
580604

581605
Inspects the inside value specified by index.

src/cider/nrepl.clj

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -313,7 +313,12 @@ Note: Documentation may be incomplete; not all return keys are described."
313313
inspector's state in the `:value` slot."
314314
:requires #{"clone" #'wrap-caught #'wrap-print}
315315
:expects #{"eval"}
316-
:handles {"inspect-pop"
316+
:handles {"inspect-print-current-value"
317+
{:doc "Print the current value of the inspector."
318+
:requires {"session" "The current session"}
319+
:optional wrap-print-optional-arguments
320+
:returns inspector-returns}
321+
"inspect-pop"
317322
{:doc "Moves one level up in the inspector stack."
318323
:requires {"session" "The current session"}
319324
:returns inspector-returns}

src/cider/nrepl/middleware/inspect.clj

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
(ns cider.nrepl.middleware.inspect
22
(:require
3+
[cider.nrepl.middleware.util :refer [respond-to]]
34
[cider.nrepl.middleware.util.cljs :as cljs]
45
[cider.nrepl.middleware.util.error-handling
56
:refer [eval-interceptor-transport with-safe-transport]]
7+
[cider.nrepl.pprint :as pprint]
8+
[nrepl.middleware.print :as print]
69
[nrepl.misc :refer [response-for]]
710
[nrepl.transport :as transport]
811
[orchard.inspect :as inspect]))
@@ -107,6 +110,15 @@
107110
(defn def-current-value [msg]
108111
(inspector-response msg (swap-inspector! msg inspect/def-current-value (symbol (:ns msg)) (:var-name msg))))
109112

113+
(defn print-current-value-reply [{:keys [::print/print-fn session] :as msg}]
114+
(let [inspector (-> session meta ::inspector)]
115+
(with-open [writer (print/replying-PrintWriter :value msg msg)]
116+
(binding [*print-length* (or *print-length* 100)
117+
*print-level* (or *print-level* 20)]
118+
((or print-fn pprint/pprint) (:value inspector) writer))
119+
(.flush writer))
120+
(respond-to msg :status :done)))
121+
110122
(defn tap-current-value [msg]
111123
(inspector-response msg (swap-inspector! msg inspect/tap-current-value)))
112124

@@ -118,6 +130,7 @@
118130
(handle-eval-inspect handler msg)
119131

120132
(with-safe-transport handler msg
133+
"inspect-print-current-value" print-current-value-reply
121134
"inspect-pop" pop-reply
122135
"inspect-push" push-reply
123136
"inspect-next-sibling" next-sibling-reply

test/clj/cider/nrepl/middleware/inspect_test.clj

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -742,6 +742,57 @@
742742
:inspect "true"
743743
:code "(range 100)"}))))))))
744744

745+
(deftest inspect-print-current-value-test
746+
(testing "inspect-print-current-value returns the currently inspected value as a printed string"
747+
(is (= [(string/join "\n" ["{:a -1,"
748+
" :bb \"111\","
749+
" :ccc (1),"
750+
" :d"
751+
" ({:a 0, :bb \"000\", :ccc ()}"
752+
" {:a -1, :bb \"111\", :ccc (1)}"
753+
" {:a -2, :bb \"222\", :ccc (2 1)}"
754+
" {:a -3, :bb \"333\", :ccc (3 2 1)}"
755+
" {:a -4, :bb \"444\", :ccc (4 3 2 1)})}"])]
756+
(:value (do
757+
(session/message {:op "eval"
758+
:code "(def test-val
759+
(for [i (range 2)]
760+
{:a (- i)
761+
:bb (str i i i)
762+
:ccc (range i 0 -1)
763+
:d (for [i (range 5)]
764+
{:a (- i)
765+
:bb (str i i i)
766+
:ccc (range i 0 -1)})}))"})
767+
(session/message {:op "eval"
768+
:inspect "true"
769+
:code "test-val"})
770+
(session/message {:op "inspect-push"
771+
:idx 2})
772+
(session/message {:op "inspect-print-current-value"
773+
:nrepl.middleware.print/print "cider.nrepl.pprint/pprint"})))))))
774+
775+
(deftest inspect-print-current-value-no-value-test
776+
(testing "inspect-print-current-value returns nil if nothing has been inspected yet"
777+
(is (= ["nil"] (:value (session/message
778+
{:op "inspect-print-current-value"
779+
:nrepl.middleware.print/print "cider.nrepl.pprint/pprint"}))))))
780+
781+
(deftest inspect-print-current-value-default-print-fn-test
782+
(testing "inspect-print-current-value uses a default print fn when none is provided"
783+
(is (= ["nil"] (:value (session/message {:op "inspect-print-current-value"}))))))
784+
785+
(deftest inspect-print-current-value-infinite-seq-test
786+
(testing "inspect-print-current-value works with infinite-seqs"
787+
(is (string/starts-with? (first (:value (do (session/message {:op "eval"
788+
:code "(def test-val (repeat :x))"})
789+
(session/message {:op "eval"
790+
:inspect "true"
791+
:code "test-val"})
792+
(session/message {:op "inspect-print-current-value"
793+
:nrepl.middleware.print/print "cider.nrepl.pprint/pprint"}))))
794+
"(:x"))))
795+
745796
(deftest inspect-def-current-value-test
746797
(testing "inspect-def-current-value defines a var with the current inspector value"
747798
(is (= "{3 4}"

0 commit comments

Comments
 (0)