Skip to content

Use sync requests in inspector-* #1821

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

Merged
merged 1 commit into from
Aug 16, 2016
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
2 changes: 1 addition & 1 deletion cider-debug.el
Original file line number Diff line number Diff line change
Expand Up @@ -636,7 +636,7 @@ needed. It is expected to contain at least \"key\", \"input-type\", and
(setq cider--debug-mode-response response)
(cider--debug-mode 1)))
(when inspect
(cider-inspector--value-handler nil inspect)))
(cider-inspector--render-value inspect)))
;; If something goes wrong, we send a "quit" or the session hangs.
(error (cider-debug-mode-send-reply ":quit" key)
(message "Error encountered while handling the debug message: %S" e)))))
Expand Down
161 changes: 88 additions & 73 deletions cider-inspector.el
Original file line number Diff line number Diff line change
Expand Up @@ -131,117 +131,132 @@ invoke any command through M-x and its variants, the value of `last-command'
is not set to the command it invokes.")

;; Operations
(defun cider-inspector--value-handler (_buffer value)
(cider-make-popup-buffer cider-inspector-buffer 'cider-inspector-mode)
(cider-inspector-render cider-inspector-buffer value)
(cider-popup-buffer-display cider-inspector-buffer t)
(with-current-buffer cider-inspector-buffer
(when (eq cider-inspector-last-command 'cider-inspector-pop)
(setq cider-inspector-last-command nil)
;; Prevents error message being displayed when we try to pop
;; from the top-level of a data struture
(when cider-inspector-location-stack
(goto-char (pop cider-inspector-location-stack))))

(when (eq cider-inspector-last-command 'cider-inspector-prev-page)
(setq cider-inspector-last-command nil)
;; Prevents error message being displayed when we try to
;; go to a prev-page from the first page
(when cider-inspector-page-location-stack
(goto-char (pop cider-inspector-page-location-stack))))))

(defun cider-inspector--out-handler (_buffer value)
(cider-emit-interactive-eval-output value))

(defun cider-inspector--err-handler (_buffer err)
(cider-emit-interactive-eval-err-output err))

(defun cider-inspector-response-handler (buffer)
"Create an inspector response handler for BUFFER.

The \"value\" slot of each successive response (if it exists) will be
rendered into `cider-inspector-buffer'. Once a response is received with a
\"status\" slot containing \"done\", `cider-inspector-buffer' will be
displayed.

Used for all inspector nREPL ops."
(nrepl-make-response-handler buffer
#'cider-inspector--value-handler
#'cider-inspector--out-handler
#'cider-inspector--err-handler
#'identity))

;;;###autoload
(defun cider-inspect-expr (expr ns)
"Evaluate EXPR in NS and inspect its value.
Interactively, EXPR is read from the minibuffer, and NS the
current buffer's namespace."
(interactive (list (cider-read-from-minibuffer "Inspect expression: " (cider-sexp-at-point))
(cider-current-ns)))
(cider--prep-interactive-eval expr)
(cider-nrepl-send-request
(append (nrepl--eval-request expr (cider-current-session) ns)
(list "inspect" "true"
"page-size" (or cider-inspector-page-size 32)))
(cider-inspector-response-handler (current-buffer))))
(when-let (value (cider-sync-request:inspect-expr expr ns (or cider-inspector-page-size 32)))
(cider-inspector--render-value value)))

(defun cider-inspector-pop ()
(interactive)
(setq cider-inspector-last-command 'cider-inspector-pop)
(cider-nrepl-send-request
(list "op" "inspect-pop"
"session" (cider-current-session))
(cider-inspector-response-handler (current-buffer))))
(when-let (value (cider-sync-request:inspect-pop))
(cider-inspector--render-value value)))

(defun cider-inspector-push (idx)
(push (point) cider-inspector-location-stack)
(cider-nrepl-send-request
(list "op" "inspect-push"
"idx" idx
"session" (cider-current-session))
(cider-inspector-response-handler (current-buffer))))
(when-let (value (cider-sync-request:inspect-push idx))
(cider-inspector--render-value value)))

(defun cider-inspector-refresh ()
(interactive)
(cider-nrepl-send-request
(list "op" "inspect-refresh"
"session" (cider-current-session))
(cider-inspector-response-handler (current-buffer))))
(when-let (value (cider-sync-request:inspect-refresh))
(cider-inspector--render-value value)))

