Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Eliminate "Containing expression ends prematurely" error #500

Merged
merged 1 commit into from
Aug 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 19 additions & 1 deletion rust-mode-tests.el
Original file line number Diff line number Diff line change
Expand Up @@ -3449,14 +3449,32 @@ 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
#'rust-dbg-wrap-or-unwrap
"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;
Expand Down
29 changes: 20 additions & 9 deletions rust-utils.el
Original file line number Diff line number Diff line change
Expand Up @@ -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."
Expand Down Expand Up @@ -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)))))
)
)

Expand Down