Skip to content

Commit

Permalink
rust-dbg-wrap-or-unwrap: adjust cursor position after insertion
Browse files Browse the repository at this point in the history
  • Loading branch information
micl2e2 committed Jul 23, 2023
1 parent 378138e commit 36c5dd9
Showing 1 changed file with 11 additions and 9 deletions.
20 changes: 11 additions & 9 deletions rust-utils.el
Original file line number Diff line number Diff line change
Expand Up @@ -37,44 +37,46 @@ visit the new file."
;;; dbg! macro

(defun rust-insert-dbg ()
"Insert the dbg! macro."
"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!"))
(insert "dbg!")
(forward-sexp))

(defun rust-insert-dbg-region ()
"Insert the dbg! macro around a 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!"))
(insert "dbg!")
(forward-sexp))

(defun rust-insert-dbg-alone ()
"Insert the dbg! macro 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)

(cond
;; alone
((or (looking-at-p " *$") (looking-at-p " *//.*"))
(rust-insert-dbg-alone))

;; 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))))
Expand Down

0 comments on commit 36c5dd9

Please sign in to comment.