comparison rc/vim-syntax-coffee @ 410:fd0a889066a6

Added syntax highlight for coffeescript.
author Edho Arief <edho@myconan.net>
date Mon, 12 Nov 2012 16:59:35 +0700
parents
children
comparison
equal deleted inserted replaced
409:fcb116fc12f9 410:fd0a889066a6
1 " Language: CoffeeScript
2 " Maintainer: Mick Koch <kchmck@gmail.com>
3 " URL: http://github.com/kchmck/vim-coffee-script
4 " License: WTFPL
5
6 " Bail if our syntax is already loaded.
7 if exists('b:current_syntax') && b:current_syntax == 'coffee'
8 finish
9 endif
10
11 " Include JavaScript for coffeeEmbed.
12 syn include @coffeeJS syntax/javascript.vim
13
14 " Highlight long strings.
15 syn sync minlines=100
16
17 " CoffeeScript identifiers can have dollar signs.
18 setlocal isident+=$
19
20 " These are `matches` instead of `keywords` because vim's highlighting
21 " priority for keywords is higher than matches. This causes keywords to be
22 " highlighted inside matches, even if a match says it shouldn't contain them --
23 " like with coffeeAssign and coffeeDot.
24 syn match coffeeStatement /\<\%(return\|break\|continue\|throw\)\>/ display
25 hi def link coffeeStatement Statement
26
27 syn match coffeeRepeat /\<\%(for\|while\|until\|loop\)\>/ display
28 hi def link coffeeRepeat Repeat
29
30 syn match coffeeConditional /\<\%(if\|else\|unless\|switch\|when\|then\)\>/
31 \ display
32 hi def link coffeeConditional Conditional
33
34 syn match coffeeException /\<\%(try\|catch\|finally\)\>/ display
35 hi def link coffeeException Exception
36
37 syn match coffeeKeyword /\<\%(new\|in\|of\|by\|and\|or\|not\|is\|isnt\|class\|extends\|super\|do\)\>/
38 \ display
39 " The `own` keyword is only a keyword after `for`.
40 syn match coffeeKeyword /\<for\s\+own\>/ contained containedin=coffeeRepeat
41 \ display
42 hi def link coffeeKeyword Keyword
43
44 syn match coffeeOperator /\<\%(instanceof\|typeof\|delete\)\>/ display
45 hi def link coffeeOperator Operator
46
47 " The first case matches symbol operators only if they have an operand before.
48 syn match coffeeExtendedOp /\%(\S\s*\)\@<=[+\-*/%&|\^=!<>?.]\{-1,}\|[-=]>\|--\|++\|:/
49 \ display
50 syn match coffeeExtendedOp /\<\%(and\|or\)=/ display
51 hi def link coffeeExtendedOp coffeeOperator
52
53 " This is separate from `coffeeExtendedOp` to help differentiate commas from
54 " dots.
55 syn match coffeeSpecialOp /[,;]/ display
56 hi def link coffeeSpecialOp SpecialChar
57
58 syn match coffeeBoolean /\<\%(true\|on\|yes\|false\|off\|no\)\>/ display
59 hi def link coffeeBoolean Boolean
60
61 syn match coffeeGlobal /\<\%(null\|undefined\)\>/ display
62 hi def link coffeeGlobal Type
63
64 " A special variable
65 syn match coffeeSpecialVar /\<\%(this\|prototype\|arguments\)\>/ display
66 hi def link coffeeSpecialVar Special
67
68 " An @-variable
69 syn match coffeeSpecialIdent /@\%(\I\i*\)\?/ display
70 hi def link coffeeSpecialIdent Identifier
71
72 " A class-like name that starts with a capital letter
73 syn match coffeeObject /\<\u\w*\>/ display
74 hi def link coffeeObject Structure
75
76 " A constant-like name in SCREAMING_CAPS
77 syn match coffeeConstant /\<\u[A-Z0-9_]\+\>/ display
78 hi def link coffeeConstant Constant
79
80 " A variable name
81 syn cluster coffeeIdentifier contains=coffeeSpecialVar,coffeeSpecialIdent,
82 \ coffeeObject,coffeeConstant
83
84 " A non-interpolated string
85 syn cluster coffeeBasicString contains=@Spell,coffeeEscape
86 " An interpolated string
87 syn cluster coffeeInterpString contains=@coffeeBasicString,coffeeInterp
88
89 " Regular strings
90 syn region coffeeString start=/"/ skip=/\\\\\|\\"/ end=/"/
91 \ contains=@coffeeInterpString
92 syn region coffeeString start=/'/ skip=/\\\\\|\\'/ end=/'/
93 \ contains=@coffeeBasicString
94 hi def link coffeeString String
95
96 " A integer, including a leading plus or minus
97 syn match coffeeNumber /\i\@<![-+]\?\d\+\%([eE][+-]\?\d\+\)\?/ display
98 " A hex, binary, or octal number
99 syn match coffeeNumber /\<0[xX]\x\+\>/ display
100 syn match coffeeNumber /\<0[bB][01]\+\>/ display
101 syn match coffeeNumber /\<0[oO][0-7]\+\>/ display
102 hi def link coffeeNumber Number
103
104 " A floating-point number, including a leading plus or minus
105 syn match coffeeFloat /\i\@<![-+]\?\d*\.\@<!\.\d\+\%([eE][+-]\?\d\+\)\?/
106 \ display
107 hi def link coffeeFloat Float
108
109 " An error for reserved keywords
110 if !exists("coffee_no_reserved_words_error")
111 syn match coffeeReservedError /\<\%(case\|default\|function\|var\|void\|with\|const\|let\|enum\|export\|import\|native\|__hasProp\|__extends\|__slice\|__bind\|__indexOf\|implements\|interface\|let\|package\|private\|protected\|public\|static\|yield\)\>/
112 \ display
113 hi def link coffeeReservedError Error
114 endif
115
116 " A normal object assignment
117 syn match coffeeObjAssign /@\?\I\i*\s*\ze::\@!/ contains=@coffeeIdentifier display
118 hi def link coffeeObjAssign Identifier
119
120 syn keyword coffeeTodo TODO FIXME XXX contained
121 hi def link coffeeTodo Todo
122
123 syn match coffeeComment /#.*/ contains=@Spell,coffeeTodo
124 hi def link coffeeComment Comment
125
126 syn region coffeeBlockComment start=/####\@!/ end=/###/
127 \ contains=@Spell,coffeeTodo
128 hi def link coffeeBlockComment coffeeComment
129
130 " A comment in a heregex
131 syn region coffeeHeregexComment start=/#/ end=/\ze\/\/\/\|$/ contained
132 \ contains=@Spell,coffeeTodo
133 hi def link coffeeHeregexComment coffeeComment
134
135 " Embedded JavaScript
136 syn region coffeeEmbed matchgroup=coffeeEmbedDelim
137 \ start=/`/ skip=/\\\\\|\\`/ end=/`/
138 \ contains=@coffeeJS
139 hi def link coffeeEmbedDelim Delimiter
140
141 syn region coffeeInterp matchgroup=coffeeInterpDelim start=/#{/ end=/}/ contained
142 \ contains=@coffeeAll
143 hi def link coffeeInterpDelim PreProc
144
145 " A string escape sequence
146 syn match coffeeEscape /\\\d\d\d\|\\x\x\{2\}\|\\u\x\{4\}\|\\./ contained display
147 hi def link coffeeEscape SpecialChar
148
149 " A regex -- must not follow a parenthesis, number, or identifier, and must not
150 " be followed by a number
151 syn region coffeeRegex start=/\%(\%()\|\i\@<!\d\)\s*\|\i\)\@<!\/=\@!\s\@!/
152 \ skip=/\[[^\]]\{-}\/[^\]]\{-}\]/
153 \ end=/\/[gimy]\{,4}\d\@!/
154 \ oneline contains=@coffeeBasicString
155 hi def link coffeeRegex String
156
157 " A heregex
158 syn region coffeeHeregex start=/\/\/\// end=/\/\/\/[gimy]\{,4}/
159 \ contains=@coffeeInterpString,coffeeHeregexComment
160 \ fold
161 hi def link coffeeHeregex coffeeRegex
162
163 " Heredoc strings
164 syn region coffeeHeredoc start=/"""/ end=/"""/ contains=@coffeeInterpString
165 \ fold
166 syn region coffeeHeredoc start=/'''/ end=/'''/ contains=@coffeeBasicString
167 \ fold
168 hi def link coffeeHeredoc String
169
170 " An error for trailing whitespace, as long as the line isn't just whitespace
171 if !exists("coffee_no_trailing_space_error")
172 syn match coffeeSpaceError /\S\@<=\s\+$/ display
173 hi def link coffeeSpaceError Error
174 endif
175
176 " An error for trailing semicolons, for help transitioning from JavaScript
177 if !exists("coffee_no_trailing_semicolon_error")
178 syn match coffeeSemicolonError /;$/ display
179 hi def link coffeeSemicolonError Error
180 endif
181
182 " Ignore reserved words in dot accesses.
183 syn match coffeeDotAccess /\.\@<!\.\s*\I\i*/he=s+1 contains=@coffeeIdentifier
184 hi def link coffeeDotAccess coffeeExtendedOp
185
186 " Ignore reserved words in prototype accesses.
187 syn match coffeeProtoAccess /::\s*\I\i*/he=s+2 contains=@coffeeIdentifier
188 hi def link coffeeProtoAccess coffeeExtendedOp
189
190 " This is required for interpolations to work.
191 syn region coffeeCurlies matchgroup=coffeeCurly start=/{/ end=/}/
192 \ contains=@coffeeAll
193 syn region coffeeBrackets matchgroup=coffeeBracket start=/\[/ end=/\]/
194 \ contains=@coffeeAll
195 syn region coffeeParens matchgroup=coffeeParen start=/(/ end=/)/
196 \ contains=@coffeeAll
197
198 " These are highlighted the same as commas since they tend to go together.
199 hi def link coffeeBlock coffeeSpecialOp
200 hi def link coffeeBracket coffeeBlock
201 hi def link coffeeCurly coffeeBlock
202 hi def link coffeeParen coffeeBlock
203
204 " This is used instead of TOP to keep things coffee-specific for good
205 " embedding. `contained` groups aren't included.
206 syn cluster coffeeAll contains=coffeeStatement,coffeeRepeat,coffeeConditional,
207 \ coffeeException,coffeeKeyword,coffeeOperator,
208 \ coffeeExtendedOp,coffeeSpecialOp,coffeeBoolean,
209 \ coffeeGlobal,coffeeSpecialVar,coffeeSpecialIdent,
210 \ coffeeObject,coffeeConstant,coffeeString,
211 \ coffeeNumber,coffeeFloat,coffeeReservedError,
212 \ coffeeObjAssign,coffeeComment,coffeeBlockComment,
213 \ coffeeEmbed,coffeeRegex,coffeeHeregex,
214 \ coffeeHeredoc,coffeeSpaceError,
215 \ coffeeSemicolonError,coffeeDotAccess,
216 \ coffeeProtoAccess,coffeeCurlies,coffeeBrackets,
217 \ coffeeParens
218
219 if !exists('b:current_syntax')
220 let b:current_syntax = 'coffee'
221 endif