coffee.vim 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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*\)\@<=[+\-*/%&|\^=!<>?.]\+\|[-=]>\|--\|++\|:/
  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. " An @-variable
  52. syn match coffeeSpecialVar /@\%(\I\i*\)\?/ display
  53. hi def link coffeeSpecialVar Special
  54. " A class-like name that starts with a capital letter
  55. syn match coffeeObject /\<\u\w*\>/ display
  56. hi def link coffeeObject Structure
  57. " A constant-like name in SCREAMING_CAPS
  58. syn match coffeeConstant /\<\u[A-Z0-9_]\+\>/ display
  59. hi def link coffeeConstant Constant
  60. " A variable name
  61. syn cluster coffeeIdentifier contains=coffeeSpecialVar,coffeeObject,
  62. \ coffeeConstant
  63. " A non-interpolated string
  64. syn cluster coffeeBasicString contains=@Spell,coffeeEscape
  65. " An interpolated string
  66. syn cluster coffeeInterpString contains=@coffeeBasicString,coffeeInterp
  67. " Regular strings
  68. syn region coffeeString start=/"/ skip=/\\\\\|\\"/ end=/"/
  69. \ contains=@coffeeInterpString
  70. syn region coffeeString start=/'/ skip=/\\\\\|\\'/ end=/'/
  71. \ contains=@coffeeBasicString
  72. hi def link coffeeString String
  73. " A integer, including a leading plus or minus
  74. syn match coffeeNumber /\i\@<![-+]\?\d\+\%([eE][+-]\?\d\+\)\?/ display
  75. " A hex number
  76. syn match coffeeNumber /\<0[xX]\x\+\>/ display
  77. syn match coffeeNumber /\<0b[01]\+\>/ display
  78. hi def link coffeeNumber Number
  79. " A floating-point number, including a leading plus or minus
  80. syn match coffeeFloat /\i\@<![-+]\?\d*\.\@<!\.\d\+\%([eE][+-]\?\d\+\)\?/
  81. \ display
  82. hi def link coffeeFloat Float
  83. " An error for reserved keywords
  84. if !exists("coffee_no_reserved_words_error")
  85. syn match coffeeReservedError /\<\%(case\|default\|function\|var\|void\|with\|const\|let\|enum\|export\|import\|native\|__hasProp\|__extends\|__slice\|__bind\|__indexOf\)\>/
  86. \ display
  87. hi def link coffeeReservedError Error
  88. endif
  89. " A normal object assignment
  90. syn match coffeeObjAssign /@\?\I\i*\s*\ze::\@!/ contains=@coffeeIdentifier display
  91. hi def link coffeeObjAssign Identifier
  92. syn keyword coffeeTodo TODO FIXME XXX contained
  93. hi def link coffeeTodo Todo
  94. syn match coffeeComment /#.*/ contains=@Spell,coffeeTodo
  95. hi def link coffeeComment Comment
  96. syn region coffeeBlockComment start=/####\@!/ end=/###/
  97. \ contains=@Spell,coffeeTodo
  98. hi def link coffeeBlockComment coffeeComment
  99. " A comment in a heregex
  100. syn region coffeeHeregexComment start=/#/ end=/\ze\/\/\/\|$/ contained
  101. \ contains=@Spell,coffeeTodo
  102. hi def link coffeeHeregexComment coffeeComment
  103. " Embedded JavaScript
  104. syn region coffeeEmbed matchgroup=coffeeEmbedDelim
  105. \ start=/`/ skip=/\\\\\|\\`/ end=/`/
  106. \ contains=@coffeeJS
  107. hi def link coffeeEmbedDelim Delimiter
  108. syn region coffeeInterp matchgroup=coffeeInterpDelim start=/#{/ end=/}/ contained
  109. \ contains=@coffeeAll
  110. hi def link coffeeInterpDelim PreProc
  111. " A string escape sequence
  112. syn match coffeeEscape /\\\d\d\d\|\\x\x\{2\}\|\\u\x\{4\}\|\\./ contained display
  113. hi def link coffeeEscape SpecialChar
  114. " A regex -- must not follow a parenthesis, number, or identifier, and must not
  115. " be followed by a number
  116. syn region coffeeRegex start=/\%(\%()\|\i\@<!\d\)\s*\|\i\)\@<!\/=\@!\s\@!/
  117. \ skip=/\[[^\]]\{-}\/[^\]]\{-}\]/
  118. \ end=/\/[gimy]\{,4}\d\@!/
  119. \ oneline contains=@coffeeBasicString
  120. hi def link coffeeRegex String
  121. " A heregex
  122. syn region coffeeHeregex start=/\/\/\// end=/\/\/\/[gimy]\{,4}/
  123. \ contains=@coffeeInterpString,coffeeHeregexComment
  124. \ fold
  125. hi def link coffeeHeregex coffeeRegex
  126. " Heredoc strings
  127. syn region coffeeHeredoc start=/"""/ end=/"""/ contains=@coffeeInterpString
  128. \ fold
  129. syn region coffeeHeredoc start=/'''/ end=/'''/ contains=@coffeeBasicString
  130. \ fold
  131. hi def link coffeeHeredoc String
  132. " An error for trailing whitespace, as long as the line isn't just whitespace
  133. if !exists("coffee_no_trailing_space_error")
  134. syn match coffeeSpaceError /\S\@<=\s\+$/ display
  135. hi def link coffeeSpaceError Error
  136. endif
  137. " An error for trailing semicolons, for help transitioning from JavaScript
  138. if !exists("coffee_no_trailing_semicolon_error")
  139. syn match coffeeSemicolonError /;$/ display
  140. hi def link coffeeSemicolonError Error
  141. endif
  142. " Ignore reserved words in dot accesses.
  143. syn match coffeeDotAccess /\.\@<!\.\s*\I\i*/he=s+1 contains=@coffeeIdentifier
  144. hi def link coffeeDotAccess coffeeExtendedOp
  145. " Ignore reserved words in prototype accesses.
  146. syn match coffeeProtoAccess /::\s*\I\i*/he=s+2 contains=@coffeeIdentifier
  147. hi def link coffeeProtoAccess coffeeExtendedOp
  148. " This is required for interpolations to work.
  149. syn region coffeeCurlies matchgroup=coffeeCurly start=/{/ end=/}/
  150. \ contains=@coffeeAll
  151. syn region coffeeBrackets matchgroup=coffeeBracket start=/\[/ end=/\]/
  152. \ contains=@coffeeAll
  153. syn region coffeeParens matchgroup=coffeeParen start=/(/ end=/)/
  154. \ contains=@coffeeAll
  155. " These are highlighted the same as commas since they tend to go together.
  156. hi def link coffeeBlock coffeeSpecialOp
  157. hi def link coffeeBracket coffeeBlock
  158. hi def link coffeeCurly coffeeBlock
  159. hi def link coffeeParen coffeeBlock
  160. " This is used instead of TOP to keep things coffee-specific for good
  161. " embedding. `contained` groups aren't included.
  162. syn cluster coffeeAll contains=coffeeStatement,coffeeRepeat,coffeeConditional,
  163. \ coffeeException,coffeeKeyword,coffeeOperator,
  164. \ coffeeExtendedOp,coffeeSpecialOp,coffeeBoolean,
  165. \ coffeeGlobal,coffeeSpecialVar,coffeeObject,
  166. \ coffeeConstant,coffeeString,coffeeNumber,
  167. \ coffeeFloat,coffeeReservedError,coffeeObjAssign,
  168. \ coffeeComment,coffeeBlockComment,coffeeEmbed,
  169. \ coffeeRegex,coffeeHeregex,coffeeHeredoc,
  170. \ coffeeSpaceError,coffeeSemicolonError,
  171. \ coffeeDotAccess,coffeeProtoAccess,
  172. \ coffeeCurlies,coffeeBrackets,coffeeParens
  173. if !exists('b:current_syntax')
  174. let b:current_syntax = 'coffee'
  175. endif