Skip to content

Vim syntax improvements #2886

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 11 commits into from
Jun 5, 2016
95 changes: 50 additions & 45 deletions utils/vim/syntax/swift.vim
Original file line number Diff line number Diff line change
Expand Up @@ -7,53 +7,46 @@ if exists("b:current_syntax")
finish
endif

syn keyword swiftImport import skipwhite nextgroup=swiftImportModule

syn match swiftImportModule /\<[A-Za-z_][A-Za-z_0-9]*\>/ contained nextgroup=swiftImportComponent
syn match swiftImportComponent /\.\<[A-Za-z_][A-Za-z_0-9]*\>/ contained nextgroup=swiftImportComponent

syn keyword swiftKeyword
\ associatedtype
\ break
\ case
\ catch
\ continue
\ convenience
\ default
\ defer
\ do
\ else
\ extension
\ final
\ for
\ guard
\ if
\ in
\ internal
\ let
\ mutating
\ nonmutating
\ override
\ private
\ protocol
\ public
\ repeat
\ required
\ return
\ static
\ switch
\ throw
\ try
\ typealias
\ var
\ where
\ while
syn match swiftMultiwordKeyword
\ "indirect case"

syn keyword swiftImport skipwhite nextgroup=swiftImportModule
\ import

syn keyword swiftDefinitionModifier
\ convenience
\ dynamic
\ final
\ internal
\ nonmutating
\ override
\ private
\ public
\ required
\ rethrows
\ static
\ throws
Expand All @@ -75,6 +68,11 @@ syn keyword swiftFuncKeyword
syn keyword swiftScope
\ autoreleasepool

syn keyword swiftMutating skipwhite nextgroup=swiftFuncDefinition
\ mutating
syn keyword swiftFuncDefinition skipwhite nextgroup=swiftTypeName,swiftOperator
\ func

syn keyword swiftTypeDefinition skipwhite nextgroup=swiftTypeName
\ class
\ enum
Expand All @@ -83,47 +81,57 @@ syn keyword swiftTypeDefinition skipwhite nextgroup=swiftTypeName
\ struct
\ typealias

syn keyword swiftNew skipwhite nextgroup=swiftTypeName
\ new
syn keyword swiftVarDefinition skipwhite nextgroup=swiftVarName
\ let
\ var

syn keyword swiftLabel
\ get
\ set

syn keyword swiftBoolean
\ false
\ true

syn keyword swiftNil
\ nil

syn match swiftImportModule contained nextgroup=swiftImportComponent
\ /\<[A-Za-z_][A-Za-z_0-9]*\>/
syn match swiftImportComponent contained nextgroup=swiftImportComponent
\ /\.\<[A-Za-z_][A-Za-z_0-9]*\>/

syn match swiftTypeName contained nextgroup=swiftTypeParameters
\ /\<[A-Za-z_][A-Za-z_0-9\.]*\>/
syn match swiftVarName contained skipwhite nextgroup=swiftTypeDeclaration
\ /\<[A-Za-z_][A-Za-z_0-9]*\>/
syn match swiftImplicitVarName
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this code trying to match the conformance constraints?

func foo<T : X, T.U : Y>() {}
         ^^^^^  ^^^^^^^^

The constrained type can be a nested one, so I'm not sure that swiftVarName would work.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, though the Archetype was matching the same way as a VarName. In fact, this is related to the reason for the previously stated "bug". We don't mark the type decl as being contained (because we have no way to contain it in the function parameter list). A VarName is a pattern of [: Type].

That said, with the trailing constraints, I think that the lifetime for this matching is limited anyways.

\ /\$\<[A-Za-z_0-9]\+\>/

