summaryrefslogtreecommitdiff
path: root/rust-utils.el
diff options
context:
space:
mode:
Diffstat (limited to 'rust-utils.el')
-rw-r--r--rust-utils.el88
1 files changed, 54 insertions, 34 deletions
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"