Skip to content

Commit ec3855f

Browse files
committed
Recognize runaway raw strings
Recognize raw strings all the way to the end of the buffer if they are not closed. This is not valid rust code, but the highlighting should show the mistake. This also eliminates glitchy behavior that can occur in this situation. Emacs assumes that edits can't change syntax at positions before the edit, and raw strings without this change violated this.
1 parent 99b128c commit ec3855f

File tree

2 files changed

+39
-1
lines changed

2 files changed

+39
-1
lines changed

rust-mode-tests.el

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1110,6 +1110,41 @@ this_is_not_a_string();)"
11101110
(should (equal nil (get-text-property 28 'face))) ;; Semicolon--should not be part of the string
11111111
))
11121112

1113+
(ert-deftest font-lock-runaway-raw-string ()
1114+
(rust-test-font-lock
1115+
"const Z = r#\"my raw string\";\n// oops this is still in the string"
1116+
'("const" font-lock-keyword-face
1117+
"Z" font-lock-type-face
1118+
"r#\"my raw string\";\n// oops this is still in the string" font-lock-string-face))
1119+
)
1120+
1121+
(ert-deftest font-lock-recognize-closing-raw-string ()
1122+
(with-temp-buffer
1123+
(rust-mode)
1124+
(insert "const foo = r##\"
1125+
1...............................................50
1126+
1...............................................50
1127+
1...............................................50
1128+
1...............195-->\"; let ...................50
1129+
1...............................................50
1130+
1...............................................50
1131+
1...............................................50
1132+
1...............................................50
1133+
1...............................................50
1134+
1......................500......................50
1135+
\"#;
1136+
")
1137+
(font-lock-fontify-buffer)
1138+
(goto-char 530)
1139+
(insert "#")
1140+
;; We have now closed the raw string. Check that the whole string is
1141+
;; recognized after the change
1142+
(font-lock-after-change-function (1- (point)) (point) 0)
1143+
(should (equal 'font-lock-string-face (get-text-property 195 'face))) ;; The "let"
1144+
(should (equal 'font-lock-string-face (get-text-property 500 'face))) ;; The "500"
1145+
(should (equal nil (get-text-property 531 'face))) ;; The second ";"
1146+
))
1147+
11131148
;;; Documentation comments
11141149

11151150
(ert-deftest font-lock-doc-line-comment-parent ()

rust-mode.el

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -531,7 +531,10 @@
531531

532532
;; No "#"s - capture the ending quote (using a backref to group 3,
533533
;; so that we can't match a quote if we had "#"s) as group 6
534-
(group (backref 3))))
534+
(group (backref 3))
535+
536+
;; If the raw string wasn't actually closed, go all the way to the end
537+
string-end))
535538

536539
;; Character literal: match the beginning ' of a character literal
537540
;; as group 7, and the ending one as group 8

0 commit comments

Comments
 (0)