kakoune

Fork of kakoune, an acceptable text editor
git clone git://git.laack.co/kakoune.git
Log | Files | Refs

swift.kak (7257B)


      1 #
      2 # MARK: - detection
      3 #
      4 
      5 hook global BufCreate .*\.(swift|swiftinterface) %{
      6     set-option buffer filetype swift
      7 }
      8 
      9 #
     10 # MARK: - initialisation
     11 #
     12 
     13 hook global WinSetOption filetype=swift %<
     14     require-module swift
     15     hook window ModeChange pop:insert:.* -group swift-trim-indent swift-trim-indent
     16 
     17     hook window InsertChar \n -group swift-insert swift-insert-on-new-line
     18     hook window InsertChar \n -group swift-indent swift-indent-on-new-line
     19     hook window InsertChar \} -group swift-indent swift-indent-on-closing
     20 
     21     hook -once -always window WinSetOption filetype=.* %< remove-hooks window swift-.+ >
     22 >
     23 
     24 hook -group swift-highlight global WinSetOption filetype=swift %{
     25     add-highlighter window/swift ref swift
     26     hook -once -always window WinSetOption filetype=.* %{ remove-highlighter window/swift }
     27 }
     28 
     29 provide-module swift %§
     30 
     31 #
     32 # MARK: - highlighters
     33 #
     34 
     35 add-highlighter shared/swift regions
     36 add-highlighter shared/swift/code default-region group
     37 
     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 
     42 add-highlighter shared/swift/string_multiline region %{(?<!')"""} %{(?<!\\)(\\\\)*"""} regions
     43 add-highlighter shared/swift/string_multiline/base default-region fill string
     44 add-highlighter shared/swift/string_multiline/interpolation region -recurse \( \Q\( \) fill meta
     45 
     46 add-highlighter shared/swift/string region %{(?<!')"} %{(?<!\\)(\\\\)*"} regions
     47 add-highlighter shared/swift/string/base default-region fill string
     48 add-highlighter shared/swift/string/interpolation region -recurse \( \Q\( \) fill meta
     49 
     50 # backticked identifiers
     51 add-highlighter shared/swift/backtick_identifier region ` ` fill meta
     52 
     53 # discard literal
     54 add-highlighter shared/swift/code/ regex "\b_\b" 0:comment
     55 
     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
     86 add-highlighter shared/swift/code/ regex "\bas\b[!?]?" 0:keyword
     87 add-highlighter shared/swift/code/ regex "(\$[0-9])\b" 0:keyword
     88 
     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
     98 add-highlighter shared/swift/code/ regex "\b(IBAction|IBOutlet)\b" 0:attribute
     99 add-highlighter shared/swift/code/ regex "@\w+\b" 0:attribute
    100 
    101 #
    102 # MARK: commands
    103 #
    104 
    105 define-command -hidden swift-trim-indent %{
    106     # delete trailing whitespace
    107     try %{ execute-keys -draft -itersel x s \h+$ <ret> d }
    108 }
    109 
    110 define-command -hidden swift-insert-on-new-line %<
    111     try %[
    112         evaluate-commands -draft -save-regs '/"\' %[
    113             # copy // comments prefix and following whitespace
    114             execute-keys -save-regs '' k x1s^\h*(//+\h*)<ret> y
    115             try %[
    116                 # if the previous comment isn't empty, create a new one
    117                 execute-keys x<a-K>^\h*//+\h*$<ret> jxs^\h*<ret>P
    118             ] catch %[
    119                 # if there is no text in the previous comment, remove it completely
    120                 execute-keys d
    121             ]
    122         ]
    123 
    124         # trim trailing whitespace on the previous line
    125         try %[ execute-keys -draft k x s\h+$<ret> d ]
    126     ]
    127 >
    128 
    129 define-command -hidden swift-indent-on-new-line %<
    130     evaluate-commands -draft -itersel %<
    131         # preserve indent level
    132         try %< execute-keys -draft <semicolon> K <a-&> >
    133         try %<
    134             # only if we didn't copy a comment
    135             execute-keys -draft x <a-K> ^\h*// <ret>
    136             # indent after lines ending in {
    137             try %< execute-keys -draft k x <a-k> \{\h*$ <ret> j <a-gt> >
    138             # indent after lines ending in 'in' (closure parameters)
    139             try %< execute-keys -draft k x <a-k> \bin\h*$ <ret> j <a-gt> >
    140             # deindent closing } when after cursor
    141             try %< execute-keys -draft x <a-k> ^\h*\} <ret> gh / \} <ret> m <a-S> 1<a-&> >
    142         >
    143         # filter previous line
    144         try %< execute-keys -draft k : swift-trim-indent <ret> >
    145     >
    146 >
    147 
    148 define-command -hidden swift-indent-on-closing %<
    149     # align lone } to indent level of opening line
    150     try %< execute-keys -draft -itersel <a-h> <a-k> ^\h*\}$ <ret> h m <a-S> 1<a-&> >
    151 >
    152 
    153 §