coffee.vim 8.7 KB

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