(defun cider-inspector-next-page ()
"Jump to the next page when inspecting a paginated sequence/map.

Does nothing if already on the last page."
(interactive)
(push (point) cider-inspector-page-location-stack)
(cider-nrepl-send-request
(list "op" "inspect-next-page"
"session" (cider-current-session))
(cider-inspector-response-handler (current-buffer))))
(when-let (value (cider-sync-request:inspect-next-page))
(cider-inspector--render-value value)))

(defun cider-inspector-prev-page ()
"Jump to the previous page when expecting a paginated sequence/map.

Does nothing if already on the first page."
(interactive)
(setq cider-inspector-last-command 'cider-inspector-prev-page)
(cider-nrepl-send-request
(list "op" "inspect-prev-page"
"session" (cider-current-session))
(cider-inspector-response-handler (current-buffer))))
(when-let (value (cider-sync-request:inspect-prev-page))
(cider-inspector--render-value value)))

(defun cider-inspector-set-page-size (page-size)
"Set the page size in pagination mode to the specified PAGE-SIZE.

Current page will be reset to zero."
(interactive "nPage size:")
(cider-nrepl-send-request
(list "op" "inspect-set-page-size"
"session" (cider-current-session)
"page-size" page-size)
(cider-inspector-response-handler (current-buffer))))
(interactive "nPage size: ")
(when-let (value (cider-sync-request:inspect-set-page-size page-size))
(cider-inspector--render-value value)))

;; nREPL interactions
(defun cider-sync-request:inspect-pop ()
"Move one level up in the inspector stack."
(thread-first (list "op" "inspect-pop"
"session" (cider-current-session))
(cider-nrepl-send-sync-request)
(nrepl-dict-get "value")))

(defun cider-sync-request:inspect-push (idx)
"Inspect the inside value specified by IDX."
(thread-first (list "op" "inspect-push"
"idx" idx
"session" (cider-current-session))
(cider-nrepl-send-sync-request)
(nrepl-dict-get "value")))

(defun cider-sync-request:inspect-refresh ()
"Re-render the currently inspected value."
(thread-first (list "op" "inspect-refresh"
"session" (cider-current-session))
(cider-nrepl-send-sync-request)
(nrepl-dict-get "value")))

(defun cider-sync-request:inspect-next-page ()
"Jump to the next page in paginated collection view."
(thread-first (list "op" "inspect-next-page"
"session" (cider-current-session))
(cider-nrepl-send-sync-request)
(nrepl-dict-get "value")))

(defun cider-sync-request:inspect-prev-page ()
"Jump to the previous page in paginated collection view."
(thread-first (list "op" "inspect-prev-page"
"session" (cider-current-session))
(cider-nrepl-send-sync-request)
(nrepl-dict-get "value")))

(defun cider-sync-request:inspect-set-page-size (page-size)
"Set the page size in paginated view to PAGE-SIZE."
(thread-first (list "op" "inspect-set-page-size"
"page-size" page-size
"session" (cider-current-session))
(cider-nrepl-send-sync-request)
(nrepl-dict-get "value")))

(defun cider-sync-request:inspect-expr (expr ns page-size)
"Evaluate EXPR in context of NS and inspect its result.
Set the page size in paginated view to PAGE-SIZE."
(thread-first (append (nrepl--eval-request expr (cider-current-session) ns)
(list "inspect" "true"
"page-size" page-size))
(cider-nrepl-send-sync-request)
(nrepl-dict-get "value")))

;; Render Inspector from Structured Values
(defun cider-inspector--render-value (value)
(cider-make-popup-buffer cider-inspector-buffer 'cider-inspector-mode)
(cider-inspector-render cider-inspector-buffer value)
(cider-popup-buffer-display cider-inspector-buffer t)
(with-current-buffer cider-inspector-buffer
(when (eq cider-inspector-last-command 'cider-inspector-pop)
(setq cider-inspector-last-command nil)
;; Prevents error message being displayed when we try to pop
;; from the top-level of a data struture
(when cider-inspector-location-stack
(goto-char (pop cider-inspector-location-stack))))

(when (eq cider-inspector-last-command 'cider-inspector-prev-page)
(setq cider-inspector-last-command nil)
;; Prevents error message being displayed when we try to
;; go to a prev-page from the first page
(when cider-inspector-page-location-stack
(goto-char (pop cider-inspector-page-location-stack))))))

(defun cider-inspector-render (buffer str)
(with-current-buffer buffer
(cider-inspector-mode)
Expand Down