branch: elpa/julia-mode
commit 796ddfd03454fbe5e8fb8abe1ffce2b5d29c9b8e
Author: Adam Beckmeyer <[email protected]>
Commit: Adam Beckmeyer <[email protected]>
Apply proper syntax to triple-quoted cmds
Previously they were being syntax-propertized as 3 single-quoted
cmds.
---
julia-mode-tests.el | 49 +++++++++++++++++++++++++++++++++++++++++++++++++
julia-mode.el | 37 +++++++++++++++++++++++++++++--------
2 files changed, 78 insertions(+), 8 deletions(-)
diff --git a/julia-mode-tests.el b/julia-mode-tests.el
index bafff50..89d1b26 100644
--- a/julia-mode-tests.el
+++ b/julia-mode-tests.el
@@ -532,6 +532,47 @@ end")
(julia--should-font-lock string 74 font-lock-type-face) ; B
))
+(ert-deftest julia--test-triple-quote-string-font-lock ()
+ "Test that triple quoted strings are font-locked correctly even with
escapes."
+ (let ((s1 "\"\"\"a\\\"\\\"\"b\"\"\"d")
+ (s2 "\"\"\"a\\\"\"\"b\"\"\"d")
+ (s3 "\"\"\"a```b\"\"\"d")
+ (s4 "\\\"\"\"a\\\"\"\"b\"\"\"d")
+ (s5 "\"\"\"a\\\"\"\"\"b"))
+ (julia--should-font-lock s1 4 font-lock-string-face)
+ (julia--should-font-lock s1 10 font-lock-string-face)
+ (julia--should-font-lock s1 14 nil)
+ (julia--should-font-lock s2 4 font-lock-string-face)
+ (julia--should-font-lock s2 9 font-lock-string-face)
+ (julia--should-font-lock s2 13 nil)
+ (julia--should-font-lock s3 4 font-lock-string-face)
+ (julia--should-font-lock s3 8 font-lock-string-face)
+ (julia--should-font-lock s3 12 nil)
+ (julia--should-font-lock s4 5 font-lock-string-face)
+ (julia--should-font-lock s4 10 font-lock-string-face)
+ (julia--should-font-lock s4 14 nil)
+ (julia--should-font-lock s5 4 font-lock-string-face)
+ (julia--should-font-lock s5 10 nil)))
+
+(ert-deftest julia--test-triple-quote-cmd-font-lock ()
+ "Test that triple-quoted cmds are font-locked correctly even with escapes."
+ (let ((s1 "```a\\`\\``b```d")
+ (s2 "```a\\```b```d")
+ (s3 "```a\"\"\"b```d")
+ (s4 "\\```a\\```b```d"))
+ (julia--should-font-lock s1 4 font-lock-string-face)
+ (julia--should-font-lock s1 10 font-lock-string-face)
+ (julia--should-font-lock s1 14 nil)
+ (julia--should-font-lock s2 4 font-lock-string-face)
+ (julia--should-font-lock s2 9 font-lock-string-face)
+ (julia--should-font-lock s2 13 nil)
+ (julia--should-font-lock s3 4 font-lock-string-face)
+ (julia--should-font-lock s3 8 font-lock-string-face)
+ (julia--should-font-lock s3 12 nil)
+ (julia--should-font-lock s4 5 font-lock-string-face)
+ (julia--should-font-lock s4 10 font-lock-string-face)
+ (julia--should-font-lock s4 14 nil)))
+
;;; Movement
(ert-deftest julia--test-beginning-of-defun-assn-1 ()
"Point moves to beginning of single-line assignment function."
@@ -697,6 +738,14 @@ hello world
;; instead of 1.
(should (= 1 (nth 8 (syntax-ppss 5))))))
+(ert-deftest julia--test-triple-quoted-cmd-syntax ()
+ (with-temp-buffer
+ (julia-mode)
+ (insert "```
+hello world
+```")
+ (should (= 1 (nth 8 (syntax-ppss 5))))))
+
(ert-deftest julia--test-backslash-syntax ()
(with-temp-buffer
(julia-mode)
diff --git a/julia-mode.el b/julia-mode.el
index 84fb115..3cdc217 100644
--- a/julia-mode.el
+++ b/julia-mode.el
@@ -303,14 +303,11 @@
(defconst julia-syntax-propertize-function
(syntax-propertize-rules
;; triple-quoted strings are a single string rather than 3
- ((rx (group ?\") ?\" (group ?\"))
- ;; First " starts a string if not already inside a string (or comment)
- (1 (let ((ppss (save-excursion (syntax-ppss (match-beginning 0)))))
- (unless (or (nth 3 ppss) (nth 4 ppss))
- (string-to-syntax "|"))))
- ;; Last " ends a string if already inside a string
- (2 (and (nth 3 (save-excursion (syntax-ppss (match-beginning 0))))
- (string-to-syntax "|"))))
+ ("\"\"\""
+ (0 (ignore (julia-syntax-stringify))))
+ ;; same with triple-quoted backticks
+ ("```"
+ (0 (ignore (julia-syntax-stringify))))
;; backslash acts as an operator if it's not inside a string
("\\\\"
(0 (unless (nth 3 (save-excursion (syntax-ppss (match-beginning 0))))
@@ -320,6 +317,30 @@
(1 "\"")
(2 "\""))))
+(defun julia-syntax-stringify ()
+ "Put `syntax-table' property correctly on triple-quoted strings and cmds."
+ (let* ((ppss (save-excursion (syntax-ppss (match-beginning 0))))
+ (string-open (and (not (nth 4 ppss)) (nth 8 ppss))))
+ (cond
+ ;; this set of quotes delimit the start of string/cmd
+ ((not string-open)
+ (put-text-property (match-beginning 0) (1+ (match-beginning 0))
+ 'syntax-table (string-to-syntax "|")))
+ ;; this set of quotes closes the current string/cmd
+ ((and
+ ;; check that """ closes """ and ``` closes ```
+ (eq (char-before) (char-after string-open))
+ ;; check that triple quote isn't escaped by odd number of backslashes
+ (let ((i 0))
+ (while (and (< (point-min) (- (match-beginning 0) i))
+ (eq (char-before (- (match-beginning 0) i)) ?\\))
+ (setq i (1+ i)))
+ (cl-evenp i)))
+ (put-text-property (1- (match-end 0)) (match-end 0)
+ 'syntax-table (string-to-syntax "|")))
+ ;; Put point after (match-beginning 0) to account for possibility
+ ;; of overlapping triple-quotes with first escaped
+ ((backward-char 2)))))
(defun julia-in-comment (&optional syntax-ppss)
"Return non-nil if point is inside a comment using SYNTAX-PPSS.