2334: Add rust-analyzer-expand-macro function for Emacs r=flodiebold a=flodiebold



Co-authored-by: Florian Diebold <flodiebold@gmail.com>
This commit is contained in:
bors[bot] 2019-11-20 19:45:57 +00:00 committed by GitHub
commit a025303ca6
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
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) [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` to load path and require it in `init.el`
* run `lsp` in a rust buffer * 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 ## Vim and NeoVim

View file

@ -16,6 +16,7 @@
;; - implements joinLines (you need to bind rust-analyzer-join-lines to a key) ;; - 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) ;; - 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-inlay-hints-mode for inline type hints
;; - provides rust-analyzer-expand-macro to expand macros
;; What's missing: ;; What's missing:
;; - file system changes in apply-source-change ;; - file system changes in apply-source-change
@ -247,5 +248,32 @@
(remove-hook 'after-change-functions #'rust-analyzer--inlay-hints-change-handler t)))) (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) (provide 'ra-emacs-lsp)
;;; ra-emacs-lsp.el ends here ;;; ra-emacs-lsp.el ends here