branch: elpa/rust-mode
commit 1b3db883bccb0956e5f05b7312485b2b7a5fa70f
Author: Roey Darwish Dror <[email protected]>
Commit: Nathan Moreau <[email protected]>
Add a function wrap and unwrap with the dbg! macro.
---
rust-mode-tests.el | 48 ++++++++++++++++++++++++++++++++++++++++++++++++
rust-mode.el | 38 ++++++++++++++++++++++++++++++++++++++
2 files changed, 86 insertions(+)
diff --git a/rust-mode-tests.el b/rust-mode-tests.el
index 42b7a44..343b6c1 100644
--- a/rust-mode-tests.el
+++ b/rust-mode-tests.el
@@ -245,6 +245,10 @@ fn bar() { }"
/// even more.
fn bar() { }" 14 85))
+(defun test-dbg-wrap (initial expected position &optional end)
+ (with-temp-buffer
+ (insert initial)))
+
(defun test-auto-fill (initial position inserted expected)
(rust-test-manip-code
initial
@@ -3138,6 +3142,50 @@ impl Two<'a> {
"Foo" font-lock-type-face
"in" font-lock-keyword-face)))
+(ert-deftest rust-test-dbg-wrap-symbol ()
+ (rust-test-manip-code
+ "let x = add(first, second);"
+ 15
+ #'rust-dbg-wrap-or-unwrap
+ "let x = add(dbg!(first), second);"))
+
+(ert-deftest rust-test-dbg-wrap-symbol-unbalanced ()
+ (rust-test-manip-code
+ "let x = add((first, second);"
+ 14
+ #'rust-dbg-wrap-or-unwrap
+ "let x = add((dbg!(first), second);"))
+
+(ert-deftest rust-test-dbg-wrap-region ()
+ (rust-test-manip-code
+ "let x = add(first, second);"
+ 9
+ (lambda ()
+ (transient-mark-mode 1)
+ (push-mark nil t t)
+ (goto-char 26)
+ (rust-dbg-wrap-or-unwrap))
+ "let x = dbg!(add(first, second));"))
+
+(defun rust-test-dbg-unwrap (position)
+ (rust-test-manip-code
+ "let x = add(dbg!(first), second);"
+ position
+ #'rust-dbg-wrap-or-unwrap
+ "let x = add(first, second);"))
+
+(ert-deftest rust-test-dbg-uwnrap-within ()
+ (rust-test-dbg-unwrap 19))
+
+(ert-deftest rust-test-dbg-uwnrap-on-paren ()
+ (rust-test-dbg-unwrap 17))
+
+(ert-deftest rust-test-dbg-uwnrap-on-dbg-middle ()
+ (rust-test-dbg-unwrap 15))
+
+(ert-deftest rust-test-dbg-uwnrap-on-dbg-start ()
+ (rust-test-dbg-unwrap 13))
+
(when (executable-find rust-cargo-bin)
(ert-deftest rust-test-project-located ()
(lexical-let* ((test-dir (expand-file-name "test-project/"
default-directory))
diff --git a/rust-mode.el b/rust-mode.el
index dd50f18..42b8d82 100644
--- a/rust-mode.el
+++ b/rust-mode.el
@@ -19,6 +19,7 @@
(require 'url-vars))
(require 'json)
+(require 'thingatpt)
(defvar electric-pair-inhibit-predicate)
(defvar electric-pair-skip-self)
@@ -1582,6 +1583,7 @@ Return the created process."
(defvar rust-mode-map
(let ((map (make-sparse-keymap)))
(define-key map (kbd "C-c C-f") 'rust-format-buffer)
+ (define-key map (kbd "C-c d") 'rust-dbg-wrap-or-unwrap)
map)
"Keymap for Rust major mode.")
@@ -1800,6 +1802,42 @@ visit the new file."
(let ((output (json-read)))
(cdr (assoc-string "root" output))))))
+(defun rust-insert-dbg ()
+ "Insert the dbg! macro."
+ (cond ((region-active-p)
+ (insert-parentheses)
+ (backward-char 1))
+ (t
+ (insert "(")
+ (forward-sexp)
+ (insert ")")
+ (backward-sexp)))
+ (insert "dbg!"))
+
+;;;###autoload
+(defun rust-dbg-wrap-or-unwrap ()
+ "Either remove or add the dbg! macro."
+ (interactive)
+ (save-excursion
+ (if (region-active-p)
+ (rust-insert-dbg)
+
+ (let ((beginning-of-symbol (ignore-errors (beginning-of-thing 'symbol))))
+ (when beginning-of-symbol
+ (goto-char beginning-of-symbol)))
+
+ (let ((dbg-point (save-excursion
+ (or (and (looking-at-p "dbg!") (+ 4 (point)))
+ (ignore-errors
+ (while (not (rust-looking-back-str "dbg!"))
+ (backward-up-list))
+ (point))))))
+ (cond (dbg-point
+ (goto-char dbg-point)
+ (delete-char -4)
+ (delete-pair))
+ (t (rust-insert-dbg)))))))
+
(provide 'rust-mode)
;;; rust-mode.el ends here