[配置分享] 修改 lsp-mode 的 rust-analyzer 的配置

我一直用 lsp-mode 写代码,最近想改 rust-analyzer 服务端的配置,一直没找到最方便的办法。

比如我想改 rust-analyzer.workspace.symbol.search.limit 的值为 1024rust-analyzer.workspace.symbol.search.kind 的值为 all_symbols

这样我就能用 lsp-ui-find-workspace-symbols 搜索到更多的 symbols ,而不是 rust-analyzer 默认的 128 个。

我试了试,可以这样做:

  (defun merge-plists (origin patch)
	"Recursively merge PATCH plist onto ORIGIN plist."
	(if (null patch)
		origin
      (let ((key (car patch))
			(value (cadr patch)))
		(setq origin
              (plist-put origin key
						 (if (and (listp value) (plist-member origin key) (listp (plist-get origin key)))
							 (merge-plists (plist-get origin key) value)
                           value)))
		(merge-plists origin (cddr patch)))))

  (setq exec/lsp-rust-analyzer-patch-options
		'(:workspace
		  (:symbol
		   (:search
			(:limit 1024 :kind "all_symbols")))))

  (defun exec/lsp-rust-analyzer-add-extra-key (orig-fun &rest args)
	"Add :extraKey to the init options for rust-analyzer."
	(let ((init-options (apply orig-fun args))
		  (patch-options exec/lsp-rust-analyzer-patch-options))
	  (merge-plists init-options patch-options)))

  (advice-add 'lsp-rust-analyzer--make-init-options :around #'exec/lsp-rust-analyzer-add-extra-key)

效果:

4 个赞

其实 eglot 也可以配置,设置 eglot-workspace-configuration 就行。

另外一些项目相关的配置可以直接写到项目根目录下的 .dir-locals.el 中,这样每次打开项目会自动加载,比如我在 rust-clippy 仓库下的配置是:

((nil
  . ((eglot-workspace-configuration
      . (:rust-analyzer
         (:rustc (:source "discover")
          :linkedProjects ["./Cargo.toml" "clippy_dev/Cargo.toml" "lintcheck/Cargo.toml"]))))))

这里用 plist 来代替 json

1 个赞