summaryrefslogtreecommitdiff
path: root/rust-utils.el
diff options
context:
space:
mode:
authorMichael Lee <micl2e2@proton.me>2023-08-04 13:52:32 +0800
committerMichael Lee <micl2e2@proton.me>2023-08-04 13:52:32 +0800
commit527e375ac18bc0742f7c62193c145c9e00f928e5 (patch)
tree421bcf9a52b6cd719b4d1cf734d0bea09db92fbf /rust-utils.el
parent601824cf552d09db62f0cef42c00dc85bd728b04 (diff)
downloadrust-mode-527e375ac18bc0742f7c62193c145c9e00f928e5.tar.gz
dbg! insertion:
- Change rust-insert-dbg to ...-sexp - Handle the cases where forwarding is impossible - Add tests
Diffstat (limited to 'rust-utils.el')
-rw-r--r--rust-utils.el29
1 files changed, 20 insertions, 9 deletions
diff --git a/rust-utils.el b/rust-utils.el
index 29590d0..cb55172 100644
--- a/rust-utils.el
+++ b/rust-utils.el
@@ -36,16 +36,27 @@ visit the new file."
;;; dbg! macro
-(defun rust-insert-dbg ()
- "Insert the dbg! macro. Move cursor to the end of macro."
+(defun rust-insert-dbg-sexp ()
+ "Insert the dbg! macro around a sexp if possible, insert at current position
+if not. 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))
+ (setq safe-to-forward t)
+ (save-excursion
+ (condition-case nil
+ (forward-sexp)
+ (error (setq safe-to-forward nil)
+ nil)))
+ (cond
+ ((not safe-to-forward)
+ (rust-insert-dbg-alone))
+ (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."
@@ -93,7 +104,7 @@ visit the new file."
(goto-char dbg-point)
(delete-char -4)
(delete-pair))
- (t (rust-insert-dbg)))))
+ (t (rust-insert-dbg-sexp)))))
)
)