summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--rust-mode-tests.el50
-rw-r--r--rust-utils.el88
2 files changed, 98 insertions, 40 deletions
diff --git a/rust-mode-tests.el b/rust-mode-tests.el
index 3b23046..7b791b6 100644
--- a/rust-mode-tests.el
+++ b/rust-mode-tests.el
@@ -45,14 +45,16 @@
(put 'rust-compare-code-after-manip 'ert-explainer
'rust-test-explain-bad-manip)
-(defun rust-test-manip-code (original point-pos manip-func expected)
+(defun rust-test-manip-code (original manip-pos manip-func expected &optional final-pos)
(with-temp-buffer
(rust-mode)
(insert original)
- (goto-char point-pos)
+ (goto-char manip-pos)
(funcall manip-func)
(should (rust-compare-code-after-manip
- original point-pos manip-func expected (buffer-string)))))
+ original manip-pos manip-func expected (buffer-string)))
+ (if final-pos
+ (should (equal (point) final-pos)))))
(defmacro rust-test-with-standard-fill-settings (&rest body)
(declare (indent defun))
@@ -3452,14 +3454,49 @@ impl Two<'a> {
"let x = add(first, second);"
15
#'rust-dbg-wrap-or-unwrap
- "let x = add(dbg!(first), second);"))
+ "let x = add(dbg!(first), second);"
+ 24))
+
+(ert-deftest rust-test-dbg-wrap-empty-line ()
+ (rust-test-manip-code
+ "let a = 1;
+
+let b = 1;"
+ 12
+ #'rust-dbg-wrap-or-unwrap
+ "let a = 1;
+dbg!()
+let b = 1;"
+ 17))
+
+(ert-deftest rust-test-dbg-wrap-empty-before-comment ()
+ (rust-test-manip-code
+ "let a = 1;
+// comment
+let b = 1;"
+ 12
+ #'rust-dbg-wrap-or-unwrap
+ "let a = 1;
+dbg!()// comment
+let b = 1;"
+ 17)
+ ;; between statements and comments
+ (rust-test-manip-code
+ "let a = 1;// comment
+let b = 1;"
+ 11
+ #'rust-dbg-wrap-or-unwrap
+ "let a = 1;dbg!()// comment
+let b = 1;"
+ 16))
(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);"))
+ "let x = add((dbg!(first), second);"
+ 25))
(ert-deftest rust-test-dbg-wrap-region ()
(rust-test-manip-code
@@ -3470,7 +3507,8 @@ impl Two<'a> {
(push-mark nil t t)
(goto-char 26)
(rust-dbg-wrap-or-unwrap))
- "let x = dbg!(add(first, second));"))
+ "let x = dbg!(add(first, second));"
+ 33))
(defun rust-test-dbg-unwrap (position)
(rust-test-manip-code
diff --git a/rust-utils.el b/rust-utils.el
index 41f1ba8..29590d0 100644
--- a/rust-utils.el
+++ b/rust-utils.el
@@ -37,45 +37,65 @@ visit the new file."
;;; dbg! macro
(defun rust-insert-dbg ()
- "Insert the dbg! macro."
- (cond ((region-active-p)
- (when (< (mark) (point))
- (exchange-point-and-mark))
- (let ((old-point (point)))
- (insert-parentheses)
- (goto-char old-point)))
- (t
- (when (rust-in-str)
- (up-list -1 t t))
- (insert "(")
- (forward-sexp)
- (insert ")")
- (backward-sexp)))
- (insert "dbg!"))
+ "Insert the dbg! macro. Move cursor to the end of macro."
+ (when (rust-in-str)
+ (up-list -1 t t))
+ (insert "(")
+ (forward-sexp)
+ (insert ")")
+ (backward-sexp)
+ (insert "dbg!")
+ (forward-sexp))
+
+(defun rust-insert-dbg-region ()
+ "Insert the dbg! macro around a region. Move cursor to the end of macro."
+ (when (< (mark) (point))
+ (exchange-point-and-mark))
+ (let ((old-point (point)))
+ (insert-parentheses)
+ (goto-char old-point))
+ (insert "dbg!")
+ (forward-sexp))
+
+(defun rust-insert-dbg-alone ()
+ "Insert the dbg! macro alone. Move cursor in between the brackets."
+ (insert "dbg!()")
+ (backward-char))
;;;###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)))))))
+
+ (cond
+
+ ;; region
+ ((region-active-p)
+ (rust-insert-dbg-region))
+
+ ;; alone
+ ((or (looking-at-p " *$") (looking-at-p " *//.*"))
+ (rust-insert-dbg-alone))
+
+ ;; symbol
+ (t
+ (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)))))
+ )
+)
(defun rust-toggle-mutability ()
"Toggles the mutability of the variable defined on the current line"