Skip to content

Commit 53c558c

Browse files
committed
Factor out rust-conditional-re-search-forward
Factor out the method of looking for a match for a regexp, but filtering out some of the matches with a filtering function. This will be used again for angle bracket filtering. This also fixes an issue with raw string handling.
1 parent f1b6007 commit 53c558c

File tree

2 files changed

+59
-17
lines changed

2 files changed

+59
-17
lines changed

rust-mode-tests.el

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -362,6 +362,29 @@ use foo::bar::baz;
362362
fn foo() { }
363363
"))
364364

365+
(ert-deftest font-lock-multi-raw-strings-in-a-row ()
366+
(rust-test-font-lock
367+
"
368+
r\"foo\\\", \"bar\", r\"bar\";
369+
r\"foo\\.\", \"bar\", r\"bar\";
370+
r\"foo\\..\", \"bar\", r\"foo\\..\\bar\";
371+
r\"\\\", \"foo\", r\"\\foo\";
372+
not_a_string();
373+
374+
"
375+
376+
(apply 'append (mapcar (lambda (s) (list s 'font-lock-string-face))
377+
'("r\"foo\\\"" "\"bar\"" "r\"bar\""
378+
"r\"foo\\.\"" "\"bar\"" "r\"bar\""
379+
"r\"foo\\..\"" "\"bar\"" "r\"foo\\..\\bar\""
380+
"r\"\\\"" "\"foo\"" "r\"\\foo\"")))
381+
))
382+
383+
(ert-deftest font-lock-raw-string-after-normal-string-ending-in-r ()
384+
(rust-test-font-lock
385+
"\"bar\" r\"foo\""
386+
'("\"bar\"" font-lock-string-face "r\"foo\"" font-lock-string-face)))
387+
365388
(ert-deftest indent-params-no-align ()
366389
(test-indent
367390
"

rust-mode.el

Lines changed: 36 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -411,6 +411,32 @@
411411
(/= font-lock-end orig-end))
412412
))
413413

414+
(defun rust-conditional-re-search-forward (regexp bound condition)
415+
;; Search forward for regexp (with bound). If found, call condition and return the found
416+
;; match only if it returns true.
417+
(let* (found
418+
found-ret-list
419+
(ret-list (save-excursion
420+
(while (and (not found) (re-search-forward regexp bound t))
421+
(setq
422+
found-ret-list (list (point) (match-data))
423+
found (save-match-data (save-excursion (ignore-errors (funcall condition)))))
424+
;; If the condition filters out a match, need to search
425+
;; again just after its beginning. This will allow
426+
;; cases such as:
427+
;; "bar" r"foo"
428+
;; where the filtered out search (r" r") should not
429+
;; prevent finding another one that begins in the middle
430+
;; of it (r"foo")
431+
(when (not found)
432+
(goto-char (1+ (match-beginning 0))))
433+
)
434+
(when found found-ret-list))))
435+
(when ret-list
436+
(goto-char (nth 0 ret-list))
437+
(set-match-data (nth 1 ret-list))
438+
(nth 0 ret-list))))
439+
414440
(defun rust-look-for-raw-string (bound)
415441
;; Find a raw string, but only if it's not in the middle of another string or
416442
;; a comment
@@ -450,23 +476,16 @@
450476

451477
;; No "#"s - capture the ending quote (using a backref to group 3,
452478
;; so that we can't match a quote if we had "#"s) as group 6
453-
(group (backref 3))))))
454-
;; If it matches, it ends up with the starting character of the string
455-
;; as group 1, any ending backslashes as group 4, and the ending
456-
;; character as either group 5 or group 6.
457-
458-
(ret-list (save-excursion
459-
(let* ((match-end (re-search-forward raw-str-regexp bound t))
460-
(ret-list (and match-end (list match-end (match-beginning 0) (match-data) (point)))))
461-
(when (and ret-list
462-
(save-excursion
463-
(goto-char (nth 1 ret-list))
464-
(not (rust-in-str-or-cmnt))))
465-
ret-list)))))
466-
(when ret-list
467-
(goto-char (nth 3 ret-list))
468-
(set-match-data (nth 2 ret-list))
469-
(nth 0 ret-list))))
479+
(group (backref 3))))
480+
;; If it matches, it ends up with the starting character of the string
481+
;; as group 1, any ending backslashes as group 4, and the ending
482+
;; character as either group 5 or group 6.
483+
)))
484+
(rust-conditional-re-search-forward
485+
raw-str-regexp bound
486+
(lambda () (save-excursion
487+
(goto-char (match-beginning 0))
488+
(not (rust-in-str-or-cmnt)))))))
470489

471490
(defvar rust-mode-font-lock-syntactic-keywords
472491
(append

0 commit comments

Comments
 (0)