1" Vim syntax file
2" Language:     TypeScript with React (JSX)
3" Maintainer:   Bram Moolenaar
4" Last Change:	2019 Nov 30
5" Based On:     Herrington Darkholme's yats.vim
6" Changes:      See https:github.com/HerringtonDarkholme/yats.vim
7" Credits:      See yats.vim on github
8
9if !exists("main_syntax")
10  if exists("b:current_syntax")
11    finish
12  endif
13  let main_syntax = 'typescriptreact'
14endif
15
16let s:cpo_save = &cpo
17set cpo&vim
18
19syntax region tsxTag
20      \ start=+<\([^/!?<>="':]\+\)\@=+
21      \ skip=+</[^ /!?<>"']\+>+
22      \ end=+/\@<!>+
23      \ end=+\(/>\)\@=+
24      \ contained
25      \ contains=tsxTagName,tsxIntrinsicTagName,tsxAttrib,tsxEscJs,
26                \tsxCloseString,@tsxComment
27
28syntax match tsxTag /<>/ contained
29
30
31" <tag></tag>
32" s~~~~~~~~~e
33" and self close tag
34" <tag/>
35" s~~~~e
36" A big start regexp borrowed from https://git.io/vDyxc
37syntax region tsxRegion
38      \ start=+<\_s*\z([a-zA-Z1-9\$_-]\+\(\.\k\+\)*\)+
39      \ skip=+<!--\_.\{-}-->+
40      \ end=+</\_s*\z1>+
41      \ matchgroup=tsxCloseString end=+/>+
42      \ fold
43      \ contains=tsxRegion,tsxCloseString,tsxCloseTag,tsxTag,tsxCommentInvalid,tsxFragment,tsxEscJs,@Spell
44      \ keepend
45      \ extend
46
47" <>   </>
48" s~~~~~~e
49" A big start regexp borrowed from https://git.io/vDyxc
50syntax region tsxFragment
51      \ start=+\(\((\|{\|}\|\[\|,\|&&\|||\|?\|:\|=\|=>\|\Wreturn\|^return\|\Wdefault\|^\|>\)\_s*\)\@<=<>+
52      \ skip=+<!--\_.\{-}-->+
53      \ end=+</>+
54      \ fold
55      \ contains=tsxRegion,tsxCloseString,tsxCloseTag,tsxTag,tsxCommentInvalid,tsxFragment,tsxEscJs,@Spell
56      \ keepend
57      \ extend
58
59" </tag>
60" ~~~~~~
61syntax match tsxCloseTag
62      \ +</\_s*[^/!?<>"']\+>+
63      \ contained
64      \ contains=tsxTagName,tsxIntrinsicTagName
65
66syntax match tsxCloseTag +</>+ contained
67
68syntax match tsxCloseString
69      \ +/>+
70      \ contained
71
72" <!-- -->
73" ~~~~~~~~
74syntax match tsxCommentInvalid /<!--\_.\{-}-->/ display
75
76syntax region tsxBlockComment
77    \ contained
78    \ start="/\*"
79    \ end="\*/"
80
81syntax match tsxLineComment
82    \ "//.*$"
83    \ contained
84    \ display
85
86syntax cluster tsxComment contains=tsxBlockComment,tsxLineComment
87
88syntax match tsxEntity "&[^; \t]*;" contains=tsxEntityPunct
89syntax match tsxEntityPunct contained "[&.;]"
90
91" <tag key={this.props.key}>
92"  ~~~
93syntax match tsxTagName
94    \ +[</]\_s*[^/!?<>"'* ]\++hs=s+1
95    \ contained
96    \ nextgroup=tsxAttrib
97    \ skipwhite
98    \ display
99syntax match tsxIntrinsicTagName
100    \ +[</]\_s*[a-z1-9-]\++hs=s+1
101    \ contained
102    \ nextgroup=tsxAttrib
103    \ skipwhite
104    \ display
105
106" <tag key={this.props.key}>
107"      ~~~
108syntax match tsxAttrib
109    \ +[a-zA-Z_][-0-9a-zA-Z_]*+
110    \ nextgroup=tsxEqual skipwhite
111    \ contained
112    \ display
113
114" <tag id="sample">
115"        ~
116syntax match tsxEqual +=+ display contained
117  \ nextgroup=tsxString skipwhite
118
119" <tag id="sample">
120"         s~~~~~~e
121syntax region tsxString contained start=+"+ end=+"+ contains=tsxEntity,@Spell display
122
123" <tag key={this.props.key}>
124"          s~~~~~~~~~~~~~~e
125syntax region tsxEscJs
126    \ contained
127    \ contains=@typescriptValue,@tsxComment
128    \ matchgroup=typescriptBraces
129    \ start=+{+
130    \ end=+}+
131    \ extend
132
133
134"""""""""""""""""""""""""""""""""""""""""""""""""""
135" Source the part common with typescriptreact.vim
136source <sfile>:h/typescriptcommon.vim
137
138
139syntax cluster typescriptExpression add=tsxRegion,tsxFragment
140
141hi def link tsxTag htmlTag
142hi def link tsxTagName Function
143hi def link tsxIntrinsicTagName htmlTagName
144hi def link tsxString String
145hi def link tsxNameSpace Function
146hi def link tsxCommentInvalid Error
147hi def link tsxBlockComment Comment
148hi def link tsxLineComment Comment
149hi def link tsxAttrib Type
150hi def link tsxEscJs tsxEscapeJs
151hi def link tsxCloseTag htmlTag
152hi def link tsxCloseString Identifier
153
154let b:current_syntax = "typescriptreact"
155if main_syntax == 'typescriptreact'
156  unlet main_syntax
157endif
158
159let &cpo = s:cpo_save
160unlet s:cpo_save
161