Skip to content

Commit 527e375

Browse files
committed
dbg! insertion:
- Change rust-insert-dbg to ...-sexp - Handle the cases where forwarding is impossible - Add tests
1 parent 601824c commit 527e375

File tree

2 files changed

+39
-10
lines changed

2 files changed

+39
-10
lines changed

rust-mode-tests.el

+19-1
Original file line numberDiff line numberDiff line change
@@ -3449,14 +3449,32 @@ impl Two<'a> {
34493449
"Foo" font-lock-type-face
34503450
"in" font-lock-keyword-face)))
34513451

3452-
(ert-deftest rust-test-dbg-wrap-symbol ()
3452+
(ert-deftest rust-test-dbg-wrap-sexp ()
3453+
"a valid sexp ahead of current pos"
34533454
(rust-test-manip-code
34543455
"let x = add(first, second);"
34553456
15
34563457
#'rust-dbg-wrap-or-unwrap
34573458
"let x = add(dbg!(first), second);"
34583459
24))
34593460

3461+
(ert-deftest rust-test-dbg-wrap-sexp-fallback ()
3462+
"a invalid sexp ahead of current pos"
3463+
;; inside
3464+
(rust-test-manip-code
3465+
"if let Ok(val) = may_val {}"
3466+
27
3467+
#'rust-dbg-wrap-or-unwrap
3468+
"if let Ok(val) = may_val {dbg!()}"
3469+
32)
3470+
;; before
3471+
(rust-test-manip-code
3472+
"let a = {}"
3473+
9
3474+
#'rust-dbg-wrap-or-unwrap
3475+
"let a = dbg!({})"
3476+
17))
3477+
34603478
(ert-deftest rust-test-dbg-wrap-empty-line ()
34613479
(rust-test-manip-code
34623480
"let a = 1;

rust-utils.el

+20-9
Original file line numberDiff line numberDiff line change
@@ -36,16 +36,27 @@ visit the new file."
3636

3737
;;; dbg! macro
3838

39-
(defun rust-insert-dbg ()
40-
"Insert the dbg! macro. Move cursor to the end of macro."
39+
(defun rust-insert-dbg-sexp ()
40+
"Insert the dbg! macro around a sexp if possible, insert at current position
41+
if not. Move cursor to the end of macro."
4142
(when (rust-in-str)
4243
(up-list -1 t t))
43-
(insert "(")
44-
(forward-sexp)
45-
(insert ")")
46-
(backward-sexp)
47-
(insert "dbg!")
48-
(forward-sexp))
44+
(setq safe-to-forward t)
45+
(save-excursion
46+
(condition-case nil
47+
(forward-sexp)
48+
(error (setq safe-to-forward nil)
49+
nil)))
50+
(cond
51+
((not safe-to-forward)
52+
(rust-insert-dbg-alone))
53+
(t
54+
(insert "(")
55+
(forward-sexp)
56+
(insert ")")
57+
(backward-sexp)
58+
(insert "dbg!")
59+
(forward-sexp))))
4960

5061
(defun rust-insert-dbg-region ()
5162
"Insert the dbg! macro around a region. Move cursor to the end of macro."
@@ -93,7 +104,7 @@ visit the new file."
93104
(goto-char dbg-point)
94105
(delete-char -4)
95106
(delete-pair))
96-
(t (rust-insert-dbg)))))
107+
(t (rust-insert-dbg-sexp)))))
97108
)
98109
)
99110

0 commit comments

Comments
 (0)