使用 Emacs 开发 Rust 两年多了,一点体验供你参考(主要使用 lsp-mode + rust-analyzer + rust-mode):
- 把 lsp 的补全关掉,太卡了。这是我目前 Rust 相关配置
(setq lsp-log-io nil
lsp-eldoc-render-all nil
lsp-completion-provider t
;; lsp-completion-enable nil
lsp-signature-render-documentation nil
lsp-rust-server 'rust-analyzer
lsp-rust-analyzer-cargo-watch-enable nil
lsp-gopls-hover-kind "NoDocumentation"
lsp-gopls-use-placeholders t
lsp-diagnostics-provider :none)
(use-package rust-mode
:hook ((rust-mode . my/rust-lsp))
:config
(setq rust-format-on-save t)
(defun my/rust-lsp ()
(setq-local lsp-completion-enable nil
compile-command "cargo build")
))
我是把 lsp-rust-analyzer-cargo-watch-enable 设为 nil 了,否则写代码时会有卡顿。程序写完后,再运行 M-X compile 一次性编译,尽量不让编译干涉写代码的体验。
- 为 rust-analyzer 定义 lsp-clients-extract-signature-on-hover , 原因可参考这里。这是我目前使用的:
(cl-defmethod lsp-clients-extract-signature-on-hover (contents (_server-id (eql rust-analyzer)))
(-let* (((&hash "value") contents)
(groups (--partition-by (s-blank? it) (s-lines (s-trim value))))
(sig_group (if (s-equals? "```rust" (car (-third-item groups)))
(-third-item groups)
(car groups)))
(sig (--> sig_group
(--drop-while (s-equals? "```rust" it) it)
(--take-while (not (s-equals? "```" it)) it)
(s-join "" it))))
(lsp--render-element (concat "```rust\n" sig "\n```"))))
- 轻易不要升级 lsp-mode/rust-analyzer,比较容易出问题。目前我使用的版本
- rust-analyzer 678a29e
- lsp-mode 7.0.1-265-g0349a1cc