Skip to content

Commit 4af8289

Browse files
committed
[lsp-ui-sideline] Display a lightbulb icon with actions
1 parent c3d816d commit 4af8289

File tree

3 files changed

+31
-3
lines changed

3 files changed

+31
-3
lines changed

lsp-ui-sideline.el

+24-3
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,8 @@ It is used to know when the window has changed of width.")
163163
This face have a low priority over the others."
164164
:group 'lsp-ui-sideline)
165165

166+
(defvar lsp-ui-resources-dir)
167+
166168
(defun lsp-ui-sideline--first-line-p (pos)
167169
"Return non-nil if POS is on the first line."
168170
(save-excursion
@@ -402,6 +404,22 @@ Push sideline overlays on `lsp-ui-sideline--ovs'."
402404
(user-error "No code actions on the current line"))
403405
(lsp-execute-code-action (lsp--select-action lsp-ui-sideline--code-actions)))
404406

407+
(defun lsp-ui-sideline--scale-lightbulb nil
408+
(--> (frame-char-height)
409+
;; 128 is the height in pixel of lightbulb.png
410+
(/ (float it) 128)))
411+
412+
(defvar lsp-ui-sideline--image-lightbulb
413+
(and lsp-ui-resources-dir
414+
(image-type-available-p 'png)
415+
`(image :type png :file ,(expand-file-name "lightbulb.png" lsp-ui-resources-dir) :ascent center)))
416+
417+
(defun lsp-ui-sideline--code-actions-image nil
418+
(when lsp-ui-sideline--image-lightbulb
419+
(concat
420+
(propertize " " 'display (append lsp-ui-sideline--image-lightbulb `(:scale ,(lsp-ui-sideline--scale-lightbulb))))
421+
(propertize " " 'display '(space :width 0.3)))))
422+
405423
(defun lsp-ui-sideline--code-actions (actions bol eol)
406424
"Show code ACTIONS."
407425
(when lsp-ui-sideline-actions-kind-regex
@@ -418,7 +436,9 @@ Push sideline overlays on `lsp-ui-sideline--ovs'."
418436
(seq-doseq (action actions)
419437
(-let* ((title (->> (lsp:code-action-title action)
420438
(replace-regexp-in-string "[\n\t ]+" " ")
421-
(concat lsp-ui-sideline-code-actions-prefix)))
439+
(concat (unless lsp-ui-sideline--image-lightbulb
440+
lsp-ui-sideline-code-actions-prefix))))
441+
(image (lsp-ui-sideline--code-actions-image))
422442
(margin (lsp-ui-sideline--margin-width))
423443
(keymap (let ((map (make-sparse-keymap)))
424444
(define-key map [down-mouse-1] (lambda () (interactive)
@@ -430,9 +450,10 @@ Push sideline overlays on `lsp-ui-sideline--ovs'."
430450
(add-face-text-property 0 len 'lsp-ui-sideline-code-action nil title)
431451
(add-text-properties 0 len `(keymap ,keymap mouse-face highlight) title)
432452
title))
433-
(string (concat (propertize " " 'display `(space :align-to (- right-fringe ,(lsp-ui-sideline--align len margin))))
453+
(string (concat (propertize " " 'display `(space :align-to (- right-fringe ,(lsp-ui-sideline--align (+ len (length image)) margin))))
454+
image
434455
(propertize title 'display (lsp-ui-sideline--compute-height))))
435-
(pos-ov (lsp-ui-sideline--find-line (1+ (length title)) bol eol t))
456+
(pos-ov (lsp-ui-sideline--find-line (+ 1 (length title) (length image)) bol eol t))
436457
(ov (and pos-ov (make-overlay (car pos-ov) (car pos-ov)))))
437458
(when pos-ov
438459
(overlay-put ov 'after-string string)

lsp-ui.el

+7
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,13 @@
3636
(require 'dash)
3737
(require 'lsp-protocol)
3838

39+
(defconst lsp-ui-resources-dir
40+
(--> (find-library-name "lsp-ui")
41+
(file-name-directory it)
42+
(expand-file-name "resources" it)
43+
(file-name-as-directory it)
44+
(and (file-exists-p it) it)))
45+
3946
(require 'lsp-ui-sideline)
4047
(require 'lsp-ui-peek)
4148
(require 'lsp-ui-imenu)

resources/lightbulb.png

2.01 KB
Loading

0 commit comments

Comments
 (0)