Skip to content

Commit 1f96821

Browse files
committed
Add support for pretty printing values in the inspector.
1 parent 58be6fc commit 1f96821

File tree

6 files changed

+75
-3
lines changed

6 files changed

+75
-3
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
## 0.55.1 (2025-04-14)
88

99
* [#931](https://github.com/clojure-emacs/cider-nrepl/pull/931): Redesign and optimize track-state middleware.
10+
* [#932](https://github.com/clojure-emacs/cider-nrepl/pull/932) Add support for pretty printing values in the inspector.
1011

1112
## 0.55.0 (2025-04-10)
1213

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

+18
Original file line numberDiff line numberDiff line change
@@ -731,6 +731,24 @@ Returns::
731731

732732

733733

734+
=== `inspect-toggle-pretty-print`
735+
736+
Toggles the pretty printing of values in the inspector.
737+
738+
Required parameters::
739+
* `:session` The current session
740+
741+
742+
Optional parameters::
743+
{blank}
744+
745+
Returns::
746+
* `:path` Printed representation of current inspector path.
747+
* `:status` "done"
748+
* `:value` The inspector result. Contains a specially-formatted string that can be ``read`` and then rendered client-side.
749+
750+
751+
734752
=== `inspect-toggle-view-mode`
735753

736754
Toggles the viewing mode of the inspector. This influences the way how inspector is rendering the current value. ``:normal`` is the default. When view mode is ``:table``, the value will be rendered as a table (only supported for sequences of maps or tuples). When view mode is ``:object``, any value will be rendered as a Java object (fields shown as is). View mode is automatically reset back to normal when navigating to child values.

project.clj

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
:url "http://www.eclipse.org/legal/epl-v10.html"}
2323
:scm {:name "git" :url "https://github.com/clojure-emacs/cider-nrepl"}
2424
:dependencies [[nrepl/nrepl "1.3.1" :exclusions [org.clojure/clojure]]
25-
[cider/orchard "0.33.0" :exclusions [org.clojure/clojure]]
25+
[cider/orchard "0.34.0" :exclusions [org.clojure/clojure]]
2626
^:inline-dep [fipp ~fipp-version] ; can be removed in unresolved-tree mode
2727
^:inline-dep [compliment "0.7.0"]
2828
^:inline-dep [org.rksm/suitable "0.6.2" :exclusions [org.clojure/clojure

src/cider/nrepl.clj

+4
Original file line numberDiff line numberDiff line change
@@ -341,6 +341,10 @@ if applicable, and re-render the updated value."
341341
"max-nested-depth" "New max nested depth of rendered collection"
342342
"view-mode" "Mode of viewing the value - either `:normal` or `:object`"}
343343
:returns inspector-returns}
344+
"inspect-toggle-pretty-print"
345+
{:doc "Toggles the pretty printing of values in the inspector."
346+
:requires {"session" "The current session"}
347+
:returns inspector-returns}
344348
"inspect-toggle-view-mode"
345349
{:doc "Toggles the viewing mode of the inspector. This influences the way how inspector is rendering the current value. `:normal` is the default. When view mode is `:table`, the value will be rendered as a table (only supported for sequences of maps or tuples). When view mode is `:object`, any value will be rendered as a Java object (fields shown as is). View mode is automatically reset back to normal when navigating to child values."
346350
:requires {"session" "The current session"}

src/cider/nrepl/middleware/inspect.clj

+8-2
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,10 @@
3131
(response-for msg data extra-response-data))))
3232

