Skip to content

Commit 63e290e

Browse files
committed
[Inspector] Configure truncation limits
The limits after which the inspector truncates collection members are now configurable. Previously they were hardcoded to 5 and 150 for collection and atom (non-collection) members.
1 parent 095dc02 commit 63e290e

File tree

5 files changed

+151
-4
lines changed

5 files changed

+151
-4
lines changed

CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
## master (unreleased)
44

5+
### New features
6+
7+
* [#694](https://github.com/clojure-emacs/cider-nrepl/pull/694): [Inspector] Configure truncation limits
8+
59
## 0.25.11 (2021-04-12)
610

711
### Bugs Fixed

project.clj

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
(defproject cider/cider-nrepl "0.25.11"
1+
(defproject cider/cider-nrepl "0.26.0-SNAPSHOT"
22
:description "A collection of nREPL middlewares designed to enhance Clojure editors."
33
:url "https://github.com/clojure-emacs/cider-nrepl"
44
:license {:name "Eclipse Public License"
55
:url "http://www.eclipse.org/legal/epl-v10.html"}
66
:scm {:name "git" :url "https://github.com/clojure-emacs/cider-nrepl"}
77
:dependencies [[nrepl "0.8.3"]
8-
^:inline-dep [cider/orchard "0.6.5"]
8+
^:inline-dep [cider/orchard "0.7.0"]
99
^:inline-dep [thunknyc/profile "0.5.2"]
1010
^:inline-dep [mvxcvi/puget "1.3.1"]
1111
^:inline-dep [fipp "0.6.23"] ; can be removed in unresolved-tree mode

src/cider/nrepl.clj

+10
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,16 @@
258258
:requires {"page-size" "New page size."
259259
"session" "The current session"}
260260
:returns {"status" "\"done\""}}
261+
"inspect-set-max-atom-length"
262+
{:doc "Set the max length of nested atoms to specified value."
263+
:requires {"max-atom-length" "New max length."
264+
"session" "The current session"}
265+
:returns {"status" "\"done\""}}
266+
"inspect-set-max-coll-size"
267+
{:doc "Set the number of nested collection members to display before truncating."
268+
:requires {"max-coll-size" "New collection size."
269+
"session" "The current session"}
270+
:returns {"status" "\"done\""}}
261271
"inspect-clear"
262272
{:doc "Clears the state state of the inspector."
263273
:requires {"session" "The current session"}

src/cider/nrepl/middleware/inspect.clj

+15-2
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,13 @@
2525
(response-for msg resp {:value value}))))
2626

2727
(defn inspect-reply
28-
[{:keys [page-size transport] :as msg} eval-response]
28+
[{:keys [page-size transport max-atom-length max-coll-size] :as msg} eval-response]
2929
(let [value (cljs/response-value msg eval-response)
3030
page-size (or page-size 32)
31-
inspector (swap-inspector! msg #(-> (assoc % :page-size page-size)
31+
inspector (swap-inspector! msg #(-> %
32+
(assoc :page-size page-size
33+
:max-atom-length max-atom-length
34+
:max-coll-size max-coll-size)
3235
(inspect/start value)))]
3336
(transport/send transport (inspector-response msg inspector {}))))
3437

@@ -84,6 +87,14 @@
8487
(defn set-page-size-reply [msg]
8588
(inspector-response msg (swap-inspector! msg inspect/set-page-size (:page-size msg))))
8689

90+
(defn set-max-atom-length-reply [msg]
91+
(inspector-response msg (swap-inspector! msg inspect/set-max-atom-length
92+
(:max-atom-length msg))))
93+
94+
(defn set-max-coll-size-reply [msg]
95+
(inspector-response msg (swap-inspector! msg inspect/set-max-coll-size
96+
(:max-coll-size msg))))
97+
8798
(defn clear-reply [msg]
8899
(inspector-response msg (swap-inspector! msg (constantly (inspect/fresh)))))
89100

@@ -102,5 +113,7 @@
102113
"inspect-next-page" next-page-reply
103114
"inspect-prev-page" prev-page-reply
104115
"inspect-set-page-size" set-page-size-reply
116+
"inspect-set-max-atom-length" set-max-atom-length-reply
117+
"inspect-set-max-coll-size" set-max-coll-size-reply
105118
"inspect-clear" clear-reply
106119
"inspect-def-current-value" def-current-value)))

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

