From 1fa0f41edd5913747dc39239bfec9f2100832007 Mon Sep 17 00:00:00 2001 From: Oleksandr Yakushev Date: Tue, 14 May 2024 15:39:06 +0300 Subject: [PATCH 1/2] [inspect] Use orchard.inspect/refresh instead of individual setters --- CHANGELOG.md | 4 + project.clj | 2 +- src/cider/nrepl/middleware/inspect.clj | 50 ++++++------- .../cider/nrepl/middleware/inspect_test.clj | 74 +++++++++++-------- 4 files changed, 71 insertions(+), 59 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index c6adff5b..c604e43e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,10 @@ ## master (unreleased) +### Changes + +* Bump `orchard` to [0.26.0](https://github.com/clojure-emacs/orchard/blob/master/CHANGELOG.md#0260-2024-06-30). + ## 0.48.0 (2024-05-13) ### Changes diff --git a/project.clj b/project.clj index 2d33ed0e..35c3be6d 100644 --- a/project.clj +++ b/project.clj @@ -8,7 +8,7 @@ :url "http://www.eclipse.org/legal/epl-v10.html"} :scm {:name "git" :url "https://github.com/clojure-emacs/cider-nrepl"} :dependencies [[nrepl "1.1.1" :exclusions [org.clojure/clojure]] - [cider/orchard "0.25.0" :exclusions [org.clojure/clojure]] + [cider/orchard "0.26.0" :exclusions [org.clojure/clojure]] ^:inline-dep [mx.cider/haystack "0.3.3" :exclusions [cider/orchard]] ^:inline-dep [thunknyc/profile "0.5.2"] ^:inline-dep [mvxcvi/puget "1.3.4" :exclusions [org.clojure/clojure]] diff --git a/src/cider/nrepl/middleware/inspect.clj b/src/cider/nrepl/middleware/inspect.clj index dfdc166f..74b903e4 100644 --- a/src/cider/nrepl/middleware/inspect.clj +++ b/src/cider/nrepl/middleware/inspect.clj @@ -62,15 +62,18 @@ (doseq [^Class interface (.getInterfaces clazz)] (info/info 'user (-> interface .getCanonicalName symbol))))))) +(defn- msg->inspector-config [msg] + (let [config (select-keys msg [:page-size :max-atom-length :max-coll-size + :max-value-length :max-nested-depth :spacious])] + (if (contains? config :spacious) + (update config :spacious #(case % + ("false" "nil") false + true)) + config))) + (defn inspect-reply* [msg value] - (let [config (select-keys msg [:page-size :max-atom-length :max-coll-size - :max-value-length :max-nested-depth :spacious]) - config (if (contains? config :spacious) - (update config :spacious #(case % - ("false" "nil") false - true)) - config) + (let [config (msg->inspector-config msg) inspector (swap-inspector! msg #(inspect/start (merge % config) value))] (warmup-javadoc-cache (class (:value inspector))) (inspector-response msg inspector {}))) @@ -128,29 +131,20 @@ (defn previous-sibling-reply [msg] (inspector-response msg (swap-inspector! msg inspect/previous-sibling))) -(defn refresh-reply [msg] - (inspector-response msg (swap-inspector! msg inspect/inspect-render))) - (defn next-page-reply [msg] (inspector-response msg (swap-inspector! msg inspect/next-page))) (defn prev-page-reply [msg] (inspector-response msg (swap-inspector! msg inspect/prev-page))) -(defn set-page-size-reply [msg] - (inspector-response msg (swap-inspector! msg inspect/set-page-size (:page-size msg)))) - -(defn set-max-atom-length-reply [msg] - (inspector-response msg (swap-inspector! msg inspect/set-max-atom-length - (:max-atom-length msg)))) - -(defn set-max-coll-size-reply [msg] - (inspector-response msg (swap-inspector! msg inspect/set-max-coll-size - (:max-coll-size msg)))) +(defn refresh-reply [msg] + (let [overrides (msg->inspector-config msg)] + (inspector-response msg (swap-inspector! msg #(inspect/refresh % overrides))))) -(defn set-max-nested-depth-reply [msg] - (inspector-response msg (swap-inspector! msg inspect/set-max-nested-depth - (:max-nested-depth msg)))) +(defn ^:deprecated set-page-size-reply [msg] (refresh-reply msg)) +(defn ^:deprecated set-max-atom-length-reply [msg] (refresh-reply msg)) +(defn ^:deprecated set-max-coll-size-reply [msg] (refresh-reply msg)) +(defn ^:deprecated set-max-nested-depth-reply [msg] (refresh-reply msg)) (defn clear-reply [msg] (inspector-response msg (swap-inspector! msg (constantly (inspect/start nil))))) @@ -173,13 +167,13 @@ "inspect-push" push-reply "inspect-next-sibling" next-sibling-reply "inspect-previous-sibling" previous-sibling-reply - "inspect-refresh" refresh-reply "inspect-next-page" next-page-reply "inspect-prev-page" prev-page-reply - "inspect-set-page-size" set-page-size-reply - "inspect-set-max-atom-length" set-max-atom-length-reply - "inspect-set-max-coll-size" set-max-coll-size-reply - "inspect-set-max-nested-depth" set-max-nested-depth-reply + "inspect-refresh" refresh-reply + "inspect-set-page-size" refresh-reply + "inspect-set-max-atom-length" refresh-reply + "inspect-set-max-coll-size" refresh-reply + "inspect-set-max-nested-depth" refresh-reply "inspect-clear" clear-reply "inspect-def-current-value" def-current-value "inspect-tap-current-value" tap-current-value diff --git a/test/clj/cider/nrepl/middleware/inspect_test.clj b/test/clj/cider/nrepl/middleware/inspect_test.clj index 56198082..8ac044ba 100644 --- a/test/clj/cider/nrepl/middleware/inspect_test.clj +++ b/test/clj/cider/nrepl/middleware/inspect_test.clj @@ -282,28 +282,43 @@ (is (:pp-stacktrace response))))) (testing "inspect-set-page-size error handling" - (with-redefs [i/swap-inspector! (fn [& _] (throw (Exception. "page-size exception")))] - (let [response (session/message {:op "inspect-set-page-size" :page-size 10})] - (is (= (:status response) #{"inspect-set-page-size-error" "done"})) - (is (= (:ex response) "class java.lang.Exception")) - (is (-> response ^String (:err) (.startsWith "java.lang.Exception: page-size exception"))) - (is (:pp-stacktrace response))))) + (let [response (session/message {:op "inspect-set-page-size" :page-size 0})] + (is (= #{"inspect-set-page-size-error" "done"} (:status response))) + (is (= "class clojure.lang.ExceptionInfo" (:ex response))) + (is (match? #".*Precondition failed: \(pos-int\? page-size\).*" (:err response))) + (is (:pp-stacktrace response))) + + (let [response (session/message {:op "inspect-refresh" :page-size 0})] + (is (= #{"inspect-refresh-error" "done"} (:status response))) + (is (= "class clojure.lang.ExceptionInfo" (:ex response))) + (is (match? #".*Precondition failed: \(pos-int\? page-size\).*" (:err response))) + (is (:pp-stacktrace response)))) (testing "inspect-set-max-atom-length error handling" - (with-redefs [i/swap-inspector! (fn [& _] (throw (Exception. "max-atom-length exception")))] - (let [response (session/message {:op "inspect-set-max-atom-length" :max-atom-length 10})] - (is (= (:status response) #{"inspect-set-max-atom-length-error" "done"})) - (is (= (:ex response) "class java.lang.Exception")) - (is (-> response ^String (:err) (.startsWith "java.lang.Exception: max-atom-length exception"))) - (is (:pp-stacktrace response))))) + (let [response (session/message {:op "inspect-set-max-atom-length" :max-atom-length 0})] + (is (= #{"inspect-set-max-atom-length-error" "done"} (:status response))) + (is (= "class clojure.lang.ExceptionInfo" (:ex response))) + (is (match? #".*Precondition failed: \(pos-int\? max-atom-length\).*" (:err response))) + (is (:pp-stacktrace response))) + + (let [response (session/message {:op "inspect-refresh" :max-atom-length 0})] + (is (= #{"inspect-refresh-error" "done"} (:status response))) + (is (= "class clojure.lang.ExceptionInfo" (:ex response))) + (is (match? #".*Precondition failed: \(pos-int\? max-atom-length\).*" (:err response))) + (is (:pp-stacktrace response)))) (testing "inspect-set-max-coll-size error handling" - (with-redefs [i/swap-inspector! (fn [& _] (throw (Exception. "max-coll-size exception")))] - (let [response (session/message {:op "inspect-set-max-coll-size" :max-coll-size 10})] - (is (= (:status response) #{"inspect-set-max-coll-size-error" "done"})) - (is (= (:ex response) "class java.lang.Exception")) - (is (-> response ^String (:err) (.startsWith "java.lang.Exception: max-coll-size exception"))) - (is (:pp-stacktrace response)))))) + (let [response (session/message {:op "inspect-set-max-coll-size" :max-coll-size 0})] + (is (= #{"inspect-set-max-coll-size-error" "done"} (:status response))) + (is (= "class clojure.lang.ExceptionInfo" (:ex response))) + (is (match? #".*Precondition failed: \(pos-int\? max-coll-size\).*" (:err response))) + (is (:pp-stacktrace response))) + + (let [response (session/message {:op "inspect-refresh" :max-coll-size 0})] + (is (= #{"inspect-refresh-error" "done"} (:status response))) + (is (= "class clojure.lang.ExceptionInfo" (:ex response))) + (is (match? #".*Precondition failed: \(pos-int\? max-coll-size\).*" (:err response))) + (is (:pp-stacktrace response))))) (deftest inspect-var-integration-test (testing "rendering a var" @@ -367,7 +382,7 @@ (session/message {:op "eval" :inspect "true" :code "(map identity (range 35))"}) - (session/message {:op "inspect-set-page-size" + (session/message {:op "inspect-refresh" :page-size 5}) (session/message {:op "inspect-next-page"}) (session/message {:op "inspect-prev-page"}))))))) @@ -473,14 +488,14 @@ (is (re-find #"Page size: 5, showing page: 2 of 20" (extract-text small-page-2))))) - (testing "page size can be changed via the inspect-set-page-size op" + (testing "page size can be changed via the inspect-refresh op" (session/message {:op "inspect-clear"}) (let [normal-page-size (session/message {:op "eval" :inspect "true" :code "(range 100)"}) normal-page-2 (session/message {:op "inspect-next-page"}) - small-page-size (session/message {:op "inspect-set-page-size" + small-page-size (session/message {:op "inspect-refresh" :page-size 5}) small-page-2 (session/message {:op "inspect-next-page"}) @@ -535,11 +550,11 @@ :code too-long}) extract-text) (x-pattern max-len "..."))) - (is (string/includes? (-> (session/message {:op "inspect-set-max-atom-length" + (is (string/includes? (-> (session/message {:op "inspect-refresh" :max-atom-length 10}) extract-text) (x-pattern 10 "..."))) - (is (string/includes? (-> (session/message {:op "inspect-set-max-atom-length" + (is (string/includes? (-> (session/message {:op "inspect-refresh" :max-atom-length 20}) extract-text) (x-pattern 20 "...")))))) @@ -548,16 +563,15 @@ (let [max-len (:max-value-length @#'orchard.inspect/default-inspector-config) extract-text #(-> % :value first)] - ;; TODO: reduce leeway once orchard.print truncation is fixed (testing "max value length can be set for the eval op" - (is (< (- max-len 10) + (is (< max-len (-> (session/message {:op "eval" :inspect "true" :code infinite-map-code}) extract-text count) - (+ max-len 2000))) - (is (< 490 + (+ max-len 300))) + (is (< 500 (-> (session/message {:op "eval" :inspect "true" :code infinite-map-code @@ -599,9 +613,9 @@ (let [default-coll-size (session/message {:op "eval" :inspect "true" :code big-coll}) - large-coll-size (session/message {:op "inspect-set-max-coll-size" + large-coll-size (session/message {:op "inspect-refresh" :max-coll-size big-size}) - smaller-coll-size (session/message {:op "inspect-set-max-coll-size" + smaller-coll-size (session/message {:op "inspect-refresh" :max-coll-size (dec big-size)}) unchanged-default-coll-size (do (session/message {:op "inspect-clear"}) (session/message {:op "eval" @@ -638,7 +652,7 @@ (let [default (session/message {:op "eval" :inspect "true" :code nested-coll}) - limited (session/message {:op "inspect-set-max-nested-depth" + limited (session/message {:op "inspect-refresh" :max-nested-depth 5})] (is (string/includes? (extract-text default) "\"[ [ [ [ [ [ [ [ [ [ 1 ] ] ] ] ] ] ] ] ] ]\"")) From bcc30871358f3ca9991da693e240023672e830bc Mon Sep 17 00:00:00 2001 From: Oleksandr Yakushev Date: Tue, 14 May 2024 15:46:58 +0300 Subject: [PATCH 2/2] Update middleware docs --- CHANGELOG.md | 2 ++ doc/modules/ROOT/pages/nrepl-api/ops.adoc | 20 ++++++++++++-------- src/cider/nrepl.clj | 18 +++++++++++------- 3 files changed, 25 insertions(+), 15 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index c604e43e..1e15174f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,8 @@ ### Changes +* [#877](https://github.com/clojure-emacs/cider-nrepl/pull/877): `inspect-refresh` middleware is now capable of setting all config options that `orchard.inspect` supports. +* [#877](https://github.com/clojure-emacs/cider-nrepl/pull/877): Deprecate all `inspect-set-*` middleware ops. * Bump `orchard` to [0.26.0](https://github.com/clojure-emacs/orchard/blob/master/CHANGELOG.md#0260-2024-06-30). ## 0.48.0 (2024-05-13) diff --git a/doc/modules/ROOT/pages/nrepl-api/ops.adoc b/doc/modules/ROOT/pages/nrepl-api/ops.adoc index 3f20f490..73ad77cb 100644 --- a/doc/modules/ROOT/pages/nrepl-api/ops.adoc +++ b/doc/modules/ROOT/pages/nrepl-api/ops.adoc @@ -675,14 +675,18 @@ Returns:: === `inspect-refresh` -Re-renders the currently inspected value. +Updates inspector with the provided config and re-renders the current value. Required parameters:: * `:session` The current session Optional parameters:: -{blank} +* `:max-atom-length` New max length of single rendered value +* `:max-coll-size` New max size of rendered collection +* `:max-nested-depth` New max nested depth of rendered collection +* `:page-size` New page size + Returns:: * `:doc-block-tags-fragments` May be absent. Represent the 'param', 'returns' and 'throws' sections a Java doc comment. It's a vector of fragments, where fragment is a map with ``:type`` ('text' or 'html') and ``:content`` plain text or html markup, respectively @@ -696,7 +700,7 @@ Returns:: === `inspect-set-max-atom-length` -Set the max length of nested atoms to specified value. +[DEPRECATED - use ``inspect-refresh`` instead] Set the max length of nested atoms to specified value. Required parameters:: * `:max-atom-length` New max length. @@ -718,7 +722,7 @@ Returns:: === `inspect-set-max-coll-size` -Set the number of nested collection members to display before truncating. +[DEPRECATED - use ``inspect-refresh`` instead] Set the number of nested collection members to display before truncating. Required parameters:: * `:max-coll-size` New collection size. @@ -740,7 +744,7 @@ Returns:: === `inspect-set-max-nested-depth` -Set the maximum nested levels to display before truncating. +[DEPRECATED - use ``inspect-refresh`` instead] Set the maximum nested levels to display before truncating. Required parameters:: * `:max-nested-depth` New nested depth. @@ -762,7 +766,7 @@ Returns:: === `inspect-set-page-size` -Sets the page size in paginated view to specified value. +[DEPRECATED - use ``inspect-refresh`` instead] Sets the page size in paginated view to specified value. Required parameters:: * `:page-size` New page size. @@ -1147,7 +1151,7 @@ Returns:: === `retest` -[DEPRECATED - ``use test-var-query`` instead] 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. +[DEPRECATED - use ``test-var-query`` instead] 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. Required parameters:: {blank} @@ -1281,7 +1285,7 @@ Returns:: === `test` -[DEPRECATED - ``use test-var-query`` instead] 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. +[DEPRECATED - use ``test-var-query`` instead] 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. Required parameters:: {blank} diff --git a/src/cider/nrepl.clj b/src/cider/nrepl.clj index 38f22577..7cac8d37 100644 --- a/src/cider/nrepl.clj +++ b/src/cider/nrepl.clj @@ -335,8 +335,12 @@ if applicable, and re-render the updated value." :requires {"session" "The current session"} :returns inspector-returns} "inspect-refresh" - {:doc "Re-renders the currently inspected value." + {:doc "Updates inspector with the provided config and re-renders the current value." :requires {"session" "The current session"} + :optional {"page-size" "New page size" + "max-atom-length" "New max length of single rendered value" + "max-coll-size" "New max size of rendered collection" + "max-nested-depth" "New max nested depth of rendered collection"} :returns inspector-returns} "inspect-next-page" {:doc "Jumps to the next page in paginated collection view." @@ -347,22 +351,22 @@ if applicable, and re-render the updated value." :requires {"session" "The current session"} :returns inspector-returns} "inspect-set-page-size" - {:doc "Sets the page size in paginated view to specified value." + {:doc "[DEPRECATED - use `inspect-refresh` instead] Sets the page size in paginated view to specified value." :requires {"page-size" "New page size." "session" "The current session"} :returns inspector-returns} "inspect-set-max-atom-length" - {:doc "Set the max length of nested atoms to specified value." + {:doc "[DEPRECATED - use `inspect-refresh` instead] Set the max length of nested atoms to specified value." :requires {"max-atom-length" "New max length." "session" "The current session"} :returns inspector-returns} "inspect-set-max-coll-size" - {:doc "Set the number of nested collection members to display before truncating." + {:doc "[DEPRECATED - use `inspect-refresh` instead] Set the number of nested collection members to display before truncating." :requires {"max-coll-size" "New collection size." "session" "The current session"} :returns inspector-returns} "inspect-set-max-nested-depth" - {:doc "Set the maximum nested levels to display before truncating." + {:doc "[DEPRECATED - use `inspect-refresh` instead] Set the maximum nested levels to display before truncating." :requires {"max-nested-depth" "New nested depth." "session" "The current session"} :returns inspector-returns} @@ -757,7 +761,7 @@ stack frame of the most recent exception. This op is deprecated, please use the :optional (merge wrap-print-optional-arguments) :returns (merge fail-fast-doc timing-info-return-doc)} "test" - {:doc "[DEPRECATED - `use test-var-query` instead] 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." + {:doc "[DEPRECATED - use `test-var-query` instead] 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." :optional wrap-print-optional-arguments :returns (merge fail-fast-doc timing-info-return-doc)} "test-all" @@ -768,7 +772,7 @@ stack frame of the most recent exception. This op is deprecated, please use the {: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." :optional wrap-print-optional-arguments} "retest" - {:doc "[DEPRECATED - `use test-var-query` instead] 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." + {:doc "[DEPRECATED - use `test-var-query` instead] 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." :optional wrap-print-optional-arguments :returns (merge fail-fast-doc timing-info-return-doc)}}})