forked from emacs-lsp/lsp-mode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lsp-lens.el
455 lines (405 loc) · 18.7 KB
/
lsp-lens.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
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
;;; lsp-lens.el --- LSP lens -*- lexical-binding: t; -*-
;;
;; Copyright (C) 2020 emacs-lsp maintainers
;;
;; 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 <https://www.gnu.org/licenses/>.
;;
;;; Commentary:
;;
;; LSP lens
;;
;;; Code:
(require 'lsp-mode)
(defgroup lsp-lens nil
"LSP support for lens"
:prefix "lsp-lens-"
:group 'lsp-mode
:tag "LSP Lens")
(defcustom lsp-lens-debounce-interval 0.001
"Debounce interval for loading lenses."
:group 'lsp-lens
:type 'number)
(defcustom lsp-lens-place-position 'end-of-line
"The position to place lens relative to returned lens position."
:group 'lsp-lens
:type '(choice (const above-line)
(const end-of-line))
:package-version '(lsp-mode . "8.0.0"))
(defface lsp-lens-mouse-face
'((t :height 0.8 :inherit link))
"The face used for code lens overlays."
:group 'lsp-lens)
(defface lsp-lens-face
'((t :inherit lsp-details-face))
"The face used for code lens overlays."
:group 'lsp-lens)
(defvar-local lsp-lens--modified? nil)
(defvar-local lsp-lens--overlays nil
"Current lenses.")
(defvar-local lsp-lens--page nil
"Pair of points which holds the last window location the lenses were loaded.")
(defvar-local lsp-lens--last-count nil
"The number of lenses the last time they were rendered.")
(defvar lsp-lens-backends '(lsp-lens--backend)
"Backends providing lenses.")
(defvar-local lsp-lens--refresh-timer nil
"Refresh timer for the lenses.")
(defvar-local lsp-lens--data nil
"Pair of points which holds the last window location the lenses were loaded.")
(defvar-local lsp-lens--backend-cache nil)
(defun lsp-lens--text-width (from to)
"Measure the width of the text between FROM and TO.
Results are meaningful only if FROM and TO are on the same line."
;; `current-column' takes prettification into account
(- (save-excursion (goto-char to) (current-column))
(save-excursion (goto-char from) (current-column))))
(defun lsp-lens--update (ov)
"Redraw quick-peek overlay OV."
(let* ((offset (lsp-lens--text-width (save-excursion
(beginning-of-visual-line)
(point))
(save-excursion
(beginning-of-line-text)
(point))))
(str (if (eq 'end-of-line lsp-lens-place-position)
(overlay-get ov 'lsp--lens-contents)
(concat (make-string offset ?\s)
(overlay-get ov 'lsp--lens-contents)))))
(save-excursion
(goto-char (overlay-start ov))
(if (eq 'end-of-line lsp-lens-place-position)
(overlay-put ov 'after-string (propertize (concat " " str) 'cursor t))
(overlay-put ov 'before-string (concat str "\n")))
(overlay-put ov 'lsp-original str))))
(defun lsp-lens--overlay-ensure-at (pos)
"Find or create a lens for the line at POS."
(-doto (save-excursion
(goto-char pos)
(if (eq 'end-of-line lsp-lens-place-position)
(make-overlay (point-at-eol) -1 nil t t)
(make-overlay (point-at-bol) (1+ (point-at-eol)) nil t t)))
(overlay-put 'lsp-lens t)
(overlay-put 'lsp-lens-position pos)))
(defun lsp-lens--show (str pos metadata)
"Show STR in an inline window at POS including METADATA."
(let ((ov (lsp-lens--overlay-ensure-at pos)))
(save-excursion
(goto-char pos)
(setf (overlay-get ov 'lsp--lens-contents) str)
(setf (overlay-get ov 'lsp--metadata) metadata)
(lsp-lens--update ov)
ov)))
(defun lsp-lens--idle-function (&optional buffer)
"Create idle function for buffer BUFFER."
(when (and (or (not buffer) (eq (current-buffer) buffer))
(not (equal (cons (window-start) (window-end)) lsp-lens--page)))
(lsp-lens--schedule-refresh nil)))
(defun lsp-lens--overlay-matches-pos (ov pos)
"Check if OV is a lens covering POS."
(and (overlay-get ov 'lsp-lens)
(overlay-start ov)
(<= (overlay-start ov) pos)
(< pos (overlay-end ov))))
(defun lsp-lens--after-save ()
"Handler for `after-save-hook' for lens mode."
(lsp-lens--schedule-refresh t))
(defun lsp-lens--schedule-refresh (&optional buffer-modified?)
"Call each of the backend.
BUFFER-MODIFIED? determines whether the buffer was modified or
not."
(-some-> lsp-lens--refresh-timer cancel-timer)
(setq lsp-lens--page (cons (window-start) (window-end)))
(setq lsp-lens--refresh-timer
(run-with-timer lsp-lens-debounce-interval
nil
#'lsp-lens-refresh
(or lsp-lens--modified? buffer-modified?)
(current-buffer))))
(defun lsp-lens--schedule-refresh-modified ()
"Schedule a lens refresh due to a buffer-modification.
See `lsp-lens--schedule-refresh' for details."
(lsp-lens--schedule-refresh t))
(defun lsp-lens--keymap (command)
"Build the lens keymap for COMMAND."
(-doto (make-sparse-keymap)
(define-key [mouse-1] (lsp-lens--create-interactive-command command))))
(defun lsp-lens--create-interactive-command (command?)
"Create an interactive COMMAND? for the lens.
COMMAND? shall be an `&Command' (e.g. `&CodeLens' :command?) and
mustn't be nil."
(if (functionp (lsp:command-command command?))
(lsp:command-command command?)
(lambda ()
(interactive)
(lsp--execute-command command?))))
(defun lsp-lens--display (lenses)
"Show LENSES."
;; rerender only if there are lenses which are not processed or if their count
;; has changed(e. g. delete lens should trigger redisplay).
(let ((scroll-preserve-screen-position t))
(setq lsp-lens--modified? nil)
(when (or (-any? (-lambda ((&CodeLens :_processed processed))
(not processed))
lenses)
(eq (length lenses) lsp-lens--last-count)
(not lenses))
(setq lsp-lens--last-count (length lenses))
(mapc #'delete-overlay lsp-lens--overlays)
(setq lsp-lens--overlays
(->> lenses
(-filter #'lsp:code-lens-command?)
(--map (prog1 it (lsp-put it :_processed t)))
(-group-by (-compose #'lsp:position-line #'lsp:range-start #'lsp:code-lens-range))
(-map
(-lambda ((_ . lenses))
(let* ((sorted (-sort (-on #'< (-compose #'lsp:position-character
#'lsp:range-start
#'lsp:code-lens-range))
lenses))
(data (-map
(-lambda ((lens &as &CodeLens
:command? (command &as
&Command :title :_face face)))
(propertize
title
'face (or face 'lsp-lens-face)
'action (lsp-lens--create-interactive-command command)
'pointer 'hand
'mouse-face 'lsp-lens-mouse-face
'local-map (lsp-lens--keymap command)))
sorted)))
(lsp-lens--show
(s-join (propertize "|" 'face 'lsp-lens-face) data)
(-> sorted cl-first lsp:code-lens-range lsp:range-start lsp--position-to-point)
data)))))))))
(defun lsp-lens-refresh (buffer-modified? &optional buffer)
"Refresh lenses using lenses backend.
BUFFER-MODIFIED? determines whether the BUFFER is modified or not."
(let ((buffer (or buffer (current-buffer))))
(when (buffer-live-p buffer)
(with-current-buffer buffer
(dolist (backend lsp-lens-backends)
(funcall backend buffer-modified?
(lambda (lenses version)
(when (buffer-live-p buffer)
(with-current-buffer buffer
(lsp-lens--process backend lenses version))))))))))
(defun lsp-lens--process (backend lenses version)
"Process LENSES originated from BACKEND.
VERSION is the version of the file. The lenses has to be
refreshed only when all backends have reported for the same
version."
(setq lsp-lens--data (or lsp-lens--data (make-hash-table)))
(puthash backend (cons version (append lenses nil)) lsp-lens--data)
(-let [backend-data (->> lsp-lens--data ht-values (-filter #'cl-rest))]
(when (and
(= (length lsp-lens-backends) (ht-size lsp-lens--data))
(seq-every-p (-lambda ((version))
(or (not version) (eq version lsp--cur-version)))
backend-data))
;; display the data only when the backends have reported data for the
;; current version of the file
(lsp-lens--display (apply #'append (-map #'cl-rest backend-data)))))
version)
(lsp-defun lsp--lens-backend-not-loaded? ((&CodeLens :range
(&Range :start)
:command?
:_pending pending))
"Return t if LENS has to be loaded."
(let ((window (get-buffer-window (current-buffer))))
;; (window-start/end) does not consider current window buffer if not passed manually
(and (< (window-start window) (lsp--position-to-point start) (window-end window))
(not command?)
(not pending))))
(lsp-defun lsp--lens-backend-present? ((&CodeLens :range (&Range :start) :command?))
"Return t if LENS has to be loaded."
(or command?
(not (< (window-start) (lsp--position-to-point start) (window-end)))))
(defun lsp-lens--backend-fetch-missing (lenses callback file-version)
"Fetch LENSES without command in for the current window.
TICK is the buffer modified tick. If it does not match
`buffer-modified-tick' at the time of receiving the updates the
updates must be discarded..
CALLBACK - the callback for the lenses.
FILE-VERSION - the version of the file."
(seq-each
(lambda (it)
(with-lsp-workspace (lsp-get it :_workspace)
(lsp-put it :_pending t)
(lsp-put it :_workspace nil)
(lsp-request-async "codeLens/resolve" it
(-lambda ((&CodeLens :command?))
(lsp-put it :_pending nil)
(lsp-put it :command command?)
(when (seq-every-p #'lsp--lens-backend-present? lenses)
(funcall callback lenses file-version)))
:mode 'tick)))
(seq-filter #'lsp--lens-backend-not-loaded? lenses)))
(defun lsp-lens--backend (modified? callback)
"Lenses backend using `textDocument/codeLens'.
MODIFIED? - t when buffer is modified since the last invocation.
CALLBACK - callback for the lenses."
(when (lsp--find-workspaces-for "textDocument/codeLens")
(if modified?
(progn
(setq lsp-lens--backend-cache nil)
(lsp-request-async "textDocument/codeLens"
`(:textDocument (:uri ,(lsp--buffer-uri)))
(lambda (lenses)
(setq lsp-lens--backend-cache
(seq-mapcat
(-lambda ((workspace . workspace-lenses))
;; preserve the original workspace so we can later use it to resolve the lens
(seq-do (-rpartial #'lsp-put :_workspace workspace) workspace-lenses)
workspace-lenses)
lenses))
(if (-every? #'lsp:code-lens-command? lsp-lens--backend-cache)
(funcall callback lsp-lens--backend-cache lsp--cur-version)
(lsp-lens--backend-fetch-missing lsp-lens--backend-cache callback lsp--cur-version)))
:error-handler #'ignore
:mode 'tick
:no-merge t
:cancel-token (concat (buffer-name (current-buffer)) "-lenses")))
(if (-all? #'lsp--lens-backend-present? lsp-lens--backend-cache)
(funcall callback lsp-lens--backend-cache lsp--cur-version)
(lsp-lens--backend-fetch-missing lsp-lens--backend-cache callback lsp--cur-version)))))
(defun lsp-lens--refresh-buffer ()
"Trigger lens refresh on buffer."
(remove-hook 'lsp-on-idle-hook #'lsp-lens--refresh-buffer t)
(when (bound-and-true-p lsp-lens-mode)
(lsp-lens-refresh t)))
(defun lsp--lens-on-refresh (workspace)
"Clear lens within all buffers of WORKSPACE, refreshing all workspace buffers."
(cl-assert (not (eq nil workspace)))
(->> (lsp--workspace-buffers workspace)
(mapc (lambda (buffer)
(lsp-with-current-buffer buffer
(if (lsp--buffer-visible-p)
(when (bound-and-true-p lsp-lens-mode)
(lsp-lens-refresh t))
(progn
(add-hook 'lsp-on-idle-hook #'lsp-lens--refresh-buffer nil t)
(lsp--idle-reschedule (current-buffer)))))))))
;;;###autoload
(defun lsp-lens--enable ()
"Enable lens mode."
(when (and lsp-lens-enable
(lsp-feature? "textDocument/codeLens"))
(lsp-lens-mode 1)))
(defun lsp-lens--disable ()
"Disable lens mode."
(lsp-lens-mode -1))
;;;###autoload
(defun lsp-lens-show ()
"Display lenses in the buffer."
(interactive)
(->> (lsp-request "textDocument/codeLens"
`(:textDocument (:uri
,(lsp--path-to-uri buffer-file-name))))
(seq-map (-lambda ((lens &as &CodeAction :command?))
(if command?
lens
(lsp-request "codeLens/resolve" lens))))
lsp-lens--display))
;;;###autoload
(defun lsp-lens-hide ()
"Delete all lenses."
(interactive)
(let ((scroll-preserve-screen-position t))
(seq-do #'delete-overlay lsp-lens--overlays)
(setq lsp-lens--overlays nil)))
;;;###autoload
(define-minor-mode lsp-lens-mode
"Toggle code-lens overlays."
:group 'lsp-lens
:global nil
:init-value nil
:lighter " Lens"
(cond
(lsp-lens-mode
(add-hook 'lsp-unconfigure-hook #'lsp-lens--disable nil t)
(add-hook 'lsp-configure-hook #'lsp-lens--enable nil t)
(add-hook 'lsp-on-idle-hook #'lsp-lens--idle-function nil t)
(add-hook 'lsp-on-change-hook #'lsp-lens--schedule-refresh-modified nil t)
(add-hook 'after-save-hook #'lsp-lens--after-save nil t)
(add-hook 'before-revert-hook #'lsp-lens-hide nil t)
(lsp-lens-refresh t))
(t
(remove-hook 'lsp-on-idle-hook #'lsp-lens--idle-function t)
(remove-hook 'lsp-on-change-hook #'lsp-lens--schedule-refresh-modified t)
(remove-hook 'after-save-hook #'lsp-lens--after-save t)
(remove-hook 'before-revert-hook #'lsp-lens-hide t)
(when lsp-lens--refresh-timer
(cancel-timer lsp-lens--refresh-timer))
(setq lsp-lens--refresh-timer nil)
(lsp-lens-hide)
(setq lsp-lens--last-count nil)
(setq lsp-lens--backend-cache nil)
(remove-hook 'lsp-configure-hook #'lsp-lens--enable t)
(remove-hook 'lsp-unconfigure-hook #'lsp-lens--disable t))))
;; avy integration
(declare-function avy-process "ext:avy" (candidates &optional overlay-fn cleanup-fn))
(declare-function avy--key-to-char "ext:avy" (c))
(defvar avy-action)
(defvar avy-style)
;;;###autoload
(defun lsp-avy-lens ()
"Click lsp lens using `avy' package."
(interactive)
(unless lsp-lens--overlays
(user-error "No lenses in current buffer"))
(let* ((avy-action 'identity)
(avy-style 'lsp-avy-lens)
(position (if (eq lsp-lens-place-position 'end-of-line)
'after-string
'before-string))
(action (cl-third
(avy-process
(-mapcat
(lambda (overlay)
(-map-indexed
(lambda (index lens-token)
(list overlay index
(get-text-property 0 'action lens-token)))
(overlay-get overlay 'lsp--metadata)))
lsp-lens--overlays)
(-lambda (path ((ov index) . _win))
(let* ((path (mapcar #'avy--key-to-char path))
(str (propertize (string (car (last path)))
'face 'avy-lead-face))
(old-str (overlay-get ov position))
(old-str-tokens (s-split "|" old-str))
(old-token (seq-elt old-str-tokens index))
(tokens `(,@(-take index old-str-tokens)
,(-if-let ((_ prefix suffix)
(s-match "\\(^[[:space:]]+\\)\\(.*\\)" old-token))
(concat prefix str suffix)
(concat str old-token))
,@(-drop (1+ index) old-str-tokens)))
(new-str (s-join (propertize "|" 'face 'lsp-lens-face) tokens))
(new-str (if (or (s-ends-with? "\n" new-str)
(eq lsp-lens-place-position 'end-of-line))
new-str
(concat new-str "\n"))))
(overlay-put ov position new-str)))
(lambda ()
(--map
(let ((original (overlay-get it 'lsp-original)))
(overlay-put it position
(if (eq lsp-lens-place-position 'end-of-line)
(concat " " original)
original)))
lsp-lens--overlays))))))
(when action (funcall-interactively action))))
(lsp-consistency-check lsp-lens)
(provide 'lsp-lens)
;;; lsp-lens.el ends here