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

90 lines
3.6 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) (locate-rust-analyzer-in-path))))
2023-08-18 21:47:17 +00:00
(let ((first-non-nil-path (seq-find (lambda (elt) elt) rust-analyzer-paths)))
first-non-nil-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
)
)
)
(defun locate-rust-analyzer-in-path ()
"Find rust-analyzer in $PATH."
(executable-find "rust-analyzer")
)
2023-05-27 19:50:52 +00:00
(use-package rust-ts-mode
:pin manual
:mode (
("\\.rs\\'" . rust-ts-mode)
)
2023-09-09 05:44:01 +00:00
:commands (rust-ts-mode)
2022-10-20 04:03:56 +00:00
:hook (
2023-05-27 04:22:26 +00:00
(rust-ts-mode . (lambda ()
(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-to-list 'eglot-server-programs `(rust-ts-mode . (,rust-analyzer-command :initializationOptions (:imports (:granularity (:enforce t :group "item")
:merge (:glob nil)
:prefix "self")
:inlayHints (:maxLength nil)
:workspace (:symbol (:search (:limit 1024)))))))
)
)
(add-hook 'before-save-hook 'eglot-format-buffer nil 'local)
))
2022-10-20 04:03:56 +00:00
)
: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
)
2023-05-27 22:07:14 +00:00
(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))
)
;; 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
2023-09-09 08:41:24 +00:00
;; . (:rust-analyzer (:cargo (:noDefaultFeatures t :features ["compare" "tracing"]))))
;; ))
;; )
2023-05-27 22:07:14 +00:00
2022-10-20 04:03:56 +00:00
(provide 'lang-rust)