vim-polyglot/syntax/swift.vim

290 lines
8.3 KiB
VimL
Raw Normal View History

if !exists('g:polyglot_disabled') || index(g:polyglot_disabled, 'swift') == -1
2015-12-06 05:31:38 -05:00
" File: swift.vim
" Author: Keith Smiley
" Description: Runtime files for Swift
" Last Modified: June 15, 2014
2014-08-12 18:17:05 -04:00
2015-12-06 05:31:38 -05:00
if exists("b:current_syntax")
2014-08-12 18:17:05 -04:00
finish
endif
2015-12-06 05:31:38 -05:00
" Comments
" Shebang
syntax match swiftShebang "\v#!.*$"
2014-08-12 18:17:05 -04:00
2015-12-06 05:31:38 -05:00
" Comment contained keywords
syntax keyword swiftTodos contained TODO XXX FIXME NOTE
syntax keyword swiftMarker contained MARK
2016-01-22 03:08:00 -05:00
" In comment identifiers
function! s:CommentKeywordMatch(keyword)
execute "syntax match swiftDocString \"\\v^\\s*-\\s*". a:keyword . "\\W\"hs=s+1,he=e-1 contained"
endfunction
syntax case ignore
call s:CommentKeywordMatch("attention")
call s:CommentKeywordMatch("author")
call s:CommentKeywordMatch("authors")
call s:CommentKeywordMatch("bug")
call s:CommentKeywordMatch("complexity")
call s:CommentKeywordMatch("copyright")
call s:CommentKeywordMatch("date")
call s:CommentKeywordMatch("experiment")
call s:CommentKeywordMatch("important")
call s:CommentKeywordMatch("invariant")
call s:CommentKeywordMatch("note")
call s:CommentKeywordMatch("parameter")
call s:CommentKeywordMatch("postcondition")
call s:CommentKeywordMatch("precondition")
call s:CommentKeywordMatch("remark")
call s:CommentKeywordMatch("remarks")
call s:CommentKeywordMatch("requires")
call s:CommentKeywordMatch("returns")
call s:CommentKeywordMatch("see")
call s:CommentKeywordMatch("since")
call s:CommentKeywordMatch("throws")
call s:CommentKeywordMatch("todo")
call s:CommentKeywordMatch("version")
call s:CommentKeywordMatch("warning")
syntax case match
delfunction s:CommentKeywordMatch
2014-08-12 18:17:05 -04:00
2015-12-06 05:31:38 -05:00
" Literals
" Strings
2016-01-22 03:08:00 -05:00
syntax region swiftString start=/"/ skip=/\\\\\|\\"/ end=/"/ contains=swiftInterpolatedWrapper oneline
2016-05-02 04:42:37 -04:00
syntax region swiftInterpolatedWrapper start="\v[^\\]\zs\\\(\s*" end="\v\s*\)" contained containedin=swiftString contains=swiftInterpolatedString,swiftString oneline
syntax match swiftInterpolatedString "\v\w+(\(\))?" contained containedin=swiftInterpolatedWrapper oneline
2014-08-12 18:17:05 -04:00
2015-12-06 05:31:38 -05:00
" Numbers
syntax match swiftNumber "\v<\d+>"
syntax match swiftNumber "\v<(\d+_+)+\d+(\.\d+(_+\d+)*)?>"
syntax match swiftNumber "\v<\d+\.\d+>"
syntax match swiftNumber "\v<\d*\.?\d+([Ee]-?)?\d+>"
2016-05-13 09:54:46 -04:00
syntax match swiftNumber "\v<0x[[:xdigit:]_]+([Pp]-?)?\x+>"
syntax match swiftNumber "\v<0b[01_]+>"
syntax match swiftNumber "\v<0o[0-7_]+>"
2014-08-12 18:17:05 -04:00
2015-12-06 05:31:38 -05:00
" BOOLs
syntax keyword swiftBoolean
\ true
\ false
" Operators
syntax match swiftOperator "\v\~"
syntax match swiftOperator "\v\s+!"
syntax match swiftOperator "\v\%"
syntax match swiftOperator "\v\^"
syntax match swiftOperator "\v\&"
syntax match swiftOperator "\v\*"
syntax match swiftOperator "\v-"
syntax match swiftOperator "\v\+"
syntax match swiftOperator "\v\="
syntax match swiftOperator "\v\|"
syntax match swiftOperator "\v\/"
syntax match swiftOperator "\v\."
syntax match swiftOperator "\v\<"
syntax match swiftOperator "\v\>"
syntax match swiftOperator "\v\?\?"
2014-08-12 18:17:05 -04:00
2016-05-02 04:42:37 -04:00
" Methods/Functions/Properties
2015-12-06 05:31:38 -05:00
syntax match swiftMethod "\(\.\)\@<=\w\+\((\)\@="
2016-05-02 04:42:37 -04:00
syntax match swiftProperty "\(\.\)\@<=\<\w\+\>(\@!"
2015-12-06 05:31:38 -05:00
" Swift closure arguments
syntax match swiftClosureArgument "\$\d\+\(\.\d\+\)\?"
syntax match swiftAvailability "\v((\*(\s*,\s*[a-zA-Z="0-9.]+)*)|(\w+\s+\d+(\.\d+(.\d+)?)?\s*,\s*)+\*)" contains=swiftString
syntax keyword swiftPlatforms OSX iOS watchOS OSXApplicationExtension iOSApplicationExtension contained containedin=swiftAvailability
syntax keyword swiftAvailabilityArg renamed unavailable introduced deprecated obsoleted message contained containedin=swiftAvailability
" Keywords {{{
syntax keyword swiftKeywords
2016-05-02 04:42:37 -04:00
\ associatedtype
2016-12-20 14:57:20 -05:00
\ associativity
2015-12-06 05:31:38 -05:00
\ atexit
\ break
\ case
\ catch
\ class
\ continue
\ convenience
\ default
\ defer
\ deinit
\ didSet
\ do
\ dynamic
\ else
\ extension
\ fallthrough
2016-07-26 07:43:53 -04:00
\ fileprivate
2015-12-06 05:31:38 -05:00
\ final
\ for
\ func
\ get
\ guard
\ if
\ import
\ in
\ infix
\ init
\ inout
\ internal
\ lazy
\ let
\ mutating
\ nil
\ nonmutating
\ operator
\ optional
\ override
\ postfix
2016-12-20 14:57:20 -05:00
\ precedence
\ precedencegroup
2015-12-06 05:31:38 -05:00
\ prefix
\ private
\ protocol
\ public
\ repeat
\ required
\ rethrows
\ return
\ self
\ set
\ static
\ subscript
\ super
\ switch
\ throw
\ throws
\ try
\ typealias
\ unowned
\ var
\ weak
\ where
\ while
\ willSet
2016-05-02 04:42:37 -04:00
syntax match swiftMultiwordKeywords "indirect case"
2017-02-02 15:16:29 -05:00
syntax match swiftMultiwordKeywords "indirect enum"
2014-08-12 18:17:05 -04:00
" }}}
2015-12-06 05:31:38 -05:00
" Names surrounded by backticks. This aren't limited to keywords because 1)
" Swift doesn't limit them to keywords and 2) I couldn't make the keywords not
" highlight at the same time
syntax region swiftEscapedReservedWord start="`" end="`" oneline
syntax keyword swiftAttributes
\ @assignment
\ @autoclosure
\ @available
\ @convention
2016-12-20 14:57:20 -05:00
\ @discardableResult
2015-12-06 05:31:38 -05:00
\ @exported
\ @IBAction
\ @IBDesignable
\ @IBInspectable
\ @IBOutlet
\ @noescape
\ @nonobjc
\ @noreturn
\ @NSApplicationMain
\ @NSCopying
\ @NSManaged
\ @objc
\ @testable
\ @UIApplicationMain
\ @warn_unused_result
syntax keyword swiftConditionStatement #available
syntax keyword swiftStructure
\ struct
\ enum
2016-05-02 04:42:37 -04:00
syntax keyword swiftDebugIdentifier
\ #column
\ #file
\ #function
\ #line
\ __COLUMN__
\ __FILE__
\ __FUNCTION__
\ __LINE__
syntax keyword swiftLineDirective #setline
syntax region swiftTypeWrapper start="\v:\s*" skip="\s*,\s*$*\s*" end="$\|/"me=e-1 contains=ALLBUT,swiftInterpolatedWrapper transparent
syntax region swiftTypeCastWrapper start="\(as\|is\)\(!\|?\)\=\s\+" end="\v(\s|$|\{)" contains=swiftType,swiftCastKeyword keepend transparent oneline
2015-12-06 05:31:38 -05:00
syntax region swiftGenericsWrapper start="\v\<" end="\v\>" contains=swiftType transparent oneline
2016-01-22 03:08:00 -05:00
syntax region swiftLiteralWrapper start="\v\=\s*" skip="\v[^\[\]]\(\)" end="\v(\[\]|\(\))" contains=ALL transparent oneline
2015-12-06 05:31:38 -05:00
syntax region swiftReturnWrapper start="\v-\>\s*" end="\v(\{|$)" contains=swiftType transparent oneline
2016-05-02 04:42:37 -04:00
syntax match swiftType "\v<\u\w*" contained containedin=swiftTypeWrapper,swiftLiteralWrapper,swiftGenericsWrapper,swiftTypeCastWrapper
2015-12-06 05:31:38 -05:00
syntax keyword swiftImports import
2016-05-02 04:42:37 -04:00
syntax keyword swiftCastKeyword is as contained
2015-12-06 05:31:38 -05:00
" 'preprocesor' stuff
syntax keyword swiftPreprocessor
\ #if
\ #elseif
\ #else
\ #endif
2016-05-02 04:42:37 -04:00
\ #selector
2015-12-06 05:31:38 -05:00
" Comment patterns
syntax match swiftComment "\v\/\/.*$"
\ contains=swiftTodos,swiftDocString,swiftMarker,@Spell oneline
syntax region swiftComment start="/\*" end="\*/"
\ contains=swiftTodos,swiftDocString,swiftMarker,swiftComment,@Spell fold
" Set highlights
highlight default link swiftTodos Todo
highlight default link swiftDocString String
highlight default link swiftShebang Comment
highlight default link swiftComment Comment
highlight default link swiftMarker Comment
highlight default link swiftString String
highlight default link swiftInterpolatedWrapper Delimiter
highlight default link swiftNumber Number
highlight default link swiftBoolean Boolean
highlight default link swiftOperator Operator
2016-05-02 04:42:37 -04:00
highlight default link swiftCastKeyword Keyword
2015-12-06 05:31:38 -05:00
highlight default link swiftKeywords Keyword
2016-05-02 04:42:37 -04:00
highlight default link swiftMultiwordKeywords Keyword
2015-12-06 05:31:38 -05:00
highlight default link swiftEscapedReservedWord Normal
highlight default link swiftClosureArgument Operator
highlight default link swiftAttributes PreProc
highlight default link swiftConditionStatement PreProc
highlight default link swiftStructure Structure
highlight default link swiftType Type
highlight default link swiftImports Include
highlight default link swiftPreprocessor PreProc
highlight default link swiftMethod Function
2016-05-02 04:42:37 -04:00
highlight default link swiftProperty Identifier
2015-12-06 05:31:38 -05:00
highlight default link swiftConditionStatement PreProc
highlight default link swiftAvailability Normal
highlight default link swiftAvailabilityArg Normal
highlight default link swiftPlatforms Keyword
2016-05-02 04:42:37 -04:00
highlight default link swiftDebugIdentifier PreProc
highlight default link swiftLineDirective PreProc
2015-12-06 05:31:38 -05:00
" Force vim to sync at least x lines. This solves the multiline comment not
" being highlighted issue
syn sync minlines=100
let b:current_syntax = "swift"
endif