diff options
Diffstat (limited to '.emacs.d/lisp/rust-utils.el')
| -rw-r--r-- | .emacs.d/lisp/rust-utils.el | 97 |
1 files changed, 64 insertions, 33 deletions
diff --git a/.emacs.d/lisp/rust-utils.el b/.emacs.d/lisp/rust-utils.el index 41f1ba8..cb55172 100644 --- a/.emacs.d/lisp/rust-utils.el +++ b/.emacs.d/lisp/rust-utils.el | |||
| @@ -36,46 +36,77 @@ visit the new file." | |||
| 36 | 36 | ||
| 37 | ;;; dbg! macro | 37 | ;;; dbg! macro |
| 38 | 38 | ||
| 39 | (defun rust-insert-dbg () | 39 | (defun rust-insert-dbg-sexp () |
| 40 | "Insert the dbg! macro." | 40 | "Insert the dbg! macro around a sexp if possible, insert at current position |
| 41 | (cond ((region-active-p) | 41 | if not. Move cursor to the end of macro." |
| 42 | (when (< (mark) (point)) | 42 | (when (rust-in-str) |
| 43 | (exchange-point-and-mark)) | 43 | (up-list -1 t t)) |
| 44 | (let ((old-point (point))) | 44 | (setq safe-to-forward t) |
| 45 | (insert-parentheses) | 45 | (save-excursion |
| 46 | (goto-char old-point))) | 46 | (condition-case nil |
| 47 | (t | 47 | (forward-sexp) |
| 48 | (when (rust-in-str) | 48 | (error (setq safe-to-forward nil) |
| 49 | (up-list -1 t t)) | 49 | nil))) |
| 50 | (insert "(") | 50 | (cond |
| 51 | (forward-sexp) | 51 | ((not safe-to-forward) |
| 52 | (insert ")") | 52 | (rust-insert-dbg-alone)) |
| 53 | (backward-sexp))) | 53 | (t |
| 54 | (insert "dbg!")) | 54 | (insert "(") |
| 55 | (forward-sexp) | ||
| 56 | (insert ")") | ||
| 57 | (backward-sexp) | ||
| 58 | (insert "dbg!") | ||
| 59 | (forward-sexp)))) | ||
| 60 | |||
| 61 | (defun rust-insert-dbg-region () | ||
| 62 | "Insert the dbg! macro around a region. Move cursor to the end of macro." | ||
| 63 | (when (< (mark) (point)) | ||
| 64 | (exchange-point-and-mark)) | ||
| 65 | (let ((old-point (point))) | ||
| 66 | (insert-parentheses) | ||
| 67 | (goto-char old-point)) | ||
| 68 | (insert "dbg!") | ||
| 69 | (forward-sexp)) | ||
| 70 | |||
| 71 | (defun rust-insert-dbg-alone () | ||
| 72 | "Insert the dbg! macro alone. Move cursor in between the brackets." | ||
| 73 | (insert "dbg!()") | ||
| 74 | (backward-char)) | ||
| 55 | 75 | ||
| 56 | ;;;###autoload | 76 | ;;;###autoload |
| 57 | (defun rust-dbg-wrap-or-unwrap () | 77 | (defun rust-dbg-wrap-or-unwrap () |
| 58 | "Either remove or add the dbg! macro." | 78 | "Either remove or add the dbg! macro." |
| 59 | (interactive) | 79 | (interactive) |
| 60 | (save-excursion | 80 | |
| 61 | (if (region-active-p) | 81 | (cond |
| 62 | (rust-insert-dbg) | 82 | |
| 83 | ;; region | ||
| 84 | ((region-active-p) | ||
| 85 | (rust-insert-dbg-region)) | ||
| 86 | |||
| 87 | ;; alone | ||
| 88 | ((or (looking-at-p " *$") (looking-at-p " *//.*")) | ||
| 89 | (rust-insert-dbg-alone)) | ||
| 63 | 90 | ||
| 64 | (let ((beginning-of-symbol (ignore-errors (beginning-of-thing 'symbol)))) | 91 | ;; symbol |
| 65 | (when beginning-of-symbol | 92 | (t |
| 66 | (goto-char beginning-of-symbol))) | 93 | (let ((beginning-of-symbol (ignore-errors (beginning-of-thing 'symbol)))) |
| 94 | (when beginning-of-symbol | ||
| 95 | (goto-char beginning-of-symbol))) | ||
| 67 | 96 | ||
| 68 | (let ((dbg-point (save-excursion | 97 | (let ((dbg-point (save-excursion |
| 69 | (or (and (looking-at-p "dbg!") (+ 4 (point))) | 98 | (or (and (looking-at-p "dbg!") (+ 4 (point))) |
| 70 | (ignore-errors | 99 | (ignore-errors |
| 71 | (while (not (rust-looking-back-str "dbg!")) | 100 | (while (not (rust-looking-back-str "dbg!")) |
| 72 | (backward-up-list)) | 101 | (backward-up-list)) |
| 73 | (point)))))) | 102 | (point)))))) |
| 74 | (cond (dbg-point | 103 | (cond (dbg-point |
| 75 | (goto-char dbg-point) | 104 | (goto-char dbg-point) |
| 76 | (delete-char -4) | 105 | (delete-char -4) |
| 77 | (delete-pair)) | 106 | (delete-pair)) |
| 78 | (t (rust-insert-dbg))))))) | 107 | (t (rust-insert-dbg-sexp))))) |
| 108 | ) | ||
| 109 | ) | ||
| 79 | 110 | ||
| 80 | (defun rust-toggle-mutability () | 111 | (defun rust-toggle-mutability () |
| 81 | "Toggles the mutability of the variable defined on the current line" | 112 | "Toggles the mutability of the variable defined on the current line" |
