[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[nongnu] elpa/rust-mode 1b3db88 422/486: Add a function wrap and unwrap
From: |
ELPA Syncer |
Subject: |
[nongnu] elpa/rust-mode 1b3db88 422/486: Add a function wrap and unwrap with the dbg! macro. |
Date: |
Sat, 7 Aug 2021 09:26:06 -0400 (EDT) |
branch: elpa/rust-mode
commit 1b3db883bccb0956e5f05b7312485b2b7a5fa70f
Author: Roey Darwish Dror <roey.ghost@gmail.com>
Commit: Nathan Moreau <nathan.moreau@m4x.org>
Add a function wrap and unwrap with the dbg! macro.
---
rust-mode-tests.el | 48 ++++++++++++++++++++++++++++++++++++++++++++++++
rust-mode.el | 38 ++++++++++++++++++++++++++++++++++++++
2 files changed, 86 insertions(+)
diff --git a/rust-mode-tests.el b/rust-mode-tests.el
index 42b7a44..343b6c1 100644
--- a/rust-mode-tests.el
+++ b/rust-mode-tests.el
@@ -245,6 +245,10 @@ fn bar() { }"
/// even more.
fn bar() { }" 14 85))
+(defun test-dbg-wrap (initial expected position &optional end)
+ (with-temp-buffer
+ (insert initial)))
+
(defun test-auto-fill (initial position inserted expected)
(rust-test-manip-code
initial
@@ -3138,6 +3142,50 @@ impl Two<'a> {
"Foo" font-lock-type-face
"in" font-lock-keyword-face)))
+(ert-deftest rust-test-dbg-wrap-symbol ()
+ (rust-test-manip-code
+ "let x = add(first, second);"
+ 15
+ #'rust-dbg-wrap-or-unwrap
+ "let x = add(dbg!(first), second);"))
+
+(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);"))
+
+(ert-deftest rust-test-dbg-wrap-region ()
+ (rust-test-manip-code
+ "let x = add(first, second);"
+ 9
+ (lambda ()
+ (transient-mark-mode 1)
+ (push-mark nil t t)
+ (goto-char 26)
+ (rust-dbg-wrap-or-unwrap))
+ "let x = dbg!(add(first, second));"))
+
+(defun rust-test-dbg-unwrap (position)
+ (rust-test-manip-code
+ "let x = add(dbg!(first), second);"
+ position
+ #'rust-dbg-wrap-or-unwrap
+ "let x = add(first, second);"))
+
+(ert-deftest rust-test-dbg-uwnrap-within ()
+ (rust-test-dbg-unwrap 19))
+
+(ert-deftest rust-test-dbg-uwnrap-on-paren ()
+ (rust-test-dbg-unwrap 17))
+
+(ert-deftest rust-test-dbg-uwnrap-on-dbg-middle ()
+ (rust-test-dbg-unwrap 15))
+
+(ert-deftest rust-test-dbg-uwnrap-on-dbg-start ()
+ (rust-test-dbg-unwrap 13))
+
(when (executable-find rust-cargo-bin)
(ert-deftest rust-test-project-located ()
(lexical-let* ((test-dir (expand-file-name "test-project/"
default-directory))
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
- [nongnu] elpa/rust-mode 48290d3 399/486: Merge pull request #318 from kraai/patch-1, (continued)
- [nongnu] elpa/rust-mode 48290d3 399/486: Merge pull request #318 from kraai/patch-1, ELPA Syncer, 2021/08/07
- [nongnu] elpa/rust-mode f6ebcbd 400/486: Update travis scripts to use EVM, ELPA Syncer, 2021/08/07
- [nongnu] elpa/rust-mode 077cba7 402/486: Add local docker based tests runner, ELPA Syncer, 2021/08/07
- [nongnu] elpa/rust-mode 1d42d69 416/486: Speed up line indentation inside strings (#313), ELPA Syncer, 2021/08/07
- [nongnu] elpa/rust-mode 4a7a860 480/486: Properly markup two additional section headings, ELPA Syncer, 2021/08/07
- [nongnu] elpa/rust-mode a789a25 266/486: Fix #168: use while in rust-rewind-irrelevant, ELPA Syncer, 2021/08/07
- [nongnu] elpa/rust-mode 936a187 276/486: handle indirect buffers and multiple windows in rust-format-buffer, ELPA Syncer, 2021/08/07
- [nongnu] elpa/rust-mode 09efc45 320/486: Merge pull request #219 from Aankhen/highlight-question-mark, ELPA Syncer, 2021/08/07
- [nongnu] elpa/rust-mode b10ad41 326/486: Merge pull request #220 from Aankhen/highlight-string-interpolation, ELPA Syncer, 2021/08/07
- [nongnu] elpa/rust-mode d71fe94 307/486: restore points when an error occurs too, ELPA Syncer, 2021/08/07
- [nongnu] elpa/rust-mode 1b3db88 422/486: Add a function wrap and unwrap with the dbg! macro.,
ELPA Syncer <=
- [nongnu] elpa/rust-mode 8733792 448/486: Drop Emacs-24, ELPA Syncer, 2021/08/07
- [nongnu] elpa/rust-mode 9eb2c63 460/486: Fix README internal link (#389), ELPA Syncer, 2021/08/07
- [nongnu] elpa/rust-mode 13c4335 462/486: Improve doc-strings, ELPA Syncer, 2021/08/07
- [nongnu] elpa/rust-mode c23453d 463/486: rearrange I: Move options and faces to beginning of file, ELPA Syncer, 2021/08/07
- [nongnu] elpa/rust-mode 649f492 484/486: Create rust-cargo.el from existing code, ELPA Syncer, 2021/08/07
- [nongnu] elpa/rust-mode 198c777 386/486: Replaced font-lock-fontify-buffer with font-lock-ensure, ELPA Syncer, 2021/08/07
- [nongnu] elpa/rust-mode a871d10 390/486: Merge pull request #298 from jjwest/master, ELPA Syncer, 2021/08/07
- [nongnu] elpa/rust-mode 0a94268 396/486: Merge pull request #307 from Bunogi/master, ELPA Syncer, 2021/08/07
- [nongnu] elpa/rust-mode 86bc286 401/486: Add specific distribution for EVM, ELPA Syncer, 2021/08/07
- [nongnu] elpa/rust-mode 06b3b60 412/486: update readme (#327), ELPA Syncer, 2021/08/07