2022-10-20 04:03:56 +00:00
|
|
|
(require 'common-lsp)
|
|
|
|
(require 'util-tree-sitter)
|
|
|
|
|
|
|
|
(defun locate-rust-analyzer ()
|
2023-05-02 00:04:59 +00:00
|
|
|
"Find rust-analyzer."
|
2023-09-09 06:47:20 +00:00
|
|
|
(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
|
2023-05-02 00:04:59 +00:00
|
|
|
)
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
(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")
|
|
|
|
)
|
|
|
|
|
2023-05-02 00:04:59 +00:00
|
|
|
(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
|
|
|
|
)
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
2023-09-09 06:47:20 +00:00
|
|
|
(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 ()
|
2023-09-09 06:47:20 +00:00
|
|
|
(eglot-ensure)
|
2023-09-21 20:56:59 +00:00
|
|
|
;; Disable on-type formatting which was incorrectly injecting parenthesis into my code.
|
|
|
|
(make-local-variable 'eglot-ignored-server-capabilities)
|
|
|
|
(add-to-list 'eglot-ignored-server-capabilities :documentOnTypeFormattingProvider)
|
|
|
|
;; Configure initialization options
|
2023-09-09 06:47:20 +00:00
|
|
|
(let ((rust-analyzer-command (locate-rust-analyzer)))
|
|
|
|
(when rust-analyzer-command
|
2023-09-09 07:47:34 +00:00
|
|
|
;; (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")
|
2023-10-12 18:28:28 +00:00
|
|
|
))))
|
2023-09-09 06:47:20 +00:00
|
|
|
)
|
|
|
|
)
|
|
|
|
(add-hook 'before-save-hook 'eglot-format-buffer nil 'local)
|
|
|
|
))
|
2022-10-20 04:03:56 +00:00
|
|
|
)
|
2023-05-27 15:53:27 +00:00
|
|
|
:init
|
|
|
|
(add-to-list 'major-mode-remap-alist '(rust-mode . rust-ts-mode))
|
2023-05-27 15:57:37 +00:00
|
|
|
(add-to-list 'treesit-language-source-alist '(rust "https://github.com/tree-sitter/tree-sitter-rust"))
|
2024-06-02 16:09:45 +00:00
|
|
|
(unless (treesit-ready-p 'rust) (treesit-install-language-grammar 'rust))
|
2023-05-27 16:09:15 +00:00
|
|
|
: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))
|
|
|
|
)
|
|
|
|
|
2023-09-09 06:47:20 +00:00
|
|
|
;; 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-09-09 06:47:20 +00:00
|
|
|
;; ))
|
|
|
|
;; )
|
|
|
|
|
2023-05-27 22:07:14 +00:00
|
|
|
|
2022-10-20 04:03:56 +00:00
|
|
|
(provide 'lang-rust)
|