Add rust-analyzer-expand-macro function for Emacs

This commit is contained in:
Florian Diebold 2019-11-20 20:30:41 +01:00
parent 2cb2fb1a48
commit c079d9b63b
2 changed files with 29 additions and 1 deletions

View file

@ -127,7 +127,7 @@ Installation:
[ra-emacs-lsp.el](https://github.com/rust-analyzer/rust-analyzer/blob/69ee5c9c5ef212f7911028c9ddf581559e6565c3/editors/emacs/ra-emacs-lsp.el)
to load path and require it in `init.el`
* run `lsp` in a rust buffer
* (Optionally) bind commands like `rust-analyzer-join-lines` or `rust-analyzer-extend-selection` to keys, and enable `rust-analyzer-inlay-hints-mode` to get inline type hints
* (Optionally) bind commands like `rust-analyzer-join-lines`, `rust-analyzer-extend-selection` and `rust-analyzer-expand-macro` to keys, and enable `rust-analyzer-inlay-hints-mode` to get inline type hints
## Vim and NeoVim

View file

@ -16,6 +16,7 @@
;; - implements joinLines (you need to bind rust-analyzer-join-lines to a key)
;; - implements selectionRanges (either bind lsp-extend-selection to a key, or use expand-region)
;; - provides rust-analyzer-inlay-hints-mode for inline type hints
;; - provides rust-analyzer-expand-macro to expand macros
;; What's missing:
;; - file system changes in apply-source-change
@ -247,5 +248,32 @@
(remove-hook 'after-change-functions #'rust-analyzer--inlay-hints-change-handler t))))
;; expand macros
(defun rust-analyzer-expand-macro ()
"Expands the macro call at point recursively."
(interactive)
(when (eq 'rust-mode major-mode)
(let* ((workspace (lsp-find-workspace 'rust-analyzer (buffer-file-name)))
(params (list :textDocument (lsp--text-document-identifier)
:position (lsp--cur-position))))
(when workspace
(let* ((response (with-lsp-workspace workspace
(lsp-send-request (lsp-make-request
"rust-analyzer/expandMacro"
params))))
(result (when response (ht-get response "expansion"))))
(if result
(let ((buf (get-buffer-create (concat "*rust-analyzer macro expansion " (with-lsp-workspace workspace (lsp-workspace-root)) "*"))))
(with-current-buffer buf
(let ((inhibit-read-only t))
(erase-buffer)
(insert result)
(setq buffer-read-only t)
(special-mode)))
(pop-to-buffer buf))
(message "No macro found at point, or it could not be expanded")))))))
(provide 'ra-emacs-lsp)
;;; ra-emacs-lsp.el ends here