Skip to content

Commit 4fd637c

Browse files
authored
rust-insert-dbg: handle the case of string literals. (#342)
1 parent 6a57253 commit 4fd637c

File tree

2 files changed

+73
-4
lines changed

2 files changed

+73
-4
lines changed

rust-mode-tests.el

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -245,10 +245,6 @@ fn bar() { }"
245245
/// even more.
246246
fn bar() { }" 14 85))
247247

248-
(defun test-dbg-wrap (initial expected position &optional end)
249-
(with-temp-buffer
250-
(insert initial)))
251-
252248
(defun test-auto-fill (initial position inserted expected)
253249
(rust-test-manip-code
254250
initial
@@ -3186,6 +3182,13 @@ impl Two<'a> {
31863182
(ert-deftest rust-test-dbg-uwnrap-on-dbg-start ()
31873183
(rust-test-dbg-unwrap 13))
31883184

3185+
(ert-deftest rust-test-dbg-unwrap-inside-string-literal ()
3186+
(rust-test-manip-code
3187+
"let x = \"foo, bar\"";"
3188+
15
3189+
#'rust-dbg-wrap-or-unwrap
3190+
"let x = dbg!(\"foo, bar\")"))
3191+
31893192
(when (executable-find rust-cargo-bin)
31903193
(ert-deftest rust-test-project-located ()
31913194
(lexical-let* ((test-dir (expand-file-name "test-project/" default-directory))

rust-mode.el

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1811,12 +1811,78 @@ visit the new file."
18111811
(insert-parentheses)
18121812
(goto-char old-point)))
18131813
(t
1814+
(when (rust-in-str)
1815+
(rust--up-list -1 t t))
18141816
(insert "(")
18151817
(forward-sexp)
18161818
(insert ")")
18171819
(backward-sexp)))
18181820
(insert "dbg!"))
18191821

1822+
(defun rust--up-list (&optional arg escape-strings no-syntax-crossing)
1823+
"Compatibility for emacs 24."
1824+
(or arg (setq arg 1))
1825+
(let ((inc (if (> arg 0) 1 -1))
1826+
(pos nil))
1827+
(while (/= arg 0)
1828+
(condition-case err
1829+
(save-restriction
1830+
;; If we've been asked not to cross string boundaries
1831+
;; and we're inside a string, narrow to that string so
1832+
;; that scan-lists doesn't find a match in a different
1833+
;; string.
1834+
(when no-syntax-crossing
1835+
(let* ((syntax (syntax-ppss))
1836+
(string-comment-start (nth 8 syntax)))
1837+
(when string-comment-start
1838+
(save-excursion
1839+
(goto-char string-comment-start)
1840+
(narrow-to-region
1841+
(point)
1842+
(if (nth 3 syntax) ; in string
1843+
(condition-case nil
1844+
(progn (forward-sexp) (point))
1845+
(scan-error (point-max)))
1846+
(forward-comment 1)
1847+
(point)))))))
1848+
(if (null forward-sexp-function)
1849+
(goto-char (or (scan-lists (point) inc 1)
1850+
(buffer-end arg)))
1851+
(condition-case err
1852+
(while (progn (setq pos (point))
1853+
(forward-sexp inc)
1854+
(/= (point) pos)))
1855+
(scan-error (goto-char (nth (if (> arg 0) 3 2) err))))
1856+
(if (= (point) pos)
1857+
(signal 'scan-error
1858+
(list "Unbalanced parentheses" (point) (point))))))
1859+
(scan-error
1860+
(let ((syntax nil))
1861+
(or
1862+
;; If we bumped up against the end of a list, see whether
1863+
;; we're inside a string: if so, just go to the beginning
1864+
;; or end of that string.
1865+
(and escape-strings
1866+
(or syntax (setf syntax (syntax-ppss)))
1867+
(nth 3 syntax)
1868+
(goto-char (nth 8 syntax))
1869+
(progn (when (> inc 0)
1870+
(forward-sexp))
1871+
t))
1872+
;; If we narrowed to a comment above and failed to escape
1873+
;; it, the error might be our fault, not an indication
1874+
;; that we're out of syntax. Try again from beginning or
1875+
;; end of the comment.
1876+
(and no-syntax-crossing
1877+
(or syntax (setf syntax (syntax-ppss)))
1878+
(nth 4 syntax)
1879+
(goto-char (nth 8 syntax))
1880+
(or (< inc 0)
1881+
(forward-comment 1))
1882+
(setf arg (+ arg inc)))
1883+
(signal (car err) (cdr err))))))
1884+
(setq arg (- arg inc)))))
1885+
18201886
;;;###autoload
18211887
(defun rust-dbg-wrap-or-unwrap ()
18221888
"Either remove or add the dbg! macro."

0 commit comments

Comments
 (0)