diff options
author | bors[bot] <26634292+bors[bot]@users.noreply.github.com> | 2019-08-12 11:23:33 +0100 |
---|---|---|
committer | bors[bot] <26634292+bors[bot]@users.noreply.github.com> | 2019-08-12 11:23:33 +0100 |
commit | b8627d8c107a58621bf6a8fd37a9990ad20bea99 (patch) | |
tree | 09a9d2e13064220cd6c6d7a470f909774a2b06f0 /editors/emacs/ra-emacs-lsp.el | |
parent | e3f8e6023d66ceb37040833bf317d77019bb10f1 (diff) | |
parent | e51c1d6bff7cdb26f8ba2f8429c91e1cdfcc1236 (diff) |
Merge #1675
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 <[email protected]>
Diffstat (limited to 'editors/emacs/ra-emacs-lsp.el')
-rw-r--r-- | editors/emacs/ra-emacs-lsp.el | 41 |
1 files changed, 24 insertions, 17 deletions
diff --git a/editors/emacs/ra-emacs-lsp.el b/editors/emacs/ra-emacs-lsp.el index d7656476e..075cbd82d 100644 --- a/editors/emacs/ra-emacs-lsp.el +++ b/editors/emacs/ra-emacs-lsp.el | |||
@@ -79,6 +79,10 @@ | |||
79 | :ignore-messages nil | 79 | :ignore-messages nil |
80 | :server-id 'rust-analyzer)) | 80 | :server-id 'rust-analyzer)) |
81 | 81 | ||
82 | (defun rust-analyzer--initialized? () | ||
83 | (when-let ((workspace (lsp-find-workspace 'rust-analyzer (buffer-file-name)))) | ||
84 | (eq 'initialized (lsp--workspace-status workspace)))) | ||
85 | |||
82 | (with-eval-after-load 'company-lsp | 86 | (with-eval-after-load 'company-lsp |
83 | ;; company-lsp provides a snippet handler for rust by default that adds () after function calls, which RA does better | 87 | ;; company-lsp provides a snippet handler for rust by default that adds () after function calls, which RA does better |
84 | (setq company-lsp--snippet-functions (cl-delete "rust" company-lsp--snippet-functions :key #'car :test #'equal))) | 88 | (setq company-lsp--snippet-functions (cl-delete "rust" company-lsp--snippet-functions :key #'car :test #'equal))) |
@@ -229,21 +233,22 @@ | |||
229 | (pop-to-buffer buf)))))) | 233 | (pop-to-buffer buf)))))) |
230 | 234 | ||
231 | ;; inlay hints | 235 | ;; inlay hints |
232 | (defun rust-analyzer--update-inlay-hints () | 236 | (defun rust-analyzer--update-inlay-hints (buffer) |
233 | (lsp-send-request-async | 237 | (if (and (rust-analyzer--initialized?) (eq buffer (current-buffer))) |
234 | (lsp-make-request "rust-analyzer/inlayHints" | 238 | (lsp-send-request-async |
235 | (list :textDocument (lsp--text-document-identifier))) | 239 | (lsp-make-request "rust-analyzer/inlayHints" |
236 | (lambda (res) | 240 | (list :textDocument (lsp--text-document-identifier))) |
237 | (remove-overlays (point-min) (point-max) 'rust-analyzer--inlay-hint t) | 241 | (lambda (res) |
238 | (dolist (hint res) | 242 | (remove-overlays (point-min) (point-max) 'rust-analyzer--inlay-hint t) |
239 | (-let* (((&hash "range" "label" "kind") hint) | 243 | (dolist (hint res) |
240 | ((beg . end) (lsp--range-to-region range)) | 244 | (-let* (((&hash "range" "label" "kind") hint) |
241 | (overlay (make-overlay beg end))) | 245 | ((beg . end) (lsp--range-to-region range)) |
242 | (overlay-put overlay 'rust-analyzer--inlay-hint t) | 246 | (overlay (make-overlay beg end))) |
243 | (overlay-put overlay 'evaporate t) | 247 | (overlay-put overlay 'rust-analyzer--inlay-hint t) |
244 | (overlay-put overlay 'after-string (propertize (concat ": " label) | 248 | (overlay-put overlay 'evaporate t) |
245 | 'font-lock-face 'font-lock-comment-face))))) | 249 | (overlay-put overlay 'after-string (propertize (concat ": " label) |
246 | 'tick) | 250 | 'font-lock-face 'font-lock-comment-face))))) |
251 | 'tick)) | ||
247 | nil) | 252 | nil) |
248 | 253 | ||
249 | (defvar-local rust-analyzer--inlay-hints-timer nil) | 254 | (defvar-local rust-analyzer--inlay-hints-timer nil) |
@@ -252,17 +257,19 @@ | |||
252 | (when rust-analyzer--inlay-hints-timer | 257 | (when rust-analyzer--inlay-hints-timer |
253 | (cancel-timer rust-analyzer--inlay-hints-timer)) | 258 | (cancel-timer rust-analyzer--inlay-hints-timer)) |
254 | (setq rust-analyzer--inlay-hints-timer | 259 | (setq rust-analyzer--inlay-hints-timer |
255 | (run-with-idle-timer 0.1 nil #'rust-analyzer--update-inlay-hints))) | 260 | (run-with-idle-timer 0.1 nil #'rust-analyzer--update-inlay-hints (current-buffer)))) |
256 | 261 | ||
257 | (define-minor-mode rust-analyzer-inlay-hints-mode | 262 | (define-minor-mode rust-analyzer-inlay-hints-mode |
258 | "Mode for showing inlay hints." | 263 | "Mode for showing inlay hints." |
259 | nil nil nil | 264 | nil nil nil |
260 | (cond | 265 | (cond |
261 | (rust-analyzer-inlay-hints-mode | 266 | (rust-analyzer-inlay-hints-mode |
262 | (rust-analyzer--update-inlay-hints) | 267 | (rust-analyzer--update-inlay-hints (current-buffer)) |
268 | (add-hook 'lsp-after-initialize-hook #'rust-analyzer--inlay-hints-change-handler nil t) | ||
263 | (add-hook 'after-change-functions #'rust-analyzer--inlay-hints-change-handler nil t)) | 269 | (add-hook 'after-change-functions #'rust-analyzer--inlay-hints-change-handler nil t)) |
264 | (t | 270 | (t |
265 | (remove-overlays (point-min) (point-max) 'rust-analyzer--inlay-hint t) | 271 | (remove-overlays (point-min) (point-max) 'rust-analyzer--inlay-hint t) |
272 | (remove-hook 'lsp-after-initialize-hook #'rust-analyzer--inlay-hints-change-handler t) | ||
266 | (remove-hook 'after-change-functions #'rust-analyzer--inlay-hints-change-handler t)))) | 273 | (remove-hook 'after-change-functions #'rust-analyzer--inlay-hints-change-handler t)))) |
267 | 274 | ||
268 | 275 | ||