Klimi's new dotfiles with stow.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

419 lines
16 KiB

преди 4 години
  1. ;;; cider-inspector.el --- Object inspector -*- lexical-binding: t -*-
  2. ;; Copyright © 2013-2019 Vital Reactor, LLC
  3. ;; Copyright © 2014-2019 Bozhidar Batsov and CIDER contributors
  4. ;; Author: Ian Eslick <ian@vitalreactor.com>
  5. ;; Bozhidar Batsov <bozhidar@batsov.com>
  6. ;; This program is free software: you can redistribute it and/or modify
  7. ;; it under the terms of the GNU General Public License as published by
  8. ;; the Free Software Foundation, either version 3 of the License, or
  9. ;; (at your option) any later version.
  10. ;; This program is distributed in the hope that it will be useful,
  11. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. ;; GNU General Public License for more details.
  14. ;; You should have received a copy of the GNU General Public License
  15. ;; along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. ;; This file is not part of GNU Emacs.
  17. ;;; Commentary:
  18. ;; Clojure object inspector inspired by SLIME.
  19. ;;; Code:
  20. (require 'cl-lib)
  21. (require 'easymenu)
  22. (require 'seq)
  23. (require 'cider-eval)
  24. ;; ===================================
  25. ;; Inspector Key Map and Derived Mode
  26. ;; ===================================
  27. (defconst cider-inspector-buffer "*cider-inspect*")
  28. ;;; Customization
  29. (defgroup cider-inspector nil
  30. "Presentation and behaviour of the CIDER value inspector."
  31. :prefix "cider-inspector-"
  32. :group 'cider
  33. :package-version '(cider . "0.10.0"))
  34. (defcustom cider-inspector-page-size 32
  35. "Default page size in paginated inspector view.
  36. The page size can be also changed interactively within the inspector."
  37. :type '(integer :tag "Page size" 32)
  38. :group 'cider-inspector
  39. :package-version '(cider . "0.10.0"))
  40. (defcustom cider-inspector-fill-frame nil
  41. "Controls whether the CIDER inspector window fills its frame."
  42. :type 'boolean
  43. :group 'cider-inspector
  44. :package-version '(cider . "0.15.0"))
  45. (defvar cider-inspector-mode-map
  46. (let ((map (make-sparse-keymap)))
  47. (set-keymap-parent map cider-popup-buffer-mode-map)
  48. (define-key map (kbd "RET") #'cider-inspector-operate-on-point)
  49. (define-key map [mouse-1] #'cider-inspector-operate-on-click)
  50. (define-key map "l" #'cider-inspector-pop)
  51. (define-key map "g" #'cider-inspector-refresh)
  52. ;; Page-up/down
  53. (define-key map [next] #'cider-inspector-next-page)
  54. (define-key map [prior] #'cider-inspector-prev-page)
  55. (define-key map " " #'cider-inspector-next-page)
  56. (define-key map (kbd "M-SPC") #'cider-inspector-prev-page)
  57. (define-key map (kbd "S-SPC") #'cider-inspector-prev-page)
  58. (define-key map "s" #'cider-inspector-set-page-size)
  59. (define-key map [tab] #'cider-inspector-next-inspectable-object)
  60. (define-key map "\C-i" #'cider-inspector-next-inspectable-object)
  61. (define-key map [(shift tab)] #'cider-inspector-previous-inspectable-object)
  62. ;; Emacs translates S-TAB to BACKTAB on X.
  63. (define-key map [backtab] #'cider-inspector-previous-inspectable-object)
  64. (easy-menu-define cider-inspector-mode-menu map
  65. "Menu for CIDER's inspector."
  66. `("CIDER Inspector"
  67. ["Inspect" cider-inspector-operate-on-point]
  68. ["Pop" cider-inspector-pop]
  69. ["Refresh" cider-inspector-refresh]
  70. "--"
  71. ["Next Inspectable Object" cider-inspector-next-inspectable-object]
  72. ["Previous Inspectable Object" cider-inspector-previous-inspectable-object]
  73. "--"
  74. ["Next Page" cider-inspector-next-page]
  75. ["Previous Page" cider-inspector-prev-page]
  76. ["Set Page Size" cider-inspector-set-page-size]
  77. "--"
  78. ["Quit" cider-popup-buffer-quit-function]
  79. ))
  80. map))
  81. (define-derived-mode cider-inspector-mode special-mode "Inspector"
  82. "Major mode for inspecting Clojure data structures.
  83. \\{cider-inspector-mode-map}"
  84. (set-syntax-table clojure-mode-syntax-table)
  85. (setq-local electric-indent-chars nil)
  86. (setq-local sesman-system 'CIDER)
  87. (visual-line-mode 1))
  88. ;;;###autoload
  89. (defun cider-inspect-last-sexp ()
  90. "Inspect the result of the the expression preceding point."
  91. (interactive)
  92. (cider-inspect-expr (cider-last-sexp) (cider-current-ns)))
  93. ;;;###autoload
  94. (defun cider-inspect-defun-at-point ()
  95. "Inspect the result of the \"top-level\" expression at point."
  96. (interactive)
  97. (cider-inspect-expr (cider-defun-at-point) (cider-current-ns)))
  98. ;;;###autoload
  99. (defun cider-inspect-last-result ()
  100. "Inspect the most recent eval result."
  101. (interactive)
  102. (cider-inspect-expr "*1" (cider-current-ns)))
  103. ;;;###autoload
  104. (defun cider-inspect (&optional arg)
  105. "Inspect the result of the preceding sexp.
  106. With a prefix argument ARG it inspects the result of the \"top-level\" form.
  107. With a second prefix argument it prompts for an expression to eval and inspect."
  108. (interactive "p")
  109. (pcase arg
  110. (1 (cider-inspect-last-sexp))
  111. (4 (cider-inspect-defun-at-point))
  112. (16 (call-interactively #'cider-inspect-expr))))
  113. (defvar cider-inspector-location-stack nil
  114. "A stack used to save point locations in inspector buffers.
  115. These locations are used to emulate `save-excursion' between
  116. `cider-inspector-push' and `cider-inspector-pop' operations.")
  117. (defvar cider-inspector-page-location-stack nil
  118. "A stack used to save point locations in inspector buffers.
  119. These locations are used to emulate `save-excursion' between
  120. `cider-inspector-next-page' and `cider-inspector-prev-page' operations.")
  121. (defvar cider-inspector-last-command nil
  122. "Contains the value of the most recently used `cider-inspector-*' command.
  123. This is used as an alternative to the built-in `last-command'. Whenever we
  124. invoke any command through \\[execute-extended-command] and its variants,
  125. the value of `last-command' is not set to the command it invokes.")
  126. (defvar cider-inspector--current-repl nil
  127. "Contains the reference to the REPL where inspector was last invoked from.
  128. This is needed for internal inspector buffer operations (push,
  129. pop) to execute against the correct REPL session.")
  130. ;; Operations
  131. ;;;###autoload
  132. (defun cider-inspect-expr (expr ns)
  133. "Evaluate EXPR in NS and inspect its value.
  134. Interactively, EXPR is read from the minibuffer, and NS the
  135. current buffer's namespace."
  136. (interactive (list (cider-read-from-minibuffer "Inspect expression: " (cider-sexp-at-point))
  137. (cider-current-ns)))
  138. (setq cider-inspector--current-repl (cider-current-repl))
  139. (when-let* ((value (cider-sync-request:inspect-expr expr ns (or cider-inspector-page-size 32))))
  140. (cider-inspector--render-value value)))
  141. (defun cider-inspector-pop ()
  142. "Pop the last value off the inspector stack and render it.
  143. See `cider-sync-request:inspect-pop' and `cider-inspector--render-value'."
  144. (interactive)
  145. (setq cider-inspector-last-command 'cider-inspector-pop)
  146. (when-let* ((value (cider-sync-request:inspect-pop)))
  147. (cider-inspector--render-value value)))
  148. (defun cider-inspector-push (idx)
  149. "Inspect the value at IDX in the inspector stack and render it.
  150. See `cider-sync-request:insepect-push' and `cider-inspector--render-value'"
  151. (push (point) cider-inspector-location-stack)
  152. (when-let* ((value (cider-sync-request:inspect-push idx)))
  153. (cider-inspector--render-value value)))
  154. (defun cider-inspector-refresh ()
  155. "Re-render the currently inspected value.
  156. See `cider-sync-request:insepect-refresh' and `cider-inspector--render-value'"
  157. (interactive)
  158. (when-let* ((value (cider-sync-request:inspect-refresh)))
  159. (cider-inspector--render-value value)))
  160. (defun cider-inspector-next-page ()
  161. "Jump to the next page when inspecting a paginated sequence/map.
  162. Does nothing if already on the last page."
  163. (interactive)
  164. (push (point) cider-inspector-page-location-stack)
  165. (when-let* ((value (cider-sync-request:inspect-next-page)))
  166. (cider-inspector--render-value value)))
  167. (defun cider-inspector-prev-page ()
  168. "Jump to the previous page when expecting a paginated sequence/map.
  169. Does nothing if already on the first page."
  170. (interactive)
  171. (setq cider-inspector-last-command 'cider-inspector-prev-page)
  172. (when-let* ((value (cider-sync-request:inspect-prev-page)))
  173. (cider-inspector--render-value value)))
  174. (defun cider-inspector-set-page-size (page-size)
  175. "Set the page size in pagination mode to the specified PAGE-SIZE.
  176. Current page will be reset to zero."
  177. (interactive "nPage size: ")
  178. (when-let* ((value (cider-sync-request:inspect-set-page-size page-size)))
  179. (cider-inspector--render-value value)))
  180. ;; nREPL interactions
  181. (defun cider-sync-request:inspect-pop ()
  182. "Move one level up in the inspector stack."
  183. (thread-first '("op" "inspect-pop")
  184. (cider-nrepl-send-sync-request cider-inspector--current-repl)
  185. (nrepl-dict-get "value")))
  186. (defun cider-sync-request:inspect-push (idx)
  187. "Inspect the inside value specified by IDX."
  188. (thread-first `("op" "inspect-push"
  189. "idx" ,idx)
  190. (cider-nrepl-send-sync-request cider-inspector--current-repl)
  191. (nrepl-dict-get "value")))
  192. (defun cider-sync-request:inspect-refresh ()
  193. "Re-render the currently inspected value."
  194. (thread-first '("op" "inspect-refresh")
  195. (cider-nrepl-send-sync-request cider-inspector--current-repl)
  196. (nrepl-dict-get "value")))
  197. (defun cider-sync-request:inspect-next-page ()
  198. "Jump to the next page in paginated collection view."
  199. (thread-first '("op" "inspect-next-page")
  200. (cider-nrepl-send-sync-request cider-inspector--current-repl)
  201. (nrepl-dict-get "value")))
  202. (defun cider-sync-request:inspect-prev-page ()
  203. "Jump to the previous page in paginated collection view."
  204. (thread-first '("op" "inspect-prev-page")
  205. (cider-nrepl-send-sync-request cider-inspector--current-repl)
  206. (nrepl-dict-get "value")))
  207. (defun cider-sync-request:inspect-set-page-size (page-size)
  208. "Set the page size in paginated view to PAGE-SIZE."
  209. (thread-first `("op" "inspect-set-page-size"
  210. "page-size" ,page-size)
  211. (cider-nrepl-send-sync-request cider-inspector--current-repl)
  212. (nrepl-dict-get "value")))
  213. (defun cider-sync-request:inspect-expr (expr ns page-size)
  214. "Evaluate EXPR in context of NS and inspect its result.
  215. Set the page size in paginated view to PAGE-SIZE."
  216. (thread-first (append (nrepl--eval-request expr ns)
  217. `("inspect" "true"
  218. "page-size" ,page-size))
  219. (cider-nrepl-send-sync-request cider-inspector--current-repl)
  220. (nrepl-dict-get "value")))
  221. ;; Render Inspector from Structured Values
  222. (defun cider-inspector--render-value (value)
  223. "Render VALUE."
  224. (cider-make-popup-buffer cider-inspector-buffer 'cider-inspector-mode 'ancillary)
  225. (cider-inspector-render cider-inspector-buffer value)
  226. (cider-popup-buffer-display cider-inspector-buffer t)
  227. (when cider-inspector-fill-frame (delete-other-windows))
  228. (with-current-buffer cider-inspector-buffer
  229. (when (eq cider-inspector-last-command 'cider-inspector-pop)
  230. (setq cider-inspector-last-command nil)
  231. ;; Prevents error message being displayed when we try to pop
  232. ;; from the top-level of a data struture
  233. (when cider-inspector-location-stack
  234. (goto-char (pop cider-inspector-location-stack))))
  235. (when (eq cider-inspector-last-command 'cider-inspector-prev-page)
  236. (setq cider-inspector-last-command nil)
  237. ;; Prevents error message being displayed when we try to
  238. ;; go to a prev-page from the first page
  239. (when cider-inspector-page-location-stack
  240. (goto-char (pop cider-inspector-page-location-stack))))))
  241. (defun cider-inspector-render (buffer str)
  242. "Render STR in BUFFER."
  243. (with-current-buffer buffer
  244. (cider-inspector-mode)
  245. (let ((inhibit-read-only t))
  246. (condition-case nil
  247. (cider-inspector-render* (car (read-from-string str)))
  248. (error (insert "\nInspector error for: " str))))
  249. (goto-char (point-min))))
  250. (defun cider-inspector-render* (elements)
  251. "Render ELEMENTS."
  252. (dolist (el elements)
  253. (cider-inspector-render-el* el)))
  254. (defun cider-inspector-render-el* (el)
  255. "Render EL."
  256. (cond ((symbolp el) (insert (symbol-name el)))
  257. ((stringp el) (insert (propertize el 'font-lock-face 'font-lock-keyword-face)))
  258. ((and (consp el) (eq (car el) :newline))
  259. (insert "\n"))
  260. ((and (consp el) (eq (car el) :value))
  261. (cider-inspector-render-value (cadr el) (cl-caddr el)))
  262. (t (message "Unrecognized inspector object: %s" el))))
  263. (defun cider-inspector-render-value (value idx)
  264. "Render VALUE at IDX."
  265. (cider-propertize-region
  266. (list 'cider-value-idx idx
  267. 'mouse-face 'highlight)
  268. (cider-inspector-render-el* (cider-font-lock-as-clojure value))))
  269. ;; ===================================================
  270. ;; Inspector Navigation (lifted from SLIME inspector)
  271. ;; ===================================================
  272. (defun cider-find-inspectable-object (direction limit)
  273. "Find the next/previous inspectable object.
  274. DIRECTION can be either 'next or 'prev.
  275. LIMIT is the maximum or minimum position in the current buffer.
  276. Return a list of two values: If an object could be found, the
  277. starting position of the found object and T is returned;
  278. otherwise LIMIT and NIL is returned."
  279. (let ((finder (cl-ecase direction
  280. (next 'next-single-property-change)
  281. (prev 'previous-single-property-change))))
  282. (let ((prop nil) (curpos (point)))
  283. (while (and (not prop) (not (= curpos limit)))
  284. (let ((newpos (funcall finder curpos 'cider-value-idx nil limit)))
  285. (setq prop (get-text-property newpos 'cider-value-idx))
  286. (setq curpos newpos)))
  287. (list curpos (and prop t)))))
  288. (defun cider-inspector-next-inspectable-object (arg)
  289. "Move point to the next inspectable object.
  290. With optional ARG, move across that many objects.
  291. If ARG is negative, move backwards."
  292. (interactive "p")
  293. (let ((maxpos (point-max)) (minpos (point-min))
  294. (previously-wrapped-p nil))
  295. ;; Forward.
  296. (while (> arg 0)
  297. (seq-let (pos foundp) (cider-find-inspectable-object 'next maxpos)
  298. (if foundp
  299. (progn (goto-char pos) (setq arg (1- arg))
  300. (setq previously-wrapped-p nil))
  301. (if (not previously-wrapped-p) ; cycle detection
  302. (progn (goto-char minpos) (setq previously-wrapped-p t))
  303. (error "No inspectable objects")))))
  304. ;; Backward.
  305. (while (< arg 0)
  306. (seq-let (pos foundp) (cider-find-inspectable-object 'prev minpos)
  307. ;; CIDER-OPEN-INSPECTOR inserts the title of an inspector page
  308. ;; as a presentation at the beginning of the buffer; skip
  309. ;; that. (Notice how this problem can not arise in ``Forward.'')
  310. (if (and foundp (/= pos minpos))
  311. (progn (goto-char pos) (setq arg (1+ arg))
  312. (setq previously-wrapped-p nil))
  313. (if (not previously-wrapped-p) ; cycle detection
  314. (progn (goto-char maxpos) (setq previously-wrapped-p t))
  315. (error "No inspectable objects")))))))
  316. (defun cider-inspector-previous-inspectable-object (arg)
  317. "Move point to the previous inspectable object.
  318. With optional ARG, move across that many objects.
  319. If ARG is negative, move forwards."
  320. (interactive "p")
  321. (cider-inspector-next-inspectable-object (- arg)))
  322. (defun cider-inspector-property-at-point ()
  323. "Return property at point."
  324. (let* ((properties '(cider-value-idx cider-range-button
  325. cider-action-number))
  326. (find-property
  327. (lambda (point)
  328. (cl-loop for property in properties
  329. for value = (get-text-property point property)
  330. when value
  331. return (list property value)))))
  332. (or (funcall find-property (point))
  333. (funcall find-property (1- (point))))))
  334. (defun cider-inspector-operate-on-point ()
  335. "Invoke the command for the text at point.
  336. 1. If point is on a value then recursively call the inspector on
  337. that value.
  338. 2. If point is on an action then call that action.
  339. 3. If point is on a range-button fetch and insert the range."
  340. (interactive)
  341. (seq-let (property value) (cider-inspector-property-at-point)
  342. (cl-case property
  343. (cider-value-idx
  344. (cider-inspector-push value))
  345. ;; TODO: range and action handlers
  346. (t (error "No object at point")))))
  347. (defun cider-inspector-operate-on-click (event)
  348. "Move to EVENT's position and operate the part."
  349. (interactive "@e")
  350. (let ((point (posn-point (event-end event))))
  351. (cond ((and point
  352. (or (get-text-property point 'cider-value-idx)))
  353. (goto-char point)
  354. (cider-inspector-operate-on-point))
  355. (t
  356. (error "No clickable part here")))))
  357. (provide 'cider-inspector)
  358. ;;; cider-inspector.el ends here