emacs-elpa-diffs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[nongnu] elpa/rust-mode 527e375ac1 1/2: dbg! insertion:


From: ELPA Syncer
Subject: [nongnu] elpa/rust-mode 527e375ac1 1/2: dbg! insertion:
Date: Sat, 5 Aug 2023 13:00:24 -0400 (EDT)

branch: elpa/rust-mode
commit 527e375ac18bc0742f7c62193c145c9e00f928e5
Author: Michael Lee <micl2e2@proton.me>
Commit: Michael Lee <micl2e2@proton.me>

    dbg! insertion:
    - Change rust-insert-dbg to ...-sexp
    - Handle the cases where forwarding is impossible
    - Add tests
---
 rust-mode-tests.el | 20 +++++++++++++++++++-
 rust-utils.el      | 29 ++++++++++++++++++++---------
 2 files changed, 39 insertions(+), 10 deletions(-)

diff --git a/rust-mode-tests.el b/rust-mode-tests.el
index 7b791b699a..5c6d547615 100644
--- a/rust-mode-tests.el
+++ b/rust-mode-tests.el
@@ -3449,7 +3449,8 @@ impl Two<'a> {
      "Foo" font-lock-type-face
      "in" font-lock-keyword-face)))
 
-(ert-deftest rust-test-dbg-wrap-symbol ()
+(ert-deftest rust-test-dbg-wrap-sexp ()
+  "a valid sexp ahead of current pos"
   (rust-test-manip-code
    "let x = add(first, second);"
    15
@@ -3457,6 +3458,23 @@ impl Two<'a> {
    "let x = add(dbg!(first), second);"
    24))
 
+(ert-deftest rust-test-dbg-wrap-sexp-fallback ()
+  "a invalid sexp ahead of current pos"
+  ;; inside
+  (rust-test-manip-code
+   "if let Ok(val) = may_val {}"
+   27
+   #'rust-dbg-wrap-or-unwrap
+   "if let Ok(val) = may_val {dbg!()}"
+   32)
+  ;; before
+  (rust-test-manip-code
+   "let a = {}"
+   9
+   #'rust-dbg-wrap-or-unwrap
+   "let a = dbg!({})"
+   17))
+
 (ert-deftest rust-test-dbg-wrap-empty-line ()
   (rust-test-manip-code
    "let a = 1;
diff --git a/rust-utils.el b/rust-utils.el
index 29590d0c07..cb55172b72 100644
--- a/rust-utils.el
+++ b/rust-utils.el
@@ -36,16 +36,27 @@ visit the new file."
 
 ;;; dbg! macro
 
-(defun rust-insert-dbg ()
-  "Insert the dbg! macro. Move cursor to the end of macro."
+(defun rust-insert-dbg-sexp ()
+  "Insert the dbg! macro around a sexp if possible, insert at current position
+if not. Move cursor to the end of macro."
   (when (rust-in-str)
     (up-list -1 t t))
-  (insert "(")
-  (forward-sexp)
-  (insert ")")
-  (backward-sexp)
-  (insert "dbg!")
-  (forward-sexp))
+  (setq safe-to-forward t)
+  (save-excursion
+    (condition-case nil
+        (forward-sexp)
+      (error (setq safe-to-forward nil)
+             nil)))
+  (cond
+   ((not safe-to-forward)
+    (rust-insert-dbg-alone))
+   (t
+    (insert "(")
+    (forward-sexp)
+    (insert ")")
+    (backward-sexp)
+    (insert "dbg!")
+    (forward-sexp))))
 
 (defun rust-insert-dbg-region ()
   "Insert the dbg! macro around a region. Move cursor to the end of macro."
@@ -93,7 +104,7 @@ visit the new file."
              (goto-char dbg-point)
              (delete-char -4)
              (delete-pair))
-            (t (rust-insert-dbg)))))
+            (t (rust-insert-dbg-sexp)))))
    )
 )
 



reply via email to

[Prev in Thread] Current Thread [Next in Thread]