Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve the usability of rust-dbg-wrap-or-unwrap #498

Merged
merged 8 commits into from
Aug 3, 2023
50 changes: 44 additions & 6 deletions rust-mode-tests.el
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down
88 changes: 54 additions & 34 deletions rust-utils.el
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down