Skip to content

Commit 1b3db88

Browse files
r-darwishmookid
authored andcommitted
Add a function wrap and unwrap with the dbg! macro.
1 parent 70ff9a0 commit 1b3db88

File tree

2 files changed

+86
-0
lines changed

2 files changed

+86
-0
lines changed

rust-mode-tests.el

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,10 @@ fn bar() { }"
245245
/// even more.
246246
fn bar() { }" 14 85))
247247

248+
(defun test-dbg-wrap (initial expected position &optional end)
249+
(with-temp-buffer
250+
(insert initial)))
251+
248252
(defun test-auto-fill (initial position inserted expected)
249253
(rust-test-manip-code
250254
initial
@@ -3138,6 +3142,50 @@ impl Two<'a> {
31383142
"Foo" font-lock-type-face
31393143
"in" font-lock-keyword-face)))
31403144

3145+
(ert-deftest rust-test-dbg-wrap-symbol ()
3146+
(rust-test-manip-code
3147+
"let x = add(first, second);"
3148+
15
3149+
#'rust-dbg-wrap-or-unwrap
3150+
"let x = add(dbg!(first), second);"))
3151+
3152+
(ert-deftest rust-test-dbg-wrap-symbol-unbalanced ()
3153+
(rust-test-manip-code
3154+
"let x = add((first, second);"
3155+
14
3156+
#'rust-dbg-wrap-or-unwrap
3157+
"let x = add((dbg!(first), second);"))
3158+
3159+
(ert-deftest rust-test-dbg-wrap-region ()
3160+
(rust-test-manip-code
3161+
"let x = add(first, second);"
3162+
9
3163+
(lambda ()
3164+
(transient-mark-mode 1)
3165+
(push-mark nil t t)
3166+
(goto-char 26)
3167+
(rust-dbg-wrap-or-unwrap))
3168+
"let x = dbg!(add(first, second));"))
3169+
3170+
(defun rust-test-dbg-unwrap (position)
3171+
(rust-test-manip-code
3172+
"let x = add(dbg!(first), second);"
3173+
position
3174+
#'rust-dbg-wrap-or-unwrap
3175+
"let x = add(first, second);"))
3176+
3177+
(ert-deftest rust-test-dbg-uwnrap-within ()
3178+
(rust-test-dbg-unwrap 19))
3179+
3180+
(ert-deftest rust-test-dbg-uwnrap-on-paren ()
3181+
(rust-test-dbg-unwrap 17))
3182+
3183+
(ert-deftest rust-test-dbg-uwnrap-on-dbg-middle ()
3184+
(rust-test-dbg-unwrap 15))
3185+
3186+
(ert-deftest rust-test-dbg-uwnrap-on-dbg-start ()
3187+
(rust-test-dbg-unwrap 13))
3188+
31413189
(when (executable-find rust-cargo-bin)
31423190
(ert-deftest rust-test-project-located ()
31433191
(lexical-let* ((test-dir (expand-file-name "test-project/" default-directory))

rust-mode.el

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
(require 'url-vars))
2020

2121
(require 'json)
22+
(require 'thingatpt)
2223

2324
(defvar electric-pair-inhibit-predicate)
2425
(defvar electric-pair-skip-self)
@@ -1582,6 +1583,7 @@ Return the created process."
15821583
(defvar rust-mode-map
15831584
(let ((map (make-sparse-keymap)))
15841585
(define-key map (kbd "C-c C-f") 'rust-format-buffer)
1586+
(define-key map (kbd "C-c d") 'rust-dbg-wrap-or-unwrap)
15851587
map)
15861588
"Keymap for Rust major mode.")
15871589

@@ -1800,6 +1802,42 @@ visit the new file."
18001802
(let ((output (json-read)))
18011803
(cdr (assoc-string "root" output))))))
18021804

1805+
(defun rust-insert-dbg ()
1806+
"Insert the dbg! macro."
1807+
(cond ((region-active-p)
1808+
(insert-parentheses)
1809+
(backward-char 1))
1810+
(t
1811+
(insert "(")
1812+
(forward-sexp)
1813+
(insert ")")
1814+
(backward-sexp)))
1815+
(insert "dbg!"))
1816+
1817+
;;;###autoload
1818+
(defun rust-dbg-wrap-or-unwrap ()
1819+
"Either remove or add the dbg! macro."
1820+
(interactive)
1821+
(save-excursion
1822+
(if (region-active-p)
1823+
(rust-insert-dbg)
1824+
1825+
(let ((beginning-of-symbol (ignore-errors (beginning-of-thing 'symbol))))
1826+
(when beginning-of-symbol
1827+
(goto-char beginning-of-symbol)))
1828+
1829+
(let ((dbg-point (save-excursion
1830+
(or (and (looking-at-p "dbg!") (+ 4 (point)))
1831+
(ignore-errors
1832+
(while (not (rust-looking-back-str "dbg!"))
1833+
(backward-up-list))
1834+
(point))))))
1835+
(cond (dbg-point
1836+
(goto-char dbg-point)
1837+
(delete-char -4)
1838+
(delete-pair))
1839+
(t (rust-insert-dbg)))))))
1840+
18031841
(provide 'rust-mode)
18041842

18051843
;;; rust-mode.el ends here

0 commit comments

Comments
 (0)