diff options
| author | Roey Darwish Dror <roey.ghost@gmail.com> | 2019-10-31 20:01:19 +0100 |
|---|---|---|
| committer | Nathan Moreau <nathan.moreau@m4x.org> | 2019-10-31 20:02:34 +0100 |
| commit | 1b3db883bccb0956e5f05b7312485b2b7a5fa70f (patch) | |
| tree | 247aa29d02bcf11e9010fccbe3f585e105d01790 /rust-mode.el | |
| parent | 70ff9a027c51118ebba07c9be832c3efa7a7df67 (diff) | |
| download | rust-mode-1b3db883bccb0956e5f05b7312485b2b7a5fa70f.tar.gz | |
Add a function wrap and unwrap with the dbg! macro.
Diffstat (limited to 'rust-mode.el')
| -rw-r--r-- | rust-mode.el | 38 |
1 files changed, 38 insertions, 0 deletions
diff --git a/rust-mode.el b/rust-mode.el index dd50f18..42b8d82 100644 --- a/rust-mode.el +++ b/rust-mode.el @@ -19,6 +19,7 @@ (require 'url-vars)) (require 'json) +(require 'thingatpt) (defvar electric-pair-inhibit-predicate) (defvar electric-pair-skip-self) @@ -1582,6 +1583,7 @@ Return the created process." (defvar rust-mode-map (let ((map (make-sparse-keymap))) (define-key map (kbd "C-c C-f") 'rust-format-buffer) + (define-key map (kbd "C-c d") 'rust-dbg-wrap-or-unwrap) map) "Keymap for Rust major mode.") @@ -1800,6 +1802,42 @@ visit the new file." (let ((output (json-read))) (cdr (assoc-string "root" output)))))) +(defun rust-insert-dbg () + "Insert the dbg! macro." + (cond ((region-active-p) + (insert-parentheses) + (backward-char 1)) + (t + (insert "(") + (forward-sexp) + (insert ")") + (backward-sexp))) + (insert "dbg!")) + +;;;###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))))))) + (provide 'rust-mode) ;;; rust-mode.el ends here |
