Skip to content

Parse and highlight raw strings correctly #32

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Feb 8, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 37 additions & 1 deletion rust-mode-tests.el
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,7 @@ very very very long string
deindented
1
(lambda ()
;; The indentation will fial in some cases if the syntax properties are
;; The indentation will fail in some cases if the syntax properties are
;; not set. This only happens when font-lock fontifies the buffer.
(font-lock-fontify-buffer)
(indent-region 1 (buffer-size)))
Expand Down Expand Up @@ -927,6 +927,42 @@ list of substrings of `STR' each followed by its face."
"let" font-lock-keyword-face
"'\\''" font-lock-string-face)))

(ert-deftest font-lock-raw-strings-no-hashes ()
(rust-test-font-lock
"r\"No hashes\";"
'("r\"No hashes\"" font-lock-string-face)))

(ert-deftest font-lock-raw-strings-double-quote ()
(rust-test-font-lock
"fn main() {
r#\"With a double quote (\")\"#;
}
"
'("fn" font-lock-keyword-face
"main" font-lock-function-name-face
"r#\"With a double quote (\")\"#" font-lock-string-face)))

(ert-deftest font-lock-raw-strings-two-hashes ()
(rust-test-font-lock
"r##\"With two hashes\"##;"
'("r##\"With two hashes\"##" font-lock-string-face)))

(ert-deftest font-lock-raw-strings-backslash-at-end ()
(rust-test-font-lock
"r\"With a backslash at the end\\\";"
'("r\"With a backslash at the end\\\"" font-lock-string-face)))

(ert-deftest font-lock-two-raw-strings ()
(rust-test-font-lock
"fn main() {
r\"With a backslash at the end\\\";
r##\"With two hashes\"##;
}"
'("fn" font-lock-keyword-face
"main" font-lock-function-name-face
"r\"With a backslash at the end\\\"" font-lock-string-face
"r##\"With two hashes\"##" font-lock-string-face)))

(ert-deftest indent-method-chains-no-align ()
(let ((rust-indent-method-chain nil)) (test-indent
"
Expand Down
24 changes: 18 additions & 6 deletions rust-mode.el
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,13 @@

table))

(defvar rust-mode-inside-raw-string-syntax-table
(let ((table (make-syntax-table rust-mode-syntax-table)))
(modify-syntax-entry ?\" "_" table)
(modify-syntax-entry ?\\ "_" table)

table))

(defgroup rust-mode nil
"Support for Rust code."
:link '(url-link "http://www.rust-lang.org/")
Expand Down Expand Up @@ -312,12 +319,17 @@
("static" . font-lock-constant-face)))))

(defvar rust-mode-font-lock-syntactic-keywords
(mapcar (lambda (re) (list re '(1 "\"") '(2 "\"")))
'("\\('\\)[^']\\('\\)"
"\\('\\)\\\\['nrt]\\('\\)"
"\\('\\)\\\\x[[:xdigit:]]\\{2\\}\\('\\)"
"\\('\\)\\\\u[[:xdigit:]]\\{4\\}\\('\\)"
"\\('\\)\\\\U[[:xdigit:]]\\{8\\}\\('\\)")))
(append
;; Handle single quoted character literals:
(mapcar (lambda (re) (list re '(1 "\"") '(2 "\"")))
'("\\('\\)[^']\\('\\)"
"\\('\\)\\\\['nrt]\\('\\)"
"\\('\\)\\\\x[[:xdigit:]]\\{2\\}\\('\\)"
"\\('\\)\\\\u[[:xdigit:]]\\{4\\}\\('\\)"
"\\('\\)\\\\U[[:xdigit:]]\\{8\\}\\('\\)"))
;; Handle raw strings:
`(("\\(r\\)\"\\([^\"]*\\)\\(\"\\)" (1 "|") (2 ,rust-mode-inside-raw-string-syntax-table) (3 "|"))
("\\(r\\)#\\(#*\\)\\(\"[^#]*\"\\2\\)\\(#\\)" (1 "|") (3 ,rust-mode-inside-raw-string-syntax-table) (4 "|")))))

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