Skip to content

Commit 240a7de

Browse files
authored
Delay post request to improve performance (#628)
* Rename timer util to public * Delay post request * Add missing module
1 parent 29976bb commit 240a7de

File tree

3 files changed

+19
-7
lines changed

3 files changed

+19
-7
lines changed

lsp-ui-doc.el

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
(require 'goto-addr)
3737
(require 'markdown-mode)
3838
(require 'cl-lib)
39+
(require 'lsp-ui-util)
3940

4041
(when (featurep 'xwidget-internal)
4142
(require 'xwidget))
@@ -864,9 +865,7 @@ HEIGHT is the documentation number of lines."
864865

865866
(defun lsp-ui-doc--make-request nil
866867
"Request the documentation to the LS."
867-
(and (not track-mouse)
868-
lsp-ui-doc-show-with-mouse
869-
(setq-local track-mouse t))
868+
(and (not track-mouse) lsp-ui-doc-show-with-mouse (setq-local track-mouse t))
870869
(when (and lsp-ui-doc-show-with-cursor
871870
(not (memq this-command lsp-ui-doc--ignore-commands))
872871
(not (bound-and-true-p lsp-ui-peek-mode))
@@ -892,6 +891,19 @@ HEIGHT is the documentation number of lines."
892891
:cancel-token :lsp-ui-doc-hover)))))))
893892
(lsp-ui-doc--hide-frame))))
894893

894+
(defcustom lsp-ui-doc-post-delay 0.2
895+
"Seconds to wait before making post request."
896+
:type 'number
897+
:group 'lsp-ui-doc)
898+
899+
(defvar-local lsp-ui-doc--post-timer nil
900+
"Timer for post command.")
901+
902+
(defun lsp-ui-doc--post-command ()
903+
"Post command hook for UI doc."
904+
(lsp-ui-util-safe-kill-timer lsp-ui-doc--post-timer)
905+
(setq lsp-ui-doc--post-timer (run-with-timer lsp-ui-doc-post-delay nil #'lsp-ui-doc--make-request)))
906+
895907
(defun lsp-ui-doc--extract-bounds (hover)
896908
(-when-let* ((hover hover)
897909
(data (lsp-get hover :range))
@@ -1091,15 +1103,15 @@ If nil, do not prevent mouse on prefix keys.")
10911103
(add-hook 'window-state-change-functions 'lsp-ui-doc--on-state-changed))
10921104
(lsp-ui-doc--setup-mouse)
10931105
(advice-add 'handle-switch-frame :before-while 'lsp-ui-doc--prevent-focus-doc)
1094-
(add-hook 'post-command-hook 'lsp-ui-doc--make-request nil t)
1106+
(add-hook 'post-command-hook 'lsp-ui-doc--post-command nil t)
10951107
(add-hook 'window-scroll-functions 'lsp-ui-doc--handle-scroll nil t)
10961108
(add-hook 'delete-frame-functions 'lsp-ui-doc--on-delete nil t))
10971109
(t
10981110
(lsp-ui-doc-hide)
10991111
(when (boundp 'window-state-change-functions)
11001112
(remove-hook 'window-state-change-functions 'lsp-ui-doc--on-state-changed))
11011113
(remove-hook 'window-scroll-functions 'lsp-ui-doc--handle-scroll t)
1102-
(remove-hook 'post-command-hook 'lsp-ui-doc--make-request t)
1114+
(remove-hook 'post-command-hook 'lsp-ui-doc--post-command t)
11031115
(remove-hook 'delete-frame-functions 'lsp-ui-doc--on-delete t))))
11041116

11051117
(defun lsp-ui-doc-enable (enable)

lsp-ui-imenu.el

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -379,7 +379,7 @@ ITEMS are used when the kind position is 'left."
379379

380380
(defun lsp-ui-imenu--start-refresh (&rest _)
381381
"Starts the auto refresh timer."
382-
(lsp-ui-util--safe-kill-timer lsp-ui-imenu--refresh-timer)
382+
(lsp-ui-util-safe-kill-timer lsp-ui-imenu--refresh-timer)
383383
(setq lsp-ui-imenu--refresh-timer
384384
(run-with-idle-timer lsp-ui-imenu-auto-refresh-delay nil #'lsp-ui-imenu--refresh)))
385385

lsp-ui-util.el

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232

3333
;;; Code:
3434

35-
(defun lsp-ui-util--safe-kill-timer (timer)
35+
(defun lsp-ui-util-safe-kill-timer (timer)
3636
"Safely kill the TIMER."
3737
(when (timerp timer) (cancel-timer timer)))
3838

0 commit comments

Comments
 (0)