-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrunk-io-tree.el
363 lines (333 loc) · 10.6 KB
/
trunk-io-tree.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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
;;; trunk-io-tree.el --- Treemacs interface for trunk.io's trunk check
;; Copyright (C) 2024 Joe Sadusk
;; Author Joe Sadusk <[email protected]>
;; Version 0.0.1
;; Package-Requires: ((dash "2.19") (treemacs "3.1"))
;;; Commentary
;; This package provides a navigable interface to the meta lint tool "trunk check"
;; provided by https://trunk.io
;;; Code:
(require 'dash)
(require 'treemacs)
(require 'treemacs-treelib)
(defun group-by-key (key hashlist)
(-map
'cdr
(-group-by
(lambda (elem)
(gethash key elem)
)
(-filter
(lambda (elem)
(gethash key elem nil)
)
hashlist)
)
)
)
(defun trunk-check-sections (trunk-result)
(let* (
(trunk-result-issues (gethash "issues" trunk-result))
(trunk-result-failures ())
)
`(
("Issues" . ,trunk-result-issues)
("Failures" . ,trunk-result-failures)
)
)
)
(defun linecol-for-issue (issue)
(format "%s:%s" (gethash "line" issue "**") (gethash "column" issue "**"))
)
(defun trunk-issues-by-linecol (issues-for-file)
(let (
(issues-by-linecol (make-hash-table :test 'equal))
(ret ())
)
(seq-doseq (issue issues-for-file)
(let* (
(linecol (linecol-for-issue issue))
(issues-for-linecol (gethash linecol issues-by-linecol ()))
)
(push issue issues-for-linecol)
(puthash linecol issues-for-linecol issues-by-linecol)
)
)
(maphash (lambda (linecol issues-for-linecol)
(push issues-for-linecol ret)
)
issues-by-linecol)
(sort ret (lambda (a b)
(let (
(aline (string-to-number (gethash "line" (car a))))
(bline (string-to-number (gethash "line" (car b))))
(acol (string-to-number (gethash "column" (car a))))
(bcol (string-to-number (gethash "column" (car b))))
)
(if (= aline bline)
(< acol bcol)
(< aline bline)
)
)
)
)
)
)
(defun trunk-issues-by-file (issues)
(let (
(issues-by-file (make-hash-table :test 'equal))
(ret ())
)
(seq-doseq (issue issues)
(let* (
(file (gethash "file" issue))
(issues-for-file (gethash file issues-by-file ()))
)
(if (string= "ISSUE_CLASS_NEW" (gethash "issueClass" issue))
(progn
(push issue issues-for-file)
(puthash file issues-for-file issues-by-file)
)
)
)
)
(maphash (lambda (file issues-for-file)
(push `(,file . ,issues-for-file) ret)
)
issues-by-file)
(sort ret (lambda (a b) (string< (car a) (car b))))
)
)
(defun trunk-tree-issue-properties (issue)
`(
,(format "[%s - %s] %s:%s"
(gethash "linter" issue)
(gethash "level" issue)
(gethash "line" issue)
(gethash "column" issue))
.
(
,(gethash "message" issue)
)
)
)
(defun trunk-tree-level (level-id)
(cond
((string= level-id "LEVEL_HIGH")
(propertize "high" 'face 'error));'(:foreground "red")))
((string= level-id "LEVEL_WARN")
(propertize "warn" 'face 'warning));'(:foreground "yellow")))
((string= level-id "LEVEL_INFO")
(propertize "info" 'face 'success));'(:foreground "green")))
(t level-id)
)
)
(defun trunk-tree-issue-label (issue)
(let* (
(linecol (format "%s %s : %s %s"
(propertize "line" 'face 'shadow)
(propertize (gethash "line" issue "**") 'face 'bold)
(propertize "col" 'face 'shadow)
(propertize (gethash "column" issue "**") 'face 'bold)))
(lintwarn (format "[%s - %s]"
(propertize (gethash "linter" issue)
'face 'font-lock-string-face)
(trunk-tree-level (trunk-tree-level
(gethash "level" issue)))))
(gap-len (- (window-body-width) (+ (length linecol) (length lintwarn) 7)))
(gap (make-string gap-len ?\s))
)
(format "%s%s%s" linecol gap lintwarn)
)
)
(defun trunk-tree-issue-linecol-label (issue)
(let* (
(linecol (format "%s %s : %s %s"
(propertize "line" 'face 'shadow)
(propertize (gethash "line" issue "**") 'face 'bold)
(propertize "col" 'face 'shadow)
(propertize (gethash "column" issue "**") 'face 'bold)))
)
linecol
)
)
(defun trunk-tree-issue-lintlevel-label (issue)
(let* (
(lintwarn (format "[%s - %s]"
(propertize (gethash "linter" issue)
'face 'font-lock-string-face)
(trunk-tree-level (trunk-tree-level
(gethash "level" issue)))))
)
lintwarn
)
)
(defun trunk-tree-RET-issue-action (&optional _)
(let* (
(file (-some-> (treemacs-current-button)
(treemacs-button-get :file)))
(line (-some-> (treemacs-current-button)
(treemacs-button-get :line)))
(column (-some-> (treemacs-current-button)
(treemacs-button-get :column)))
)
(find-file-other-window file)
(goto-char (point-min))
(forward-line (1- (string-to-number line)))
(move-to-column (1- (string-to-number column)))
)
)
(treemacs-define-variadic-entry-node-type trunk-result-variadic
:key 'trunk-result-variadic
:children (trunk-check-sections trunk-result)
:child-type 'trunk-tree-section-group)
(treemacs-define-expandable-node-type
trunk-tree-section-group
:closed-icon "+ "
:open-icon "- "
:label (propertize (car item) 'face 'font-lock-keyword-face)
:key 'trunk-tree-section-group
:children (if (string= (car item) "Issues") (trunk-issues-by-file (cdr item)) ())
:child-type 'trunk-tree-file-issue-group
)
(treemacs-define-expandable-node-type
trunk-tree-file-issue-group
:closed-icon "+ "
:open-icon "- "
:label (propertize (car item) 'face 'font-lock-variable-name-face)
:key 'trunk-tree-file-issue-group
:children (trunk-issues-by-linecol (cdr item))
:child-type 'trunk-tree-issue-linecol-group
)
(treemacs-define-expandable-node-type
trunk-tree-failure-group
:closed-icon "+ "
:open-icon "- "
:label ""
:key 'trunk-tree-failure-group
:children '()
:child-type 'trunk-tree-failure-leaf
)
(treemacs-define-leaf-node-type
trunk-tree-failure-leaf
:icon ""
:label ""
:key item
)
(treemacs-define-expandable-node-type
trunk-tree-issue-linecol-group
:closed-icon "+ "
:open-icon "- "
:label (trunk-tree-issue-linecol-label (car item))
:key 'trunk-tree-issue-group
:children (group-by-key "linter" item )
:child-type 'trunk-tree-issue-group
:more-properties `(
:file ,(gethash "file" (car item))
:line ,(gethash "line" (car item))
:column ,(gethash "column" (car item))
:column1 ,(gethash "column" (car item))
)
:ret-action #'trunk-tree-RET-issue-action
)
(treemacs-define-expandable-node-type
trunk-tree-issue-group
:closed-icon "+ "
:open-icon "- "
:label (trunk-tree-issue-lintlevel-label (car item))
:key 'trunk-tree-issue-group
:children item
:child-type 'trunk-tree-message-leaf
:more-properties `(
:file ,(gethash "file" (car item))
:line ,(gethash "line" (car item))
:column ,(gethash "column" (car item))
:column2 ,(gethash "column" (car item))
)
:ret-action #'trunk-tree-RET-issue-action
)
(treemacs-define-leaf-node-type
trunk-tree-message-leaf
:icon ""
:label (gethash "message" item)
:key item
:more-properties `(
:file ,(gethash "file" item)
:line ,(gethash "line" item)
:column ,(gethash "column" item)
:column3 ,(gethash "column" item)
)
:ret-action #'trunk-tree-RET-issue-action
)
(defun matches-in-buffer (regexp &optional buffer)
"return a list of matches of REGEXP in BUFFER or the current buffer if not given."
(let ((matches))
(save-match-data
(save-excursion
(with-current-buffer (or buffer (current-buffer))
(save-restriction
(widen)
(goto-char 1)
(while (search-forward-regexp regexp nil t 1)
(push (match-string 0) matches)))))
matches)))
(defun json-at-last-line (buffer)
(with-current-buffer buffer
(progn
(goto-char (point-max))
(beginning-of-line)
(json-parse-buffer)
)
)
)
(defun trunk-check ()
(interactive)
(message "Running trunk check")
(let* (
(default-directory (project-root (project-current)))
(buffer-name (concat "*trunk check <" (project-root (project-current)) ">*"))
(trunk-buffer (get-buffer-create buffer-name))
(trunk-command "tools/trunk check --no-progress --output=json |grep '^{.*}$'|head -n1 | sed -z '$ s/\\n$//'")
)
(message trunk-command)
(with-current-buffer trunk-buffer
(if buffer-read-only
(read-only-mode)
)
(dolist (thismode local-minor-modes)
(funcall thismode 0)
)
(erase-buffer)
(insert "Running trunk check\n")
(comint-mode)
)
(let (
(trunk-process (start-file-process-shell-command buffer-name trunk-buffer trunk-command))
)
(set-process-sentinel
trunk-process
(lambda (process event)
(let* (
(trunk-buffer (process-buffer process))
(trunk-result (json-at-last-line trunk-buffer))
)
(message "Completed trunk check")
(save-selected-window
(pop-to-buffer trunk-buffer)
(let (
(treemacs-user-mode-line-format (format "*** Trunk check <%s> ***" (project-root (project-current))))
)
(treemacs-initialize
trunk-result-variadic
:with-expand-depth 'all
:and-do (setf treemacs-space-between-root-nodes t)
)
)
)
)
)
)
)
)
)
(provide 'trunk-io-tree)