Skip to content

Commit de4796c

Browse files
authored
Organize, clean up, and improve performance (#674)
* Align doc constant * Remove duplicate peek function * Move utility module to just lsp-ui * Place in line * Try place on top * Try remove delete overlay function completely * Apply no redisplay * Some cleanup * Foward declare macro
1 parent 39043fe commit de4796c

7 files changed

+295
-341
lines changed

lsp-ui-doc.el

+93-104
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,6 @@
3030

3131
;;; Code:
3232

33-
(require 'lsp-ui-util)
34-
3533
(require 'lsp-protocol)
3634
(require 'lsp-mode)
3735
(require 'dash)
@@ -54,6 +52,9 @@
5452
(declare-function xwidget-webkit-execute-script-rv "ext:xwidget" (xwidget script &optional default))
5553
(declare-function xwidget-resize "ext:xwidget" (xwidget new-width new-height))
5654

55+
(declare-function lsp-ui--with-no-redisplay 'lsp-ui)
56+
(declare-function lsp-ui--safe-kill-timer 'lsp-ui)
57+
5758
(defgroup lsp-ui-doc nil
5859
"Display informations of the current line."
5960
:group 'tools
@@ -180,29 +181,29 @@ Only the `background' is used in this face."
180181
:group 'lsp-ui-doc)
181182

182183
(defvar lsp-ui-doc-frame-parameters
183-
'((left . -1)
184-
(no-focus-on-map . t)
185-
(min-width . 0)
186-
(width . 0)
187-
(min-height . 0)
188-
(height . 0)
189-
(internal-border-width . 1)
190-
(vertical-scroll-bars . nil)
191-
(horizontal-scroll-bars . nil)
192-
(right-fringe . 0)
193-
(menu-bar-lines . 0)
194-
(tool-bar-lines . 0)
195-
(line-spacing . 0)
196-
(unsplittable . t)
197-
(undecorated . t)
198-
(top . -1)
199-
(visibility . nil)
200-
(mouse-wheel-frame . nil)
201-
(no-other-frame . t)
184+
'((left . -1)
185+
(no-focus-on-map . t)
186+
(min-width . 0)
187+
(width . 0)
188+
(min-height . 0)
189+
(height . 0)
190+
(internal-border-width . 1)
191+
(vertical-scroll-bars . nil)
192+
(horizontal-scroll-bars . nil)
193+
(right-fringe . 0)
194+
(menu-bar-lines . 0)
195+
(tool-bar-lines . 0)
196+
(line-spacing . 0)
197+
(unsplittable . t)
198+
(undecorated . t)
199+
(top . -1)
200+
(visibility . nil)
201+
(mouse-wheel-frame . nil)
202+
(no-other-frame . t)
202203
(inhibit-double-buffering . t)
203-
(drag-internal-border . t)
204-
(no-special-glyphs . t)
205-
(desktop-dont-save . t))
204+
(drag-internal-border . t)
205+
(no-special-glyphs . t)
206+
(desktop-dont-save . t))
206207
"Frame parameters used to create the frame.")
207208

208209
(defvar lsp-ui-doc-render-function nil
@@ -246,19 +247,14 @@ Because some variables are buffer local.")
246247
(defmacro lsp-ui-doc--with-buffer (&rest body)
247248
"Execute BODY in the lsp-ui-doc buffer."
248249
(declare (indent 0) (debug t))
249-
`(let ((parent-vars (list :buffer (current-buffer)
250-
:window (get-buffer-window)))
251-
(buffer-list-update-hook nil))
252-
(with-current-buffer (get-buffer-create (lsp-ui-doc--make-buffer-name))
253-
(setq lsp-ui-doc--parent-vars parent-vars)
254-
(prog1 (let ((buffer-read-only nil)
255-
(inhibit-modification-hooks t)
256-
(inhibit-point-motion-hooks t)
257-
(inhibit-redisplay t))
258-
,@body)
259-
(setq buffer-read-only t)
260-
(let ((text-scale-mode-step 1.1))
261-
(text-scale-set lsp-ui-doc-text-scale-level))))))
250+
`(lsp-ui--with-no-redisplay
251+
(let ((parent-vars (list :buffer (current-buffer) :window (get-buffer-window))))
252+
(with-current-buffer (get-buffer-create (lsp-ui-doc--make-buffer-name))
253+
(setq lsp-ui-doc--parent-vars parent-vars)
254+
(prog1 (let (buffer-read-only) ,@body)
255+
(setq buffer-read-only t)
256+
(let ((text-scale-mode-step 1.1))
257+
(text-scale-set lsp-ui-doc-text-scale-level)))))))
262258

263259
(defmacro lsp-ui-doc--get-parent (var)
264260
"Return VAR in `lsp-ui-doc--parent-vars'."
@@ -312,7 +308,7 @@ Because some variables are buffer local.")
312308
(or (lsp:marked-string-language marked-string)
313309
(lsp:markup-content-kind marked-string)))
314310
language))
315-
(markdown-hr-display-char nil))
311+
markdown-hr-display-char)
316312
(cond
317313
(lsp-ui-doc-use-webkit
318314
(if (and language
@@ -402,8 +398,7 @@ We don't extract the string that `lps-line' is already displaying."
402398

403399
(defun lsp-ui-doc--webkit-get-xwidget ()
404400
"Return Xwidget instance."
405-
(lsp-ui-doc--with-buffer
406-
(xwidget-at 1)))
401+
(lsp-ui-doc--with-buffer (xwidget-at 1)))
407402

408403
(defun lsp-ui-doc--webkit-execute-script (script &optional fn)
409404
"Execute SCRIPT in embedded Xwidget and run optional callback FN."
@@ -419,11 +414,10 @@ We don't extract the string that `lps-line' is already displaying."
419414
"Hide the frame."
420415
(setq lsp-ui-doc--bounds nil
421416
lsp-ui-doc--from-mouse nil)
422-
(lsp-ui-util-safe-delete-overlay lsp-ui-doc--inline-ov)
423-
(lsp-ui-util-safe-delete-overlay lsp-ui-doc--highlight-ov)
417+
(when (overlayp lsp-ui-doc--inline-ov) (delete-overlay lsp-ui-doc--inline-ov))
418+
(when (overlayp lsp-ui-doc--highlight-ov) (delete-overlay lsp-ui-doc--highlight-ov))
424419
(when-let ((frame (lsp-ui-doc--get-frame)))
425-
(when (frame-visible-p frame)
426-
(make-frame-invisible frame))))
420+
(when (frame-visible-p frame) (make-frame-invisible frame))))
427421

428422
(defun lsp-ui-doc--buffer-width ()
429423
"Calcul the max width of the buffer."
@@ -512,59 +506,57 @@ FRAME just below the symbol at point."
512506

513507
(defun lsp-ui-doc--move-frame (frame)
514508
"Place our FRAME on screen."
515-
(-let* (((left top right _bottom) (window-edges nil t nil t))
516-
(window (frame-root-window frame))
517-
(char-h (frame-char-height frame))
518-
(char-w (frame-char-width frame))
519-
((width . height) (window-text-pixel-size window nil nil 10000 10000 t))
520-
(width (+ width (* char-w 1))) ;; margins
521-
(height (min (- (* lsp-ui-doc-max-height char-h) (/ char-h 2)) height))
522-
(width (min width (* lsp-ui-doc-max-width char-w)))
523-
(frame-right (pcase lsp-ui-doc-alignment
524-
('frame (frame-pixel-width))
525-
('window right)))
526-
((left . top) (if (eq lsp-ui-doc-position 'at-point)
527-
(lsp-ui-doc--mv-at-point width height left top)
528-
(cons (max (- frame-right width char-w) 10)
529-
(pcase lsp-ui-doc-position
530-
('top (+ top char-w))
531-
('bottom (- (lsp-ui-doc--line-height 'mode-line)
532-
height
533-
10))))))
534-
(frame-resize-pixelwise t)
535-
(move-frame-functions nil)
536-
(window-size-change-functions nil)
537-
(window-state-change-hook nil)
538-
(window-state-change-functions nil)
539-
(window-configuration-change-hook nil)
540-
(inhibit-redisplay t))
541-
;; Dirty way to fix unused variable in emacs 26
542-
(and window-state-change-functions
543-
window-state-change-hook)
544-
;; Make frame invisible before moving/resizing it to avoid flickering:
545-
;; We set the position and size in 1 call, modify-frame-parameters, but
546-
;; internally emacs makes 2 different calls, which can be visible
547-
;; to the user
548-
(and (frame-visible-p frame)
549-
(lsp-ui-doc--size-and-pos-changed frame left top width height)
550-
(make-frame-invisible frame))
551-
(modify-frame-parameters
552-
frame
553-
`((width . (text-pixels . ,width))
554-
(height . (text-pixels . ,height))
555-
(user-size . t)
556-
(left . (+ ,left))
557-
(top . (+ ,top))
558-
(user-position . t)
559-
(lsp-ui-doc--window-origin . ,(selected-window))
560-
(lsp-ui-doc--buffer-origin . ,(current-buffer))
561-
(lsp-ui-doc--no-focus . t)
562-
(right-fringe . 0)
563-
(left-fringe . 0)))
564-
;; Insert hr lines after width is computed
565-
(lsp-ui-doc--fix-hr-props)
566-
(unless (frame-visible-p frame)
567-
(make-frame-visible frame))))
509+
(lsp-ui--with-no-redisplay
510+
(-let* (((left top right _bottom) (window-edges nil t nil t))
511+
(window (frame-root-window frame))
512+
(char-h (frame-char-height frame))
513+
(char-w (frame-char-width frame))
514+
((width . height) (window-text-pixel-size window nil nil 10000 10000 t))
515+
(width (+ width (* char-w 1))) ;; margins
516+
(height (min (- (* lsp-ui-doc-max-height char-h) (/ char-h 2)) height))
517+
(width (min width (* lsp-ui-doc-max-width char-w)))
518+
(frame-right (pcase lsp-ui-doc-alignment
519+
('frame (frame-pixel-width))
520+
('window right)))
521+
((left . top) (if (eq lsp-ui-doc-position 'at-point)
522+
(lsp-ui-doc--mv-at-point width height left top)
523+
(cons (max (- frame-right width char-w) 10)
524+
(pcase lsp-ui-doc-position
525+
('top (+ top char-w))
526+
('bottom (- (lsp-ui-doc--line-height 'mode-line)
527+
height
528+
10))))))
529+
(frame-resize-pixelwise t)
530+
move-frame-functions
531+
window-size-change-functions
532+
window-state-change-hook
533+
window-state-change-functions)
534+
;; Dirty way to fix unused variable in emacs 26
535+
(and window-state-change-functions
536+
window-state-change-hook)
537+
;; Make frame invisible before moving/resizing it to avoid flickering:
538+
;; We set the position and size in 1 call, modify-frame-parameters, but
539+
;; internally emacs makes 2 different calls, which can be visible
540+
;; to the user
541+
(and (frame-visible-p frame)
542+
(lsp-ui-doc--size-and-pos-changed frame left top width height)
543+
(make-frame-invisible frame))
544+
(modify-frame-parameters
545+
frame
546+
`((width . (text-pixels . ,width))
547+
(height . (text-pixels . ,height))
548+
(user-size . t)
549+
(left . (+ ,left))
550+
(top . (+ ,top))
551+
(user-position . t)
552+
(lsp-ui-doc--window-origin . ,(selected-window))
553+
(lsp-ui-doc--buffer-origin . ,(current-buffer))
554+
(lsp-ui-doc--no-focus . t)
555+
(right-fringe . 0)
556+
(left-fringe . 0)))
557+
;; Insert hr lines after width is computed
558+
(lsp-ui-doc--fix-hr-props)
559+
(unless (frame-visible-p frame) (make-frame-visible frame)))))
568560

569561
(defun lsp-ui-doc--visit-file (filename)
570562
"Visit FILENAME in the parent frame."
@@ -913,7 +905,7 @@ HEIGHT is the documentation number of lines."
913905
(and (looking-at "[[:graph:]]") (cons (point) (1+ (point))))))
914906
(unless (equal lsp-ui-doc--bounds bounds)
915907
(lsp-ui-doc--hide-frame)
916-
(lsp-ui-util-safe-kill-timer lsp-ui-doc--timer)
908+
(lsp-ui--safe-kill-timer lsp-ui-doc--timer)
917909
(setq lsp-ui-doc--timer
918910
(run-with-idle-timer
919911
lsp-ui-doc-delay nil
@@ -1193,8 +1185,7 @@ It is supposed to be called from `lsp-ui--toggle'"
11931185
(visible (lsp-ui-doc--frame-visible-p)))
11941186
(set-frame-parameter frame 'lsp-ui-doc--no-focus nil)
11951187
(set-frame-parameter frame 'cursor-type t)
1196-
(lsp-ui-doc--with-buffer
1197-
(setq cursor-type 'box))
1188+
(lsp-ui-doc--with-buffer (setq cursor-type 'box))
11981189
(select-frame-set-input-focus frame)))
11991190

12001191
(defun lsp-ui-doc-unfocus-frame ()
@@ -1204,10 +1195,8 @@ It is supposed to be called from `lsp-ui--toggle'"
12041195
(when-let* ((frame (lsp-ui-doc--get-frame)))
12051196
(set-frame-parameter frame 'lsp-ui-doc--no-focus t)
12061197
(set-frame-parameter frame 'cursor-type nil)
1207-
(lsp-ui-doc--with-buffer
1208-
(setq cursor-type nil))
1209-
(when lsp-ui-doc--from-mouse
1210-
(make-frame-invisible frame))))
1198+
(lsp-ui-doc--with-buffer (setq cursor-type nil))
1199+
(when lsp-ui-doc--from-mouse (make-frame-invisible frame))))
12111200

12121201
(provide 'lsp-ui-doc)
12131202
;;; lsp-ui-doc.el ends here

lsp-ui-flycheck.el

+4-6
Original file line numberDiff line numberDiff line change
@@ -50,14 +50,12 @@ whole workspace."
5050
(defvar-local lsp-ui-flycheck--save-mode nil)
5151

5252
(defun lsp-ui-flycheck-list--post-command ()
53-
(when (eobp)
54-
(forward-line -1)))
53+
(when (eobp) (forward-line -1)))
5554

5655
(defun lsp-ui-flycheck-list--update (window workspace)
5756
"Update flycheck buffer in WINDOW belonging to WORKSPACE.
5857
Use `lsp-diagnostics' to receive diagnostics from your LSP server."
59-
(let ((buffer-read-only nil)
60-
(lsp--cur-workspace workspace))
58+
(let ((lsp--cur-workspace workspace) buffer-read-only)
6159
(erase-buffer)
6260
(remove-overlays)
6361
(maphash (lambda (file diagnostic)
@@ -161,8 +159,8 @@ Use `lsp-diagnostics' to receive diagnostics from your LSP server."
161159

162160
(define-derived-mode lsp-ui-flycheck-list-mode special-mode "lsp-ui-flycheck-list"
163161
"Mode showing flycheck diagnostics for the whole workspace."
164-
(setq truncate-lines t)
165-
(setq mode-line-format nil)
162+
(setq truncate-lines t
163+
mode-line-format nil)
166164
(add-hook 'post-command-hook 'lsp-ui-flycheck-list--post-command nil t))
167165

168166
(declare-function lsp-ui--workspace-path "lsp-ui" (path))

lsp-ui-imenu.el

+3-2
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,8 @@
3535

3636
(require 'lsp-mode)
3737
(require 'dash)
38-
(require 'lsp-ui-util)
38+
39+
(declare-function lsp-ui--safe-kill-timer 'lsp-ui)
3940

4041
(defgroup lsp-ui-imenu nil
4142
"Display imenu entries."
@@ -379,7 +380,7 @@ ITEMS are used when the kind position is 'left."
379380

380381
(defun lsp-ui-imenu--start-refresh (&rest _)
381382
"Starts the auto refresh timer."
382-
(lsp-ui-util-safe-kill-timer lsp-ui-imenu--refresh-timer)
383+
(lsp-ui--safe-kill-timer lsp-ui-imenu--refresh-timer)
383384
(setq lsp-ui-imenu--refresh-timer
384385
(run-with-idle-timer lsp-ui-imenu-auto-refresh-delay nil #'lsp-ui-imenu--refresh)))
385386

lsp-ui-peek.el

+10-10
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@
3939
(require 'xref)
4040
(require 'dash)
4141

42+
(declare-function lsp-ui--mute-apply 'lsp-ui)
43+
4244
(defgroup lsp-ui-peek nil
4345
"Improve version of xref with peek feature."
4446
:group 'tools
@@ -181,12 +183,12 @@ will cause performances issues.")
181183
(eval '(progn
182184
(evil-define-motion lsp-ui-peek-jump-backward (count)
183185
(lsp-ui-peek--with-evil-jumps
184-
(evil--jump-backward count)
185-
(run-hooks 'xref-after-return-hook)))
186+
(evil--jump-backward count)
187+
(run-hooks 'xref-after-return-hook)))
186188
(evil-define-motion lsp-ui-peek-jump-forward (count)
187189
(lsp-ui-peek--with-evil-jumps
188-
(evil--jump-forward count)
189-
(run-hooks 'xref-after-return-hook))))
190+
(evil--jump-forward count)
191+
(run-hooks 'xref-after-return-hook))))
190192
t))
191193

192194
(defmacro lsp-ui-peek--prop (prop &optional string)
@@ -346,10 +348,8 @@ XREFS is a list of references/definitions."
346348
(with-temp-buffer
347349
(insert string)
348350
(delay-mode-hooks
349-
(let ((inhibit-message t))
350-
(funcall major))
351-
(ignore-errors
352-
(font-lock-ensure)))
351+
(lsp-ui--mute-apply (funcall major))
352+
(ignore-errors (font-lock-ensure)))
353353
(buffer-string)))
354354

355355
(defun lsp-ui-peek--peek ()
@@ -687,9 +687,9 @@ LOCATION can be either a LSP Location or SymbolInformation."
687687
(unless buffer-file-name
688688
(make-local-variable 'delay-mode-hooks)
689689
(let ((buffer-file-name filename)
690-
(enable-local-variables nil)
691690
(inhibit-message t)
692-
(delay-mode-hooks t))
691+
(delay-mode-hooks t)
692+
enable-local-variables)
693693
(set-auto-mode)))
694694
(font-lock-ensure)))
695695

0 commit comments

Comments
 (0)