0

I'm writing Rust using rust-analyzer and lsp-mode

Suppose I have code:

let x = 10;
let y = 20 + x;

I want to put my cursor over x and hit some shortcut (or whatever) and the code becomes:

let y = 20 + 10;

From searching the internet it seems I'm the first person in the world who ever wanted to inline a variable in Emacs⁈

5
  • I don't know about emacs specifically, but I'd think it needs to be supported by rust-analyzer/clippy to first have it a s a lint suggestion and offer a replacement code. Then, I believe, your editor should already have a shortcut to apply the suggestion. Commented May 26 at 15:03
  • @EugeneSh. Looks like rust-analyzer in VSCode can do it - image. So I figure it should be possible, but I also am not familiar with emacs. Commented May 26 at 16:17
  • @kmdreko Interesting. Looks like it is a part of the Rust extension rather than rust-analyzer? Commented May 26 at 16:19
  • @EugeneSh. no idea why you would think that, the rust-analyzer assist is inline_local_variable Commented May 26 at 16:26
  • Yeah, just checked it, sure still depends on rust-analyzer, which absolutely makes sense. Commented May 26 at 16:27

2 Answers 2

1

Since rust-analyzer supports that code action, you should be able to use it using lsp-mode's support of rust-analyzer. I did a quick testing using my Emacs instance and can confirm that the code action works fine via lsp-mode.

Sign up to request clarification or add additional context in comments.

Comments

0

So after a lot of digging around I'm pretty sure there isn't a shortcut to do this, or even a specific command that can be mapped to a shortcut. Rather one must find the action from a list. So I had to write a function to automate that as follows:

;; Inline variable
(defun lsp-rust-inline-action ()
  "Execute the Rust Analyzer inline refactor code action."
  (interactive)
  (lsp-request-async
   "textDocument/codeAction"
   (lsp--text-document-code-action-params)
   (lambda (actions)
     (let ((inline-action
            (seq-find (lambda (action)
                        (string= (gethash "kind" action) "refactor.inline"))
                      actions)))
       (if inline-action
           (lsp-execute-code-action inline-action)
         (message "No inline action available here"))))
   :error-handler #'lsp--error
   :mode 'detached))

(with-eval-after-load 'rust-mode
  (define-key rust-mode-map (kbd "M-v") #'lsp-rust-inline-action))

So after adding this to my ~/.emacs.d/init.el I can inline variables with M-v (ALT + v).

NOTE: It doesn't seem inlining is always available for all variables, it seems the rust-analyzer only supports a subclass of situations - this can make things very confusing.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.