+120
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
(:require
33
[cider.nrepl.middleware.inspect :as i]
44
[cider.nrepl.test-session :as session]
5+
[clojure.string :as string]
56
[clojure.test :refer :all]))
67

78
(def nil-result ["(\"nil\" (:newline))"])
@@ -129,6 +130,22 @@
129130
(is (= (:status response) #{"inspect-set-page-size-error" "done"}))
130131
(is (= (:ex response) "class java.lang.Exception"))
131132
(is (.startsWith (:err response) "java.lang.Exception: page-size exception"))
133+
(is (:pp-stacktrace response)))))
134+
135+
(testing "inspect-set-max-atom-length error handling"
136+
(with-redefs [i/swap-inspector! (fn [& _] (throw (Exception. "max-atom-length exception")))]
137+
(let [response (session/message {:op "inspect-set-max-atom-length" :max-atom-length 10})]
138+
(is (= (:status response) #{"inspect-set-max-atom-length-error" "done"}))
139+
(is (= (:ex response) "class java.lang.Exception"))
140+
(is (.startsWith (:err response) "java.lang.Exception: max-atom-length exception"))
141+
(is (:pp-stacktrace response)))))
142+
143+
(testing "inspect-set-max-coll-size error handling"
144+
(with-redefs [i/swap-inspector! (fn [& _] (throw (Exception. "max-coll-size exception")))]
145+
(let [response (session/message {:op "inspect-set-max-coll-size" :max-coll-size 10})]
146+
(is (= (:status response) #{"inspect-set-max-coll-size-error" "done"}))
147+
(is (= (:ex response) "class java.lang.Exception"))
148+
(is (.startsWith (:err response) "java.lang.Exception: max-coll-size exception"))
132149
(is (:pp-stacktrace response))))))
133150

134151
(deftest inspect-var-integration-test
@@ -273,6 +290,109 @@
273290
(is (re-find #"Page size: 5, showing page: 2 of 20"
274291
(extract-text small-page-2))))))
275292

293+
(deftest max-atom-length-integration-test
294+
;; Default max length is 150, so 150 - 3 = 147 chars is too long because we
295+
;; need to leave room for an opening quote and trailing ellipsis "xxxxxxxx...
296+
;; NOTE: We'd prefer to use `orchard.inspect/*max-atom-length*` vs.
297+
;; hard-coding `max-len` to 150 in case the default ever changes, but test
298+
;; code isn't processed by mranderson and so can't refer to inlined deps
299+
;; including orchard.
300+
;; See also https://github.com/benedekfazekas/mranderson/issues/5
301+
(let [max-len 150 #_(var-get #'orchard.inspect/*max-atom-length*)
302+
_ (assert (> max-len 3) "inspect/*max-atom-length* is too short for this test.")
303+
too-long (pr-str [(string/join (repeat (- max-len 3) "x"))]) ;; 147
304+
trunc-len (- max-len 4) ;; 146
305+
x-pattern #(re-pattern (format "\"\\\\\"%s...\"" (string/join (repeat % "x"))))
306+
extract-text #(-> % :value first)]
307+
308+
(testing "max atom length can be set for the eval op"
309+
(let [default-atom-length (session/message {:op "eval"
310+
:inspect "true"
311+
:code too-long})
312+
short-atom-length (session/message {:op "eval"
313+
:inspect "true"
314+
:code too-long
315+
:max-atom-length 10})
316+
unchanged-default-atom-length (session/message {:op "eval"
317+
:inspect "true"
318+
:code too-long})]
319+
(is (re-find (x-pattern trunc-len)
320+
(extract-text default-atom-length)))
321+
(is (re-find (x-pattern 6)
322+
(extract-text short-atom-length)))
323+
(is (re-find (x-pattern trunc-len)
324+
(extract-text unchanged-default-atom-length)))))
325+
326+
(testing "max atom length can be changed without re-eval'ing last form"
327+
(let [default-atom-length (session/message {:op "eval"
328+
:inspect "true"
329+
:code too-long})
330+
shorten-atom-length (session/message {:op "inspect-set-max-atom-length"
331+
:max-atom-length 10})
332+
longer-atom-length (session/message {:op "inspect-set-max-atom-length"
333+
:max-atom-length 20})
334+
unchanged-default-atom-length (session/message {:op "eval"
335+
:inspect "true"
336+
:code too-long})]
337+
(is (re-find (x-pattern trunc-len)
338+
(extract-text default-atom-length)))
339+
(is (re-find (x-pattern 6)
340+
(extract-text shorten-atom-length)))
341+
(is (re-find (x-pattern 16)
342+
(extract-text longer-atom-length)))
343+
(is (re-find (x-pattern trunc-len)
344+
(extract-text unchanged-default-atom-length)))))))
345+
346+
(deftest max-coll-size-integration-test
347+
;; See NOTE in `max-atom-length-integration-test` about hard-coding
348+
;; `size-limit` here.
349+
(let [size-limit 5 #_(var-get #'orchard.inspect/*max-coll-size*)
350+
big-size (* 2 size-limit) ;; 10
351+
big-coll (format "[(range %d)]" big-size)
352+
coll-pattern (fn [len & [truncate]]
353+
(re-pattern (format "\\( %s%s \\)"
354+
(string/join " " (range len))
355+
(if truncate " ..." ""))))
356+
extract-text #(-> % :value first)]
357+
358+
(testing "max coll size can be set for the eval op"
359+
(let [default-coll-size (session/message {:op "eval"
360+
:inspect "true"
361+
:code big-coll})
362+
large-coll-size (session/message {:op "eval"
363+
:inspect "true"
364+
:code big-coll
365+
:max-coll-size big-size})
366+
unchanged-default-coll-size (session/message {:op "eval"
367+
:inspect "true"
368+
:code big-coll})]
369+
(is (re-find (coll-pattern size-limit :truncate) ;; #"\( 0 1 2 3 4 ... \)"
370+
(extract-text default-coll-size)))
371+
(is (re-find (coll-pattern big-size) ;; #"\( 0 1 2 3 4 5 6 7 8 9 \)"
372+
(extract-text large-coll-size)))
373+
(is (re-find (coll-pattern size-limit :truncate)
374+
(extract-text unchanged-default-coll-size)))))
375+
376+
(testing "max coll size can be changed without re-eval'ing last form"
377+
(let [default-coll-size (session/message {:op "eval"
378+
:inspect "true"
379+
:code big-coll})
380+
large-coll-size (session/message {:op "inspect-set-max-coll-size"
381+
:max-coll-size big-size})
382+
smaller-coll-size (session/message {:op "inspect-set-max-coll-size"
383+
:max-coll-size (dec big-size)})
384+
unchanged-default-coll-size (session/message {:op "eval"
385+
:inspect "true"
386+
:code big-coll})]
387+
(is (re-find (coll-pattern size-limit :truncate)
388+
(extract-text default-coll-size)))
389+
(is (re-find (coll-pattern big-size)
390+
(extract-text large-coll-size)))
391+
(is (re-find (coll-pattern (dec big-size) :truncate)
392+
(extract-text smaller-coll-size)))
393+
(is (re-find (coll-pattern size-limit :truncate)
394+
(extract-text unchanged-default-coll-size)))))))
395+
276396
(deftest print-length-independence-test
277397
(testing "*print-length* doesn't break rendering of long collections"
278398
(is (re-find #"showing page: \d+ of \d+"

0 commit comments

Comments
 (0)