[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[nongnu] elpa/rust-mode b10ad41 326/486: Merge pull request #220 from Aa
From: |
ELPA Syncer |
Subject: |
[nongnu] elpa/rust-mode b10ad41 326/486: Merge pull request #220 from Aankhen/highlight-string-interpolation |
Date: |
Sat, 7 Aug 2021 09:25:45 -0400 (EDT) |
branch: elpa/rust-mode
commit b10ad4177786a139623984c4855eb9de3864c697
Merge: afeddec 89320ad
Author: Tom Tromey <tom@tromey.com>
Commit: GitHub <noreply@github.com>
Merge pull request #220 from Aankhen/highlight-string-interpolation
Highlight interpolation in arguments to print! &c.
---
rust-mode-tests.el | 220 +++++++++++++++++++++++++++++++++++++++++++++++++++++
rust-mode.el | 82 ++++++++++++++++++++
2 files changed, 302 insertions(+)
diff --git a/rust-mode-tests.el b/rust-mode-tests.el
index de8a2cc..a9b4af9 100644
--- a/rust-mode-tests.el
+++ b/rust-mode-tests.el
@@ -2074,6 +2074,226 @@ fn main() {
'("r#\"\"\"#" font-lock-string-face
"'q'" font-lock-string-face)))
+(ert-deftest rust-macro-font-lock ()
+ (rust-test-font-lock
+ "foo!\(\);"
+ '("foo!" font-lock-preprocessor-face))
+ (rust-test-font-lock
+ "foo!{};"
+ '("foo!" font-lock-preprocessor-face))
+ (rust-test-font-lock
+ "foo![];"
+ '("foo!" font-lock-preprocessor-face)))
+
+(ert-deftest rust-string-interpolation-matcher-works ()
+ (dolist (test '(("print!\(\"\"\)" 9 11 nil)
+ ("print!\(\"abcd\"\)" 9 15 nil)
+ ("print!\(\"abcd {{}}\"\);" 9 19 nil)
+ ("print!\(\"abcd {{\"\);" 9 18 nil)
+ ("print!\(\"abcd {}\"\);" 9 18 ((14 16)))
+ ("print!\(\"abcd {{{}\"\);" 9 20 ((16 18)))
+ ("print!\(\"abcd {}{{\"\);" 9 20 ((14 16)))
+ ("print!\(\"abcd {} {{\"\);" 9 21 ((14 16)))
+ ("print!\(\"abcd {}}}\"\);" 9 20 ((14 16)))
+ ("print!\(\"abcd {{{}}}\"\);" 9 20 ((16 18)))
+ ("print!\(\"abcd {0}\"\);" 9 18 ((14 17)))
+ ("print!\(\"abcd {0} efgh\"\);" 9 23 ((14 17)))
+ ("print!\(\"{1} abcd {0} efgh\"\);" 9 27 ((9 12) (18 21)))
+ ("print!\(\"{{{1} abcd }} {0}}} {{efgh}}\"\);" 9 33 ((11 14)
(23 26)))))
+ (destructuring-bind (text cursor limit matches) test
+ (with-temp-buffer
+ ;; make sure we have a clean slate
+ (save-match-data
+ (set-match-data nil)
+ (insert text)
+ (goto-char cursor)
+ (if (null matches)
+ (should (equal (progn
+ (rust-string-interpolation-matcher limit)
+ (match-data))
+ nil))
+ (dolist (pair matches)
+ (rust-string-interpolation-matcher limit)
+ (should (equal (match-beginning 0) (car pair)))
+ (should (equal (match-end 0) (cadr pair))))))))))
+
+(ert-deftest rust-formatting-macro-font-lock ()
+ ;; test that the block delimiters aren't highlighted and the comment
+ ;; is ignored
+ (rust-test-font-lock
+ "print!(\"\"); { /* print!(\"\"); */ }"
+ '("print!" rust-builtin-formatting-macro-face
+ "\"\"" font-lock-string-face
+ "/* " font-lock-comment-delimiter-face
+ "print!(\"\"); */" font-lock-comment-face))
+ ;; other delimiters
+ (rust-test-font-lock
+ "print!{\"\"}; { /* no-op */ }"
+ '("print!" rust-builtin-formatting-macro-face
+ "\"\"" font-lock-string-face
+ "/* " font-lock-comment-delimiter-face
+ "no-op */" font-lock-comment-face))
+ ;; other delimiters
+ (rust-test-font-lock
+ "print![\"\"]; { /* no-op */ }"
+ '("print!" rust-builtin-formatting-macro-face
+ "\"\"" font-lock-string-face
+ "/* " font-lock-comment-delimiter-face
+ "no-op */" font-lock-comment-face))
+ ;; no interpolation
+ (rust-test-font-lock
+ "print!(\"abcd\"); { /* no-op */ }"
+ '("print!" rust-builtin-formatting-macro-face
+ "\"abcd\"" font-lock-string-face
+ "/* " font-lock-comment-delimiter-face
+ "no-op */" font-lock-comment-face))
+ ;; only interpolation
+ (rust-test-font-lock
+ "print!(\"{}\"); { /* no-op */ }"
+ '("print!" rust-builtin-formatting-macro-face
+ "\"" font-lock-string-face
+ "{}" rust-string-interpolation-face
+ "\"" font-lock-string-face
+ "/* " font-lock-comment-delimiter-face
+ "no-op */" font-lock-comment-face))
+ ;; text + interpolation
+ (rust-test-font-lock
+ "print!(\"abcd {}\", foo); { /* no-op */ }"
+ '("print!" rust-builtin-formatting-macro-face
+ "\"abcd " font-lock-string-face
+ "{}" rust-string-interpolation-face
+ "\"" font-lock-string-face
+ "/* " font-lock-comment-delimiter-face
+ "no-op */" font-lock-comment-face))
+ ;; text + interpolation with specification
+ (rust-test-font-lock
+ "print!(\"abcd {0}\", foo); { /* no-op */ }"
+ '("print!" rust-builtin-formatting-macro-face
+ "\"abcd " font-lock-string-face
+ "{0}" rust-string-interpolation-face
+ "\"" font-lock-string-face
+ "/* " font-lock-comment-delimiter-face
+ "no-op */" font-lock-comment-face))
+ ;; text + interpolation with specification and escape
+ (rust-test-font-lock
+ "print!(\"abcd {0}}}\", foo); { /* no-op */ }"
+ '("print!" rust-builtin-formatting-macro-face
+ "\"abcd " font-lock-string-face
+ "{0}" rust-string-interpolation-face
+ "}}\"" font-lock-string-face
+ "/* " font-lock-comment-delimiter-face
+ "no-op */" font-lock-comment-face))
+ ;; multiple pairs
+ (rust-test-font-lock
+ "print!(\"abcd {0} efgh {1}\", foo, bar); { /* no-op */ }"
+ '("print!" rust-builtin-formatting-macro-face
+ "\"abcd " font-lock-string-face
+ "{0}" rust-string-interpolation-face
+ " efgh " font-lock-string-face
+ "{1}" rust-string-interpolation-face
+ "\"" font-lock-string-face
+ "/* " font-lock-comment-delimiter-face
+ "no-op */" font-lock-comment-face))
+ ;; println
+ (rust-test-font-lock
+ "println!(\"abcd {0} efgh {1}\", foo, bar); { /* no-op */ }"
+ '("println!" rust-builtin-formatting-macro-face
+ "\"abcd " font-lock-string-face
+ "{0}" rust-string-interpolation-face
+ " efgh " font-lock-string-face
+ "{1}" rust-string-interpolation-face
+ "\"" font-lock-string-face
+ "/* " font-lock-comment-delimiter-face
+ "no-op */" font-lock-comment-face))
+ ;; eprint
+ (rust-test-font-lock
+ "eprint!(\"abcd {0} efgh {1}\", foo, bar); { /* no-op */ }"
+ '("eprint!" rust-builtin-formatting-macro-face
+ "\"abcd " font-lock-string-face
+ "{0}" rust-string-interpolation-face
+ " efgh " font-lock-string-face
+ "{1}" rust-string-interpolation-face
+ "\"" font-lock-string-face
+ "/* " font-lock-comment-delimiter-face
+ "no-op */" font-lock-comment-face))
+ ;; eprintln
+ (rust-test-font-lock
+ "eprintln!(\"abcd {0} efgh {1}\", foo, bar); { /* no-op */ }"
+ '("eprintln!" rust-builtin-formatting-macro-face
+ "\"abcd " font-lock-string-face
+ "{0}" rust-string-interpolation-face
+ " efgh " font-lock-string-face
+ "{1}" rust-string-interpolation-face
+ "\"" font-lock-string-face
+ "/* " font-lock-comment-delimiter-face
+ "no-op */" font-lock-comment-face))
+ ;; format
+ (rust-test-font-lock
+ "format!(\"abcd {0} efgh {1}\", foo, bar); { /* no-op */ }"
+ '("format!" rust-builtin-formatting-macro-face
+ "\"abcd " font-lock-string-face
+ "{0}" rust-string-interpolation-face
+ " efgh " font-lock-string-face
+ "{1}" rust-string-interpolation-face
+ "\"" font-lock-string-face
+ "/* " font-lock-comment-delimiter-face
+ "no-op */" font-lock-comment-face))
+ ;; print + raw string
+ (rust-test-font-lock
+ "format!(r\"abcd {0} efgh {1}\", foo, bar); { /* no-op */ }"
+ '("format!" rust-builtin-formatting-macro-face
+ "r\"abcd " font-lock-string-face
+ "{0}" rust-string-interpolation-face
+ " efgh " font-lock-string-face
+ "{1}" rust-string-interpolation-face
+ "\"" font-lock-string-face
+ "/* " font-lock-comment-delimiter-face
+ "no-op */" font-lock-comment-face))
+ ;; print + raw string with hash
+ (rust-test-font-lock
+ "format!(r#\"abcd {0} efgh {1}\"#, foo, bar); { /* no-op */ }"
+ '("format!" rust-builtin-formatting-macro-face
+ "r#\"abcd " font-lock-string-face
+ "{0}" rust-string-interpolation-face
+ " efgh " font-lock-string-face
+ "{1}" rust-string-interpolation-face
+ "\"#" font-lock-string-face
+ "/* " font-lock-comment-delimiter-face
+ "no-op */" font-lock-comment-face))
+ ;; print + raw string with two hashes
+ (rust-test-font-lock
+ "format!(r##\"abcd {0} efgh {1}\"##, foo, bar); { /* no-op */ }"
+ '("format!" rust-builtin-formatting-macro-face
+ "r##\"abcd " font-lock-string-face
+ "{0}" rust-string-interpolation-face
+ " efgh " font-lock-string-face
+ "{1}" rust-string-interpolation-face
+ "\"##" font-lock-string-face
+ "/* " font-lock-comment-delimiter-face
+ "no-op */" font-lock-comment-face)))
+
+(ert-deftest rust-write-macro-font-lock ()
+ (rust-test-font-lock
+ "write!(f, \"abcd {0}}} efgh {1}\", foo, bar); { /* no-op */ }"
+ '("write!" rust-builtin-formatting-macro-face
+ "\"abcd " font-lock-string-face
+ "{0}" rust-string-interpolation-face
+ "}} efgh " font-lock-string-face
+ "{1}" rust-string-interpolation-face
+ "\"" font-lock-string-face
+ "/* " font-lock-comment-delimiter-face
+ "no-op */" font-lock-comment-face))
+ (rust-test-font-lock
+ "writeln!(f, \"abcd {0}}} efgh {1}\", foo, bar); { /* no-op */ }"
+ '("writeln!" rust-builtin-formatting-macro-face
+ "\"abcd " font-lock-string-face
+ "{0}" rust-string-interpolation-face
+ "}} efgh " font-lock-string-face
+ "{1}" rust-string-interpolation-face
+ "\"" font-lock-string-face
+ "/* " font-lock-comment-delimiter-face
+ "no-op */" font-lock-comment-face)))
+
(ert-deftest rust-test-basic-paren-matching ()
(rust-test-matching-parens
"
diff --git a/rust-mode.el b/rust-mode.el
index 33e6b28..3b2a856 100644
--- a/rust-mode.el
+++ b/rust-mode.el
@@ -176,7 +176,18 @@ function or trait. When nil, where will be aligned with
fn or trait."
"Face for the question mark operator."
:group 'rust-mode)
+(defface rust-builtin-formatting-macro-face
+ '((t :inherit font-lock-builtin-face))
+ "Face for builtin formatting macros (print! &c.)."
+ :group 'rust-mode)
+
+(defface rust-string-interpolation-face
+ '((t :slant italic :inherit font-lock-string-face))
+ "Face for interpolating braces in builtin formatting macro strings."
+ :group 'rust-mode)
+
(defun rust-paren-level () (nth 0 (syntax-ppss)))
+(defun rust-in-str () (nth 3 (syntax-ppss)))
(defun rust-in-str-or-cmnt () (nth 8 (syntax-ppss)))
(defun rust-rewind-past-str-cmnt () (goto-char (nth 8 (syntax-ppss))))
@@ -579,6 +590,54 @@ the desired identifiers), but does not match type
annotations \"foo::<\"."
((not (looking-at (rx (0+ space) "<")))
(throw 'rust-path-font-lock-matcher match))))))))
+(defun rust-next-string-interpolation (limit)
+ "Search forward from point for next Rust interpolation marker
+before LIMIT.
+Set point to the end of the occurrence found, and return match beginning
+and end."
+ (catch 'match
+ (save-match-data
+ (save-excursion
+ (while (search-forward "{" limit t)
+ (if (eql (char-after (point)) ?{)
+ (forward-char)
+ (let ((start (match-beginning 0)))
+ ;; According to fmt_macros::Parser::next, an opening brace
+ ;; must be followed by an optional argument and/or format
+ ;; specifier, then a closing brace. A single closing brace
+ ;; without a corresponding unescaped opening brace is an
+ ;; error. We don't need to do anything special with
+ ;; arguments, specifiers, or errors, so we only search for
+ ;; the single closing brace.
+ (when (search-forward "}" limit t)
+ (throw 'match (list start (point)))))))))))
+
+(defun rust-string-interpolation-matcher (limit)
+ "Match next Rust interpolation marker before LIMIT and set
+match data if found. Returns nil if not within a Rust string."
+ (when (rust-in-str)
+ (let ((match (rust-next-string-interpolation limit)))
+ (when match
+ (set-match-data match)
+ (goto-char (cadr match))
+ match))))
+
+(defvar rust-builtin-formatting-macros
+ '("eprint"
+ "eprintln"
+ "format"
+ "print"
+ "println")
+ "List of builtin Rust macros for string formatting used by
`rust-mode-font-lock-keywords'. (`write!' is handled separately.)")
+
+(defvar rust-formatting-macro-opening-re
+ "[[:space:]]*[({[][[:space:]]*"
+ "Regular expression to match the opening delimiter of a Rust formatting
macro.")
+
+(defvar rust-start-of-string-re
+ "\\(?:r#*\\)?\""
+ "Regular expression to match the start of a Rust raw string.")
+
(defvar rust-mode-font-lock-keywords
(append
`(
@@ -599,6 +658,22 @@ the desired identifiers), but does not match type
annotations \"foo::<\"."
(,(rust-re-grab (concat "#\\!?\\[" rust-re-ident "[^]]*\\]"))
1 font-lock-preprocessor-face keep)
+ ;; Builtin formatting macros
+ (,(concat (rust-re-grab (concat (regexp-opt
rust-builtin-formatting-macros) "!")) (concat rust-formatting-macro-opening-re
rust-start-of-string-re))
+ (1 'rust-builtin-formatting-macro-face)
+ (rust-string-interpolation-matcher
+ (rust-end-of-string)
+ nil
+ (0 'rust-string-interpolation-face t nil)))
+
+ ;; write! macro
+ (,(concat (rust-re-grab "write\\(ln\\)?!") (concat
rust-formatting-macro-opening-re "[[:space:]]*[^\"]+,[[:space:]]*"
rust-start-of-string-re))
+ (1 'rust-builtin-formatting-macro-face)
+ (rust-string-interpolation-matcher
+ (rust-end-of-string)
+ nil
+ (0 'rust-string-interpolation-face t nil)))
+
;; Syntax extension invocations like `foo!`, highlight including the !
(,(concat (rust-re-grab (concat rust-re-ident "!")) "[({[:space:][]")
1 font-lock-preprocessor-face)
@@ -1239,6 +1314,13 @@ This is written mainly to be used as
`end-of-defun-function' for Rust."
;; There is no opening brace, so consider the whole buffer to be one
"defun"
(goto-char (point-max))))
+(defun rust-end-of-string ()
+ "Skip to the end of the current string."
+ (save-excursion
+ (skip-syntax-forward "^\"|")
+ (skip-syntax-forward "\"|")
+ (point)))
+
;; Formatting using rustfmt
(defun rust--format-call (buf)
"Format BUF using rustfmt."
- [nongnu] elpa/rust-mode c1059d6 387/486: Added angle bracket tests, some of which fail without the fix, (continued)
- [nongnu] elpa/rust-mode c1059d6 387/486: Added angle bracket tests, some of which fail without the fix, ELPA Syncer, 2021/08/07
- [nongnu] elpa/rust-mode 06f1c68 389/486: Merge pull request #300 from mmilenko/rust-looking-back-macro--backward-char--fix, ELPA Syncer, 2021/08/07
- [nongnu] elpa/rust-mode 48290d3 399/486: Merge pull request #318 from kraai/patch-1, ELPA Syncer, 2021/08/07
- [nongnu] elpa/rust-mode f6ebcbd 400/486: Update travis scripts to use EVM, ELPA Syncer, 2021/08/07
- [nongnu] elpa/rust-mode 077cba7 402/486: Add local docker based tests runner, ELPA Syncer, 2021/08/07
- [nongnu] elpa/rust-mode 1d42d69 416/486: Speed up line indentation inside strings (#313), ELPA Syncer, 2021/08/07
- [nongnu] elpa/rust-mode 4a7a860 480/486: Properly markup two additional section headings, ELPA Syncer, 2021/08/07
- [nongnu] elpa/rust-mode a789a25 266/486: Fix #168: use while in rust-rewind-irrelevant, ELPA Syncer, 2021/08/07
- [nongnu] elpa/rust-mode 936a187 276/486: handle indirect buffers and multiple windows in rust-format-buffer, ELPA Syncer, 2021/08/07
- [nongnu] elpa/rust-mode 09efc45 320/486: Merge pull request #219 from Aankhen/highlight-question-mark, ELPA Syncer, 2021/08/07
- [nongnu] elpa/rust-mode b10ad41 326/486: Merge pull request #220 from Aankhen/highlight-string-interpolation,
ELPA Syncer <=
- [nongnu] elpa/rust-mode d71fe94 307/486: restore points when an error occurs too, ELPA Syncer, 2021/08/07
- [nongnu] elpa/rust-mode 1b3db88 422/486: Add a function wrap and unwrap with the dbg! macro., ELPA Syncer, 2021/08/07
- [nongnu] elpa/rust-mode 8733792 448/486: Drop Emacs-24, ELPA Syncer, 2021/08/07
- [nongnu] elpa/rust-mode 9eb2c63 460/486: Fix README internal link (#389), ELPA Syncer, 2021/08/07
- [nongnu] elpa/rust-mode 13c4335 462/486: Improve doc-strings, ELPA Syncer, 2021/08/07
- [nongnu] elpa/rust-mode c23453d 463/486: rearrange I: Move options and faces to beginning of file, ELPA Syncer, 2021/08/07
- [nongnu] elpa/rust-mode 649f492 484/486: Create rust-cargo.el from existing code, ELPA Syncer, 2021/08/07
- [nongnu] elpa/rust-mode 198c777 386/486: Replaced font-lock-fontify-buffer with font-lock-ensure, ELPA Syncer, 2021/08/07
- [nongnu] elpa/rust-mode a871d10 390/486: Merge pull request #298 from jjwest/master, ELPA Syncer, 2021/08/07
- [nongnu] elpa/rust-mode 0a94268 396/486: Merge pull request #307 from Bunogi/master, ELPA Syncer, 2021/08/07