[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[nongnu] elpa/swift-mode 889586c 102/496: Merge pull request #28 from ap
From: |
ELPA Syncer |
Subject: |
[nongnu] elpa/swift-mode 889586c 102/496: Merge pull request #28 from ap4y/highlight_interpolation |
Date: |
Sun, 29 Aug 2021 11:33:16 -0400 (EDT) |
branch: elpa/swift-mode
commit 889586cd9df2d08285a6d6d186fc0d3181c05182
Merge: 20e4ecf b1334e1
Author: Bozhidar Batsov <bozhidar@batsov.com>
Commit: Bozhidar Batsov <bozhidar@batsov.com>
Merge pull request #28 from ap4y/highlight_interpolation
Highlight interpolation
---
swift-mode.el | 101 +++++++++++++++++++++++++++++++++---------------
test/font-lock-tests.el | 2 +
2 files changed, 71 insertions(+), 32 deletions(-)
diff --git a/swift-mode.el b/swift-mode.el
index d0b4caf..febdd43 100644
--- a/swift-mode.el
+++ b/swift-mode.el
@@ -261,71 +261,106 @@
(defvar swift-mode--constants
'("true" "false" "nil"))
-(defvar swift-mode--font-lock-defaults
- (list
- (list
-
+(defvar swift-font-lock-keywords
+ `(
;; Keywords
;;
;; Swift allows reserved words to be used as identifiers when enclosed
;; with backticks, in which case they should be highlighted as
;; identifiers, not keywords.
- (cons (rx-to-string
- `(and (or bol (not (any "`"))) bow
- (group (or ,@swift-mode--keywords))
- eow)
- t)
- 1)
+ (,(rx-to-string
+ `(and (or bol (not (any "`"))) bow
+ (group (or ,@swift-mode--keywords))
+ eow)
+ t)
+ 1 font-lock-keyword-face)
;; Types
;;
;; Any token beginning with an uppercase character is highlighted as a
;; type.
- (cons (rx bow upper (* word) eow)
- font-lock-type-face)
+ (,(rx bow upper (* word) eow)
+ 0 font-lock-type-face)
;; Function names
;;
;; Any token beginning after `func' is highlighted as a function name.
- (cons (rx bow "func" eow (+ space) (group bow (+ word) eow))
- (list 1 font-lock-function-name-face))
+ (,(rx bow "func" eow (+ space) (group bow (+ word) eow))
+ 1 font-lock-function-name-face)
;; Value bindings
;;
;; Any token beginning after `let' or `var' is highlighted as an
;; identifier.
- (cons (rx-to-string `(and bow
- (or ,@swift-mode--val-decl-keywords)
- eow
- (+ space)
- (? "(")
- (group (+ (or (+ (? ?`) word (? ?`)) ?, space)))
- (? ")"))
- t)
- (list 1 font-lock-variable-name-face))
+ (,(rx-to-string `(and bow
+ (or ,@swift-mode--val-decl-keywords)
+ eow
+ (+ space)
+ (? "(")
+ (group (+ (or (+ (? ?`) word (? ?`)) ?, space)))
+ (? ")"))
+ t)
+ 1 font-lock-variable-name-face)
;; Use high-visibility face for pattern match wildcards.
- (cons (rx (not (any word digit)) (group "_") (or eol (not (any word
digit))))
- (list 1 font-lock-negation-char-face))
+ (,(rx (not (any word digit)) (group "_") (or eol (not (any word digit))))
+ 1 font-lock-negation-char-face)
;; Constants
;;
;; Highlight nil and boolean literals.
- (cons (rx-to-string `(and bow (or ,@swift-mode--constants) eow))
- font-lock-constant-face)
+ (,(rx-to-string `(and bow (or ,@swift-mode--constants) eow))
+ 0 font-lock-constant-face)
;; Attributes
;;
;; Use string face for attribute name.
- (cons (rx (or bol space)(group "@" (+ word)) eow)
- (list 1 font-lock-string-face))
+ (,(rx (or bol space)(group "@" (+ word)) eow)
+ 1 font-lock-string-face)
;; Imported modules
;;
;; Highlight the names of imported modules. Use `font-lock-string-face' for
;; consistency with C modes.
- (cons (rx bow "import" eow (+ space) (group (+ word)))
- (list 1 font-lock-string-face)))))
+ (,(rx bow "import" eow (+ space) (group (+ word)))
+ 1 font-lock-string-face)
+
+ ;; String interpolation
+ ;;
+ ;; Highlight interpolation expression as identifier.
+ (swift-match-interpolation 0 font-lock-variable-name-face t)
+ ))
+
+(defun swift-syntax-propertize-function (start end)
+ "Syntactic keywords for Swift mode."
+ (let (case-fold-search)
+ (goto-char start)
+ (remove-text-properties start end '(swift-interpolation-match-data))
+ (funcall
+ (syntax-propertize-rules
+ ((rx (or line-start (not (any "\\")))
+ (zero-or-more "\\\\")
+ (group "\\(" (zero-or-more any) ")"))
+ (0 (ignore (swift-syntax-propertize-interpolation)))))
+ start end)))
+
+(defun swift-syntax-propertize-interpolation ()
+ (let* ((beg (match-beginning 0))
+ (context (save-excursion (save-match-data (syntax-ppss beg)))))
+ (put-text-property beg (1+ beg) 'swift-interpolation-match-data
+ (cons (nth 3 context) (match-data)))))
+
+(defun swift-match-interpolation (limit)
+ (let ((pos (next-single-char-property-change (point)
'swift-interpolation-match-data
+ nil limit)))
+ (when (and pos (> pos (point)))
+ (goto-char pos)
+ (let ((value (get-text-property pos 'swift-interpolation-match-data)))
+ (if (eq (car value) ?\")
+ (progn
+ (set-match-data (cdr value))
+ t)
+ (swift-match-interpolation limit))))))
;;; Imenu
@@ -495,7 +530,9 @@ You can send text to the REPL process from other buffers
containing source.
\\<swift-mode-map>"
:group 'swift
:syntax-table swift-mode-syntax-table
- (setq-local font-lock-defaults swift-mode--font-lock-defaults)
+ (setq font-lock-defaults '((swift-font-lock-keywords) nil nil))
+ (setq-local syntax-propertize-function #'swift-syntax-propertize-function)
+
(setq-local imenu-generic-expression swift-mode--imenu-generic-expression)
(setq-local comment-start "// ")
diff --git a/test/font-lock-tests.el b/test/font-lock-tests.el
index 361ba62..acd14b0 100644
--- a/test/font-lock-tests.el
+++ b/test/font-lock-tests.el
@@ -186,6 +186,8 @@ test will fail."
(check-face class/base-type-has-type-face/4 font-lock-type-face "class T<U> :
{{Base}}")
(check-face class/base-type-colon-has-default-face/1 nil "class T {{:}} Base")
+(check-face string-interpolation/has-variable-face/1
font-lock-variable-name-face "\"foo {{\\\(bar)}}\"")
+
(provide 'font-lock-tests)
;;; font-lock-tests.el ends here
- [nongnu] elpa/swift-mode 34be010 074/496: Update features in readme, (continued)
- [nongnu] elpa/swift-mode 34be010 074/496: Update features in readme, ELPA Syncer, 2021/08/29
- [nongnu] elpa/swift-mode 6553249 062/496: Rename predicate (add question mark), ELPA Syncer, 2021/08/29
- [nongnu] elpa/swift-mode ff32221 068/496: Add font lock tests, ELPA Syncer, 2021/08/29
- [nongnu] elpa/swift-mode 5f97c99 060/496: Add tests, ELPA Syncer, 2021/08/29
- [nongnu] elpa/swift-mode c3a0a66 065/496: Fix custom variable types, ELPA Syncer, 2021/08/29
- [nongnu] elpa/swift-mode a17f9e8 066/496: Rename predicate functions ('?' -> -p), ELPA Syncer, 2021/08/29
- [nongnu] elpa/swift-mode 8dc7eb9 078/496: Clean up after pull request, ELPA Syncer, 2021/08/29
- [nongnu] elpa/swift-mode 98b627a 075/496: Add @ap4y to contributors, ELPA Syncer, 2021/08/29
- [nongnu] elpa/swift-mode 812b4f6 084/496: Remove redundant hook definition, ELPA Syncer, 2021/08/29
- [nongnu] elpa/swift-mode 8fc94c7 097/496: Merge pull request #25 from ap4y/smie_indentation, ELPA Syncer, 2021/08/29
- [nongnu] elpa/swift-mode 889586c 102/496: Merge pull request #28 from ap4y/highlight_interpolation,
ELPA Syncer <=
- [nongnu] elpa/swift-mode 0fcf3ab 115/496: Restructure test for multi-line hash in method call to test against case, ELPA Syncer, 2021/08/29
- [nongnu] elpa/swift-mode b0f5d13 113/496: Make commas non sticky, fixes indentation issues with optionals and, ELPA Syncer, 2021/08/29
- [nongnu] elpa/swift-mode d77b2df 122/496: Enable nested flag 'n' for C-style multi-line comments, ELPA Syncer, 2021/08/29
- [nongnu] elpa/swift-mode b43c1e2 129/496: Merge pull request #38 from ap4y/fix_36, ELPA Syncer, 2021/08/29
- [nongnu] elpa/swift-mode a028f68 127/496: Add "_" to the allowed symbols in string interpolation expression, ELPA Syncer, 2021/08/29
- [nongnu] elpa/swift-mode 2593659 131/496: Improve indentation for multi-line dot expression with dot positioned on, ELPA Syncer, 2021/08/29
- [nongnu] elpa/swift-mode 835c3d3 132/496: Merge pull request #40 from ap4y/improve_37, ELPA Syncer, 2021/08/29
- [nongnu] elpa/swift-mode 2eb3952 136/496: Merge pull request #45 from ap4y/dot_in_string_interpolation, ELPA Syncer, 2021/08/29
- [nongnu] elpa/swift-mode 5e3ab8c 139/496: Apply swift-indent-multiline-statement-offset to the multi-line, ELPA Syncer, 2021/08/29
- [nongnu] elpa/swift-mode 4e615b7 143/496: Merge pull request #50 from ap4y/string_interpolation_all_op, ELPA Syncer, 2021/08/29