forked from leafOfTree/vim-svelte-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsvelte.vim
311 lines (274 loc) · 9.43 KB
/
svelte.vim
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" Vim syntax file
"
" Language: Svelte
" Maintainer: leaf <[email protected]>
"
" CREDITS: Inspired by mxw/vim-jsx.
"
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
if exists("b:current_syntax") && b:current_syntax == 'svelte'
finish
endif
" For advanced users, this variable can be used to avoid overload
let b:current_loading_main_syntax = 'svelte'
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
"
" Config {{{
"
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
let s:load_full_syntax = svelte#GetConfig('load_full_syntax', 0)
let s:use_pug = svelte#GetConfig('use_pug', 0)
let s:use_less = svelte#GetConfig('use_less', 0)
let s:use_sass = svelte#GetConfig('use_sass', 0)
let s:use_stylus = svelte#GetConfig('use_stylus', 0)
let s:use_coffee = svelte#GetConfig('use_coffee', 0)
let s:use_typescript = svelte#GetConfig('use_typescript', 0)
"}}}
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
"
" Functions {{{
"
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
function! s:LoadSyntax(group, type)
if s:load_full_syntax
call s:LoadFullSyntax(a:group, a:type)
else
call s:LoadDefaultSyntax(a:group, a:type)
endif
endfunction
function! s:LoadDefaultSyntax(group, type)
unlet! b:current_syntax
let syntaxPaths = ['$VIMRUNTIME', '$VIM/vimfiles', '$HOME/.vim']
for path in syntaxPaths
let file = expand(path).'/syntax/'.a:type.'.vim'
if filereadable(file)
execute 'syntax include '.a:group.' '.file
endif
endfor
endfunction
function! s:LoadFullSyntax(group, type)
call s:SetCurrentSyntax(a:type)
exec 'syntax include '.a:group.' syntax/'.a:type.'.vim'
endfunction
" Settings to avoid syntax overload
function! s:SetCurrentSyntax(type)
if a:type == 'coffee'
syntax cluster coffeeJS contains=@htmlJavaScript
" Avoid overload of `javascript.vim`
let b:current_syntax = 'svelte'
else
unlet! b:current_syntax
endif
endfunction
"}}}
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
"
" Load main syntax {{{
"
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" Load syntax/html.vim to syntax group, which loads full JavaScript and CSS
" syntax. It defines group htmlJavaScript and htmlCss.
call s:LoadSyntax('@HTMLSyntax', 'html')
" Avoid overload
if !hlexists('cssTagName')
call s:LoadSyntax('@htmlCss', 'css')
endif
" Avoid overload
if !hlexists('javaScriptComment')
call s:LoadSyntax('@htmlJavaScript', 'javascript')
endif
"}}}
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
"
" Load pre-processors syntax {{{
"
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" If pug is enabled, load vim-pug syntax
if s:use_pug
call s:LoadFullSyntax('@PugSyntax', 'pug')
endif
" If less is enabled, load less syntax
if s:use_less
call s:LoadSyntax('@LessSyntax', 'less')
runtime! after/syntax/less.vim
endif
" If sass is enabled, load sass syntax
if s:use_stylus
call s:LoadSyntax('@StylusSyntax', 'stylus')
runtime! after/syntax/stylus.vim
endif
" If sass is enabled, load sass syntax
if s:use_sass
call s:LoadSyntax('@SassSyntax', 'sass')
runtime! after/syntax/sass.vim
endif
" If CoffeeScript is enabled, load the syntax. Keep name consistent with
" vim-coffee-script/after/html.vim
if s:use_coffee
call s:LoadFullSyntax('@htmlCoffeeScript', 'coffee')
endif
" If TypeScript is enabled, load the syntax.
if s:use_typescript
call s:LoadFullSyntax('@TypeScript', 'typescript')
endif
"}}}
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
"
" Load Svelte specific html syntax {{{
"
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
syntax include syntax/svelte-html.vim
"}}}
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
"
" Syntax highlight {{{
"
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" All start with html/javascript/css for emmet-vim in-file type detection
" Normal tag template
syntax region htmlSvelteTemplate fold
\ start=+<[-:a-zA-Z0-9]\+[^>]*>$+
\ end=+^</[-:a-zA-Z0-9]\+>+
\ keepend contains=@HTMLSyntax
" Start tag across multiple lines or Empty tag across multiple lines
syntax region htmlSvelteTemplate fold
\ start=+<[-:a-zA-Z0-9]\+[^>]*$+
\ end=+^\(<\/[-:a-zA-Z0-9]\+>\|\(\S[^<]*\)\?\/>\)+
\ keepend contains=@HTMLSyntax
" Tag in one line
syntax match htmlSvelteTemplate fold
\ +<[-:a-zA-Z0-9]\+[^>]*>.*</[-:a-zA-Z0-9]\+>+
\ contains=@HTMLSyntax
" Empty tag in one line
syntax match htmlSvelteTemplate fold
\ +<[-:a-zA-Z0-9]\+[^>]*/>+
\ contains=@HTMLSyntax
" @html,@debug tag in one line
syntax match htmlSvelteTemplate fold
\ +{@\(html\|debug\)[^}]*}+
\ contains=@HTMLSyntax
" Control blocks like {#if ...}, {#each ...}
syntax region htmlSvelteTemplate fold
\ start=+{#[-a-zA-Z0-9]\+[^}]*}+
\ end=+^{/[-a-zA-Z0-9]\+}+
\ keepend contains=@HTMLSyntax
syntax region javascriptSvelteScript fold
\ start=+<script[^>]*>+
\ end=+</script>+
\ keepend
\ contains=@htmlJavaScript,jsImport,jsExport,svelteTag,svelteKeyword
syntax region cssSvelteStyle fold
\ start=+<style[^>]*>+
\ end=+</style>+
\ keepend contains=@htmlCss,svelteTag
" Preprocessors syntax
syntax region pugSvelteTemplate fold
\ start=+<template[^>]*lang="pug"[^>]*>+
\ end=+</template>+
\ keepend contains=@PugSyntax,svelteTag
syntax region coffeeSvelteScript fold
\ start=+<script[^>]*lang="coffee"[^>]*>+
\ end=+</script>+
\ keepend contains=@htmlCoffeeScript,jsImport,jsExport,svelteTag
syntax region typescriptSvelteScript fold
\ start=+<script[^>]*lang="\(ts\|typescript\)"[^>]*>+
\ end=+</script>+
\ keepend contains=@TypeScript,svelteTag
syntax region cssLessSvelteStyle fold
\ start=+<style[^>]*lang="less"[^>]*>+
\ end=+</style>+
\ keepend contains=@LessSyntax,svelteTag
syntax region cssSassSvelteStyle fold
\ start=+<style[^>]*lang="sass"[^>]*>+
\ end=+</style>+
\ keepend contains=@SassSyntax,svelteTag
syntax region cssScssSvelteStyle fold
\ start=+<style[^>]*lang="scss"[^>]*>+
\ end=+</style>+
\ keepend contains=@SassSyntax,svelteTag
syntax region cssStylusSvelteStyle fold
\ start=+<style[^>]*lang="stylus"[^>]*>+
\ end=+</style>+
\ keepend contains=@StylusSyntax,svelteTag
syntax region svelteTag
\ start=+^<[^/]+ end=+>+ skip=+></+
\ contained contains=htmlTagN,htmlString,htmlArg fold
syntax region svelteTag
\ start=+^</+ end=+>+
\ contained contains=htmlTagN,htmlString,htmlArg
syntax keyword svelteKeyword $ contained
highlight default link svelteTag htmlTag
highlight default link svelteKeyword Keyword
highlight default link cssUnitDecorators2 Number
highlight default link cssKeyFrameProp2 Constant
"}}}
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
"
" Syntax patch {{{
"
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" Patch 7.4.1142
if has("patch-7.4-1142")
if has("win32")
syn iskeyword @,48-57,_,128-167,224-235,$
else
syn iskeyword @,48-57,_,192-255,$
endif
endif
" Style
" Redefine (less|sass)Definition to highlight <style> correctly and
" enable emmet-vim css type.
if s:use_less
silent! syntax clear lessDefinition
syntax region cssLessDefinition matchgroup=cssBraces
\ contains=@LessSyntax,cssLessDefinition
\ contained containedin=cssLessSvelteStyle
\ start="{" end="}"
endif
if s:use_sass
silent! syntax clear sassDefinition
syntax region cssSassDefinition matchgroup=cssBraces
\ contains=@SassSyntax,cssSassDefinition
\ contained containedin=cssScssSvelteStyle,cssSassSvelteStyle
\ start="{" end="}"
endif
if s:use_stylus
silent! syntax clear stylusDefinition
syntax region cssStylusDefinition matchgroup=cssBraces
\ contains=@StylusSyntax,cssStylusDefinition
\ contained containedin=cssStylusSvelteStyle
\ start="{" end="}"
endif
" Avoid css syntax interference
silent! syntax clear cssUnitDecorators
" Have to use a different name
syntax match cssUnitDecorators2
\ /\(#\|-\|+\|%\|mm\|cm\|in\|pt\|pc\|em\|ex\|px\|ch\|rem\|vh\|vw\|vmin\|vmax\|dpi\|dppx\|dpcm\|Hz\|kHz\|s\|ms\|deg\|grad\|rad\)\ze\(;\|$\)/
\ contained
\ containedin=cssAttrRegion,sassCssAttribute,lessCssAttribute
silent! syntax clear cssKeyFrameProp
syn match cssKeyFrameProp2 /\d*%\|from\|to/
\ contained nextgroup=cssDefinition
\ containedin=cssAttrRegion,sassCssAttribute,lessCssAttribute
" HTML
" Clear htmlHead that may cause highlighting out of bounds
silent! syntax clear htmlHead
" JavaScript
" Number with minus
syntax match javaScriptNumber '\v<-?\d+L?>|0[xX][0-9a-fA-F]+>'
\ containedin=@javascriptSvelteScript display
" TypeScript
" Fix template string `...` breaking syntax highlighting
syntax region typescriptTemplate
\ start=/`/ skip=/\\\\\|\\`\|\n/ end=/`\|$/
\ contains=typescriptTemplateSubstitution,typescriptSpecial,@Spell
\ containedin=typescriptObjectLiteral
\ nextgroup=@typescriptSymbols
\ skipwhite skipempty
" html5 data-*
syntax match htmlArg '\v<data(-[.a-z0-9]+)+>' containedin=@HTMLSyntax
"}}}
syntax sync fromstart
let b:current_syntax = 'svelte'
" vim: fdm=marker