Skip to content

Commit be9709d

Browse files
committed
Add support for highlighting function calls / properties / enum cases
1 parent 869eff9 commit be9709d

File tree

1 file changed

+48
-10
lines changed

1 file changed

+48
-10
lines changed

swift-mode-font-lock.el

Lines changed: 48 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -32,27 +32,54 @@
3232

3333
;;; Code:
3434

35-
(defun swift-mode:function-name-pos-p (pos)
36-
"Return t if POS is at just before function name."
35+
(defun swift-mode:expr-pos-p (pos expr)
36+
"Return t if POS is just before the given expression."
3737
(save-excursion
3838
(save-match-data
3939
(goto-char pos)
4040
(forward-comment (- (point)))
4141
(skip-syntax-backward "w_")
42-
(looking-at "\\<\\(func\\|enum\\|struct\\|class\\|protocol\\|extension\\)\\>"))))
42+
(funcall expr))))
43+
44+
(defun swift-mode:function-name-pos-p (pos)
45+
"Return t if POS is just before a function name."
46+
(swift-mode:expr-pos-p
47+
pos
48+
(lambda ()
49+
(progn
50+
(skip-syntax-backward "w_")
51+
(looking-at "\\<\\(func\\|enum\\|struct\\|class\\|protocol\\|extension\\)\\>")))))
52+
53+
(defun swift-mode:property-call-pos-p (pos)
54+
"Return t if POS is just before a property call."
55+
(swift-mode:expr-pos-p
56+
pos
57+
(lambda ()
58+
(progn
59+
(skip-syntax-backward "w_")
60+
(skip-syntax-backward ".")
61+
(looking-at "\\.\\s-*\\(\\<[^[:digit:]][_[:alnum:]]*?\\)\\b\\s-*[^(]")))))
62+
63+
(defun swift-mode:font-lock-match-expr (limit fn)
64+
"Return t if POS is just before the given keyword expression."
65+
(and
66+
(< (point) limit)
67+
(re-search-forward "\\<\\(\\sw\\|\\s_\\)+\\>" limit t)
68+
(or
69+
(funcall fn (match-beginning 0))
70+
(swift-mode:font-lock-match-expr limit fn))))
4371

4472
(defun swift-mode:font-lock-match-function-names (limit)
4573
"Move the cursor just after a function name or others.
4674
4775
Others includes enum, struct, class, protocol name.
4876
Set `match-data', and return t if a function name found before position LIMIT.
4977
Return nil otherwise."
50-
(and
51-
(< (point) limit)
52-
(re-search-forward "\\<\\(\\sw\\|\\s_\\)+\\>" limit t)
53-
(or
54-
(swift-mode:function-name-pos-p (match-beginning 0))
55-
(swift-mode:font-lock-match-function-names limit))))
78+
(swift-mode:font-lock-match-expr limit #'swift-mode:function-name-pos-p))
79+
80+
(defun swift-mode:font-lock-match-property-calls (limit)
81+
"Move the cursor just after a property call."
82+
(swift-mode:font-lock-match-expr limit #'swift-mode:property-call-pos-p))
5683

5784
;; Keywords
5885
;; https://developer.apple.com/library/ios/documentation/Swift/Conceptual/Swift_Programming_Language/LexicalStructure.html#//apple_ref/doc/uid/TP40014097-CH30-ID410
@@ -382,7 +409,18 @@ between these and the properties")
382409
.
383410
font-lock-builtin-face)
384411

385-
(swift-mode:font-lock-match-function-names . font-lock-function-name-face))
412+
;; Method/function calls
413+
(
414+
"\\b\\(\\<[^[:digit:]][_[:alnum:]]*?\\)\\b\\s-*("
415+
1
416+
font-lock-function-name-face
417+
)
418+
419+
;; Function declarations
420+
(swift-mode:font-lock-match-function-names . font-lock-function-name-face)
421+
422+
;; Property calls
423+
(swift-mode:font-lock-match-property-calls . font-lock-variable-name-face))
386424
"Swift mode keywords for Font Lock.")
387425

388426

0 commit comments

Comments
 (0)