Skip to content

Latest commit

 

History

History
143 lines (90 loc) · 5.02 KB

lsp-rust-analyzer.md

File metadata and controls

143 lines (90 loc) · 5.02 KB
author template root_file
yyoncho
comment.html
docs/manual-language-docs/lsp-rust-analyzer.md

Server note

NOTE: If you are using rustic-mode, you have to change rustic-lsp-server instead of lsp-rust-server, since it also supports eglot as a lightweight alternative to lsp-mode.

  • lsp-rust-server Choose LSP server (default is rust-analyzer)

  • lsp-rust-switch-server Switch priorities of lsp servers

rust-analyzer

Commands

lsp-rust-analyzer-syntax-tree

Display syntax tree for current buffer

lsp-rust-analyzer-status

Display status information for rust-analyzer

lsp-rust-analyzer-join-lines

Join selected lines into one, smartly fixing up whitespace and trailing commas

before:

after:

inlay-hints

lsp-rust-analyzer-inlay-hints-mode enables displaying of inlay hints

Additionally, lsp-rust-analyzer-server-display-inlay-hints must be set to t in order for inlay hints to render.

NOTE: the inlay hints interact badly with the lsp-ui sideline, because it doesn't seem to consider the overlays in its width calculation, which often leads to lines wrapping around.

Macro expansion

lsp-rust-analyzer-expand-macro expand macro call at point recursively

Use your own function for displaying macro expansion by customizing lsp-rust-analyzer-macro-expansion-method

Formatted and highlighted result with the default function of rustic.

auto-import

Get a list of possible auto import candidates with lsp-execute-code-action

Snippet insertion/refactor

To support refactorings that require snippet insertion(eg. generating derive clause etc), make sure that you have enabled yasnippet and yas-minor-mode. If you are using use-package, you can do something like this:

(use-package yasnippet
  :ensure t
  :hook ((lsp-mode . yas-minor-mode)))

Open Cargo.toml

lsp-rust-analyzer-open-cargo-toml opens the Cargo.toml closest to the current file. Calling it with a universal argument will open the Cargo.toml in another window.

Corresponds to the rust-analyzer LSP extension

Open external documentation

lsp-rust-analyzer-open-external-docs opens external documentation related to the current position in a browser.

Corresponds to the rust-analyzer LSP extension

Find and execute tests related to current position

lsp-rust-analyzer-related-tests find all tests related to the current position, asks for user completion and executes the selected test in a compilation buffer.

Corresponds to the rust-analyzer LSP extension

In the example below, first you see that:

  • On the left, the function check_infer is defined, on the right another file is opened with many test functions, some of which call check_infer. With the cursor on check_infer, call lsp-rust-analyzer-related-tests and select infer_pattern_match_slice with fuzzy matching. The test is executed on the right with compilation major mode

  • Move the cursor to fn ellipsize and attempt to find related tests to no avail. Confirm that the function is indeed untested by using swiper and finding one place in the file, where the function is called

Caveats

extract signature

This unmerged PR contains an example method that allows modifying the signature that is displayed by eldoc.

TRAMP Example

The following is an example configuration for using lsp-mode with a remote rust-analyzer server:

(with-eval-after-load "lsp-rust"
  (lsp-register-client
   (make-lsp-client
    :new-connection (lsp-tramp-connection "rust-analyzer")
    :remote? t
    :major-modes '(rust-mode rustic-mode)
    :initialization-options 'lsp-rust-analyzer--make-init-options
    :notification-handlers (ht<-alist lsp-rust-notification-handlers)
    :action-handlers (ht ("rust-analyzer.runSingle" #'lsp-rust--analyzer-run-single))
    :library-folders-fn (lambda (_workspace) lsp-rust-analyzer-library-directories)
    :after-open-fn (lambda ()
                     (when lsp-rust-analyzer-server-display-inlay-hints
                       (lsp-rust-analyzer-inlay-hints-mode)))
    :ignore-messages nil
    :server-id 'rust-analyzer-remote)))