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
|
if polyglot#init#is_disabled(expand('<sfile>:p'), 'svelte', 'indent/svelte.vim')
finish
endif
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" Vim indent file
"
" Language: Svelte
" Maintainer: leafOfTree <leafvocation@gmail.com>
"
" CREDITS: Inspired by mxw/vim-jsx.
"
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
if exists("b:did_indent")
finish
endif
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
"
" Variables {{{
"
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
let s:name = 'vim-svelte-plugin'
" Let <template> handled by HTML
let s:svelte_tag_start = '\v^\<(script|style)'
let s:svelte_tag_end = '\v^\<\/(script|style)'
let s:template_tag = '\v^\s*\<\/?template'
" https://developer.mozilla.org/en-US/docs/Glossary/Empty_element
let s:empty_tagname = '(area|base|br|col|embed|hr|input|img|keygen|link|meta|param|source|track|wbr)'
let s:empty_tag = '\v\C\<'.s:empty_tagname.'[^/]*\>'
let s:empty_tag_start = '\v\<'.s:empty_tagname.'[^\>]*$'
let s:empty_tag_end = '\v^\s*[^\<\>\/]*\>\s*'
let s:tag_end = '\v^\s*\/?\>\s*'
let s:oneline_block = '^\s*{#.*{/.*}\s*$'
"}}}
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
"
" Config {{{
"
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
let s:use_pug = svelte#GetConfig('use_pug', 0)
let s:use_sass = svelte#GetConfig('use_sass', 0)
let s:use_coffee = svelte#GetConfig('use_coffee', 0)
let s:use_typescript = svelte#GetConfig('use_typescript', 0)
let s:has_init_indent = svelte#GetConfig('has_init_indent', 1)
let s:debug = svelte#GetConfig('debug', 0)
"}}}
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
"
" Load indent method {{{
"
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" Save shiftwidth
let s:sw = &sw
" Use lib/indent/ files for compatibility
unlet! b:did_indent
runtime lib/indent/xml.vim
unlet! b:did_indent
runtime lib/indent/css.vim
" Use normal indent files
unlet! b:did_indent
runtime! indent/javascript.vim
let b:javascript_indentexpr = &indentexpr
if s:use_pug
unlet! b:did_indent
let s:save_formatoptions = &formatoptions
runtime! indent/pug.vim
let &formatoptions = s:save_formatoptions
endif
if s:use_sass
unlet! b:did_indent
runtime! indent/sass.vim
endif
if s:use_coffee
unlet! b:did_indent
runtime! indent/coffee.vim
endif
if s:use_typescript
unlet! b:did_indent
runtime! indent/typescript.vim
endif
" Recover shiftwidth
let &sw = s:sw
"}}}
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
"
" Settings {{{
"
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" JavaScript indentkeys
setlocal indentkeys=0{,0},0),0],0\,,!^F,o,O,e,:,=:else
" XML indentkeys
setlocal indentkeys+=*<Return>,<>>,<<>,/
setlocal indentexpr=GetSvelteIndent()
"}}}
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
"
" Functions {{{
"
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
function! GetSvelteIndent()
let prevlnum = prevnonblank(v:lnum-1)
let prevline = getline(prevlnum)
let prevsyns = s:SynsSOL(prevlnum)
let curline = getline(v:lnum)
let cursyns = s:SynsSOL(v:lnum)
let cursyn = get(cursyns, 0, '')
if s:SynHTML(cursyn)
call s:Log('syntax: html')
let ind = XmlIndentGet(v:lnum, 0)
if prevline =~? s:empty_tag
call s:Log('previous line is empty tag')
let ind = ind - &sw
endif
if s:IsBlockStart(prevsyns) && prevline !~ s:oneline_block
call s:Log('increase block indent')
let ind = ind + &sw
endif
if s:IsBlockEnd(cursyns, curline)
call s:Log('decrease block indent')
let ind = ind - &sw
endif
" Align '/>' and '>' with '<' for multiline tags.
if curline =~? s:tag_end
let ind = ind - &sw
endif
" Then correct the indentation of any element following '/>' or '>'.
if prevline =~? s:tag_end
let ind = ind + &sw
"Decrease indent if prevlines are a multiline empty tag
let [start, end] = s:PrevMultilineEmptyTag(v:lnum)
if end == prevlnum
call s:Log('previous line is a multiline empty tag')
let ind = indent(v:lnum - 1)
endif
endif
elseif s:SynPug(cursyn)
call s:Log('syntax: pug')
let ind = GetPugIndent()
elseif s:SynCoffee(cursyn)
call s:Log('syntax: coffee')
let ind = GetCoffeeIndent(v:lnum)
elseif s:SynTypeScript(cursyn)
call s:Log('syntax: typescript')
let ind = GetTypescriptIndent()
elseif s:SynSASS(cursyn)
call s:Log('syntax: sass')
let ind = GetSassIndent()
elseif s:SynStyle(cursyn)
call s:Log('syntax: style')
let ind = GetCSSIndent()
else
call s:Log('syntax: javascript')
if len(b:javascript_indentexpr)
let ind = eval(b:javascript_indentexpr)
else
let ind = cindent(v:lnum)
endif
endif
if curline =~? s:svelte_tag_start || curline =~? s:svelte_tag_end
\|| prevline =~? s:svelte_tag_end
\|| (curline =~ s:template_tag && s:SynPug(cursyn))
call s:Log('current line is svelte tag or previous line is svelte tag end')
call s:Log('... or current line is pug template tag')
let ind = 0
elseif s:has_init_indent
if s:SynSvelteScriptOrStyle(cursyn) && ind < 1
call s:Log('add initial indent')
let ind = &sw
endif
elseif prevline =~? s:svelte_tag_start
call s:Log('previous line is svelte tag start')
let ind = 0
endif
call s:Log('indent: '.ind)
return ind
endfunction
function! s:IsBlockStart(prevsyns)
let prevsyn_second = get(a:prevsyns, 1, '')
" Some HTML tags add an extra syntax layer
let prevsyn_third = get(a:prevsyns, 2, '')
return s:SynBlockBody(prevsyn_second)
\ || s:SynBlockStart(prevsyn_second)
\ || s:SynBlockBody(prevsyn_third)
\ || s:SynBlockStart(prevsyn_third)
endfunction
function! s:IsBlockEnd(cursyns, curline)
let cursyn_second = get(a:cursyns, 1, '')
" Some HTML tags add an extra syntax layer
let cursyn_third = get(a:cursyns, 2, '')
return a:curline !~ '^\s*$'
\ && (s:SynBlockBody(cursyn_second)
\ || s:SynBlockEnd(cursyn_second)
\ || s:SynBlockBody(cursyn_third)
\ || s:SynBlockEnd(cursyn_third))
endfunction
function! s:SynsEOL(lnum)
let lnum = prevnonblank(a:lnum)
let col = strlen(getline(lnum))
return map(synstack(lnum, col), 'synIDattr(v:val, "name")')
endfunction
function! s:SynsSOL(lnum)
let lnum = prevnonblank(a:lnum)
let col = match(getline(lnum), '\S') + 1
return map(synstack(lnum, col), 'synIDattr(v:val, "name")')
endfunction
function! s:SynHTML(syn)
return a:syn ==? 'htmlSvelteTemplate'
endfunction
function! s:SynBlockBody(syn)
return a:syn ==? 'svelteBlockBody'
endfunction
function! s:SynBlockStart(syn)
return a:syn ==? 'svelteBlockStart'
endfunction
function! s:SynBlockEnd(syn)
return a:syn ==? 'svelteBlockEnd'
endfunction
function! s:SynPug(syn)
return a:syn ==? 'pugSvelteTemplate'
endfunction
function! s:SynCoffee(syn)
return a:syn ==? 'coffeeSvelteScript'
endfunction
function! s:SynTypeScript(syn)
return a:syn ==? 'typescriptSvelteScript'
endfunction
function! s:SynSASS(syn)
return a:syn ==? 'cssSassSvelteStyle'
endfunction
function! s:SynStyle(syn)
return a:syn =~? 'SvelteStyle'
endfunction
function! s:SynSvelteScriptOrStyle(syn)
return a:syn =~? '\v(SvelteStyle)|(SvelteScript)'
endfunction
function! s:PrevMultilineEmptyTag(lnum)
let lnum = a:lnum - 1
let lnums = [0, 0]
while lnum > 0
let line = getline(lnum)
if line =~? s:empty_tag_end
let lnums[1] = lnum
endif
if line =~? s:tag_start
if line =~? s:empty_tag_start
let lnums[0] = lnum
return lnums
else
return [0, 0]
endif
endif
let lnum = lnum - 1
endwhile
endfunction
function! s:Log(msg)
if s:debug
echom '['.s:name.']['.v:lnum.'] '.a:msg
endif
endfunction
"}}}
let b:did_indent = 1
" vim: fdm=marker
|