Skip to content

Commit 601824c

Browse files
authored
Improve the usability of rust-dbg-wrap-or-unwrap (#498)
* rust-dbg-wrap-or-unwrap: Cut unnecessary conditional branches for region-active-p * rust-dbg-wrap-or-unwrap: Do not using save-excursion anymore * rust-dbg-wrap-or-unwrap: Use cond, allow more other cases. * rust-dbg-wrap-or-unwrap: Introduce rust-insert-dbg-alone * rust-dbg-wrap-or-unwrap: Adjust cursor position after insertion * rust-dbg-wrap-or-unwrap: Add tests for empty line * rust-dbg-wrap-or-unwrap: Test final position in empty line case * rust-dbg-wrap-or-unwrap: Test final cursor position in existing wrap tests
1 parent e443ccf commit 601824c

File tree

2 files changed

+98
-40
lines changed

2 files changed

+98
-40
lines changed

rust-mode-tests.el

+44-6
Original file line numberDiff line numberDiff line change
@@ -45,14 +45,16 @@
4545
(put 'rust-compare-code-after-manip 'ert-explainer
4646
'rust-test-explain-bad-manip)
4747

48-
(defun rust-test-manip-code (original point-pos manip-func expected)
48+
(defun rust-test-manip-code (original manip-pos manip-func expected &optional final-pos)
4949
(with-temp-buffer
5050
(rust-mode)
5151
(insert original)
52-
(goto-char point-pos)
52+
(goto-char manip-pos)
5353
(funcall manip-func)
5454
(should (rust-compare-code-after-manip
55-
original point-pos manip-func expected (buffer-string)))))
55+
original manip-pos manip-func expected (buffer-string)))
56+
(if final-pos
57+
(should (equal (point) final-pos)))))
5658

5759
(defmacro rust-test-with-standard-fill-settings (&rest body)
5860
(declare (indent defun))
@@ -3452,14 +3454,49 @@ impl Two<'a> {
34523454
"let x = add(first, second);"
34533455
15
34543456
#'rust-dbg-wrap-or-unwrap
3455-
"let x = add(dbg!(first), second);"))
3457+
"let x = add(dbg!(first), second);"
3458+
24))
3459+
3460+
(ert-deftest rust-test-dbg-wrap-empty-line ()
3461+
(rust-test-manip-code
3462+
"let a = 1;
3463+
3464+
let b = 1;"
3465+
12
3466+
#'rust-dbg-wrap-or-unwrap
3467+
"let a = 1;
3468+
dbg!()
3469+
let b = 1;"
3470+
17))
3471+
3472+
(ert-deftest rust-test-dbg-wrap-empty-before-comment ()
3473+
(rust-test-manip-code
3474+
"let a = 1;
3475+
// comment
3476+
let b = 1;"
3477+
12
3478+
#'rust-dbg-wrap-or-unwrap
3479+
"let a = 1;
3480+
dbg!()// comment
3481+
let b = 1;"
3482+
17)
3483+
;; between statements and comments
3484+
(rust-test-manip-code
3485+
"let a = 1;// comment
3486+
let b = 1;"
3487+
11
3488+
#'rust-dbg-wrap-or-unwrap
3489+
"let a = 1;dbg!()// comment
3490+
let b = 1;"
3491+
16))
34563492

34573493
(ert-deftest rust-test-dbg-wrap-symbol-unbalanced ()
34583494
(rust-test-manip-code
34593495
"let x = add((first, second);"
34603496
14
34613497
#'rust-dbg-wrap-or-unwrap
3462-
"let x = add((dbg!(first), second);"))
3498+
"let x = add((dbg!(first), second);"
3499+
25))
34633500

34643501
(ert-deftest rust-test-dbg-wrap-region ()
34653502
(rust-test-manip-code
@@ -3470,7 +3507,8 @@ impl Two<'a> {
34703507
(push-mark nil t t)
34713508
(goto-char 26)
34723509
(rust-dbg-wrap-or-unwrap))
3473-
"let x = dbg!(add(first, second));"))
3510+
"let x = dbg!(add(first, second));"
3511+
33))
34743512

