(require 'common-lsp)
(require 'util-tree-sitter)

(defun locate-rust-analyzer ()
  "Find rust-analyzer through rustup."
  (run-command-in-directory nil "rustup" "which" "rust-analyzer")
  )

(use-package rust-mode
  :mode "\\.rs\\'"
  :hook (
         (rust-mode . (lambda ()
                        (eglot-ensure)
                        (let ((rust-analyzer-command (locate-rust-analyzer)))
                          (when rust-analyzer-command
                            (add-to-list 'eglot-server-programs (cons 'rust-mode (list rust-analyzer-command)))
                            )
                          )
                        (when-linux
                          (tree-sitter-hl-mode +1)
                          )
                        (add-hook 'before-save-hook 'eglot-format-buffer nil 'local)
                        ))
         )
  :config
  ;; Add keybindings for interacting with Cargo
  (use-package cargo
    :hook (rust-mode . cargo-minor-mode))
  )

(provide 'lang-rust)