" TypeName[Optionality]?
syn match swiftType contained nextgroup=swiftTypeParameters
\ /\<[A-Za-z_][A-Za-z_0-9\.]*\>[!?]\?/
" [Type:Type] (dictionary) or [Type] (array)
syn region swiftType contained contains=swiftTypePair,swiftType
\ start=/\[/ end=/\]/
\ matchgroup=Delimiter start=/\[/ end=/\]/
syn match swiftTypePair contained nextgroup=swiftTypeParameters,swiftTypeDeclaration
\ /\<[A-Za-z_][A-Za-z_0-9\.]*\>[!?]\?/
" (Type[, Type]) (tuple)
" FIXME: we should be able to use skip="," and drop swiftParamDelim
syn region swiftType contained contains=swiftType,swiftParamDelim
\ start="[^@](" end=")"
\ matchgroup=Delimiter start="[^@](" end=")" matchgroup=NONE skip=","
syn match swiftParamDelim contained
\ /,/
" <Generic Clause> (generics)
syn region swiftTypeParameters contained contains=swiftArchetype,swiftConstraint
\ start="<" end=">"
syn match swiftArchetype contained skipwhite nextgroup=swiftTypeDeclaration
\ /\<[A-Za-z_][A-Za-z_0-9]*\>/
syn region swiftTypeParameters contained contains=swiftVarName,swiftConstraint
\ matchgroup=Delimiter start="<" end=">" matchgroup=NONE skip=","
syn keyword swiftConstraint contained
\ where

syn keyword swiftMutating mutating skipwhite nextgroup=swiftFuncDefinition
syn keyword swiftFuncDefinition func skipwhite nextgroup=swiftFuncName,swiftOperator
syn match swiftFuncName /\<[A-Za-z_][A-Za-z_0-9]*\>/ contained skipwhite nextgroup=swiftTypeParameters

syn keyword swiftVarDefinition var skipwhite nextgroup=swiftVarName
syn keyword swiftVarDefinition let skipwhite nextgroup=swiftVarName
syn match swiftVarName /\<[A-Za-z_][A-Za-z_0-9]*\>/ contained

syn match swiftImplicitVarName /\$\<[A-Za-z_0-9]\+\>/

syn match swiftTypeDeclaration /:/ skipwhite nextgroup=swiftType
syn match swiftTypeDeclaration /->/ skipwhite nextgroup=swiftType

syn keyword swiftBoolean true false
syn match swiftTypeDeclaration skipwhite nextgroup=swiftType
\ /:/
syn match swiftTypeDeclaration skipwhite nextgroup=swiftType
\ /->/

syn region swiftString start=/"/ skip=/\\\\\|\\"/ end=/"/ contains=swiftInterpolation
syn region swiftInterpolation start=/\\(/ end=/)/ contained
Expand All @@ -140,16 +148,15 @@ syn match swiftOperator "\.\.[<.]" skipwhite nextgroup=swiftTypeParameters

syn match swiftChar /'\([^'\\]\|\\\(["'tnr0\\]\|x[0-9a-fA-F]\{2}\|u[0-9a-fA-F]\{4}\|U[0-9a-fA-F]\{8}\)\)'/

syn keyword swiftLabel get set

syn match swiftPreproc /#\(\<file\>\|\<line\>\)/
syn match swiftPreproc /^\s*#\(\<if\>\|\<else\>\|\<elseif\>\|\<endif\>\)/
syn region swiftPreprocFalse start="^\s*#\<if\>\s\+\<false\>" end="^\s*#\(\<else\>\|\<elseif\>\|\<endif\>\)"

syn match swiftAttribute /@\<\w\+\>/ skipwhite nextgroup=swiftType

syn keyword swiftTodo TODO FIXME contained
syn keyword swiftNil nil

syn match swiftCastOp "\<is\>" skipwhite nextgroup=swiftType
syn match swiftCastOp "\<as\>[!?]\?" skipwhite nextgroup=swiftType

syn match swiftNilOps "??"
Expand All @@ -161,15 +168,14 @@ hi def link swiftImport Include
hi def link swiftImportModule Title
hi def link swiftImportComponent Identifier
hi def link swiftKeyword Statement
hi def link swiftMultiwordKeyword Statement
hi def link swiftTypeDefinition Define
hi def link swiftType Type
hi def link swiftTypePair Type
hi def link swiftTypeName Function
hi def link swiftArchetype Identifier
hi def link swiftConstraint Special
hi def link swiftFuncDefinition Define
hi def link swiftDefinitionModifier Define
hi def link swiftFuncName Function
hi def link swiftFuncKeyword Function
hi def link swiftFuncKeywordGeneral Function
hi def link swiftVarDefinition Define
Expand All @@ -190,7 +196,6 @@ hi def link swiftBin Number
hi def link swiftOperator Function
hi def link swiftChar Character
hi def link swiftLabel Operator
hi def link swiftNew Operator
hi def link swiftMutating Statement
hi def link swiftPreproc PreCondit
hi def link swiftPreprocFalse Comment
Expand Down