-
Notifications
You must be signed in to change notification settings - Fork 216
/
lsp-bridge-code-action.el
415 lines (357 loc) · 16.2 KB
/
lsp-bridge-code-action.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
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
;;; lsp-bridge-code-action.el --- Code action for lsp-bridge -*- lexical-binding: t; no-byte-compile: t; -*-*-
;; Filename: lsp-bridge-code-action.el
;; Description: Code action for lsp-bridge
;; Author: Andy Stewart <[email protected]>
;; Maintainer: Andy Stewart <[email protected]>
;; Copyright (C) 2022, Andy Stewart, all rights reserved.
;; Created: 2022-12-14 18:10:57
;; Version: 0.1
;; Last-Updated: 2022-12-14 18:10:57
;; By: Andy Stewart
;; URL: https://www.github.org/manateelazycat/lsp-bridge-code-action
;; Keywords:
;; Compatibility: GNU Emacs 28.2
;;
;; Features that might be required by this library:
;;
;;
;;
;;; This file is NOT part of GNU Emacs
;;; License
;;
;; 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, 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; see the file COPYING. If not, write to
;; the Free Software Foundation, Inc., 51 Franklin Street, Fifth
;; Floor, Boston, MA 02110-1301, USA.
;;; Commentary:
;;
;; Code action for lsp-bridge
;;
;;; Installation:
;;
;; Put lsp-bridge-code-action.el to your load-path.
;; The load-path is usually ~/elisp/.
;; It's set in your ~/.emacs like this:
;; (add-to-list 'load-path (expand-file-name "~/elisp"))
;;
;; And the following to your ~/.emacs startup file.
;;
;; (require 'lsp-bridge-code-action)
;;
;; No need more.
;;; Customize:
;;
;;
;;
;; All of the above can customize by:
;; M-x customize-group RET lsp-bridge-code-action RET
;;
;;; Change log:
;;
;; 2022/12/14
;; * First released.
;;
;;; Acknowledgements:
;;
;;
;;
;;; TODO
;;
;;
;;
;;; Require
(require 'acm)
(require 'acm-frame)
(require 'lsp-bridge-call-hierarchy)
;;; Code:
(defcustom lsp-bridge-code-action-preview-delay 0.5
"The delay to show code action diff preview.
Default is 0.5 second, preview window will stick if this value too small."
:type 'float
:group 'lsp-bridge)
(defcustom lsp-bridge-code-action-enable-popup-menu t
"Enable code action menu and changes preview popup,
Set it `nil' to improve performance."
:type 'boolean
:group 'lsp-bridge)
(defcustom lsp-bridge-code-action-command-handlers
'(java.apply.workspaceEdit lsp-bridge-jdtls-apply-workspace-edit
java.action.overrideMethodsPrompt lsp-bridge-jdtls-override-methods-prompt)
"LSP extra command handlers"
:type 'cons
:group 'lsp-bridge)
(defvar lsp-bridge-code-action--current-buffer nil)
(defvar lsp-bridge-code-action-popup-maybe-preview-timer nil)
(defvar lsp-bridge-code-action--oldfile nil)
(defvar lsp-bridge-code-action--preview-alist nil)
(defvar lsp-bridge-code-action--preview-index nil)
(defun lsp-bridge-code-action-get-range ()
(cond ((region-active-p)
(cons (region-beginning) (region-end)))
(t
(let ((overlay (lsp-bridge-diagnostic-overlay-at-point)))
(if overlay
(cons (overlay-start overlay) (overlay-end overlay))
(cons (point) (point)))))))
(defun lsp-bridge-code-action (&optional action-kind)
"Send code action to LSP server to fixes code or problems.
Default request all kind code-action.
You can send special kind of code-action, parameter `action-kind' can use one of below:
'quickfix'
'refactor'
'refactor.extract'
'refactor.inline'
'refactor.rewrite'
'source'
'source.organizeImports'
'source.fixAll'
Please read https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/#codeActionKind for detail explain.
"
(interactive)
(when (lsp-bridge-has-lsp-server-p)
(let ((range (lsp-bridge-code-action-get-range)))
(lsp-bridge-call-file-api "try_code_action"
(lsp-bridge--point-position (car range))
(lsp-bridge--point-position (cdr range))
action-kind)
)))
(defun lsp-bridge-code-action-popup-select ()
(interactive)
(lsp-bridge-code-action-popup-quit)
(lsp-bridge-code-action--fix-do
(cdr (nth lsp-bridge-call-hierarchy--index lsp-bridge-call-hierarchy--popup-response))))
(defun lsp-bridge-code-action-popup-quit ()
(interactive)
(acm-cancel-timer lsp-bridge-code-action-popup-maybe-preview-timer)
(acm-frame-delete-frame lsp-bridge-call-hierarchy--frame)
(kill-buffer "*lsp-bridge-code-action-menu*")
(advice-remove 'lsp-bridge-call-hierarchy-maybe-preview #'lsp-bridge-code-action-popup-maybe-preview)
(advice-remove 'lsp-bridge-call-hierarchy-select #'lsp-bridge-code-action-popup-select)
(advice-remove 'lsp-bridge-call-hierarchy-quit #'lsp-bridge-code-action-popup-quit)
(when (get-buffer-window lsp-bridge-code-action--current-buffer)
(select-window (get-buffer-window lsp-bridge-code-action--current-buffer))))
(defun lsp-bridge-code-action-popup-maybe-preview-show (&optional buffer-content)
(when-let* ((pos (frame-position lsp-bridge-call-hierarchy--frame))
(call-frame-height (frame-height lsp-bridge-call-hierarchy--frame))
(call-frame-width (frame-width lsp-bridge-call-hierarchy--frame))
(width 0) (height 0)
(menu-window-height
(min (length lsp-bridge-call-hierarchy--popup-response)
(/ (frame-height acm-frame--emacs-frame) 4))) ;; make sure menu-window not exceed 1/4 of frame height
(window-min-height 0) (window-min-width 0)
(live-p (frame-live-p lsp-bridge-call-hierarchy--frame)))
(with-current-buffer "*lsp-bridge-code-action-preview*"
(read-only-mode -1)
(cond
(buffer-content
(delete-region (point-min) (point-max))
(insert buffer-content))
(t
(goto-char (point-min))
;; Delete info lines.
(kill-line 3)
;; Delete finish.
(goto-char (point-min))
(when (search-forward "Diff finished." nil t)
(beginning-of-line)
(kill-line 1))
(push (cons lsp-bridge-code-action--preview-index (buffer-string))
lsp-bridge-code-action--preview-alist)))
;; Get width and height.
(goto-char (point-min))
(while (not (eobp))
(cl-incf height)
(setq width (max (- (line-end-position)
(line-beginning-position))
width))
(forward-line))
(setq width (min width 80))
;; Show top.
(goto-char (point-min))
;; Truncate lines.
(setq-local cursor-type nil)
(setq-local mode-line-format nil)
(setq-local truncate-lines t)
;; Render ANSI string.
(ansi-color-apply-on-region (point-min) (point-max)))
(acm-frame-set-frame-size lsp-bridge-call-hierarchy--frame
(max width call-frame-width)
(+ height menu-window-height))
(acm-frame-adjust-frame-pos lsp-bridge-call-hierarchy--frame nil nil t)
(select-frame-set-input-focus lsp-bridge-call-hierarchy--frame)
(let ((menu-window (get-buffer-window "*lsp-bridge-code-action-menu*"
lsp-bridge-call-hierarchy--frame))
(frame-window-list (window-list lsp-bridge-call-hierarchy--frame))
(nodiff (= height 0)))
(if nodiff
;; for code action based on execute_command, we are not support preview
(delete-other-windows menu-window)
;; split preview window
(when (eq (length frame-window-list) 1)
(split-window (selected-window) height 'below)
(setq frame-window-list (window-list lsp-bridge-call-hierarchy--frame)))
;; set preview buffer
(dolist (window frame-window-list)
(unless (eq window menu-window)
(set-window-buffer window "*lsp-bridge-code-action-preview*")))
(select-window menu-window)
(set-window-text-height menu-window menu-window-height)))))
(defun lsp-bridge-code-action-popup-maybe-preview-do ()
(let* ((buffer (buffer-name lsp-bridge-code-action--current-buffer))
(buffer-content (with-current-buffer buffer (buffer-string)))
(recentf-keep '(".*" . nil)) ;; not push temp file in recentf-list
(recentf-exclude '(".*"))
(action (cdr (nth lsp-bridge-call-hierarchy--index
lsp-bridge-call-hierarchy--popup-response)))
(cache-buffer-string (alist-get lsp-bridge-call-hierarchy--index
lsp-bridge-code-action--preview-alist)))
(setq lsp-bridge-code-action--preview-index lsp-bridge-call-hierarchy--index)
(if cache-buffer-string
;; use cache
(lsp-bridge-code-action-popup-maybe-preview-show cache-buffer-string)
;; diff
(with-temp-buffer
(insert buffer-content)
(lsp-bridge-code-action--fix-do action (current-buffer))
(diff-no-select lsp-bridge-code-action--current-buffer (current-buffer)
nil nil (get-buffer-create "*lsp-bridge-code-action-preview*")))
(if-let* ((proc (get-buffer-process "*lsp-bridge-code-action-preview*")))
(set-process-sentinel
proc (lambda (proc _msg)
(with-current-buffer (process-buffer proc)
(diff-sentinel (process-exit-status proc))
(lsp-bridge-code-action-popup-maybe-preview-show))))
(lsp-bridge-code-action-popup-maybe-preview-show)))))
(defun lsp-bridge-code-action-popup-maybe-preview ()
(acm-cancel-timer lsp-bridge-code-action-popup-maybe-preview-timer)
(setq lsp-bridge-code-action-popup-maybe-preview-timer
(run-with-idle-timer lsp-bridge-code-action-preview-delay nil #'lsp-bridge-code-action-popup-maybe-preview-do)))
(defun lsp-bridge-code-action-popup-menu (menu-items default-action)
(let ((recentf-keep '(".*" . nil)) ;; not push temp file in recentf-list
(recentf-exclude '(".*"))
(menu-length (length menu-items))
(menu-buffer (get-buffer-create "*lsp-bridge-code-action-menu*"))
(menu-width 0)
(menu-frame-exist (frame-live-p lsp-bridge-call-hierarchy--frame))
cursor)
;; Calcuate cursor position when menu frame is not visible.
(unless menu-frame-exist
(setq cursor (acm-frame-get-popup-position (point))))
;; Prepare for previewing.
(setq lsp-bridge-code-action--current-buffer (current-buffer))
(setq lsp-bridge-code-action--oldfile (make-temp-file
(buffer-name) nil nil (buffer-string)))
(setq lsp-bridge-code-action--preview-alist '())
;; Reuse hierarchy popup keymap and mode here.
(setq lsp-bridge-call-hierarchy--popup-response menu-items)
(acm-frame-create-frame-if-not-exist lsp-bridge-call-hierarchy--frame
menu-buffer "code action" 0 nil)
(with-current-buffer menu-buffer
;; Erase menu buffer for multiple code-action response from Python side.
(read-only-mode -1)
(erase-buffer)
(cl-loop for i from 0 to (1- (length menu-items))
do (let* ((title (car (nth i menu-items)))
(format-line (format "%d. %s\n" (1+ i) title))
(line-width (length format-line)))
(insert format-line)
(setq menu-width (max line-width menu-width))))
(lsp-bridge-call-hierarchy-mode)
(goto-char (point-min))
(setq-local cursor-type nil)
(setq-local truncate-lines t)
(setq-local mode-line-format nil))
;; Don't adjust frame position if code action menu current is visible.
(unless menu-frame-exist
(acm-frame-set-frame-position lsp-bridge-call-hierarchy--frame (car cursor) (+ (cdr cursor) (line-pixel-height)))
(acm-frame-set-frame-size lsp-bridge-call-hierarchy--frame menu-width
(min menu-length (/ (frame-height acm-frame--emacs-frame) 4))))
(advice-add 'lsp-bridge-call-hierarchy-maybe-preview :override #'lsp-bridge-code-action-popup-maybe-preview)
(advice-add 'lsp-bridge-call-hierarchy-select :override #'lsp-bridge-code-action-popup-select)
(advice-add 'lsp-bridge-call-hierarchy-quit :override #'lsp-bridge-code-action-popup-quit)
(lsp-bridge-code-action-popup-maybe-preview)))
(defun lsp-bridge-code-action--fix-do (action &optional temp-buffer)
(let* ((command (plist-get action :command))
(edit (plist-get action :edit))
(server_name (plist-get action :server-name))
(arguments (plist-get action :arguments))
(handler (when (stringp command)
(plist-get lsp-bridge-code-action-command-handlers (intern command)))))
(cond
;; Apply workspace if found `edit' argument.
(edit
(lsp-bridge-workspace-apply-edit edit temp-buffer))
;; Prioritize custom handlers.
(handler
(funcall handler action temp-buffer))
;; Arguments are for `workspaceApplyEdit'.
((and arguments
(cl-every (lambda (x) (or (plist-get x :documentChanges) (plist-get x :changes))) arguments))
(dolist (argument arguments)
(lsp-bridge-workspace-apply-edit argument temp-buffer)))
;; Command is string, send `workspace/executeCommand' request to LSP server. arguments are cached in Python side.
((stringp command)
;; Execute command with temp-buffer.
(lsp-bridge-call-file-api "execute_command" server_name command arguments))
;; Parse command argument if command is plist, and call `lsp-bridge-code-action--fix-do' again.
((lsp-bridge-plistp command)
;; We need add server-name in `next-action', otherwise `lsp-bridge-code-action--fix-do' will send empty server name to Python.
(let ((next-action command))
(plist-put next-action :server-name server_name)
(lsp-bridge-code-action--fix-do next-action temp-buffer))))
(unless temp-buffer
(message "[LSP-BRIDGE] Execute code action '%s'" (plist-get action :title)))
;; Hide diagnostics after code action.
(lsp-bridge-diagnostic-hide-overlays)))
(defun lsp-bridge-plistp (object)
"Non-nil if and only if OBJECT is a valid plist."
(declare (pure t) (side-effect-free error-free))
(let ((len (proper-list-p object)))
(and len (zerop (% len 2)))))
(defun lsp-bridge-code-action--filter (action action-kind)
(when (or (not action-kind)
(equal action-kind (plist-get action :kind)))
(cons (plist-get action :title) action)))
(defun lsp-bridge-code-action--fix (actions action-kind)
(let* ((menu-items
(or
(cl-remove-if #'null
(mapcar #'(lambda (action)
(lsp-bridge-code-action--filter action action-kind))
actions))
(apply #'error
(if action-kind
(format "No '%s' code action here" action-kind)
"No code actions here"))))
(preferred-action (cl-find-if
(lambda (menu-item)
(plist-get (cdr menu-item) :isPreferred))
menu-items))
(default-action (car (or preferred-action (car menu-items))))
(action (if (and action-kind (null (cadr menu-items)))
(cdr (car menu-items)) nil)))
(cond
;; Fix action if only one match.
(action
(lsp-bridge-code-action--fix-do action))
;; Popup menu if `lsp-bridge-code-action-enable-popup-menu' option is non-nil.
((and lsp-bridge-code-action-enable-popup-menu
(acm-frame-can-display-p))
(lsp-bridge-code-action-popup-menu menu-items default-action))
(t
;; Choose action from minibuffer.
(let ((select-name
(completing-read
(format "[LSP-BRIDGE] Pick an action (default: %s): " default-action)
menu-items nil t nil nil default-action)))
(lsp-bridge-code-action--fix-do (cdr (assoc select-name menu-items))))))))
(provide 'lsp-bridge-code-action)
;;; lsp-bridge-code-action.el ends here