From ec3855f1c6895eb12f140a84f9dd98392c4566a8 Mon Sep 17 00:00:00 2001 From: Micah Chalmer Date: Thu, 30 Jul 2015 20:52:06 -0400 Subject: 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. --- rust-mode-tests.el | 35 +++++++++++++++++++++++++++++++++++ rust-mode.el | 5 ++++- 2 files changed, 39 insertions(+), 1 deletion(-) diff --git a/rust-mode-tests.el b/rust-mode-tests.el index 5312f38..8f25f06 100644 --- a/rust-mode-tests.el +++ b/rust-mode-tests.el @@ -1110,6 +1110,41 @@ this_is_not_a_string();)" (should (equal nil (get-text-property 28 'face))) ;; Semicolon--should not be part of the string )) +(ert-deftest font-lock-runaway-raw-string () + (rust-test-font-lock + "const Z = r#\"my raw string\";\n// oops this is still in the string" + '("const" font-lock-keyword-face + "Z" font-lock-type-face + "r#\"my raw string\";\n// oops this is still in the string" font-lock-string-face)) + ) + +(ert-deftest font-lock-recognize-closing-raw-string () + (with-temp-buffer + (rust-mode) + (insert "const foo = r##\" +1...............................................50 +1...............................................50 +1...............................................50 +1...............195-->\"; let ...................50 +1...............................................50 +1...............................................50 +1...............................................50 +1...............................................50 +1...............................................50 +1......................500......................50 +\"#; +") + (font-lock-fontify-buffer) + (goto-char 530) + (insert "#") + ;; We have now closed the raw string. Check that the whole string is + ;; recognized after the change + (font-lock-after-change-function (1- (point)) (point) 0) + (should (equal 'font-lock-string-face (get-text-property 195 'face))) ;; The "let" + (should (equal 'font-lock-string-face (get-text-property 500 'face))) ;; The "500" + (should (equal nil (get-text-property 531 'face))) ;; The second ";" + )) + ;;; Documentation comments (ert-deftest font-lock-doc-line-comment-parent () diff --git a/rust-mode.el b/rust-mode.el index 9e20aa2..9899ad1 100644 --- a/rust-mode.el +++ b/rust-mode.el @@ -531,7 +531,10 @@ ;; No "#"s - capture the ending quote (using a backref to group 3, ;; so that we can't match a quote if we had "#"s) as group 6 - (group (backref 3)))) + (group (backref 3)) + + ;; If the raw string wasn't actually closed, go all the way to the end + string-end)) ;; Character literal: match the beginning ' of a character literal ;; as group 7, and the ending one as group 8 -- cgit v1.2.3