emacs-elpa-diffs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[nongnu] elpa/dart-mode 735868e 154/192: Copy/paste multiline string syn


From: ELPA Syncer
Subject: [nongnu] elpa/dart-mode 735868e 154/192: Copy/paste multiline string syntax-propertize from python.el
Date: Sun, 29 Aug 2021 11:02:08 -0400 (EDT)

branch: elpa/dart-mode
commit 735868e0f72eebfd03fd72578f8cf7b53dc87b7f
Author: Brady Trainor <mail@bradyt.com>
Commit: Brady Trainor <mail@bradyt.com>

    Copy/paste multiline string syntax-propertize from python.el
    
    This provides correct fontification of for example,
    
    String advice = ''' Don't do that ''';
    
    The code is copied from python.el, as of commit:
    
    
<https://github.com/emacs-mirror/emacs/commit/05345babc988060cca540770599282102c34f2a7>
---
 dart-mode.el | 61 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 61 insertions(+)

diff --git a/dart-mode.el b/dart-mode.el
index 9d4bac8..c40ea01 100644
--- a/dart-mode.el
+++ b/dart-mode.el
@@ -517,6 +517,66 @@ Returns nil if `dart-sdk-path' is nil."
              nil
              (0 font-lock-variable-name-face))))))
 
+(setq dart-string-delimiter (rx (and
+                            ;; Match even number of backslashes.
+                            (or (not (any ?\\ ?\' ?\")) point
+                                ;; Quotes might be preceded by an escaped 
quote.
+                                (and (or (not (any ?\\)) point) ?\\
+                                     (* ?\\ ?\\) (any ?\' ?\")))
+                            (* ?\\ ?\\)
+                            ;; Match single or triple quotes of any kind.
+                            (group (or  "\"" "\"\"\"" "'" "'''")))))
+
+(defconst dart-syntax-propertize-function
+  (syntax-propertize-rules
+   (dart-string-delimiter
+    (0 (ignore (dart-syntax-stringify))))))
+
+(defsubst dart-syntax-count-quotes (quote-char &optional point limit)
+  "Count number of quotes around point (max is 3).
+QUOTE-CHAR is the quote char to count.  Optional argument POINT is
+the point where scan starts (defaults to current point), and LIMIT
+is used to limit the scan."
+  (let ((i 0))
+    (while (and (< i 3)
+                (or (not limit) (< (+ point i) limit))
+                (eq (char-after (+ point i)) quote-char))
+      (setq i (1+ i)))
+    i))
+
+(defun dart-syntax-stringify ()
+  "Put `syntax-table' property correctly on single/triple quotes."
+  (let* ((num-quotes (length (match-string-no-properties 1)))
+         (ppss (prog2
+                   (backward-char num-quotes)
+                   (syntax-ppss)
+                 (forward-char num-quotes)))
+         (string-start (and (not (nth 4 ppss)) (nth 8 ppss)))
+         (quote-starting-pos (- (point) num-quotes))
+         (quote-ending-pos (point))
+         (num-closing-quotes
+          (and string-start
+               (dart-syntax-count-quotes
+                (char-before) string-start quote-starting-pos))))
+    (cond ((and string-start (= num-closing-quotes 0))
+           ;; This set of quotes doesn't match the string starting
+           ;; kind. Do nothing.
+           nil)
+          ((not string-start)
+           ;; This set of quotes delimit the start of a string.
+           (put-text-property quote-starting-pos (1+ quote-starting-pos)
+                              'syntax-table (string-to-syntax "|")))
+          ((= num-quotes num-closing-quotes)
+           ;; This set of quotes delimit the end of a string.
+           (put-text-property (1- quote-ending-pos) quote-ending-pos
+                              'syntax-table (string-to-syntax "|")))
+          ((> num-quotes num-closing-quotes)
+           ;; This may only happen whenever a triple quote is closing
+           ;; a single quoted string. Add string delimiter syntax to
+           ;; all three quotes.
+           (put-text-property quote-starting-pos quote-ending-pos
+                              'syntax-table (string-to-syntax "|"))))))
+
 (defun dart-fontify-region (beg end)
   "Use fontify the region between BEG and END as Dart.
 
@@ -1718,6 +1778,7 @@ Key bindings:
   (setq indent-line-function 'dart-indent-line-function)
   (setq indent-tabs-mode nil)
   (setq tab-width 2)
+  (setq-local syntax-propertize-function dart-syntax-propertize-function)
   (when dart-enable-analysis-server
     (if (null dart-sdk-path)
         (dart-log



reply via email to

[Prev in Thread] Current Thread [Next in Thread]