Skip to content

Commit ba4a17c

Browse files
committed
vim: differentiate between a type and a typename
A typename is a name for a type being defined. A type may contain optionality as well. This ensures that we do not accidentally highlight an invalid name such as `InvalidName?` as a type name. Fix the fact that I had previously created a special group for the array. An array is a type, so it should be part of the same group.
1 parent 99365ad commit ba4a17c

File tree

1 file changed

+32
-11
lines changed

1 file changed

+32
-11
lines changed

utils/vim/syntax/swift.vim

Lines changed: 32 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -72,10 +72,33 @@ syn keyword swiftFuncKeyword nextgroup=swiftTypeParameters
7272
syn keyword swiftScope
7373
\ autoreleasepool
7474

75-
syn keyword swiftTypeDefinition class extension protocol struct typealias enum skipwhite nextgroup=swiftTypeName
75+
syn keyword swiftTypeDefinition skipwhite nextgroup=swiftTypeName
76+
\ class
77+
\ enum
78+
\ extension
79+
\ protocol
80+
\ struct
81+
\ typealias
7682

77-
syn match swiftTypeName /\<[A-Za-z_][A-Za-z_0-9\.]*\>[!?]\?/ contained nextgroup=swiftTypeParameters
78-
syn region swiftArrayType start=/\[/ end=/\]/ contained skipwhite nextgroup=swiftTypeName
83+
syn keyword swiftNew skipwhite nextgroup=swiftTypeName
84+
\ new
85+
86+
syn match swiftTypeName contained nextgroup=swiftTypeParameters
87+
\ /\<[A-Za-z_][A-Za-z_0-9\.]*\>/
88+
89+
" TypeName[Optionality]?
90+
syn match swiftType contained nextgroup=swiftTypeParameters
91+
\ /\<[A-Za-z_][A-Za-z_0-9\.]*\>[!?]\?/
92+
" [Type:Type] (dictionary) or [Type] (array)
93+
syn region swiftType contained contains=swiftTypePair,swiftType
94+
\ start=/\[/ end=/\]/
95+
syn match swiftTypePair contained nextgroup=swiftTypeParameters,swiftTypeDeclaration
96+
\ /\<[A-Za-z_][A-Za-z_0-9\.]*\>[!?]\?/
97+
" (Type[, Type]) (tuple)
98+
syn region swiftType contained contains=swiftType,swiftParamDelim
99+
\ start="[^@](" end=")"
100+
syn match swiftParamDelim contained
101+
\ /,/
79102

80103
syn region swiftTypeParameters start="<" end=">" contained
81104

@@ -89,11 +112,8 @@ syn match swiftVarName /\<[A-Za-z_][A-Za-z_0-9]*\>/ contained
89112

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

92-
syn match swiftTypeDeclaration /:/ skipwhite nextgroup=swiftTypeName
93-
syn match swiftTypeDeclaration /->/ skipwhite nextgroup=swiftTypeName
94-
95-
96-
syn keyword swiftNew new skipwhite nextgroup=swiftTypeName
115+
syn match swiftTypeDeclaration /:/ skipwhite nextgroup=swiftType
116+
syn match swiftTypeDeclaration /->/ skipwhite nextgroup=swiftType
97117

98118
syn keyword swiftBoolean true false
99119

@@ -122,7 +142,7 @@ syn match swiftAttribute /@\<\w\+\>/
122142
syn keyword swiftTodo TODO FIXME contained
123143
syn keyword swiftNil nil
124144

125-
syn match swiftCastOp "\<as\>[!?]\?" skipwhite nextgroup=swiftTypeName,swiftArrayType
145+
syn match swiftCastOp "\<as\>[!?]\?" skipwhite nextgroup=swiftType
126146

127147
syn match swiftNilOps "??"
128148

@@ -131,8 +151,9 @@ hi def link swiftImportModule Title
131151
hi def link swiftImportComponent Identifier
132152
hi def link swiftKeyword Statement
133153
hi def link swiftTypeDefinition Define
134-
hi def link swiftTypeName Type
135-
hi def link swiftArrayType Type
154+
hi def link swiftType Type
155+
hi def link swiftTypePair Type
156+
hi def link swiftTypeName Function
136157
hi def link swiftTypeParameters Special
137158
hi def link swiftFuncDefinition Define
138159
hi def link swiftDefinitionModifier Define

0 commit comments

Comments
 (0)