Skip to content

Commit dba539e

Browse files
committed
Tweak and clean-up font-lock
1 parent 560242d commit dba539e

File tree

1 file changed

+102
-59
lines changed

1 file changed

+102
-59
lines changed

swift-mode-font-lock.el

Lines changed: 102 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -32,54 +32,91 @@
3232

3333
;;; Code:
3434

35-
(defun swift-mode:expr-pos-p (pos expr)
36-
"Return t if POS is just before the given expression."
37-
(save-excursion
38-
(save-match-data
39-
(goto-char pos)
40-
(forward-comment (- (point)))
41-
(skip-syntax-backward "w_")
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."
35+
(defun swift-mode:function-name-pos-p (pos limit)
36+
"Return t if POS is just before the name of a function declaration.
37+
38+
This function does not search beyond LIMIT."
39+
(goto-char pos)
40+
(forward-comment (- (point)))
41+
(skip-syntax-backward "w_")
42+
(looking-at
43+
"\\<\\(func\\|enum\\|struct\\|class\\|protocol\\|extension\\)\\>"))
44+
45+
(defun swift-mode:property-access-pos-p (pos limit)
46+
"Return t if POS is just before the property name of a member expression.
47+
48+
This function does not search beyond LIMIT."
49+
;; foo.bar // property access
50+
;; foo .bar // property access
51+
;; foo . bar // INVALID
52+
;; foo. bar // INVALID
53+
;; foo?.bar // property access
54+
;; foo?. bar // INVALID
55+
;; foo ?.bar // INVALID, but highlight as a property access anyway
56+
;; foo? .bar // property access
57+
;; foo.bar() // NOT property access
58+
;; foo.bar () // NOT property access
59+
;; foo.1 // property access
60+
;; foo1.1 // property access
61+
;; 1.1 // NOT property access
62+
;; .1 // NOT property access
63+
;; $1.1 // property access
64+
;; .x // property access
65+
(and
66+
;; Just after dot
67+
(progn
68+
(goto-char pos)
69+
(eq (char-before) ?.))
70+
71+
;; Not floating-point literal
72+
(progn
73+
(goto-char pos)
74+
(backward-char)
75+
(skip-syntax-backward "w_")
76+
(not (looking-at "[0-9]*\\.[0-9]+\\>")))
77+
78+
;; Not method/function call
79+
(progn
80+
(goto-char pos)
81+
(skip-syntax-forward "w_" limit)
82+
(skip-chars-forward "?")
83+
;; I don't sure we can use `forward-comment' beyond limit, so assuming
84+
;; no comments here.
85+
(skip-syntax-forward " " limit)
86+
(not (eq (char-after) ?\()))))
87+
88+
(defun swift-mode:font-lock-match-expr (limit match-p)
89+
"Move the cursor just after an identifier that satisfy given predicate.
90+
91+
Set `match-data', and return t if the identifier found before position LIMIT.
92+
Return nil otherwise.
93+
94+
The predicate MATCH-P is called with two arguments:
95+
- the position of the identifier, and
96+
- the limit of search functions."
6597
(and
6698
(< (point) limit)
6799
(re-search-forward "\\<\\(\\sw\\|\\s_\\)+\\>" limit t)
68100
(or
69-
(funcall fn (match-beginning 0))
70-
(swift-mode:font-lock-match-expr limit fn))))
101+
(save-excursion
102+
(save-match-data
103+
(funcall match-p (match-beginning 0) limit)))
104+
(swift-mode:font-lock-match-expr limit match-p))))
71105

72106
(defun swift-mode:font-lock-match-function-names (limit)
73107
"Move the cursor just after a function name or others.
74108
75-
Others includes enum, struct, class, protocol name.
76-
Set `match-data', and return t if a function name found before position LIMIT.
109+
Others includes enum, struct, class, protocol, and extension name.
110+
Set `match-data', and return t if a function name or others found before
111+
position LIMIT.
77112
Return nil otherwise."
78113
(swift-mode:font-lock-match-expr limit #'swift-mode:function-name-pos-p))
79114

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))
115+
(defun swift-mode:font-lock-match-property-accesss (limit)
116+
"Move the cursor just after a property access.
117+
Set `match-data', and return t if a property access found before position LIMIT.
118+
Return nil otherwise."
119+
(swift-mode:font-lock-match-expr limit #'swift-mode:property-access-pos-p))
83120

84121
;; Keywords
85122
;; https://developer.apple.com/library/ios/documentation/Swift/Conceptual/Swift_Programming_Language/LexicalStructure.html#//apple_ref/doc/uid/TP40014097-CH30-ID410
@@ -90,8 +127,9 @@ Return nil otherwise."
90127
"withUnsafeMutableBufferPointer" "withUnsafeMutablePointers"
91128
"withUnsafeMutablePointerToElements" "withUnsafeMutablePointerToHeader"
92129
"withUnsafeBufferPointer" "withUTF8Buffer" "min" "map" "max" "compactMap")
93-
"Member functions in the standard library in Swift 3 which may be used
94-
with trailing closures and no parentheses.")
130+
"Member functions with closures in the standard library in Swift 3.
131+
132+
They may be used with trailing closures and no parentheses.")
95133

96134
(defconst swift-mode:member-functions
97135
'("symmetricDifference" "storeBytes" "starts" "stride" "sortInPlace"
@@ -139,8 +177,9 @@ Return nil otherwise."
139177

140178
(defconst swift-mode:global-functions-trailing-closure
141179
'("anyGenerator" "autoreleasepool")
142-
"Global functions available in Swift 3 which may be used with trailing
143-
closures and no parentheses.")
180+
"Global functions with closures available in Swift 3.
181+
182+
They may be used with trailing closures and no parentheses.")
144183

145184
(defconst swift-mode:global-functions
146185
'("stride" "strideof" "strideofValue" "sizeof" "sizeofValue" "sequence" "swap"
@@ -187,8 +226,9 @@ Return nil otherwise."
187226
"point" "plus" "error" "emptyInput" "view" "quietNaN" "float"
188227
"attributedString" "awayFromZero" "rectangle" "range" "generated" "minus"
189228
"bool" "bezierPath")
190-
"Enum cases in the standard library - note that there is some overlap
191-
between these and the properties")
229+
"Enum cases in the standard library.
230+
231+
Note that there is some overlap between these and the properties.")
192232

193233
(defconst swift-mode:enum-types
194234
'("ImplicitlyUnwrappedOptional" "Representation" "MemoryLayout"
@@ -303,8 +343,7 @@ between these and the properties")
303343
"Precedence groups in the standard library.")
304344

305345
(defconst swift-mode:constant-keywords
306-
'(
307-
"true" "false" "nil"
346+
'("true" "false" "nil"
308347
;; CommandLine is an enum, but it acts like a constant.
309348
"CommandLine" "Process"
310349
;; The return type of a function that never returns.
@@ -331,8 +370,9 @@ between these and the properties")
331370
(defconst swift-mode:expression-keywords
332371
'("as" "catch" "dynamicType" "is" "rethrows" "super" "self" "Self" "throws"
333372
"throw" "try")
334-
"Keywords used in expressions and types (without true, false, and
335-
keywords begin with a number sign)")
373+
"Keywords used in expressions and types.
374+
375+
Excludes true, false, and keywords begin with a number sign.")
336376

337377
(defconst swift-mode:context-keywords
338378
'("Protocol" "Type" "and" "assignment" "associativity" "convenience" "didSet"
@@ -363,15 +403,16 @@ between these and the properties")
363403
,(regexp-opt (append swift-mode:declaration-keywords
364404
swift-mode:statement-keywords
365405
swift-mode:expression-keywords
366-
swift-mode:context-keywords) 'words)
406+
swift-mode:context-keywords)
407+
'words)
367408

368-
(,(concat "\\.\\s-*"
409+
(,(concat "\\."
369410
(regexp-opt swift-mode:member-functions-trailing-closure 'words)
370411
"\\s-*[({]")
371412
1
372413
font-lock-builtin-face)
373414

374-
(,(concat "\\.\\s-*"
415+
(,(concat "\\."
375416
(regexp-opt swift-mode:member-functions 'words)
376417
"\\s-*(")
377418
1
@@ -387,8 +428,9 @@ between these and the properties")
387428
1
388429
font-lock-builtin-face)
389430

390-
(,(concat "\\.\\s-*" (regexp-opt (append swift-mode:properties
391-
swift-mode:enum-cases) 'words))
431+
(,(concat "\\." (regexp-opt (append swift-mode:properties
432+
swift-mode:enum-cases)
433+
'words))
392434
1
393435
font-lock-builtin-face)
394436

@@ -399,7 +441,8 @@ between these and the properties")
399441
swift-mode:foundation-structs
400442
swift-mode:protocols
401443
swift-mode:structs
402-
swift-mode:typealiases) 'words)
444+
swift-mode:typealiases)
445+
'words)
403446
.
404447
font-lock-builtin-face)
405448

@@ -410,17 +453,17 @@ between these and the properties")
410453
font-lock-builtin-face)
411454

412455
;; Method/function calls
413-
(
414-
"\\b\\(\\<[^[:digit:]][_[:alnum:]]*?\\)\\b\\s-*\\??\\s-*("
456+
("\\<\\(\\(\\sw\\|\\s_\\)+\\)\\>\\??\\s-*("
415457
1
416-
font-lock-function-name-face
417-
)
458+
font-lock-function-name-face)
418459

419460
;; Function declarations
420461
(swift-mode:font-lock-match-function-names . font-lock-function-name-face)
421462

422-
;; Property calls
423-
(swift-mode:font-lock-match-property-calls . font-lock-variable-name-face))
463+
;; Property accesses
464+
(swift-mode:font-lock-match-property-accesss
465+
.
466+
font-lock-variable-name-face))
424467
"Swift mode keywords for Font Lock.")
425468

426469

0 commit comments

Comments
 (0)