Skip to content

Commit c62185a

Browse files
authored
Merge pull request #304 from kurnevsky/raw-string-propertize
Don't insert string delimiter inside strings.
2 parents 0a94268 + e53dc8a commit c62185a

File tree

2 files changed

+39
-23
lines changed

2 files changed

+39
-23
lines changed

rust-mode-tests.el

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1452,6 +1452,19 @@ fn g() {
14521452
"g" font-lock-function-name-face
14531453
"\"xs\"" font-lock-string-face)))
14541454

1455+
(ert-deftest font-lock-string-ending-with-r-word-boundary ()
1456+
(with-temp-buffer
1457+
(rust-mode)
1458+
(insert "const foo = \"foo bar\"")
1459+
(font-lock-fontify-buffer)
1460+
;; right-word should move the point to the end of the words.
1461+
(goto-char 14)
1462+
(right-word)
1463+
(should (equal 17 (point)))
1464+
(right-word)
1465+
(should (equal 21 (point)))
1466+
))
1467+
14551468
(ert-deftest font-lock-raw-string-trick-ending-followed-by-string-with-quote ()
14561469
(rust-test-font-lock
14571470
"r\"With what looks like the start of a raw string at the end r#\";

rust-mode.el

Lines changed: 26 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1105,41 +1105,44 @@ should be considered a paired angle bracket."
11051105
(group "'")))
11061106
"A regular expression matching a character literal."))
11071107

1108-
(defun rust--syntax-propertize-raw-string (end)
1108+
(defun rust--syntax-propertize-raw-string (str-start end)
11091109
"A helper for rust-syntax-propertize.
11101110
1111-
If point is already in a raw string, this will apply the
1112-
appropriate string syntax to the character up to the end of the
1113-
raw string, or to END, whichever comes first."
1114-
(let ((str-start (nth 8 (syntax-ppss))))
1115-
(when str-start
1116-
(when (save-excursion
1117-
(goto-char str-start)
1118-
(looking-at "r\\(#*\\)\\(\"\\)"))
1119-
;; In a raw string, so try to find the end.
1120-
(let ((hashes (match-string 1)))
1121-
;; Match \ characters at the end of the string to suppress
1122-
;; their normal character-quote syntax.
1123-
(when (re-search-forward (concat "\\(\\\\*\\)\\(\"" hashes "\\)") end t)
1124-
(put-text-property (match-beginning 1) (match-end 1)
1125-
'syntax-table (string-to-syntax "_"))
1126-
(put-text-property (1- (match-end 2)) (match-end 2)
1127-
'syntax-table (string-to-syntax "|"))
1128-
(goto-char (match-end 0))))))))
1111+
This will apply the appropriate string syntax to the character
1112+
from the STR-START up to the end of the raw string, or to END,
1113+
whichever comes first."
1114+
(when (save-excursion
1115+
(goto-char str-start)
1116+
(looking-at "r\\(#*\\)\\(\"\\)"))
1117+
;; In a raw string, so try to find the end.
1118+
(let ((hashes (match-string 1)))
1119+
;; Match \ characters at the end of the string to suppress
1120+
;; their normal character-quote syntax.
1121+
(when (re-search-forward (concat "\\(\\\\*\\)\\(\"" hashes "\\)") end t)
1122+
(put-text-property (match-beginning 1) (match-end 1)
1123+
'syntax-table (string-to-syntax "_"))
1124+
(put-text-property (1- (match-end 2)) (match-end 2)
1125+
'syntax-table (string-to-syntax "|"))
1126+
(goto-char (match-end 0))))))
11291127

11301128
(defun rust-syntax-propertize (start end)
11311129
"A `syntax-propertize-function' to apply properties from START to END."
11321130
(goto-char start)
1133-
(rust--syntax-propertize-raw-string end)
1131+
(let ((str-start (rust-in-str-or-cmnt)))
1132+
(when str-start
1133+
(rust--syntax-propertize-raw-string str-start end)))
11341134
(funcall
11351135
(syntax-propertize-rules
11361136
;; Character literals.
11371137
(rust--char-literal-rx (1 "\"") (2 "\""))
11381138
;; Raw strings.
11391139
("\\(r\\)#*\""
1140-
(1 (prog1 "|"
1141-
(goto-char (match-end 0))
1142-
(rust--syntax-propertize-raw-string end))))
1140+
(0 (ignore
1141+
(goto-char (match-end 0))
1142+
(unless (save-excursion (nth 8 (syntax-ppss (match-beginning 0))))
1143+
(put-text-property (match-beginning 1) (match-end 1)
1144+
'syntax-table (string-to-syntax "|"))
1145+
(rust--syntax-propertize-raw-string (match-beginning 0) end)))))
11431146
("[<>]"
11441147
(0 (ignore
11451148
(when (save-match-data

0 commit comments

Comments
 (0)