(require 'common-lsp) (require 'util-tree-sitter) (defun locate-rust-analyzer () "Find rust-analyzer." (let ((rust-analyzer-paths (list (locate-rust-analyzer-rustup) (locate-rust-analyzer-ansible-built)))) (let ((first-non-nil-path (seq-find (lambda (elt) elt) rust-analyzer-paths))) first-non-nil-path ) ) ) (defun locate-rust-analyzer-rustup () "Find rust-analyzer through rustup." (run-command-in-directory nil "rustup" "which" "rust-analyzer") ) (defun locate-rust-analyzer-ansible-built () "Find rust-analyzer where the ansible playbook built it." (let ((rust-analyzer-path "/opt/rust-analyzer/target/release/rust-analyzer")) (when (file-exists-p rust-analyzer-path) rust-analyzer-path ) ) ) (use-package rust-ts-mode :pin manual :mode ( ("\\.rs\\'" . 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) )) ) :init (add-to-list 'major-mode-remap-alist '(rust-mode . rust-ts-mode)) (add-to-list 'treesit-language-source-alist '(rust "https://github.com/tree-sitter/tree-sitter-rust")) (unless (treesit-ready-p 'yaml) (treesit-install-language-grammar 'rust)) :config ;; Add keybindings for interacting with Cargo (use-package cargo :hook (rust-ts-mode . cargo-minor-mode)) ) (use-package toml-ts-mode :ensure nil :pin manual :mode ( ("\\.toml\\'" . toml-ts-mode) ) :commands (toml-ts-mode) :init (add-to-list 'treesit-language-source-alist '(toml "https://github.com/tree-sitter/tree-sitter-toml")) (unless (treesit-ready-p 'toml) (treesit-install-language-grammar 'toml)) ) (provide 'lang-rust)