machine_setup/ansible/roles/emacs/files/lang-rust.el

52 lines
1.7 KiB
EmacsLisp
Raw Normal View History

2022-10-20 04:03:56 +00:00
(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-nill-path (seq-find (lambda (elt) elt) rust-analyzer-paths)))
first-non-nill-path
)
)
)
(defun locate-rust-analyzer-rustup ()
2022-10-20 04:03:56 +00:00
"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
)
)
)
2022-10-20 04:03:56 +00:00
(use-package rust-mode
2023-05-27 04:22:26 +00:00
:pin nongnu
2022-10-20 04:03:56 +00:00
:mode "\\.rs\\'"
:hook (
2023-05-27 04:22:26 +00:00
(rust-ts-mode . (lambda ()
2022-10-20 04:03:56 +00:00
(eglot-ensure)
(let ((rust-analyzer-command (locate-rust-analyzer)))
(when rust-analyzer-command
2023-05-27 04:22:26 +00:00
(add-to-list 'eglot-server-programs (cons 'rust-ts-mode (list rust-analyzer-command)))
2022-10-20 04:03:56 +00:00
)
)
(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
2022-10-20 04:03:56 +00:00
;; Add keybindings for interacting with Cargo
(use-package cargo
2023-05-27 04:22:26 +00:00
:hook (rust-ts-mode . cargo-minor-mode))
2022-10-20 04:03:56 +00:00
)
(provide 'lang-rust)