diff --git a/docs/user/README.md b/docs/user/README.md index 1861c69abc..bfb1904492 100644 --- a/docs/user/README.md +++ b/docs/user/README.md @@ -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 diff --git a/editors/emacs/ra-emacs-lsp.el b/editors/emacs/ra-emacs-lsp.el index 79822c8ced..fafb9cbe73 100644 --- a/editors/emacs/ra-emacs-lsp.el +++ b/editors/emacs/ra-emacs-lsp.el @@ -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