From db5b40040822621642870a6be1776e189f654f79 Mon Sep 17 00:00:00 2001 From: Tom Alexander Date: Sat, 9 Sep 2023 02:47:20 -0400 Subject: [PATCH] Locate rust-analyzer on the /home/talexander/.pyenv/shims:/opt/google-cloud-sdk/bin:/usr/local/sbin:/usr/local/bin:/usr/bin:/usr/lib/jvm/default/bin:/usr/bin/site_perl:/usr/bin/vendor_perl:/usr/bin/core_perl:/usr/lib/rustup/bin. --- ansible/roles/base/files/gitignore_global | 1 + ansible/roles/emacs/files/elisp/lang-rust.el | 35 +++++++++++++++----- 2 files changed, 27 insertions(+), 9 deletions(-) diff --git a/ansible/roles/base/files/gitignore_global b/ansible/roles/base/files/gitignore_global index e0809b4..b1ecf38 100644 --- a/ansible/roles/base/files/gitignore_global +++ b/ansible/roles/base/files/gitignore_global @@ -1,2 +1,3 @@ .idea .python-version +.dir-locals.el diff --git a/ansible/roles/emacs/files/elisp/lang-rust.el b/ansible/roles/emacs/files/elisp/lang-rust.el index 6cb30bf..c6160ae 100644 --- a/ansible/roles/emacs/files/elisp/lang-rust.el +++ b/ansible/roles/emacs/files/elisp/lang-rust.el @@ -3,7 +3,7 @@ (defun locate-rust-analyzer () "Find rust-analyzer." - (let ((rust-analyzer-paths (list (locate-rust-analyzer-rustup) (locate-rust-analyzer-ansible-built)))) + (let ((rust-analyzer-paths (list (locate-rust-analyzer-rustup) (locate-rust-analyzer-ansible-built) (locate-rust-analyzer-in-path)))) (let ((first-non-nil-path (seq-find (lambda (elt) elt) rust-analyzer-paths))) first-non-nil-path ) @@ -24,6 +24,11 @@ ) ) +(defun locate-rust-analyzer-in-path () + "Find rust-analyzer in $PATH." + (executable-find "rust-analyzer") + ) + (use-package rust-ts-mode :pin manual :mode ( @@ -32,14 +37,14 @@ :commands (rust-ts-mode) :hook ( (rust-ts-mode . (lambda () - (eglot-ensure) - (let ((rust-analyzer-command (locate-rust-analyzer))) - (when rust-analyzer-command - (add-to-list 'eglot-server-programs (cons 'rust-ts-mode (list rust-analyzer-command))) - ) - ) - (add-hook 'before-save-hook 'eglot-format-buffer nil 'local) - )) + (eglot-ensure) + (let ((rust-analyzer-command (locate-rust-analyzer))) + (when rust-analyzer-command + (add-to-list 'eglot-server-programs `(rust-ts-mode . (,rust-analyzer-command))) + ) + ) + (add-hook 'before-save-hook 'eglot-format-buffer nil 'local) + )) ) :init (add-to-list 'major-mode-remap-alist '(rust-mode . rust-ts-mode)) @@ -63,5 +68,17 @@ (unless (treesit-ready-p 'toml) (treesit-install-language-grammar 'toml)) ) +;; Set additional rust-analyzer settings: +;; +;; (add-to-list 'eglot-server-programs `(rust-ts-mode . (,rust-analyzer-command :initializationOptions (:cargo (:features "all"))))) +;; +;; In addition to the above, directory-specific settings can be written to a .dir-locals.el with the contents: +;; +;; ( +;; (rust-ts-mode . ((eglot-workspace-configuration +;; . (:rust-analyzer (:cargo (:features "compare")))) +;; )) +;; ) + (provide 'lang-rust)