From 72c479bd0d91befe9b2a77636c6b2731163ffcd6 Mon Sep 17 00:00:00 2001 From: Aankhen Date: Mon, 26 Jun 2017 16:14:27 +0530 Subject: Add `rust-run-clippy' and `rust-buffer-project' with testing paraphernalia. --- rust-mode-tests.el | 9 +++++++++ rust-mode.el | 37 ++++++++++++++++++++++++++++++++++++- test-project/Cargo.toml | 1 + 3 files changed, 46 insertions(+), 1 deletion(-) create mode 100644 test-project/Cargo.toml diff --git a/rust-mode-tests.el b/rust-mode-tests.el index 6d58906..42792ac 100644 --- a/rust-mode-tests.el +++ b/rust-mode-tests.el @@ -2650,6 +2650,15 @@ extern \"rust-intrinsic\" fn five() { "four" "five")))) +(ert-deftest rust-test-project-located () + (lexical-let* ((this-dir default-directory) + (test-dir (expand-file-name "test-project" this-dir)) + (manifest-file (expand-file-name "Cargo.toml" test-dir))) + (find-file (expand-file-name "test-project/foo.rs")) + (unwind-protect + (should (equal (expand-file-name (rust-buffer-project)) manifest-file)) + (kill-buffer)))) + ;; If electric-pair-mode is available, load it and run the tests that use it. If not, ;; no error--the tests will be skipped. (require 'elec-pair nil t) 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 diff --git a/test-project/Cargo.toml b/test-project/Cargo.toml new file mode 100644 index 0000000..f413f6b --- /dev/null +++ b/test-project/Cargo.toml @@ -0,0 +1 @@ +1 -- cgit v1.2.3 From c00c8a935249d1cbd8b31c7bfd9656f743188172 Mon Sep 17 00:00:00 2001 From: Aankhen Date: Mon, 26 Jun 2017 16:41:24 +0530 Subject: Require `json'. --- rust-mode.el | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/rust-mode.el b/rust-mode.el index 0e18138..3d002f1 100644 --- a/rust-mode.el +++ b/rust-mode.el @@ -16,7 +16,8 @@ (eval-when-compile (require 'rx) (require 'compile) - (require 'url-vars)) + (require 'url-vars) + (require 'json)) (defvar electric-pair-inhibit-predicate) (defvar electric-indent-chars) -- cgit v1.2.3 From 9afe9972ca97eaa1fdeae86a691b2c448552843c Mon Sep 17 00:00:00 2001 From: Aankhen Date: Mon, 26 Jun 2017 18:22:55 +0530 Subject: Declare `rust-buffer-project' and require `json' at runtime. --- rust-mode.el | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/rust-mode.el b/rust-mode.el index 3d002f1..a1d98b0 100644 --- a/rust-mode.el +++ b/rust-mode.el @@ -16,12 +16,16 @@ (eval-when-compile (require 'rx) (require 'compile) - (require 'url-vars) - (require 'json)) + (require 'url-vars)) + +(require 'json) (defvar electric-pair-inhibit-predicate) (defvar electric-indent-chars) +(defvar rust-buffer-project) +(make-variable-buffer-local 'rust-buffer-project) + ;; for GNU Emacs < 24.3 (eval-when-compile (unless (fboundp 'setq-local) -- cgit v1.2.3 From b4077f8be2163b71b33a12932ef1013874646a9c Mon Sep 17 00:00:00 2001 From: Aankhen Date: Mon, 26 Jun 2017 18:37:52 +0530 Subject: Add `rust-cargo-bin' custom variable. --- rust-mode.el | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/rust-mode.el b/rust-mode.el index a1d98b0..2c58545 100644 --- a/rust-mode.el +++ b/rust-mode.el @@ -149,6 +149,11 @@ function or trait. When nil, where will be aligned with fn or trait." :type 'string :group 'rust-mode) +(defcustom rust-cargo-bin "cargo" + "Path to cargo executable." + :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." @@ -1569,7 +1574,7 @@ visit the new file." (interactive) (when (null rust-buffer-project) (rust-update-buffer-project)) - (let* ((args (list "cargo" "clippy" (concat "--manifest-path=" rust-buffer-project))) + (let* ((args (list rust-cargo-bin "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 " "))) @@ -1581,7 +1586,7 @@ visit the new file." (defun rust-buffer-project () "Get project root if possible." (with-temp-buffer - (let ((ret (call-process "cargo" nil t nil "locate-project"))) + (let ((ret (call-process rust-cargo-bin nil t nil "locate-project"))) (when (/= ret 0) (error "`cargo locate-project' returned %s status: %s" ret (buffer-string))) (goto-char 0) -- cgit v1.2.3 From d2bb17ce165242a28c0fe22ae2a98f0ed79d76fe Mon Sep 17 00:00:00 2001 From: Aankhen Date: Mon, 26 Jun 2017 18:38:14 +0530 Subject: Skip `rust-test-project-located' without cargo and avoid `find-file' in test. --- rust-mode-tests.el | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/rust-mode-tests.el b/rust-mode-tests.el index 42792ac..1e03795 100644 --- a/rust-mode-tests.el +++ b/rust-mode-tests.el @@ -2651,13 +2651,11 @@ extern \"rust-intrinsic\" fn five() { "five")))) (ert-deftest rust-test-project-located () - (lexical-let* ((this-dir default-directory) - (test-dir (expand-file-name "test-project" this-dir)) + (skip-unless (executable-find "cargo")) + (lexical-let* ((test-dir (expand-file-name "test-project" default-directory)) (manifest-file (expand-file-name "Cargo.toml" test-dir))) - (find-file (expand-file-name "test-project/foo.rs")) - (unwind-protect - (should (equal (expand-file-name (rust-buffer-project)) manifest-file)) - (kill-buffer)))) + (let ((default-directory test-dir)) + (should (equal (expand-file-name (rust-buffer-project)) manifest-file))))) ;; If electric-pair-mode is available, load it and run the tests that use it. If not, ;; no error--the tests will be skipped. -- cgit v1.2.3 From 954221f39982a5fe2b60077600c2ed7c8d02c086 Mon Sep 17 00:00:00 2001 From: Aankhen Date: Mon, 26 Jun 2017 18:46:55 +0530 Subject: Define `rust-test-project-located' conditionally instead of using `skip-unless'. --- rust-mode-tests.el | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/rust-mode-tests.el b/rust-mode-tests.el index 1e03795..b1d51c9 100644 --- a/rust-mode-tests.el +++ b/rust-mode-tests.el @@ -2650,12 +2650,12 @@ extern \"rust-intrinsic\" fn five() { "four" "five")))) -(ert-deftest rust-test-project-located () - (skip-unless (executable-find "cargo")) - (lexical-let* ((test-dir (expand-file-name "test-project" default-directory)) - (manifest-file (expand-file-name "Cargo.toml" test-dir))) - (let ((default-directory test-dir)) - (should (equal (expand-file-name (rust-buffer-project)) manifest-file))))) +(when (executable-find rust-cargo-bin) + (ert-deftest rust-test-project-located () + (lexical-let* ((test-dir (expand-file-name "test-project" default-directory)) + (manifest-file (expand-file-name "Cargo.toml" test-dir))) + (let ((default-directory test-dir)) + (should (equal (expand-file-name (rust-buffer-project)) manifest-file)))))) ;; If electric-pair-mode is available, load it and run the tests that use it. If not, ;; no error--the tests will be skipped. -- cgit v1.2.3 From 128601b687e7802cf28a1eadd292c52303e92d39 Mon Sep 17 00:00:00 2001 From: Aankhen Date: Sun, 16 Jul 2017 09:47:46 +0530 Subject: Improve content of `test-project/Cargo.toml'. --- test-project/Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test-project/Cargo.toml b/test-project/Cargo.toml index f413f6b..f741b14 100644 --- a/test-project/Cargo.toml +++ b/test-project/Cargo.toml @@ -1 +1 @@ -1 +# Dummy file needed for test -- cgit v1.2.3