34753513
(defun rust-test-dbg-unwrap (position)
34763514
(rust-test-manip-code

rust-utils.el

+54-34
Original file line numberDiff line numberDiff line change
@@ -37,45 +37,65 @@ visit the new file."
3737
;;; dbg! macro
3838

3939
(defun rust-insert-dbg ()
40-
"Insert the dbg! macro."
41-
(cond ((region-active-p)
42-
(when (< (mark) (point))
43-
(exchange-point-and-mark))
44-
(let ((old-point (point)))
45-
(insert-parentheses)
46-
(goto-char old-point)))
47-
(t
48-
(when (rust-in-str)
49-
(up-list -1 t t))
50-
(insert "(")
51-
(forward-sexp)
52-
(insert ")")
53-
(backward-sexp)))
54-
(insert "dbg!"))
40+
"Insert the dbg! macro. Move cursor to the end of macro."
41+
(when (rust-in-str)
42+
(up-list -1 t t))
43+
(insert "(")
44+
(forward-sexp)
45+
(insert ")")
46+
(backward-sexp)
47+
(insert "dbg!")
48+
(forward-sexp))
49+
50+
(defun rust-insert-dbg-region ()
51+
"Insert the dbg! macro around a region. Move cursor to the end of macro."
52+
(when (< (mark) (point))
53+
(exchange-point-and-mark))
54+
(let ((old-point (point)))
55+
(insert-parentheses)
56+
(goto-char old-point))
57+
(insert "dbg!")
58+
(forward-sexp))
59+
60+
(defun rust-insert-dbg-alone ()
61+
"Insert the dbg! macro alone. Move cursor in between the brackets."
62+
(insert "dbg!()")
63+
(backward-char))
5564

5665
;;;###autoload
5766
(defun rust-dbg-wrap-or-unwrap ()
5867
"Either remove or add the dbg! macro."
5968
(interactive)
60-
(save-excursion
61-
(if (region-active-p)
62-
(rust-insert-dbg)
63-
64-
(let ((beginning-of-symbol (ignore-errors (beginning-of-thing 'symbol))))
65-
(when beginning-of-symbol
66-
(goto-char beginning-of-symbol)))
67-
68-
(let ((dbg-point (save-excursion
69-
(or (and (looking-at-p "dbg!") (+ 4 (point)))
70-
(ignore-errors
71-
(while (not (rust-looking-back-str "dbg!"))
72-
(backward-up-list))
73-
(point))))))
74-
(cond (dbg-point
75-
(goto-char dbg-point)
76-
(delete-char -4)
77-
(delete-pair))
78-
(t (rust-insert-dbg)))))))
69+
70+
(cond
71+
72+
;; region
73+
((region-active-p)
74+
(rust-insert-dbg-region))
75+
76+
;; alone
77+
((or (looking-at-p " *$") (looking-at-p " *//.*"))
78+
(rust-insert-dbg-alone))
79+
80+
;; symbol
81+
(t
82+
(let ((beginning-of-symbol (ignore-errors (beginning-of-thing 'symbol))))
83+
(when beginning-of-symbol
84+
(goto-char beginning-of-symbol)))
85+
86+
(let ((dbg-point (save-excursion
87+
(or (and (looking-at-p "dbg!") (+ 4 (point)))
88+
(ignore-errors
89+
(while (not (rust-looking-back-str "dbg!"))
90+
(backward-up-list))
91+
(point))))))
92+
(cond (dbg-point
93+
(goto-char dbg-point)
94+
(delete-char -4)
95+
(delete-pair))
96+
(t (rust-insert-dbg)))))
97+
)
98+
)
7999

80100
(defun rust-toggle-mutability ()
81101
"Toggles the mutability of the variable defined on the current line"

0 commit comments

Comments
 (0)