blob: c08e60e55aef8a6059addfd609efb3b331f829c1 [file] [log] [blame]
Bram Moolenaare4a3bcf2016-08-26 19:52:37 +02001" Vim syntax file
2" Language: Scala
3" Maintainer: Derek Wyatt
4" URL: https://github.com/derekwyatt/vim-scala
5" License: Same as Vim
Bram Moolenaar6f4754b2022-01-23 12:07:04 +00006" Last Change: 23 January 2022
Bram Moolenaare4a3bcf2016-08-26 19:52:37 +02007" ----------------------------------------------------------------------------
8
9if !exists('main_syntax')
Bram Moolenaar89bcfda2016-08-30 23:26:57 +020010 " quit when a syntax file was already loaded
11 if exists("b:current_syntax")
Bram Moolenaare4a3bcf2016-08-26 19:52:37 +020012 finish
13 endif
14 let main_syntax = 'scala'
15endif
16
17scriptencoding utf-8
18
19let b:current_syntax = "scala"
20
21" Allows for embedding, see #59; main_syntax convention instead? Refactor TOP
22"
23" The @Spell here is a weird hack, it means *exclude* if the first group is
24" TOP. Otherwise we get spelling errors highlighted on code elements that
25" match scalaBlock, even with `syn spell notoplevel`.
26function! s:ContainedGroup()
27 try
28 silent syn list @scala
29 return '@scala,@NoSpell'
30 catch /E392/
31 return 'TOP,@Spell'
32 endtry
33endfunction
34
35unlet! b:current_syntax
36
37syn case match
38syn sync minlines=200 maxlines=1000
39
40syn keyword scalaKeyword catch do else final finally for forSome if
41syn keyword scalaKeyword match return throw try while yield macro
42syn keyword scalaKeyword class trait object extends with nextgroup=scalaInstanceDeclaration skipwhite
43syn keyword scalaKeyword case nextgroup=scalaKeyword,scalaCaseFollowing skipwhite
44syn keyword scalaKeyword val nextgroup=scalaNameDefinition,scalaQuasiQuotes skipwhite
45syn keyword scalaKeyword def var nextgroup=scalaNameDefinition skipwhite
Bram Moolenaar6f4754b2022-01-23 12:07:04 +000046hi def link scalaKeyword Keyword
Bram Moolenaare4a3bcf2016-08-26 19:52:37 +020047
48exe 'syn region scalaBlock start=/{/ end=/}/ contains=' . s:ContainedGroup() . ' fold'
49
50syn keyword scalaAkkaSpecialWord when goto using startWith initialize onTransition stay become unbecome
Bram Moolenaar6f4754b2022-01-23 12:07:04 +000051hi def link scalaAkkaSpecialWord PreProc
Bram Moolenaare4a3bcf2016-08-26 19:52:37 +020052
53syn keyword scalatestSpecialWord shouldBe
54syn match scalatestShouldDSLA /^\s\+\zsit should/
55syn match scalatestShouldDSLB /\<should\>/
Bram Moolenaar6f4754b2022-01-23 12:07:04 +000056hi def link scalatestSpecialWord PreProc
57hi def link scalatestShouldDSLA PreProc
58hi def link scalatestShouldDSLB PreProc
Bram Moolenaare4a3bcf2016-08-26 19:52:37 +020059
60syn match scalaSymbol /'[_A-Za-z0-9$]\+/
Bram Moolenaar6f4754b2022-01-23 12:07:04 +000061hi def link scalaSymbol Number
Bram Moolenaare4a3bcf2016-08-26 19:52:37 +020062
63syn match scalaChar /'.'/
64syn match scalaChar /'\\[\\"'ntbrf]'/ contains=scalaEscapedChar
65syn match scalaChar /'\\u[A-Fa-f0-9]\{4}'/ contains=scalaUnicodeChar
66syn match scalaEscapedChar /\\[\\"'ntbrf]/
67syn match scalaUnicodeChar /\\u[A-Fa-f0-9]\{4}/
Bram Moolenaar6f4754b2022-01-23 12:07:04 +000068hi def link scalaChar Character
69hi def link scalaEscapedChar Special
70hi def link scalaUnicodeChar Special
Bram Moolenaare4a3bcf2016-08-26 19:52:37 +020071
72syn match scalaOperator "||"
73syn match scalaOperator "&&"
Bram Moolenaar036986f2017-03-16 17:41:02 +010074syn match scalaOperator "|"
75syn match scalaOperator "&"
Bram Moolenaar6f4754b2022-01-23 12:07:04 +000076hi def link scalaOperator Special
Bram Moolenaare4a3bcf2016-08-26 19:52:37 +020077
78syn match scalaNameDefinition /\<[_A-Za-z0-9$]\+\>/ contained nextgroup=scalaPostNameDefinition,scalaVariableDeclarationList
79syn match scalaNameDefinition /`[^`]\+`/ contained nextgroup=scalaPostNameDefinition
80syn match scalaVariableDeclarationList /\s*,\s*/ contained nextgroup=scalaNameDefinition
81syn match scalaPostNameDefinition /\_s*:\_s*/ contained nextgroup=scalaTypeDeclaration
Bram Moolenaar6f4754b2022-01-23 12:07:04 +000082hi def link scalaNameDefinition Function
Bram Moolenaare4a3bcf2016-08-26 19:52:37 +020083
84syn match scalaInstanceDeclaration /\<[_\.A-Za-z0-9$]\+\>/ contained nextgroup=scalaInstanceHash
85syn match scalaInstanceDeclaration /`[^`]\+`/ contained
86syn match scalaInstanceHash /#/ contained nextgroup=scalaInstanceDeclaration
Bram Moolenaar6f4754b2022-01-23 12:07:04 +000087hi def link scalaInstanceDeclaration Special
88hi def link scalaInstanceHash Type
Bram Moolenaare4a3bcf2016-08-26 19:52:37 +020089
90syn match scalaUnimplemented /???/
Bram Moolenaar6f4754b2022-01-23 12:07:04 +000091hi def link scalaUnimplemented ERROR
Bram Moolenaare4a3bcf2016-08-26 19:52:37 +020092
93syn match scalaCapitalWord /\<[A-Z][A-Za-z0-9$]*\>/
Bram Moolenaar6f4754b2022-01-23 12:07:04 +000094hi def link scalaCapitalWord Special
Bram Moolenaare4a3bcf2016-08-26 19:52:37 +020095
96" Handle type declarations specially
97syn region scalaTypeStatement matchgroup=Keyword start=/\<type\_s\+\ze/ end=/$/ contains=scalaTypeTypeDeclaration,scalaSquareBrackets,scalaTypeTypeEquals,scalaTypeStatement
98
99" Ugh... duplication of all the scalaType* stuff to handle special highlighting
100" of `type X =` declarations
101syn match scalaTypeTypeDeclaration /(/ contained nextgroup=scalaTypeTypeExtension,scalaTypeTypeEquals contains=scalaRoundBrackets skipwhite
102syn match scalaTypeTypeDeclaration /\%(⇒\|=>\)\ze/ contained nextgroup=scalaTypeTypeDeclaration contains=scalaTypeTypeExtension skipwhite
103syn match scalaTypeTypeDeclaration /\<[_\.A-Za-z0-9$]\+\>/ contained nextgroup=scalaTypeTypeExtension,scalaTypeTypeEquals skipwhite
104syn match scalaTypeTypeEquals /=\ze[^>]/ contained nextgroup=scalaTypeTypePostDeclaration skipwhite
Bram Moolenaar89a9c152021-08-29 21:55:35 +0200105syn match scalaTypeTypeExtension /)\?\_s*\zs\%(⇒\|=>\|<:\|:>\|=:=\|::\|#\)/ contained contains=scalaTypeOperator nextgroup=scalaTypeTypeDeclaration skipwhite
Bram Moolenaare4a3bcf2016-08-26 19:52:37 +0200106syn match scalaTypeTypePostDeclaration /\<[_\.A-Za-z0-9$]\+\>/ contained nextgroup=scalaTypeTypePostExtension skipwhite
Bram Moolenaar89a9c152021-08-29 21:55:35 +0200107syn match scalaTypeTypePostExtension /\%(⇒\|=>\|<:\|:>\|=:=\|::\)/ contained contains=scalaTypeOperator nextgroup=scalaTypeTypePostDeclaration skipwhite
Bram Moolenaar6f4754b2022-01-23 12:07:04 +0000108hi def link scalaTypeTypeDeclaration Type
109hi def link scalaTypeTypeExtension Keyword
110hi def link scalaTypeTypePostDeclaration Special
111hi def link scalaTypeTypePostExtension Keyword
Bram Moolenaare4a3bcf2016-08-26 19:52:37 +0200112
113syn match scalaTypeDeclaration /(/ contained nextgroup=scalaTypeExtension contains=scalaRoundBrackets skipwhite
114syn match scalaTypeDeclaration /\%(⇒\|=>\)\ze/ contained nextgroup=scalaTypeDeclaration contains=scalaTypeExtension skipwhite
115syn match scalaTypeDeclaration /\<[_\.A-Za-z0-9$]\+\>/ contained nextgroup=scalaTypeExtension skipwhite
Bram Moolenaar89a9c152021-08-29 21:55:35 +0200116syn match scalaTypeExtension /)\?\_s*\zs\%(⇒\|=>\|<:\|:>\|=:=\|::\|#\)/ contained contains=scalaTypeOperator nextgroup=scalaTypeDeclaration skipwhite
Bram Moolenaar6f4754b2022-01-23 12:07:04 +0000117hi def link scalaTypeDeclaration Type
118hi def link scalaTypeExtension Keyword
119hi def link scalaTypePostExtension Keyword
Bram Moolenaare4a3bcf2016-08-26 19:52:37 +0200120
121syn match scalaTypeAnnotation /\%([_a-zA-Z0-9$\s]:\_s*\)\ze[_=(\.A-Za-z0-9$]\+/ skipwhite nextgroup=scalaTypeDeclaration contains=scalaRoundBrackets
122syn match scalaTypeAnnotation /)\_s*:\_s*\ze[_=(\.A-Za-z0-9$]\+/ skipwhite nextgroup=scalaTypeDeclaration
Bram Moolenaar89a9c152021-08-29 21:55:35 +0200123hi clear scalaTypeAnnotation
Bram Moolenaare4a3bcf2016-08-26 19:52:37 +0200124
Bram Moolenaar89a9c152021-08-29 21:55:35 +0200125syn match scalaCaseFollowing /\<[_\.A-Za-z0-9$]\+\>/ contained contains=scalaCapitalWord
126syn match scalaCaseFollowing /`[^`]\+`/ contained contains=scalaCapitalWord
Bram Moolenaar6f4754b2022-01-23 12:07:04 +0000127hi def link scalaCaseFollowing Special
Bram Moolenaare4a3bcf2016-08-26 19:52:37 +0200128
Bram Moolenaar89a9c152021-08-29 21:55:35 +0200129syn keyword scalaKeywordModifier abstract override final lazy implicit private protected sealed null super
130syn keyword scalaSpecialFunction implicitly require
Bram Moolenaar6f4754b2022-01-23 12:07:04 +0000131hi def link scalaKeywordModifier Function
132hi def link scalaSpecialFunction Function
Bram Moolenaare4a3bcf2016-08-26 19:52:37 +0200133
134syn keyword scalaSpecial this true false ne eq
135syn keyword scalaSpecial new nextgroup=scalaInstanceDeclaration skipwhite
136syn match scalaSpecial "\%(=>\|⇒\|<-\|←\|->\|→\)"
137syn match scalaSpecial /`[^`]\+`/ " Backtick literals
Bram Moolenaar6f4754b2022-01-23 12:07:04 +0000138hi def link scalaSpecial PreProc
Bram Moolenaare4a3bcf2016-08-26 19:52:37 +0200139
140syn keyword scalaExternal package import
Bram Moolenaar6f4754b2022-01-23 12:07:04 +0000141hi def link scalaExternal Include
Bram Moolenaare4a3bcf2016-08-26 19:52:37 +0200142
143syn match scalaStringEmbeddedQuote /\\"/ contained
144syn region scalaString start=/"/ end=/"/ contains=scalaStringEmbeddedQuote,scalaEscapedChar,scalaUnicodeChar
Bram Moolenaar6f4754b2022-01-23 12:07:04 +0000145hi def link scalaString String
146hi def link scalaStringEmbeddedQuote String
Bram Moolenaare4a3bcf2016-08-26 19:52:37 +0200147
148syn region scalaIString matchgroup=scalaInterpolationBrackets start=/\<[a-zA-Z][a-zA-Z0-9_]*"/ skip=/\\"/ end=/"/ contains=scalaInterpolation,scalaInterpolationB,scalaEscapedChar,scalaUnicodeChar
Bram Moolenaar036986f2017-03-16 17:41:02 +0100149syn region scalaTripleIString matchgroup=scalaInterpolationBrackets start=/\<[a-zA-Z][a-zA-Z0-9_]*"""/ end=/"""\ze\%([^"]\|$\)/ contains=scalaInterpolation,scalaInterpolationB,scalaEscapedChar,scalaUnicodeChar
Bram Moolenaar6f4754b2022-01-23 12:07:04 +0000150hi def link scalaIString String
151hi def link scalaTripleIString String
Bram Moolenaare4a3bcf2016-08-26 19:52:37 +0200152
153syn match scalaInterpolation /\$[a-zA-Z0-9_$]\+/ contained
154exe 'syn region scalaInterpolationB matchgroup=scalaInterpolationBoundary start=/\${/ end=/}/ contained contains=' . s:ContainedGroup()
Bram Moolenaar6f4754b2022-01-23 12:07:04 +0000155hi def link scalaInterpolation Function
Bram Moolenaar89a9c152021-08-29 21:55:35 +0200156hi clear scalaInterpolationB
Bram Moolenaare4a3bcf2016-08-26 19:52:37 +0200157
158syn region scalaFString matchgroup=scalaInterpolationBrackets start=/f"/ skip=/\\"/ end=/"/ contains=scalaFInterpolation,scalaFInterpolationB,scalaEscapedChar,scalaUnicodeChar
159syn match scalaFInterpolation /\$[a-zA-Z0-9_$]\+\(%[-A-Za-z0-9\.]\+\)\?/ contained
160exe 'syn region scalaFInterpolationB matchgroup=scalaInterpolationBoundary start=/${/ end=/}\(%[-A-Za-z0-9\.]\+\)\?/ contained contains=' . s:ContainedGroup()
Bram Moolenaar6f4754b2022-01-23 12:07:04 +0000161hi def link scalaFString String
162hi def link scalaFInterpolation Function
Bram Moolenaar89a9c152021-08-29 21:55:35 +0200163hi clear scalaFInterpolationB
Bram Moolenaare4a3bcf2016-08-26 19:52:37 +0200164
165syn region scalaTripleString start=/"""/ end=/"""\%([^"]\|$\)/ contains=scalaEscapedChar,scalaUnicodeChar
166syn region scalaTripleFString matchgroup=scalaInterpolationBrackets start=/f"""/ end=/"""\%([^"]\|$\)/ contains=scalaFInterpolation,scalaFInterpolationB,scalaEscapedChar,scalaUnicodeChar
Bram Moolenaar6f4754b2022-01-23 12:07:04 +0000167hi def link scalaTripleString String
168hi def link scalaTripleFString String
Bram Moolenaare4a3bcf2016-08-26 19:52:37 +0200169
Bram Moolenaar6f4754b2022-01-23 12:07:04 +0000170hi def link scalaInterpolationBrackets Special
171hi def link scalaInterpolationBoundary Function
Bram Moolenaare4a3bcf2016-08-26 19:52:37 +0200172
173syn match scalaNumber /\<0[dDfFlL]\?\>/ " Just a bare 0
174syn match scalaNumber /\<[1-9]\d*[dDfFlL]\?\>/ " A multi-digit number - octal numbers with leading 0's are deprecated in Scala
175syn match scalaNumber /\<0[xX][0-9a-fA-F]\+[dDfFlL]\?\>/ " Hex number
176syn match scalaNumber /\%(\<\d\+\.\d*\|\.\d\+\)\%([eE][-+]\=\d\+\)\=[fFdD]\=/ " exponential notation 1
177syn match scalaNumber /\<\d\+[eE][-+]\=\d\+[fFdD]\=\>/ " exponential notation 2
178syn match scalaNumber /\<\d\+\%([eE][-+]\=\d\+\)\=[fFdD]\>/ " exponential notation 3
Bram Moolenaar6f4754b2022-01-23 12:07:04 +0000179hi def link scalaNumber Number
Bram Moolenaare4a3bcf2016-08-26 19:52:37 +0200180
181syn region scalaRoundBrackets start="(" end=")" skipwhite contained contains=scalaTypeDeclaration,scalaSquareBrackets,scalaRoundBrackets
182
183syn region scalaSquareBrackets matchgroup=scalaSquareBracketsBrackets start="\[" end="\]" skipwhite nextgroup=scalaTypeExtension contains=scalaTypeDeclaration,scalaSquareBrackets,scalaTypeOperator,scalaTypeAnnotationParameter
184syn match scalaTypeOperator /[-+=:<>]\+/ contained
185syn match scalaTypeAnnotationParameter /@\<[`_A-Za-z0-9$]\+\>/ contained
Bram Moolenaar6f4754b2022-01-23 12:07:04 +0000186hi def link scalaSquareBracketsBrackets Type
187hi def link scalaTypeOperator Keyword
188hi def link scalaTypeAnnotationParameter Function
Bram Moolenaare4a3bcf2016-08-26 19:52:37 +0200189
190syn match scalaShebang "\%^#!.*" display
191syn region scalaMultilineComment start="/\*" end="\*/" contains=scalaMultilineComment,scalaDocLinks,scalaParameterAnnotation,scalaCommentAnnotation,scalaTodo,scalaCommentCodeBlock,@Spell keepend fold
192syn match scalaCommentAnnotation "@[_A-Za-z0-9$]\+" contained
193syn match scalaParameterAnnotation "\%(@tparam\|@param\|@see\)" nextgroup=scalaParamAnnotationValue skipwhite contained
194syn match scalaParamAnnotationValue /[.`_A-Za-z0-9$]\+/ contained
195syn region scalaDocLinks start="\[\[" end="\]\]" contained
196syn region scalaCommentCodeBlock matchgroup=Keyword start="{{{" end="}}}" contained
197syn match scalaTodo "\vTODO|FIXME|XXX" contained
Bram Moolenaar6f4754b2022-01-23 12:07:04 +0000198hi def link scalaShebang Comment
199hi def link scalaMultilineComment Comment
200hi def link scalaDocLinks Function
201hi def link scalaParameterAnnotation Function
202hi def link scalaParamAnnotationValue Keyword
203hi def link scalaCommentAnnotation Function
204hi def link scalaCommentCodeBlock String
205hi def link scalaTodo Todo
Bram Moolenaare4a3bcf2016-08-26 19:52:37 +0200206
207syn match scalaAnnotation /@\<[`_A-Za-z0-9$]\+\>/
Bram Moolenaar6f4754b2022-01-23 12:07:04 +0000208hi def link scalaAnnotation PreProc
Bram Moolenaare4a3bcf2016-08-26 19:52:37 +0200209
210syn match scalaTrailingComment "//.*$" contains=scalaTodo,@Spell
Bram Moolenaar6f4754b2022-01-23 12:07:04 +0000211hi def link scalaTrailingComment Comment
Bram Moolenaare4a3bcf2016-08-26 19:52:37 +0200212
213syn match scalaAkkaFSM /goto([^)]*)\_s\+\<using\>/ contains=scalaAkkaFSMGotoUsing
214syn match scalaAkkaFSM /stay\_s\+using/
215syn match scalaAkkaFSM /^\s*stay\s*$/
216syn match scalaAkkaFSM /when\ze([^)]*)/
217syn match scalaAkkaFSM /startWith\ze([^)]*)/
218syn match scalaAkkaFSM /initialize\ze()/
219syn match scalaAkkaFSM /onTransition/
220syn match scalaAkkaFSM /onTermination/
221syn match scalaAkkaFSM /whenUnhandled/
222syn match scalaAkkaFSMGotoUsing /\<using\>/
223syn match scalaAkkaFSMGotoUsing /\<goto\>/
Bram Moolenaar6f4754b2022-01-23 12:07:04 +0000224hi def link scalaAkkaFSM PreProc
225hi def link scalaAkkaFSMGotoUsing PreProc
Bram Moolenaare4a3bcf2016-08-26 19:52:37 +0200226
227let b:current_syntax = 'scala'
228
229if main_syntax ==# 'scala'
230 unlet main_syntax
231endif
232
233" vim:set sw=2 sts=2 ts=8 et: