]> git.lizzy.rs Git - rust.git/commitdiff
Add rust-analyzer-expand-macro function for Emacs
authorFlorian Diebold <flodiebold@gmail.com>
Wed, 20 Nov 2019 19:30:41 +0000 (20:30 +0100)
committerFlorian Diebold <flodiebold@gmail.com>
Wed, 20 Nov 2019 19:45:25 +0000 (20:45 +0100)
docs/user/README.md
editors/emacs/ra-emacs-lsp.el

index 1861c69abc854508619eb8f83d4cf09e8b42d8e2..bfb19044924f4be6f20f25f04d52f459692ccb50 100644 (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
index 79822c8ceda518eede47675024d0d66a7e3d7d15..fafb9cbe733413578f1aca28d09890e51d0e8780 100644 (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
     (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