From 41470dbe9931c884a0aaa5237e20b345f40cbf01 Mon Sep 17 00:00:00 2001 From: Oleksandr Yakushev Date: Sun, 2 Jun 2024 00:01:25 +0300 Subject: [PATCH 1/2] [inspect] Re-implement :view-mode with a stack same as paging --- src/orchard/inspect.clj | 25 ++++++++++++++------- test/orchard/inspect_test.clj | 41 ++++++++++++++++++++++++++++++++--- 2 files changed, 55 insertions(+), 11 deletions(-) diff --git a/src/orchard/inspect.clj b/src/orchard/inspect.clj index aa75d548..0fb55813 100644 --- a/src/orchard/inspect.clj +++ b/src/orchard/inspect.clj @@ -38,7 +38,6 @@ (def ^:private default-inspector-config "Default configuration values for the inspector." {:page-size 32 ; = Clojure's default chunked sequences chunk size. - :view-mode :normal :max-atom-length 150 :max-value-length 10000 ; To avoid printing huge graphs and Exceptions. :max-coll-size 5 @@ -126,13 +125,15 @@ (defn- up* "Move one value up the stack without re-rendering." - [{:keys [stack pages-stack] :as inspector}] + [{:keys [stack pages-stack view-modes-stack] :as inspector}] (if (empty? stack) inspector (-> inspector (update :path pop) (assoc :current-page (peek pages-stack)) (update :pages-stack pop) + (assoc :view-mode (peek view-modes-stack)) + (update :view-modes-stack pop) (assoc :value (peek stack)) (update :stack pop)))) @@ -144,7 +145,7 @@ (defn- down* "Navigate to `child` value of the current inspector without re-rendering." [inspector child child-role child-key] - (let [{:keys [value current-page page-size]} inspector + (let [{:keys [value current-page page-size view-mode]} inspector ;; If down* was called on an invisible element (e.g. by sibling*), ;; :current-page may be wrong, recompute it. current-page (if (number? child-key) @@ -155,6 +156,8 @@ (update :stack conj value) (assoc :current-page 0) (update :pages-stack conj current-page) + (assoc :view-mode :normal) + (update :view-modes-stack conj view-mode) (update :path push-item-to-path child-role child-key)))) (defn down @@ -196,7 +199,7 @@ (sibling* inspector 1)) (defn- validate-config [{:keys [page-size max-atom-length max-value-length - max-coll-size max-nested-depth spacious view-mode] + max-coll-size max-nested-depth spacious] :as config}] (when (some? page-size) (pre-ex (pos-int? page-size))) (when (some? max-atom-length) (pre-ex (pos-int? max-atom-length))) @@ -204,7 +207,6 @@ (when (some? max-coll-size) (pre-ex (pos-int? max-coll-size))) (when (some? max-nested-depth) (pre-ex (pos-int? max-nested-depth))) (when (some? spacious) (pre-ex (boolean? spacious))) - (when (some? view-mode) (pre-ex (supported-view-modes view-mode))) (select-keys config (keys default-inspector-config))) (defn refresh @@ -216,8 +218,7 @@ `:max-value-length` - maximum length of a whole printed value before truncating `:max-coll-size` - maximum number of collection items to print before truncating `:max-nested-depth` - maximum nesting level to print before truncating - `:spacious` - if true, collection values will have extra space around parens - `:view-mode` - one of #{`:normal`, `:object`}" + `:spacious` - if true, collection values will have extra space around parens" [inspector config-override] (as-> (validate-config config-override) config ;; If page size is changed, reset the current page. @@ -245,6 +246,13 @@ (tap> (:value (get index idx))) (inspect-render inspector)) +(defn set-view-mode + "Set the view mode for the current value to `mode`. See allowed values in + `supported-view-modes`." + [inspector mode] + (pre-ex (contains? supported-view-modes mode)) + (inspect-render (assoc inspector :view-mode mode))) + (defn render-onto [inspector coll] (update inspector :rendered into coll)) @@ -786,7 +794,8 @@ ([config value] (-> default-inspector-config (merge (validate-config config)) - (assoc :stack [], :path [], :pages-stack [], :current-page 0) + (assoc :stack [], :path [], :pages-stack [], :current-page 0, + :view-modes-stack [], :view-mode :normal) (inspect-render value)))) (defn ^:deprecated clear diff --git a/test/orchard/inspect_test.clj b/test/orchard/inspect_test.clj index ad3ec3bd..360ee712 100644 --- a/test/orchard/inspect_test.clj +++ b/test/orchard/inspect_test.clj @@ -1411,7 +1411,10 @@ (deftest object-view-mode-test (testing "in :object view-mode recognized objects are rendered as :default" - (let [rendered (->> (list 1 2 3) (inspect/start {:view-mode :object}) render)] + (let [rendered (-> (list 1 2 3) + (inspect/start) + (inspect/set-view-mode :object) + render)] (is (match? (matchers/embeds '("--- Instance fields:" (:newline) @@ -1423,7 +1426,10 @@ (is (match? '("--- View mode:" (:newline) " " ":object") (section "View mode" rendered)))) - (let [rendered (->> (atom "foo") (inspect/start {:view-mode :object}) render)] + (let [rendered (-> (atom "foo") + (inspect/start) + (inspect/set-view-mode :object) + render)] (is (match? (matchers/embeds '("--- Instance fields:" (:newline) @@ -1434,7 +1440,36 @@ (:newline))) (section "Instance fields" rendered))) (is (match? '("--- View mode:" (:newline) " " ":object") - (section "View mode" rendered)))))) + (section "View mode" rendered))))) + + (testing "navigating away from an object changes the view mode back to normal" + (let [rendered (-> (list 1 2 3) + (inspect/start) + (inspect/set-view-mode :object) + (inspect/down 13) + render)] + (is (match? (matchers/prefix + '("--- Contents:" + (:newline) + " " "0" ". " (:value "2" 1) (:newline) + " " "1" ". " (:value "3" 2) (:newline))) + (section "Contents" rendered))))) + + (testing "going back to value viewed with a different mode will remember that view mode" + (let [rendered (-> (list 1 2 3) + (inspect/start) + (inspect/set-view-mode :object) + (inspect/down 13) + (inspect/set-view-mode :normal) + (inspect/up) + render)] + (is (match? (matchers/prefix + '("--- Instance fields:" + (:newline) + " " (:value "_count" 2) " = " (:value "3" 3) (:newline) + " " (:value "_first" 4) " = " (:value "1" 5) (:newline) + " " (:value "_hash" 6) " = " (:value "0" 7) (:newline))) + (section "Instance fields" rendered)))))) (deftest tap-test ;; NOTE: this deftest is flaky - wrap the body in the following (and remove the `Thread/sleep`) to reproduce. From 46efdffb4b04f686da4ca74f3ae63df510d27f9e Mon Sep 17 00:00:00 2001 From: Oleksandr Yakushev Date: Sun, 2 Jun 2024 00:06:28 +0300 Subject: [PATCH 2/2] Update CHANGELOG --- CHANGELOG.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 02efbdc8..aa1ac1f0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,7 +2,9 @@ ## master (unreleased) -## 0.26.0 (2024-06-30) +* [#275](https://github.com/clojure-emacs/orchard/issues/275): Inspector: re-implement :view-mode as a transient stack-remembered option. + +## 0.26.0 (2024-05-30) * [#272](https://github.com/clojure-emacs/orchard/issues/272): Inspector: display superclass hierarchy when inspecting a class. * [#267](https://github.com/clojure-emacs/orchard/issues/267): Inspector: don't truncate constructor/field/method names.