diff options
| author | Aankhen <Aankhen@users.noreply.github.com> | 2017-06-26 16:14:27 +0530 |
|---|---|---|
| committer | Aankhen <Aankhen@users.noreply.github.com> | 2017-07-13 11:46:06 +0530 |
| commit | 72c479bd0d91befe9b2a77636c6b2731163ffcd6 (patch) | |
| tree | 032d13362f28ab6fc7fae2a7f945822bb0c49f45 /rust-mode.el | |
| parent | 60a1f36f4111e825d20d9c3aed561981c470806a (diff) | |
| download | rust-mode-72c479bd0d91befe9b2a77636c6b2731163ffcd6.tar.gz | |
Add `rust-run-clippy' and `rust-buffer-project' with testing paraphernalia.
Diffstat (limited to 'rust-mode.el')
| -rw-r--r-- | rust-mode.el | 37 |
1 files changed, 36 insertions, 1 deletions
diff --git a/rust-mode.el b/rust-mode.el index f3f799c..0e18138 100644 --- a/rust-mode.el +++ b/rust-mode.el @@ -144,6 +144,12 @@ function or trait. When nil, where will be aligned with fn or trait." :type 'string :group 'rust-mode) +(defcustom rust-always-locate-project-on-open nil + "Whether to run `cargo locate-project' every time `rust-mode' + is activated." + :type 'boolean + :group 'rust-mode) + (defface rust-unsafe-face '((t :inherit font-lock-warning-face)) "Face for the `unsafe' keyword." @@ -1411,7 +1417,12 @@ This is written mainly to be used as `end-of-defun-function' for Rust." (setq-local compile-command "cargo build") - (add-hook 'before-save-hook 'rust--before-save-hook nil t)) + (add-hook 'before-save-hook 'rust--before-save-hook nil t) + + (setq-local rust-buffer-project nil) + + (when rust-always-locate-project-on-open + (rust-update-buffer-project))) ;;;###autoload (add-to-list 'auto-mode-alist '("\\.rs\\'" . rust-mode)) @@ -1548,6 +1559,30 @@ visit the new file." (rename-file filename new-name 1) (set-visited-file-name new-name)))))) +(defun rust-run-clippy () + "Run `cargo clippy'." + (interactive) + (when (null rust-buffer-project) + (rust-update-buffer-project)) + (let* ((args (list "cargo" "clippy" (concat "--manifest-path=" rust-buffer-project))) + ;; set `compile-command' temporarily so `compile' doesn't + ;; clobber the existing value + (compile-command (mapconcat #'shell-quote-argument args " "))) + (compile compile-command))) + +(defun rust-update-buffer-project () + (setq-local rust-buffer-project (rust-buffer-project))) + +(defun rust-buffer-project () + "Get project root if possible." + (with-temp-buffer + (let ((ret (call-process "cargo" nil t nil "locate-project"))) + (when (/= ret 0) + (error "`cargo locate-project' returned %s status: %s" ret (buffer-string))) + (goto-char 0) + (let ((output (json-read))) + (cdr (assoc-string "root" output)))))) + (provide 'rust-mode) ;;; rust-mode.el ends here |
