1675: Improvements to emacs inlay hints r=matklad a=flodiebold

 - only send request if workspace is initialized (emacs-lsp doesn't seem to
   prevent sending requests before the initialized notification is sent)
 - check whether we're still in the correct buffer before sending request

Co-authored-by: Florian Diebold <flodiebold@gmail.com>
This commit is contained in:
bors[bot] 2019-08-12 10:23:33 +00:00
commit b8627d8c10

View file

@ -79,6 +79,10 @@
:ignore-messages nil :ignore-messages nil
:server-id 'rust-analyzer)) :server-id 'rust-analyzer))
(defun rust-analyzer--initialized? ()
(when-let ((workspace (lsp-find-workspace 'rust-analyzer (buffer-file-name))))
(eq 'initialized (lsp--workspace-status workspace))))
(with-eval-after-load 'company-lsp (with-eval-after-load 'company-lsp
;; company-lsp provides a snippet handler for rust by default that adds () after function calls, which RA does better ;; company-lsp provides a snippet handler for rust by default that adds () after function calls, which RA does better
(setq company-lsp--snippet-functions (cl-delete "rust" company-lsp--snippet-functions :key #'car :test #'equal))) (setq company-lsp--snippet-functions (cl-delete "rust" company-lsp--snippet-functions :key #'car :test #'equal)))
@ -229,21 +233,22 @@
(pop-to-buffer buf)))))) (pop-to-buffer buf))))))
;; inlay hints ;; inlay hints
(defun rust-analyzer--update-inlay-hints () (defun rust-analyzer--update-inlay-hints (buffer)
(lsp-send-request-async (if (and (rust-analyzer--initialized?) (eq buffer (current-buffer)))
(lsp-make-request "rust-analyzer/inlayHints" (lsp-send-request-async
(list :textDocument (lsp--text-document-identifier))) (lsp-make-request "rust-analyzer/inlayHints"
(lambda (res) (list :textDocument (lsp--text-document-identifier)))
(remove-overlays (point-min) (point-max) 'rust-analyzer--inlay-hint t) (lambda (res)
(dolist (hint res) (remove-overlays (point-min) (point-max) 'rust-analyzer--inlay-hint t)
(-let* (((&hash "range" "label" "kind") hint) (dolist (hint res)
((beg . end) (lsp--range-to-region range)) (-let* (((&hash "range" "label" "kind") hint)
(overlay (make-overlay beg end))) ((beg . end) (lsp--range-to-region range))
(overlay-put overlay 'rust-analyzer--inlay-hint t) (overlay (make-overlay beg end)))
(overlay-put overlay 'evaporate t) (overlay-put overlay 'rust-analyzer--inlay-hint t)
(overlay-put overlay 'after-string (propertize (concat ": " label) (overlay-put overlay 'evaporate t)
'font-lock-face 'font-lock-comment-face))))) (overlay-put overlay 'after-string (propertize (concat ": " label)
'tick) 'font-lock-face 'font-lock-comment-face)))))
'tick))
nil) nil)
(defvar-local rust-analyzer--inlay-hints-timer nil) (defvar-local rust-analyzer--inlay-hints-timer nil)
@ -252,17 +257,19 @@
(when rust-analyzer--inlay-hints-timer (when rust-analyzer--inlay-hints-timer
(cancel-timer rust-analyzer--inlay-hints-timer)) (cancel-timer rust-analyzer--inlay-hints-timer))
(setq rust-analyzer--inlay-hints-timer (setq rust-analyzer--inlay-hints-timer
(run-with-idle-timer 0.1 nil #'rust-analyzer--update-inlay-hints))) (run-with-idle-timer 0.1 nil #'rust-analyzer--update-inlay-hints (current-buffer))))
(define-minor-mode rust-analyzer-inlay-hints-mode (define-minor-mode rust-analyzer-inlay-hints-mode
"Mode for showing inlay hints." "Mode for showing inlay hints."
nil nil nil nil nil nil
(cond (cond
(rust-analyzer-inlay-hints-mode (rust-analyzer-inlay-hints-mode
(rust-analyzer--update-inlay-hints) (rust-analyzer--update-inlay-hints (current-buffer))
(add-hook 'lsp-after-initialize-hook #'rust-analyzer--inlay-hints-change-handler nil t)
(add-hook 'after-change-functions #'rust-analyzer--inlay-hints-change-handler nil t)) (add-hook 'after-change-functions #'rust-analyzer--inlay-hints-change-handler nil t))
(t (t
(remove-overlays (point-min) (point-max) 'rust-analyzer--inlay-hint t) (remove-overlays (point-min) (point-max) 'rust-analyzer--inlay-hint t)
(remove-hook 'lsp-after-initialize-hook #'rust-analyzer--inlay-hints-change-handler t)
(remove-hook 'after-change-functions #'rust-analyzer--inlay-hints-change-handler t)))) (remove-hook 'after-change-functions #'rust-analyzer--inlay-hints-change-handler t))))