diff options
Diffstat (limited to 'editors/emacs')
-rw-r--r-- | editors/emacs/ra-emacs-lsp.el | 73 |
1 files changed, 27 insertions, 46 deletions
diff --git a/editors/emacs/ra-emacs-lsp.el b/editors/emacs/ra-emacs-lsp.el index d7656476e..79822c8ce 100644 --- a/editors/emacs/ra-emacs-lsp.el +++ b/editors/emacs/ra-emacs-lsp.el | |||
@@ -14,7 +14,7 @@ | |||
14 | ;; - 'hover' type information & documentation (with lsp-ui) | 14 | ;; - 'hover' type information & documentation (with lsp-ui) |
15 | ;; - implements source changes (for code actions etc.), except for file system changes | 15 | ;; - implements source changes (for code actions etc.), except for file system changes |
16 | ;; - implements joinLines (you need to bind rust-analyzer-join-lines to a key) | 16 | ;; - implements joinLines (you need to bind rust-analyzer-join-lines to a key) |
17 | ;; - implements extendSelection (either bind rust-analyzer-extend-selection to a key, or use expand-region) | 17 | ;; - implements selectionRanges (either bind lsp-extend-selection to a key, or use expand-region) |
18 | ;; - provides rust-analyzer-inlay-hints-mode for inline type hints | 18 | ;; - provides rust-analyzer-inlay-hints-mode for inline type hints |
19 | 19 | ||
20 | ;; What's missing: | 20 | ;; What's missing: |
@@ -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))) |
@@ -99,39 +103,13 @@ | |||
99 | (rust-analyzer--join-lines-params))) | 103 | (rust-analyzer--join-lines-params))) |
100 | (rust-analyzer--apply-source-change))) | 104 | (rust-analyzer--apply-source-change))) |
101 | 105 | ||
102 | ;; extend selection | 106 | ;; selection ranges |
103 | |||
104 | (defun rust-analyzer-extend-selection () | ||
105 | (interactive) | ||
106 | (-let (((&hash "start" "end") (rust-analyzer--extend-selection))) | ||
107 | (rust-analyzer--goto-lsp-loc start) | ||
108 | (set-mark (point)) | ||
109 | (rust-analyzer--goto-lsp-loc end) | ||
110 | (exchange-point-and-mark))) | ||
111 | |||
112 | (defun rust-analyzer--extend-selection-params () | ||
113 | "Extend selection params." | ||
114 | (list :textDocument (lsp--text-document-identifier) | ||
115 | :selections | ||
116 | (vector | ||
117 | (if (use-region-p) | ||
118 | (lsp--region-to-range (region-beginning) (region-end)) | ||
119 | (lsp--region-to-range (point) (point)))))) | ||
120 | |||
121 | (defun rust-analyzer--extend-selection () | ||
122 | (-> | ||
123 | (lsp-send-request | ||
124 | (lsp-make-request | ||
125 | "rust-analyzer/extendSelection" | ||
126 | (rust-analyzer--extend-selection-params))) | ||
127 | (ht-get "selections") | ||
128 | (seq-first))) | ||
129 | 107 | ||
130 | (defun rust-analyzer--add-er-expansion () | 108 | (defun rust-analyzer--add-er-expansion () |
131 | (make-variable-buffer-local 'er/try-expand-list) | 109 | (make-variable-buffer-local 'er/try-expand-list) |
132 | (setq er/try-expand-list (append | 110 | (setq er/try-expand-list (append |
133 | er/try-expand-list | 111 | er/try-expand-list |
134 | '(rust-analyzer-extend-selection)))) | 112 | '(lsp-extend-selection)))) |
135 | 113 | ||
136 | (with-eval-after-load 'expand-region | 114 | (with-eval-after-load 'expand-region |
137 | ;; add the expansion for all existing rust-mode buffers. If expand-region is | 115 | ;; add the expansion for all existing rust-mode buffers. If expand-region is |
@@ -229,21 +207,22 @@ | |||
229 | (pop-to-buffer buf)))))) | 207 | (pop-to-buffer buf)))))) |
230 | 208 | ||
231 | ;; inlay hints | 209 | ;; inlay hints |
232 | (defun rust-analyzer--update-inlay-hints () | 210 | (defun rust-analyzer--update-inlay-hints (buffer) |
233 | (lsp-send-request-async | 211 | (if (and (rust-analyzer--initialized?) (eq buffer (current-buffer))) |
234 | (lsp-make-request "rust-analyzer/inlayHints" | 212 | (lsp-send-request-async |
235 | (list :textDocument (lsp--text-document-identifier))) | 213 | (lsp-make-request "rust-analyzer/inlayHints" |
236 | (lambda (res) | 214 | (list :textDocument (lsp--text-document-identifier))) |
237 | (remove-overlays (point-min) (point-max) 'rust-analyzer--inlay-hint t) | 215 | (lambda (res) |
238 | (dolist (hint res) | 216 | (remove-overlays (point-min) (point-max) 'rust-analyzer--inlay-hint t) |
239 | (-let* (((&hash "range" "label" "kind") hint) | 217 | (dolist (hint res) |
240 | ((beg . end) (lsp--range-to-region range)) | 218 | (-let* (((&hash "range" "label" "kind") hint) |
241 | (overlay (make-overlay beg end))) | 219 | ((beg . end) (lsp--range-to-region range)) |
242 | (overlay-put overlay 'rust-analyzer--inlay-hint t) | 220 | (overlay (make-overlay beg end))) |
243 | (overlay-put overlay 'evaporate t) | 221 | (overlay-put overlay 'rust-analyzer--inlay-hint t) |
244 | (overlay-put overlay 'after-string (propertize (concat ": " label) | 222 | (overlay-put overlay 'evaporate t) |
245 | 'font-lock-face 'font-lock-comment-face))))) | 223 | (overlay-put overlay 'after-string (propertize (concat ": " label) |
246 | 'tick) | 224 | 'font-lock-face 'font-lock-comment-face))))) |
225 | 'tick)) | ||
247 | nil) | 226 | nil) |
248 | 227 | ||
249 | (defvar-local rust-analyzer--inlay-hints-timer nil) | 228 | (defvar-local rust-analyzer--inlay-hints-timer nil) |
@@ -252,17 +231,19 @@ | |||
252 | (when rust-analyzer--inlay-hints-timer | 231 | (when rust-analyzer--inlay-hints-timer |
253 | (cancel-timer rust-analyzer--inlay-hints-timer)) | 232 | (cancel-timer rust-analyzer--inlay-hints-timer)) |
254 | (setq rust-analyzer--inlay-hints-timer | 233 | (setq rust-analyzer--inlay-hints-timer |
255 | (run-with-idle-timer 0.1 nil #'rust-analyzer--update-inlay-hints))) | 234 | (run-with-idle-timer 0.1 nil #'rust-analyzer--update-inlay-hints (current-buffer)))) |
256 | 235 | ||
257 | (define-minor-mode rust-analyzer-inlay-hints-mode | 236 | (define-minor-mode rust-analyzer-inlay-hints-mode |
258 | "Mode for showing inlay hints." | 237 | "Mode for showing inlay hints." |
259 | nil nil nil | 238 | nil nil nil |
260 | (cond | 239 | (cond |
261 | (rust-analyzer-inlay-hints-mode | 240 | (rust-analyzer-inlay-hints-mode |
262 | (rust-analyzer--update-inlay-hints) | 241 | (rust-analyzer--update-inlay-hints (current-buffer)) |
242 | (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)) | 243 | (add-hook 'after-change-functions #'rust-analyzer--inlay-hints-change-handler nil t)) |
264 | (t | 244 | (t |
265 | (remove-overlays (point-min) (point-max) 'rust-analyzer--inlay-hint t) | 245 | (remove-overlays (point-min) (point-max) 'rust-analyzer--inlay-hint t) |
246 | (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)))) | 247 | (remove-hook 'after-change-functions #'rust-analyzer--inlay-hints-change-handler t)))) |
267 | 248 | ||
268 | 249 | ||