Skip to content

Commit 4d633fc

Browse files
committed
Merge pull request #32 from MicahChalmer/raw-string-handling
Parse and highlight raw strings correctly
2 parents 19bc0e9 + bddc933 commit 4d633fc

File tree

2 files changed

+55
-7
lines changed

2 files changed

+55
-7
lines changed

rust-mode-tests.el

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -290,7 +290,7 @@ very very very long string
290290
deindented
291291
1
292292
(lambda ()
293-
;; The indentation will fial in some cases if the syntax properties are
293+
;; The indentation will fail in some cases if the syntax properties are
294294
;; not set. This only happens when font-lock fontifies the buffer.
295295
(font-lock-fontify-buffer)
296296
(indent-region 1 (buffer-size)))
@@ -927,6 +927,42 @@ list of substrings of `STR' each followed by its face."
927927
"let" font-lock-keyword-face
928928
"'\\''" font-lock-string-face)))
929929

930+
(ert-deftest font-lock-raw-strings-no-hashes ()
931+
(rust-test-font-lock
932+
"r\"No hashes\";"
933+
'("r\"No hashes\"" font-lock-string-face)))
934+
935+
(ert-deftest font-lock-raw-strings-double-quote ()
936+
(rust-test-font-lock
937+
"fn main() {
938+
r#\"With a double quote (\")\"#;
939+
}
940+
"
941+
'("fn" font-lock-keyword-face
942+
"main" font-lock-function-name-face
943+
"r#\"With a double quote (\")\"#" font-lock-string-face)))
944+
945+
(ert-deftest font-lock-raw-strings-two-hashes ()
946+
(rust-test-font-lock
947+
"r##\"With two hashes\"##;"
948+
'("r##\"With two hashes\"##" font-lock-string-face)))
949+
950+
(ert-deftest font-lock-raw-strings-backslash-at-end ()
951+
(rust-test-font-lock
952+
"r\"With a backslash at the end\\\";"
953+
'("r\"With a backslash at the end\\\"" font-lock-string-face)))
954+
955+
(ert-deftest font-lock-two-raw-strings ()
956+
(rust-test-font-lock
957+
"fn main() {
958+
r\"With a backslash at the end\\\";
959+
r##\"With two hashes\"##;
960+
}"
961+
'("fn" font-lock-keyword-face
962+
"main" font-lock-function-name-face
963+
"r\"With a backslash at the end\\\"" font-lock-string-face
964+
"r##\"With two hashes\"##" font-lock-string-face)))
965+
930966
(ert-deftest indent-method-chains-no-align ()
931967
(let ((rust-indent-method-chain nil)) (test-indent
932968
"

rust-mode.el

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,13 @@
5151

5252
table))
5353

54+
(defvar rust-mode-inside-raw-string-syntax-table
55+
(let ((table (make-syntax-table rust-mode-syntax-table)))
56+
(modify-syntax-entry ?\" "_" table)
57+
(modify-syntax-entry ?\\ "_" table)
58+
59+
table))
60+
5461
(defgroup rust-mode nil
5562
"Support for Rust code."
5663
:link '(url-link "http://www.rust-lang.org/")
@@ -312,12 +319,17 @@
312319
("static" . font-lock-constant-face)))))
313320

314321
(defvar rust-mode-font-lock-syntactic-keywords
315-
(mapcar (lambda (re) (list re '(1 "\"") '(2 "\"")))
316-
'("\\('\\)[^']\\('\\)"
317-
"\\('\\)\\\\['nrt]\\('\\)"
318-
"\\('\\)\\\\x[[:xdigit:]]\\{2\\}\\('\\)"
319-
"\\('\\)\\\\u[[:xdigit:]]\\{4\\}\\('\\)"
320-
"\\('\\)\\\\U[[:xdigit:]]\\{8\\}\\('\\)")))
322+
(append
323+
;; Handle single quoted character literals:
324+
(mapcar (lambda (re) (list re '(1 "\"") '(2 "\"")))
325+
'("\\('\\)[^']\\('\\)"
326+
"\\('\\)\\\\['nrt]\\('\\)"
327+
"\\('\\)\\\\x[[:xdigit:]]\\{2\\}\\('\\)"
328+
"\\('\\)\\\\u[[:xdigit:]]\\{4\\}\\('\\)"
329+
"\\('\\)\\\\U[[:xdigit:]]\\{8\\}\\('\\)"))
330+
;; Handle raw strings:
331+
`(("\\(r\\)\"\\([^\"]*\\)\\(\"\\)" (1 "|") (2 ,rust-mode-inside-raw-string-syntax-table) (3 "|"))
332+
("\\(r\\)#\\(#*\\)\\(\"[^#]*\"\\2\\)\\(#\\)" (1 "|") (3 ,rust-mode-inside-raw-string-syntax-table) (4 "|")))))
321333

322334
(defun rust-fill-prefix-for-comment-start (line-start)
323335
"Determine what to use for `fill-prefix' based on what is at the beginning of a line."

0 commit comments

Comments
 (0)