scala.vim 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. " Vim syntax file
  2. " Language: Scala
  3. " Maintainer: Derek Wyatt
  4. " URL: https://github.com/derekwyatt/vim-scala
  5. " License: Same as Vim
  6. " Last Change: 23 January 2022
  7. " ----------------------------------------------------------------------------
  8. if !exists('main_syntax')
  9. " quit when a syntax file was already loaded
  10. if exists("b:current_syntax")
  11. finish
  12. endif
  13. let main_syntax = 'scala'
  14. endif
  15. scriptencoding utf-8
  16. let b:current_syntax = "scala"
  17. " Allows for embedding, see #59; main_syntax convention instead? Refactor TOP
  18. "
  19. " The @Spell here is a weird hack, it means *exclude* if the first group is
  20. " TOP. Otherwise we get spelling errors highlighted on code elements that
  21. " match scalaBlock, even with `syn spell notoplevel`.
  22. function! s:ContainedGroup()
  23. try
  24. silent syn list @scala
  25. return '@scala,@NoSpell'
  26. catch /E392/
  27. return 'TOP,@Spell'
  28. endtry
  29. endfunction
  30. unlet! b:current_syntax
  31. syn case match
  32. syn sync minlines=200 maxlines=1000
  33. syn keyword scalaKeyword catch do else final finally for forSome if
  34. syn keyword scalaKeyword match return throw try while yield macro
  35. syn keyword scalaKeyword class trait object extends with nextgroup=scalaInstanceDeclaration skipwhite
  36. syn keyword scalaKeyword case nextgroup=scalaKeyword,scalaCaseFollowing skipwhite
  37. syn keyword scalaKeyword val nextgroup=scalaNameDefinition,scalaQuasiQuotes skipwhite
  38. syn keyword scalaKeyword def var nextgroup=scalaNameDefinition skipwhite
  39. hi def link scalaKeyword Keyword
  40. exe 'syn region scalaBlock start=/{/ end=/}/ contains=' . s:ContainedGroup() . ' fold'
  41. syn keyword scalaAkkaSpecialWord when goto using startWith initialize onTransition stay become unbecome
  42. hi def link scalaAkkaSpecialWord PreProc
  43. syn keyword scalatestSpecialWord shouldBe
  44. syn match scalatestShouldDSLA /^\s\+\zsit should/
  45. syn match scalatestShouldDSLB /\<should\>/
  46. hi def link scalatestSpecialWord PreProc
  47. hi def link scalatestShouldDSLA PreProc
  48. hi def link scalatestShouldDSLB PreProc
  49. syn match scalaSymbol /'[_A-Za-z0-9$]\+/
  50. hi def link scalaSymbol Number
  51. syn match scalaChar /'.'/
  52. syn match scalaChar /'\\[\\"'ntbrf]'/ contains=scalaEscapedChar
  53. syn match scalaChar /'\\u[A-Fa-f0-9]\{4}'/ contains=scalaUnicodeChar
  54. syn match scalaEscapedChar /\\[\\"'ntbrf]/
  55. syn match scalaUnicodeChar /\\u[A-Fa-f0-9]\{4}/
  56. hi def link scalaChar Character
  57. hi def link scalaEscapedChar Special
  58. hi def link scalaUnicodeChar Special
  59. syn match scalaOperator "||"
  60. syn match scalaOperator "&&"
  61. syn match scalaOperator "|"
  62. syn match scalaOperator "&"
  63. hi def link scalaOperator Special
  64. syn match scalaNameDefinition /\<[_A-Za-z0-9$]\+\>/ contained nextgroup=scalaPostNameDefinition,scalaVariableDeclarationList
  65. syn match scalaNameDefinition /`[^`]\+`/ contained nextgroup=scalaPostNameDefinition
  66. syn match scalaVariableDeclarationList /\s*,\s*/ contained nextgroup=scalaNameDefinition
  67. syn match scalaPostNameDefinition /\_s*:\_s*/ contained nextgroup=scalaTypeDeclaration
  68. hi def link scalaNameDefinition Function
  69. syn match scalaInstanceDeclaration /\<[_\.A-Za-z0-9$]\+\>/ contained nextgroup=scalaInstanceHash
  70. syn match scalaInstanceDeclaration /`[^`]\+`/ contained
  71. syn match scalaInstanceHash /#/ contained nextgroup=scalaInstanceDeclaration
  72. hi def link scalaInstanceDeclaration Special
  73. hi def link scalaInstanceHash Type
  74. syn match scalaUnimplemented /???/
  75. hi def link scalaUnimplemented ERROR
  76. syn match scalaCapitalWord /\<[A-Z][A-Za-z0-9$]*\>/
  77. hi def link scalaCapitalWord Special
  78. " Handle type declarations specially
  79. syn region scalaTypeStatement matchgroup=Keyword start=/\<type\_s\+\ze/ end=/$/ contains=scalaTypeTypeDeclaration,scalaSquareBrackets,scalaTypeTypeEquals,scalaTypeStatement
  80. " Ugh... duplication of all the scalaType* stuff to handle special highlighting
  81. " of `type X =` declarations
  82. syn match scalaTypeTypeDeclaration /(/ contained nextgroup=scalaTypeTypeExtension,scalaTypeTypeEquals contains=scalaRoundBrackets skipwhite
  83. syn match scalaTypeTypeDeclaration /\%(⇒\|=>\)\ze/ contained nextgroup=scalaTypeTypeDeclaration contains=scalaTypeTypeExtension skipwhite
  84. syn match scalaTypeTypeDeclaration /\<[_\.A-Za-z0-9$]\+\>/ contained nextgroup=scalaTypeTypeExtension,scalaTypeTypeEquals skipwhite
  85. syn match scalaTypeTypeEquals /=\ze[^>]/ contained nextgroup=scalaTypeTypePostDeclaration skipwhite
  86. syn match scalaTypeTypeExtension /)\?\_s*\zs\%(⇒\|=>\|<:\|:>\|=:=\|::\|#\)/ contained contains=scalaTypeOperator nextgroup=scalaTypeTypeDeclaration skipwhite
  87. syn match scalaTypeTypePostDeclaration /\<[_\.A-Za-z0-9$]\+\>/ contained nextgroup=scalaTypeTypePostExtension skipwhite
  88. syn match scalaTypeTypePostExtension /\%(⇒\|=>\|<:\|:>\|=:=\|::\)/ contained contains=scalaTypeOperator nextgroup=scalaTypeTypePostDeclaration skipwhite
  89. hi def link scalaTypeTypeDeclaration Type
  90. hi def link scalaTypeTypeExtension Keyword
  91. hi def link scalaTypeTypePostDeclaration Special
  92. hi def link scalaTypeTypePostExtension Keyword
  93. syn match scalaTypeDeclaration /(/ contained nextgroup=scalaTypeExtension contains=scalaRoundBrackets skipwhite
  94. syn match scalaTypeDeclaration /\%(⇒\|=>\)\ze/ contained nextgroup=scalaTypeDeclaration contains=scalaTypeExtension skipwhite
  95. syn match scalaTypeDeclaration /\<[_\.A-Za-z0-9$]\+\>/ contained nextgroup=scalaTypeExtension skipwhite
  96. syn match scalaTypeExtension /)\?\_s*\zs\%(⇒\|=>\|<:\|:>\|=:=\|::\|#\)/ contained contains=scalaTypeOperator nextgroup=scalaTypeDeclaration skipwhite
  97. hi def link scalaTypeDeclaration Type
  98. hi def link scalaTypeExtension Keyword
  99. hi def link scalaTypePostExtension Keyword
  100. syn match scalaTypeAnnotation /\%([_a-zA-Z0-9$\s]:\_s*\)\ze[_=(\.A-Za-z0-9$]\+/ skipwhite nextgroup=scalaTypeDeclaration contains=scalaRoundBrackets
  101. syn match scalaTypeAnnotation /)\_s*:\_s*\ze[_=(\.A-Za-z0-9$]\+/ skipwhite nextgroup=scalaTypeDeclaration
  102. hi clear scalaTypeAnnotation
  103. syn match scalaCaseFollowing /\<[_\.A-Za-z0-9$]\+\>/ contained contains=scalaCapitalWord
  104. syn match scalaCaseFollowing /`[^`]\+`/ contained contains=scalaCapitalWord
  105. hi def link scalaCaseFollowing Special
  106. syn keyword scalaKeywordModifier abstract override final lazy implicit private protected sealed null super
  107. syn keyword scalaSpecialFunction implicitly require
  108. hi def link scalaKeywordModifier Function
  109. hi def link scalaSpecialFunction Function
  110. syn keyword scalaSpecial this true false ne eq
  111. syn keyword scalaSpecial new nextgroup=scalaInstanceDeclaration skipwhite
  112. syn match scalaSpecial "\%(=>\|⇒\|<-\|←\|->\|→\)"
  113. syn match scalaSpecial /`[^`]\+`/ " Backtick literals
  114. hi def link scalaSpecial PreProc
  115. syn keyword scalaExternal package import
  116. hi def link scalaExternal Include
  117. syn match scalaStringEmbeddedQuote /\\"/ contained
  118. syn region scalaString start=/"/ end=/"/ contains=scalaStringEmbeddedQuote,scalaEscapedChar,scalaUnicodeChar
  119. hi def link scalaString String
  120. hi def link scalaStringEmbeddedQuote String
  121. syn region scalaIString matchgroup=scalaInterpolationBrackets start=/\<[a-zA-Z][a-zA-Z0-9_]*"/ skip=/\\"/ end=/"/ contains=scalaInterpolation,scalaInterpolationB,scalaEscapedChar,scalaUnicodeChar
  122. syn region scalaTripleIString matchgroup=scalaInterpolationBrackets start=/\<[a-zA-Z][a-zA-Z0-9_]*"""/ end=/"""\ze\%([^"]\|$\)/ contains=scalaInterpolation,scalaInterpolationB,scalaEscapedChar,scalaUnicodeChar
  123. hi def link scalaIString String
  124. hi def link scalaTripleIString String
  125. syn match scalaInterpolation /\$[a-zA-Z0-9_$]\+/ contained
  126. exe 'syn region scalaInterpolationB matchgroup=scalaInterpolationBoundary start=/\${/ end=/}/ contained contains=' . s:ContainedGroup()
  127. hi def link scalaInterpolation Function
  128. hi clear scalaInterpolationB
  129. syn region scalaFString matchgroup=scalaInterpolationBrackets start=/f"/ skip=/\\"/ end=/"/ contains=scalaFInterpolation,scalaFInterpolationB,scalaEscapedChar,scalaUnicodeChar
  130. syn match scalaFInterpolation /\$[a-zA-Z0-9_$]\+\(%[-A-Za-z0-9\.]\+\)\?/ contained
  131. exe 'syn region scalaFInterpolationB matchgroup=scalaInterpolationBoundary start=/${/ end=/}\(%[-A-Za-z0-9\.]\+\)\?/ contained contains=' . s:ContainedGroup()
  132. hi def link scalaFString String
  133. hi def link scalaFInterpolation Function
  134. hi clear scalaFInterpolationB
  135. syn region scalaTripleString start=/"""/ end=/"""\%([^"]\|$\)/ contains=scalaEscapedChar,scalaUnicodeChar
  136. syn region scalaTripleFString matchgroup=scalaInterpolationBrackets start=/f"""/ end=/"""\%([^"]\|$\)/ contains=scalaFInterpolation,scalaFInterpolationB,scalaEscapedChar,scalaUnicodeChar
  137. hi def link scalaTripleString String
  138. hi def link scalaTripleFString String
  139. hi def link scalaInterpolationBrackets Special
  140. hi def link scalaInterpolationBoundary Function
  141. syn match scalaNumber /\<0[dDfFlL]\?\>/ " Just a bare 0
  142. syn match scalaNumber /\<[1-9]\d*[dDfFlL]\?\>/ " A multi-digit number - octal numbers with leading 0's are deprecated in Scala
  143. syn match scalaNumber /\<0[xX][0-9a-fA-F]\+[dDfFlL]\?\>/ " Hex number
  144. syn match scalaNumber /\%(\<\d\+\.\d*\|\.\d\+\)\%([eE][-+]\=\d\+\)\=[fFdD]\=/ " exponential notation 1
  145. syn match scalaNumber /\<\d\+[eE][-+]\=\d\+[fFdD]\=\>/ " exponential notation 2
  146. syn match scalaNumber /\<\d\+\%([eE][-+]\=\d\+\)\=[fFdD]\>/ " exponential notation 3
  147. hi def link scalaNumber Number
  148. syn region scalaRoundBrackets start="(" end=")" skipwhite contained contains=scalaTypeDeclaration,scalaSquareBrackets,scalaRoundBrackets
  149. syn region scalaSquareBrackets matchgroup=scalaSquareBracketsBrackets start="\[" end="\]" skipwhite nextgroup=scalaTypeExtension contains=scalaTypeDeclaration,scalaSquareBrackets,scalaTypeOperator,scalaTypeAnnotationParameter
  150. syn match scalaTypeOperator /[-+=:<>]\+/ contained
  151. syn match scalaTypeAnnotationParameter /@\<[`_A-Za-z0-9$]\+\>/ contained
  152. hi def link scalaSquareBracketsBrackets Type
  153. hi def link scalaTypeOperator Keyword
  154. hi def link scalaTypeAnnotationParameter Function
  155. syn match scalaShebang "\%^#!.*" display
  156. syn region scalaMultilineComment start="/\*" end="\*/" contains=scalaMultilineComment,scalaDocLinks,scalaParameterAnnotation,scalaCommentAnnotation,scalaTodo,scalaCommentCodeBlock,@Spell keepend fold
  157. syn match scalaCommentAnnotation "@[_A-Za-z0-9$]\+" contained
  158. syn match scalaParameterAnnotation "\%(@tparam\|@param\|@see\)" nextgroup=scalaParamAnnotationValue skipwhite contained
  159. syn match scalaParamAnnotationValue /[.`_A-Za-z0-9$]\+/ contained
  160. syn region scalaDocLinks start="\[\[" end="\]\]" contained
  161. syn region scalaCommentCodeBlock matchgroup=Keyword start="{{{" end="}}}" contained
  162. syn match scalaTodo "\vTODO|FIXME|XXX" contained
  163. hi def link scalaShebang Comment
  164. hi def link scalaMultilineComment Comment
  165. hi def link scalaDocLinks Function
  166. hi def link scalaParameterAnnotation Function
  167. hi def link scalaParamAnnotationValue Keyword
  168. hi def link scalaCommentAnnotation Function
  169. hi def link scalaCommentCodeBlock String
  170. hi def link scalaTodo Todo
  171. syn match scalaAnnotation /@\<[`_A-Za-z0-9$]\+\>/
  172. hi def link scalaAnnotation PreProc
  173. syn match scalaTrailingComment "//.*$" contains=scalaTodo,@Spell
  174. hi def link scalaTrailingComment Comment
  175. syn match scalaAkkaFSM /goto([^)]*)\_s\+\<using\>/ contains=scalaAkkaFSMGotoUsing
  176. syn match scalaAkkaFSM /stay\_s\+using/
  177. syn match scalaAkkaFSM /^\s*stay\s*$/
  178. syn match scalaAkkaFSM /when\ze([^)]*)/
  179. syn match scalaAkkaFSM /startWith\ze([^)]*)/
  180. syn match scalaAkkaFSM /initialize\ze()/
  181. syn match scalaAkkaFSM /onTransition/
  182. syn match scalaAkkaFSM /onTermination/
  183. syn match scalaAkkaFSM /whenUnhandled/
  184. syn match scalaAkkaFSMGotoUsing /\<using\>/
  185. syn match scalaAkkaFSMGotoUsing /\<goto\>/
  186. hi def link scalaAkkaFSM PreProc
  187. hi def link scalaAkkaFSMGotoUsing PreProc
  188. let b:current_syntax = 'scala'
  189. if main_syntax ==# 'scala'
  190. unlet main_syntax
  191. endif
  192. " vim:set sw=2 sts=2 ts=8 et: