Manager the global timer gracefully

Don't cancel and create new timers naively.
Only cancel timer (and set to nil) when there is no live buffer
with eldoc-box-hover-mode enabled;
and only create new timer when none exists.
This commit is contained in:
Yuan Fu 2018-12-12 01:58:56 -05:00
parent 388282f59f
commit fcfd701f57
No known key found for this signature in database
GPG Key ID: 1CF5ECABEC37A901

View File

@ -198,6 +198,16 @@ Checkout `lsp-ui-doc--make-frame', `lsp-ui-doc--move-frame'."
(when (frame-parameter eldoc-box--frame 'visibility)
(eldoc-box-quit-frame))))
(defvar eldoc-box--enabled-buffer-list nil
"A list of buffers that enabled `eldoc-box-hover-mode'.")
(defun eldoc-box--maybe-cancel-timer ()
"Cancel `eldoc-box--cleanup-timer' there is no live buffer with `eldoc-box-hover-mode' enabled left."
(when (and eldoc-box--cleanup-timer
(eq (cl-count-if #'buffer-live-p eldoc-box--enabled-buffer-list) 0))
(cancel-timer eldoc-box--cleanup-timer)
(setq eldoc-box--cleanup-timer nil)))
(defun eldoc-box--eldoc-message-function (str &rest args)
"Frontend for eldoc. Display STR in childframe and ARGS works like `message'."
(if (stringp str)
@ -215,10 +225,15 @@ Checkout `lsp-ui-doc--make-frame', `lsp-ui-doc--move-frame'."
;; Why a timer? ElDoc is mainly use in minibuffer,
;; where the text is constantly being flushed by other commands
;; so ElDoc doesn't try very hard to cleanup
(setq eldoc-box--cleanup-timer (run-with-idle-timer 1 t #'eldoc-box--maybe-cleanup))
(unless eldoc-box--cleanup-timer
(setq eldoc-box--cleanup-timer (run-with-idle-timer 1 t #'eldoc-box--maybe-cleanup)))
(add-to-list 'eldoc-box--enabled-buffer-list (current-buffer))
(setq-local eldoc-message-function #'eldoc-box--eldoc-message-function))
(setq-local eldoc-message-function #'eldoc-minibuffer-message)
(when eldoc-box--cleanup-timer (cancel-timer eldoc-box--cleanup-timer))
(delete (current-buffer) eldoc-box--enabled-buffer-list)
;; this function has to be called after the current buffer is
;; removed from buffer list
(eldoc-box--maybe-cancel-timer)
;; if minor mode is turned off when childframe is visible
;; hide it
(eldoc-box-quit-frame)))