Skip to content
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
- [#346](https://github.com/clojure-emacs/orchard/pull/346): Inspector: only show those datafied collection items that have unique datafy represantation.
- [#348](https://github.com/clojure-emacs/orchard/pull/348): Inspector: display length of inspected strings.
- [#348](https://github.com/clojure-emacs/orchard/pull/348): Inspector: display class flags.
- [#349](https://github.com/clojure-emacs/orchard/pull/349): Inspector: add ability to sort maps by key.

## 0.35.0 (2025-05-28)

Expand Down
21 changes: 15 additions & 6 deletions src/orchard/inspect.clj
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
:max-nested-depth nil
:display-analytics-hint nil
:analytics-size-cutoff 100000
:sort-maps false
:pretty-print false})

(defn- reset-render-state [inspector]
Expand Down Expand Up @@ -100,11 +101,18 @@
(defn- pagination-info
"Calculate if the object should be paginated given the page size. Return a map
with pagination info, or nil if object fits in a single page."
[{:keys [page-size current-page view-mode value] :as inspector}]
[{:keys [page-size current-page view-mode sort-maps value] :as inspector}]
(let [page-size (if (= view-mode :hex)
(* page-size 16) ;; In hex view mode, each row is 16 bytes.
page-size)
start-idx (* current-page page-size)
;; Sort maps early to ensure proper paging.
sort-map? (and (= (object-type value) :map) sort-maps)
value (if sort-map?
(try (sort-by key value)
;; May throw if keys are not comparable.
(catch Exception _ value))
value)
;; Try grab a chunk that is one element longer than asked in
;; page-size. This is how we know there are elements beyond the
;; current page.
Expand All @@ -114,18 +122,19 @@
count+1 (count chunk+1)
paginate? (or (> current-page 0) ;; In non-paginated it's always 0.
(> count+1 page-size))
chunk (cond-> chunk+1
(> count+1 page-size) pop)
clength (or (counted-length inspector value)
(when (<= count+1 page-size)
(+ (* page-size current-page) count+1)))
last-page (if clength
(quot (dec clength) page-size)
;; Possibly infinite
Integer/MAX_VALUE)]
(when paginate?
{:chunk (cond-> chunk+1
(> count+1 page-size) pop)
:start-idx start-idx
:last-page last-page})))
(cond paginate? {:chunk chunk
:start-idx start-idx
:last-page last-page}
sort-map? {:chunk chunk})))

(defn- decide-if-paginated
"Make early decision if the inspected object should be paginated. If so,
Expand Down
35 changes: 35 additions & 0 deletions test/orchard/inspect_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -1736,6 +1736,41 @@
(is+ [#"--- View mode" [:newline] " ●normal object ●pretty"]
(section rendered "View mode")))))

(deftest sort-maps-test
(testing "with :sort-map-keys enabled, may keys are sorted"
(is+ (matchers/prefix
["--- Contents:" [:newline]
" " [:value "0" pos?] " = " [:value "0" pos?] [:newline]
" " [:value "1" pos?] " = " [:value "1" pos?] [:newline]
" " [:value "2" pos?] " = " [:value "2" pos?] [:newline]
" " [:value "3" pos?] " = " [:value "3" pos?] [:newline]])
(-> (zipmap (range 100) (range 100))
inspect
(inspect/refresh {:sort-maps true})
render
contents-section)))

(testing "works if map is smaller than page size"
(is+ ["--- Contents:" [:newline]
" " [:value "0" pos?] " = " [:value "0" pos?] [:newline]
" " [:value "1" pos?] " = " [:value "1" pos?] [:newline]
" " [:value "2" pos?] " = " [:value "2" pos?] [:newline]
" " [:value "3" pos?] " = " [:value "3" pos?] [:newline]
" " [:value "4" pos?] " = " [:value "4" pos?]]
(-> (zipmap (range 5) (range 5))
inspect
(inspect/refresh {:sort-maps true, :page-size 100})
render
contents-section)))

(testing "doesn't fail if keys are non-comparable"
(is+ (matchers/prefix ["--- Contents:"])
(-> {(byte-array 1) 1 (byte-array 2) 2}
inspect
(inspect/refresh {:sort-maps true})
render
contents-section))))

(deftest tap-test
(testing "tap-current-value"
(let [proof (atom [])
Expand Down