forked from NicolasPetton/Indium
-
Notifications
You must be signed in to change notification settings - Fork 0
/
indium-inspector.el
186 lines (156 loc) · 6.71 KB
/
indium-inspector.el
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
;;; indium-inspector.el --- Inspector for JavaScript objects -*- lexical-binding: t; -*-
;; Copyright (C) 2016-2018 Nicolas Petton
;; Author: Nicolas Petton <[email protected]>
;; Keywords: convenience, tools, javascript
;; This program is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.
;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;; You should have received a copy of the GNU General Public License
;; along with this program. If not, see <http://www.gnu.org/licenses/>.
;;; Commentary:
;;
;;; Code:
(require 'seq)
(require 'map)
(require 'subr-x)
(require 'indium-structs)
(require 'indium-render)
(require 'indium-faces)
(declare-function indium-client-get-properties "indium-client.el")
(defvar indium-inspector-history nil)
(make-variable-buffer-local 'indium-inspector-history)
(defun indium-inspector-inspect (obj)
"Open an inspector on the remote object OBJ."
(if (indium-remote-object-reference-p obj)
(indium-client-get-properties
(indium-remote-object-id obj)
(lambda (properties)
(indium-inspector--inspect-properties properties obj)))
(message "Cannot inspect %S" (indium-remote-object-description obj))))
(defun indium-inspector--inspect-properties (properties obj)
"Insert all PROPERTIES for the remote object OBJ."
(let ((buf (indium-inspector-get-buffer-create))
(inhibit-read-only t))
(with-current-buffer buf
(indium-inspector-push-to-history obj)
(save-excursion
(erase-buffer)
(indium-render-keyword (indium-remote-object-to-string obj t))
(insert "\n\n")
(indium-inspector--insert-sorted-properties properties)))
(pop-to-buffer buf)))
(defun indium-inspector--insert-sorted-properties (properties)
"Insert sorted PROPERTIES."
(let ((sorted-properties (indium-inspector--split-properties properties)))
(indium-render-properties (cadr sorted-properties))
(insert "\n")
(when-let (native (car sorted-properties))
(indium-render-properties native)
(insert "\n"))))
(defun indium-inspector--split-properties (properties)
"Split PROPERTIES into list where the first element is native properties and the second is the rest."
(let ((split (seq-reduce (lambda (result property)
(push property
(if (indium-property-native-p property)
(car result)
(cadr result)))
result)
properties
(list nil nil))))
(seq-map (lambda (list) (nreverse list)) split)))
(defun indium-inspector-pop ()
"Go back in the history to the last object inspected."
(interactive)
(if (cdr indium-inspector-history)
(progn
(pop indium-inspector-history)
(funcall #'indium-inspector-inspect (car indium-inspector-history)))
(message "No previous object to inspect")))
(defun indium-inspector-goto-reference (direction)
"Move point to the next object reference in DIRECTION.
DIRECTION can be either `next' or `previous'."
(let* ((delta (pcase direction
(`next 1)
(`previous -1)))
(limit-check (pcase direction
(`next #'eobp)
(`previous #'bobp)))
(reference (save-excursion
(forward-line delta)
(when (eq direction 'previous)
(end-of-line))
(while (and (not (funcall limit-check))
(not (get-text-property (point) 'indium-reference)))
(forward-char delta))
(when (get-text-property (point) 'indium-reference)
(point)))))
(when reference
(goto-char reference)
;; go to the first char of the reference
(while (get-text-property (point) 'indium-reference)
(backward-char 1))
(forward-char 1))))
(defun indium-inspector-next-reference ()
"Move the point to the next object reference."
(interactive)
(indium-inspector-goto-reference 'next))
(defun indium-inspector-previous-reference ()
"Move the point to the previous object reference."
(interactive)
(indium-inspector-goto-reference 'previous))
(defun indium-inspector-refresh ()
"Request new data to the backend and update the inspector buffer."
(interactive)
(when indium-inspector-history
(funcall #'indium-inspector-inspect (car indium-inspector-history))))
(defun indium-inspector-push-to-history (reference)
"Add REFERENCE to the inspected objects history."
(let-alist reference
(when (or (seq-empty-p indium-inspector-history)
(not (equal (indium-remote-object-id reference)
(indium-remote-object-id (car indium-inspector-history)))))
(push reference indium-inspector-history))))
(defun indium-inspector-get-buffer ()
"Return the inspector buffer, or nil if no inspector buffer exists."
(get-buffer (indium-inspector-buffer-name)))
(defun indium-inspector-get-buffer-create ()
"Return an inspector buffer for the current connection.
If no buffer exists, create one."
(let ((buf (indium-inspector-get-buffer)))
(unless buf
(setq buf (get-buffer-create (indium-inspector-buffer-name)))
(indium-inspector-setup-buffer buf))
buf))
(defun indium-inspector-setup-buffer (buffer)
"Setup the inspector BUFFER."
(with-current-buffer buffer
(indium-inspector-mode)))
(defun indium-inspector-buffer-name ()
"Return the inspector buffer name for the current connection."
"*JS Inspector*")
(defvar indium-inspector-mode-map
(let ((map (make-sparse-keymap)))
(define-key map [return] #'indium-follow-link)
(define-key map "\C-m" #'indium-follow-link)
(define-key map [mouse-1] #'indium-follow-link)
(define-key map "l" #'indium-inspector-pop)
(define-key map "g" #'indium-inspector-refresh)
(define-key map "n" #'indium-inspector-next-reference)
(define-key map "p" #'indium-inspector-previous-reference)
(define-key map [tab] #'indium-inspector-next-reference)
(define-key map [backtab] #'indium-inspector-previous-reference)
map))
(define-derived-mode indium-inspector-mode special-mode "Inspector"
"Major mode for inspecting JavaScript objects.
\\{indium-inspector-mode-map}"
(setq buffer-read-only t)
(font-lock-ensure)
(setq-local electric-indent-chars nil)
(setq-local truncate-lines t))
(provide 'indium-inspector)
;;; indium-inspector.el ends here