|
374 | 374 | ("fn" . font-lock-function-name-face)
|
375 | 375 | ("static" . font-lock-constant-face)))))
|
376 | 376 |
|
| 377 | +(defun rust-extend-region-raw-string () |
| 378 | + "Extend the region given by `font-lock-beg' and `font-lock-end' |
| 379 | + to include the beginning of a string if it includes part of it. |
| 380 | + Adjusts to include the r[#] of a raw string as well." |
| 381 | + |
| 382 | + (let* ((orig-beg font-lock-beg) |
| 383 | + (orig-end font-lock-end) |
| 384 | + (beg-ppss (syntax-ppss font-lock-beg)) |
| 385 | + (beg-in-str (nth 3 beg-ppss)) |
| 386 | + (end-ppss (syntax-ppss font-lock-end)) |
| 387 | + (end-in-str (nth 3 end-ppss))) |
| 388 | + |
| 389 | + (when (and beg-in-str (> font-lock-beg (nth 8 beg-ppss))) |
| 390 | + (setq font-lock-beg str-beg) |
| 391 | + (while (equal ?# (char-before font-lock-beg)) |
| 392 | + (setq font-lock-beg (1- font-lock-beg))) |
| 393 | + (when (equal ?r (char-before font-lock-beg)) |
| 394 | + (setq font-lock-beg (1- font-lock-beg)))) |
| 395 | + |
| 396 | + (when end-in-str |
| 397 | + (save-excursion |
| 398 | + (goto-char (nth 8 end-ppss)) |
| 399 | + (ignore-errors (forward-sexp)) |
| 400 | + (setq font-lock-end (max font-lock-end (point))))) |
| 401 | + |
| 402 | + ;; If we have the beginning of a raw string in the region, make sure we have the end of |
| 403 | + ;; it. |
| 404 | + (when (or beg-in-str end-in-str) |
| 405 | + (save-excursion |
| 406 | + (goto-char font-lock-beg) |
| 407 | + (while (and (< (point) font-lock-end) (ignore-errors (rust-look-for-raw-string (buffer-end 1))))) |
| 408 | + (setq font-lock-end (max font-lock-end (point))))) |
| 409 | + |
| 410 | + (or (/= font-lock-beg orig-beg) |
| 411 | + (/= font-lock-end orig-end)) |
| 412 | + )) |
| 413 | + |
377 | 414 | (defun rust-look-for-raw-string (bound)
|
378 | 415 | ;; Find a raw string, but only if it's not in the middle of another string or
|
379 | 416 | ;; a comment
|
@@ -702,6 +739,7 @@ This is written mainly to be used as `end-of-defun-function' for Rust."
|
702 | 739 | (setq-local indent-line-function 'rust-mode-indent-line)
|
703 | 740 |
|
704 | 741 | ;; Fonts
|
| 742 | + (add-to-list 'font-lock-extend-region-functions 'rust-extend-region-raw-string) |
705 | 743 | (setq-local font-lock-defaults '(rust-mode-font-lock-keywords nil nil nil nil (font-lock-syntactic-keywords . rust-mode-font-lock-syntactic-keywords)))
|
706 | 744 |
|
707 | 745 | ;; Misc
|
|
0 commit comments