branch: elpa/rust-mode
commit 649f492e073f449fb5e5557c4f7ff30ada8800e2
Author: Jonas Bernoulli <[email protected]>
Commit: brotzeit <[email protected]>
Create rust-cargo.el from existing code
---
Makefile | 1 +
rust-cargo.el | 103 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
rust-mode.el | 88 +------------------------------------------------
3 files changed, 105 insertions(+), 87 deletions(-)
diff --git a/Makefile b/Makefile
index ded2945..aaa652c 100644
--- a/Makefile
+++ b/Makefile
@@ -10,6 +10,7 @@ EMACS ?= emacs
EMACS_ARGS ?=
ELS = rust-mode.el
+ELS += rust-cargo.el
ELS += rust-compile.el
ELS += rust-playpen.el
ELS += rust-rustfmt.el
diff --git a/rust-cargo.el b/rust-cargo.el
new file mode 100644
index 0000000..30ba611
--- /dev/null
+++ b/rust-cargo.el
@@ -0,0 +1,103 @@
+;;; rust-cargo.el --- Support for cargo -*-lexical-binding: t-*-
+;;; Commentary:
+
+;; This library implements support for running `cargo'.
+
+;;; Code:
+
+(require 'json)
+
+;;; Options
+
+(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."
+ :type 'boolean
+ :group 'rust-mode)
+
+;;; Buffer Project
+
+(defvar-local rust-buffer-project nil)
+
+(defun rust-buffer-project ()
+ "Get project root if possible."
+ (with-temp-buffer
+ (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)
+ (let ((output (json-read)))
+ (cdr (assoc-string "root" output))))))
+
+(defun rust-update-buffer-project ()
+ (setq-local rust-buffer-project (rust-buffer-project)))
+
+(defun rust-maybe-initialize-buffer-project ()
+ (setq-local rust-buffer-project nil)
+ (when rust-always-locate-project-on-open
+ (rust-update-buffer-project)))
+
+(add-hook 'rust-mode-hook 'rust-maybe-initialize-buffer-project)
+
+;;; Internal
+
+(defun rust--compile (format-string &rest args)
+ (when (null rust-buffer-project)
+ (rust-update-buffer-project))
+ (let ((default-directory
+ (or (and rust-buffer-project
+ (file-name-directory rust-buffer-project))
+ default-directory)))
+ (compile (apply #'format format-string args))))
+
+;;; Commands
+
+(defun rust-check ()
+ "Compile using `cargo check`"
+ (interactive)
+ (rust--compile "%s check" rust-cargo-bin))
+
+(defun rust-compile ()
+ "Compile using `cargo build`"
+ (interactive)
+ (rust--compile "%s build" rust-cargo-bin))
+
+(defun rust-compile-release ()
+ "Compile using `cargo build --release`"
+ (interactive)
+ (rust--compile "%s build --release" rust-cargo-bin))
+
+(defun rust-run ()
+ "Run using `cargo run`"
+ (interactive)
+ (rust--compile "%s run" rust-cargo-bin))
+
+(defun rust-run-release ()
+ "Run using `cargo run --release`"
+ (interactive)
+ (rust--compile "%s run --release" rust-cargo-bin))
+
+(defun rust-test ()
+ "Test using `cargo test`"
+ (interactive)
+ (rust--compile "%s test" rust-cargo-bin))
+
+(defun rust-run-clippy ()
+ "Run `cargo clippy'."
+ (interactive)
+ (when (null rust-buffer-project)
+ (rust-update-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 " ")))
+ (rust--compile compile-command)))
+
+;;; _
+(provide 'rust-cargo)
+;;; rust-cargo.el ends here
diff --git a/rust-mode.el b/rust-mode.el
index 0b1f5d1..d4d61d7 100644
--- a/rust-mode.el
+++ b/rust-mode.el
@@ -17,16 +17,12 @@
(eval-when-compile (require 'rx))
-(require 'json)
(require 'thingatpt)
(defvar electric-pair-inhibit-predicate)
(defvar electric-pair-skip-self)
(defvar electric-indent-chars)
-(defvar rust-buffer-project nil)
-(make-variable-buffer-local 'rust-buffer-project)
-
;;; Customization
(defgroup rust-mode nil
@@ -59,16 +55,6 @@ When nil, `where' will be aligned with `fn' or `trait'."
:safe #'booleanp
: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."
- :type 'boolean
- :group 'rust-mode)
-
(defcustom rust-indent-return-type-to-arguments t
"Indent a line starting with the `->' (RArrow) following a function, aligning
to the function arguments. When nil, `->' will be indented one level."
@@ -266,11 +252,7 @@ Use idomenu (imenu with `ido-mode') for best mileage.")
(add-hook 'before-save-hook 'rust-before-save-hook nil t)
(add-hook 'after-save-hook 'rust-after-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))
@@ -1569,45 +1551,6 @@ This is written mainly to be used as
`end-of-defun-function' for Rust."
;; There is no opening brace, so consider the whole buffer to be one
"defun"
(goto-char (point-max))))
-(defun rust--compile (format-string &rest args)
- (when (null rust-buffer-project)
- (rust-update-buffer-project))
- (let ((default-directory
- (or (and rust-buffer-project
- (file-name-directory rust-buffer-project))
- default-directory)))
- (compile (apply #'format format-string args))))
-
-(defun rust-check ()
- "Compile using `cargo check`"
- (interactive)
- (rust--compile "%s check" rust-cargo-bin))
-
-(defun rust-compile ()
- "Compile using `cargo build`"
- (interactive)
- (rust--compile "%s build" rust-cargo-bin))
-
-(defun rust-compile-release ()
- "Compile using `cargo build --release`"
- (interactive)
- (rust--compile "%s build --release" rust-cargo-bin))
-
-(defun rust-run ()
- "Run using `cargo run`"
- (interactive)
- (rust--compile "%s run" rust-cargo-bin))
-
-(defun rust-run-release ()
- "Run using `cargo run --release`"
- (interactive)
- (rust--compile "%s run --release" rust-cargo-bin))
-
-(defun rust-test ()
- "Test using `cargo test`"
- (interactive)
- (rust--compile "%s test" rust-cargo-bin))
-
;;; Secondary Commands
(defun rust-promote-module-into-dir ()
@@ -1632,35 +1575,6 @@ 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 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 " ")))
- (rust--compile compile-command)))
-
-;;; Utilities
-
-(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 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)
- (let ((output (json-read)))
- (cdr (assoc-string "root" output))))))
-
-;;; Secondary Commands
-
(defun rust-insert-dbg ()
"Insert the dbg! macro."
(cond ((region-active-p)