3333
(defn- msg->inspector-config [msg]
34-
(select-keys msg [:page-size :max-atom-length :max-coll-size
35-
:max-value-length :max-nested-depth :display-analytics-hint]))
34+
(-> (select-keys msg [:page-size :max-atom-length :max-coll-size
35+
:max-value-length :max-nested-depth :display-analytics-hint
36+
:pretty-print])
37+
(update :pretty-print #(= "true" %))))
3638

3739
(defn inspect-reply* [{:keys [view-mode] :as msg} value]
3840
(let [config (msg->inspector-config msg)
@@ -77,6 +79,9 @@
7779
(let [overrides (msg->inspector-config msg)]
7880
(inspector-response msg (swap-inspector! msg #(inspect/refresh % overrides)))))
7981

82+
(defn toggle-pretty-print-reply [msg]
83+
(inspector-response msg (swap-inspector! msg #(-> (update % :pretty-print not) (inspect/inspect-render)))))
84+
8085
(defn- toggle-view-mode [{:keys [view-mode] :as inspector}]
8186
;; The order in which view modes are cycled depends on the inspected object.
8287
(let [toggle-order (if (inspect/supports-table-view-mode? inspector)
@@ -120,6 +125,7 @@
120125
"inspect-next-page" next-page-reply
121126
"inspect-prev-page" prev-page-reply
122127
"inspect-refresh" refresh-reply
128+
"inspect-toggle-pretty-print" toggle-pretty-print-reply
123129
"inspect-toggle-view-mode" toggle-view-mode-reply
124130
"inspect-display-analytics" display-analytics-reply
125131
"inspect-set-page-size" refresh-reply

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

+43
Original file line numberDiff line numberDiff line change
@@ -691,6 +691,49 @@
691691
(is+ (matchers/prefix table-mode-prefix)
692692
(value-skip-header (session/message {:op "inspect-toggle-view-mode"})))))
693693

694+
(deftest pretty-print-integration-test
695+
(testing "renders an object with the pretty printer"
696+
(session/message {:op "inspect-clear"})
697+
(session/message {:op "eval"
698+
:inspect "true"
699+
:code "(repeat 5 {:a (repeat 5 {:b 2}) :c (repeat 5 {:d 2})})"})
700+
(testing "toggle pretty printing and turn it on"
701+
(is+ ["--- Contents:" [:newline]
702+
" 0. " [:value (str "{:a ({:b 2} {:b 2} {:b 2} {:b 2} {:b 2}),"
703+
"\n :c ({:d 2} {:d 2} {:d 2} {:d 2} {:d 2})}") 1]
704+
[:newline]
705+
" 1. " [:value (str "{:a ({:b 2} {:b 2} {:b 2} {:b 2} {:b 2}),"
706+
"\n :c ({:d 2} {:d 2} {:d 2} {:d 2} {:d 2})}") 2]
707+
[:newline]
708+
" 2. " [:value (str "{:a ({:b 2} {:b 2} {:b 2} {:b 2} {:b 2}),"
709+
"\n :c ({:d 2} {:d 2} {:d 2} {:d 2} {:d 2})}") 3]
710+
[:newline]
711+
" 3. " [:value (str "{:a ({:b 2} {:b 2} {:b 2} {:b 2} {:b 2}),"
712+
"\n :c ({:d 2} {:d 2} {:d 2} {:d 2} {:d 2})}") 4]
713+
[:newline]
714+
" 4. " [:value (str "{:a ({:b 2} {:b 2} {:b 2} {:b 2} {:b 2}),"
715+
"\n :c ({:d 2} {:d 2} {:d 2} {:d 2} {:d 2})}") 5]
716+
[:newline]]
717+
(value-skip-header (session/message {:op "inspect-toggle-pretty-print"}))))
718+
(testing "toggle pretty printing and turn it off"
719+
(is+ ["--- Contents:" [:newline]
720+
" 0. " [:value (str "{:a ({:b 2} {:b 2} {:b 2} {:b 2} {:b 2}),"
721+
" :c ({:d 2} {:d 2} {:d 2} {:d 2} {:d 2})}") 1]
722+
[:newline]
723+
" 1. " [:value (str "{:a ({:b 2} {:b 2} {:b 2} {:b 2} {:b 2}),"
724+
" :c ({:d 2} {:d 2} {:d 2} {:d 2} {:d 2})}") 2]
725+
[:newline]
726+
" 2. " [:value (str "{:a ({:b 2} {:b 2} {:b 2} {:b 2} {:b 2}),"
727+
" :c ({:d 2} {:d 2} {:d 2} {:d 2} {:d 2})}") 3]
728+
[:newline]
729+
" 3. " [:value (str "{:a ({:b 2} {:b 2} {:b 2} {:b 2} {:b 2}),"
730+
" :c ({:d 2} {:d 2} {:d 2} {:d 2} {:d 2})}") 4]
731+
[:newline]
732+
" 4. " [:value (str "{:a ({:b 2} {:b 2} {:b 2} {:b 2} {:b 2}),"
733+
" :c ({:d 2} {:d 2} {:d 2} {:d 2} {:d 2})}") 5]
734+
[:newline]]
735+
(value-skip-header (session/message {:op "inspect-toggle-pretty-print"}))))))
736+
694737
(deftest print-length-independence-test
695738
(testing "*print-length* doesn't break rendering of long collections"
696739
(is (re-find #"showing page: \d+ of \d+"

0 commit comments

Comments
 (0)