Skip to content

Commit 4c579de

Browse files
committed
feat(swift): modernise syntax highlighting
- added better numeric literal support (hex, binary, octal, floats with underscores) - expanded keyword list with async/await, actor, protocol, and other modern Swift keywords - expanded type list including standard library types and SIMD types - added compilation directive support (#if, #available, etc.) - added discard literal (_) highlighting - updated attributes list with modern Swift modifiers - added support for escaped identifiers using backticks Signed-Off-By: Paal Øye-Strømme <paal.o.eye@gmail.com>
1 parent b4ce128 commit 4c579de

File tree

1 file changed

+47
-8
lines changed

1 file changed

+47
-8
lines changed

rc/filetype/swift.kak

Lines changed: 47 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,10 @@ provide-module swift %§
3535
add-highlighter shared/swift regions
3636
add-highlighter shared/swift/code default-region group
3737

38+
add-highlighter shared/swift/comment region /\* \*/ group
39+
add-highlighter shared/swift/line_comment region // $ ref swift/comment
40+
add-highlighter shared/swift/comment/ fill comment
41+
3842
add-highlighter shared/swift/string_multiline region %{(?<!')"""} %{(?<!\\)(\\\\)*"""} regions
3943
add-highlighter shared/swift/string_multiline/base default-region fill string
4044
add-highlighter shared/swift/string_multiline/interpolation region -recurse \( \Q\( \) fill meta
@@ -43,19 +47,54 @@ add-highlighter shared/swift/string region %{(?<!')"} %{(?<!\\)(\\\\)*"} regions
4347
add-highlighter shared/swift/string/base default-region fill string
4448
add-highlighter shared/swift/string/interpolation region -recurse \( \Q\( \) fill meta
4549

46-
add-highlighter shared/swift/comment region /\* \*/ group
47-
add-highlighter shared/swift/line_comment region // $ ref swift/comment
50+
# backticked identifiers
51+
add-highlighter shared/swift/backtick_identifier region ` ` fill meta
4852

49-
add-highlighter shared/swift/comment/ fill comment
53+
# discard literal
54+
add-highlighter shared/swift/code/ regex "\b_\b" 0:comment
5055

51-
add-highlighter shared/swift/code/ regex %{\b(true|false|nil)\b|\b-?(?!\$)\d+[fdiu]?|'((\\.)?|[^'\\])'} 0:value
52-
add-highlighter shared/swift/code/ regex "\b(let|var|while|in|for|if|guard|else|do|switch|case|default|break|continue|return|try|catch|throw|new|delete|and|or|not|operator|explicit|func|import|return|init|deinit|get|set)\b" 0:keyword
56+
# values
57+
add-highlighter shared/swift/code/ regex \b(?:true|false|nil)\b 0:value
58+
add-highlighter shared/swift/code/ regex \b(?:self|super)\b 0:value
59+
60+
# Numeric literals with underscores, hex, binary, octal, floats (plus/minus are not painted, matching Xcode)
61+
# - Decimal with underscores: 1_000_000, 3.141_592_653_59
62+
# - Hexadecimal: 0xFF, 0x1A_2B, 0xDEAD_BEEF
63+
# - Hex floats: 0xF.8p2, +0x1_A.F_Fp3
64+
# - Binary: 0b1010, 0b1111_0000
65+
# - Octal: 0o17, +0o7_7_7, 0o7_7_7
66+
# - Decimal floats: 6.28, 1.5e10, +1.5e-5, 1.5E+10
67+
# - Negative numbers: -42, -3.14
68+
#
69+
# Pattern breakdown:
70+
# [-+]? # optional plus or minus
71+
# (?:
72+
# 0x[0-9a-fA-F][_0-9a-fA-F]* # hex integer/float start
73+
# (?:\.[0-9a-fA-F][_0-9a-fA-F]*)? # optional hex fraction
74+
# (?:[pP][+-]?[0-9][_0-9]*)? # optional binary exponent
75+
# |0o[0-7][_0-7]* # octal
76+
# |0b[01][_01]* # binary
77+
# |[0-9][_0-9]* # decimal integer/float start
78+
# (?:\.[0-9][_0-9]*)? # optional decimal fraction
79+
# (?:[eE][+-]?[0-9][_0-9]*)? # optional decimal exponent
80+
# )
81+
#
82+
add-highlighter shared/swift/code/ regex \b[+-]?(?:0x[0-9a-fA-F][_0-9a-fA-F]*(?:\.[0-9a-fA-F][_0-9a-fA-F]*)?(?:[pP][+-]?[0-9][_0-9]*)?|0o[0-7][_0-7]*|0b[01][_01]*|[0-9][_0-9]*(?:\.[0-9][_0-9]*)?(?:[eE][+-]?[0-9][_0-9]*)?)\b 0:value
83+
84+
# keywords
85+
add-highlighter shared/swift/code/ regex "\b(let|var|while|in|for|if|guard|else|do|switch|case|default|break|continue|return|try|catch|throw|operator|func|import|init|deinit|get|set|defer|repeat|fallthrough|async|await|throws|rethrows|inout|where|is|subscript|macro|protocol|typealias|actor|class|struct|enum|extension|some|any|associatedtype|distributed|isolated|nonisolated|consuming|borrowing|borrow|move|discard|sending|nonsending)\b" 0:keyword
5386
add-highlighter shared/swift/code/ regex "\bas\b[!?]?" 0:keyword
5487
add-highlighter shared/swift/code/ regex "(\$[0-9])\b" 0:keyword
55-
add-highlighter shared/swift/code/ regex "\b(const|mutable|auto|namespace|inline|static|volatile|class|struct|enum|union|extension|open|public|protected|private|fileprivate|internal|typedef|virtual|friend|extern|typename|override|final|required|convenience|dynamic)\b" 0:attribute
5688

57-
add-highlighter shared/swift/code/ regex "\b(self|nil|id|super)\b" 0:value
58-
add-highlighter shared/swift/code/ regex "\b(Bool|String|UInt|UInt16|UInt32|UInt64|UInt8)\b" 0:type
89+
# types
90+
add-highlighter shared/swift/code/ regex "[\[]?\b(Bool|String|Character|Int|Int8|Int16|Int32|Int64|Int128|UInt|UInt8|UInt16|UInt32|UInt64|UInt128|Float|Float16|Float32|Float64|Float80|Double|Void|Never|Any|AnyObject|AnyClass|Optional|Array|Dictionary|Set|Range|ClosedRange|PartialRangeFrom|PartialRangeThrough|PartialRangeUpTo|Result|Error|Equatable|Hashable|Comparable|Codable|Encodable|Decodable|Sendable|CaseIterable|CodingKey|Task|TaskGroup|AsyncStream|AsyncThrowingStream|GlobalActor|UnsafePointer|UnsafeMutablePointer|UnsafeRawPointer|UnsafeMutableRawPointer|UnsafeBufferPointer|UnsafeMutableBufferPointer|UnsafeRawBufferPointer|UnsafeMutableRawBufferPointer|Unmanaged|AutoreleasingUnsafeMutablePointer|SIMD|SIMD2|SIMD3|SIMD4|SIMD8|SIMD16|SIMD32|SIMD64|SIMDMask|SIMDScalar|SIMDStorage)\b[\]]?[!?]?" 0:type
91+
92+
# compilation directives
93+
add-highlighter shared/swift/code/ regex "#(if|elseif|else|endif|available|unavailable|warning|error|sourceLocation|file|fileID|filePath|line|column|function|dsohandle|selector|keyPath|colorLiteral|imageLiteral|fileLiteral)\b" 0:meta
94+
add-highlighter shared/swift/code/ regex "\b(canImport|os|arch|swift|compiler|targetEnvironment)[\s\(]+\b" 1:meta
95+
96+
# attributes
97+
add-highlighter shared/swift/code/ regex "\b(inline|static|open|public|private|fileprivate|internal|package|override|final|required|convenience|dynamic|lazy|mutating|nonmutating|indirect|weak|unowned|didSet|willSet)\b" 0:attribute
5998
add-highlighter shared/swift/code/ regex "\b(IBAction|IBOutlet)\b" 0:attribute
6099
add-highlighter shared/swift/code/ regex "@\w+\b" 0:attribute
61100

0 commit comments

Comments
 (0)