Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[inspect] Re-implement :view-mode with a stack same as paging #275

Merged
merged 2 commits into from
Jun 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
25 changes: 17 additions & 8 deletions src/orchard/inspect.clj
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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))))

Expand All @@ -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)
Expand All @@ -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
Expand Down Expand Up @@ -196,15 +199,14 @@
(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)))
(when (some? max-value-length) (pre-ex (pos-int? max-value-length)))
(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
Expand All @@ -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.
Expand Down Expand Up @@ -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))

Expand Down Expand Up @@ -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
Expand Down
41 changes: 38 additions & 3 deletions test/orchard/inspect_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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.
Expand Down