blob: 8077442ed00b88a200103b69c528121bd24b6a3a [file] [log] [blame]
Bram Moolenaardd2a3cd2007-05-05 17:10:09 +00001" Vim indent file
Bram Moolenaar09521312016-08-12 22:54:35 +02002" Language: Javascript
Bram Moolenaare4a3bcf2016-08-26 19:52:37 +02003" Maintainer: Chris Paul ( https://github.com/bounceme )
Bram Moolenaar09521312016-08-12 22:54:35 +02004" URL: https://github.com/pangloss/vim-javascript
Bram Moolenaarf0b03c42017-12-17 17:17:07 +01005" Last Change: December 4, 2017
Bram Moolenaardd2a3cd2007-05-05 17:10:09 +00006
7" Only load this indent file when no other was loaded.
Bram Moolenaar09521312016-08-12 22:54:35 +02008if exists('b:did_indent')
9 finish
Bram Moolenaardd2a3cd2007-05-05 17:10:09 +000010endif
11let b:did_indent = 1
12
Bram Moolenaar09521312016-08-12 22:54:35 +020013" Now, set up our indentation expression and keys that trigger it.
14setlocal indentexpr=GetJavascriptIndent()
Bram Moolenaar68563932017-01-10 13:31:15 +010015setlocal autoindent nolisp nosmartindent
16setlocal indentkeys+=0],0)
Bram Moolenaar3c2881d2017-03-21 19:18:29 +010017" Testable with something like:
18" vim -eNs "+filetype plugin indent on" "+syntax on" "+set ft=javascript" \
19" "+norm! gg=G" '+%print' '+:q!' testfile.js \
20" | diff -uBZ testfile.js -
Bram Moolenaardd2a3cd2007-05-05 17:10:09 +000021
Bram Moolenaar68563932017-01-10 13:31:15 +010022let b:undo_indent = 'setlocal indentexpr< smartindent< autoindent< indentkeys<'
Bram Moolenaar09521312016-08-12 22:54:35 +020023
24" Only define the function once.
25if exists('*GetJavascriptIndent')
26 finish
27endif
28
29let s:cpo_save = &cpo
30set cpo&vim
31
Bram Moolenaarf0b03c42017-12-17 17:17:07 +010032" indent correctly if inside <script>
33" vim/vim@690afe1 for the switch from cindent
34" overridden with b:html_indent_script1
35call extend(g:,{'html_indent_script1': 'inc'},'keep')
36
37" Regex of syntax group names that are or delimit string or are comments.
38let s:bvars = {
39 \ 'syng_strcom': 'string\|comment\|regex\|special\|doc\|template\%(braces\)\@!',
40 \ 'syng_str': 'string\|template\|special' }
41" template strings may want to be excluded when editing graphql:
42" au! Filetype javascript let b:syng_str = '^\%(.*template\)\@!.*string\|special'
43" au! Filetype javascript let b:syng_strcom = '^\%(.*template\)\@!.*string\|comment\|regex\|special\|doc'
44
45function s:GetVars()
46 call extend(b:,extend(s:bvars,{'js_cache': [0,0,0]}),'keep')
47endfunction
48
Bram Moolenaar09521312016-08-12 22:54:35 +020049" Get shiftwidth value
50if exists('*shiftwidth')
51 function s:sw()
52 return shiftwidth()
53 endfunction
54else
55 function s:sw()
Bram Moolenaar37c64c72017-09-19 22:06:03 +020056 return &l:shiftwidth ? &l:shiftwidth : &l:tabstop
Bram Moolenaar09521312016-08-12 22:54:35 +020057 endfunction
58endif
59
Bram Moolenaar3c2881d2017-03-21 19:18:29 +010060" Performance for forwards search(): start search at pos rather than masking
61" matches before pos.
62let s:z = has('patch-7.4.984') ? 'z' : ''
63
Bram Moolenaar37c64c72017-09-19 22:06:03 +020064" Expression used to check whether we should skip a match with searchpair().
65let s:skip_expr = "s:SynAt(line('.'),col('.')) =~? b:syng_strcom"
66let s:in_comm = s:skip_expr[:-14] . "'comment\\|doc'"
67
68let s:rel = has('reltime')
Bram Moolenaar68563932017-01-10 13:31:15 +010069" searchpair() wrapper
Bram Moolenaar37c64c72017-09-19 22:06:03 +020070if s:rel
71 function s:GetPair(start,end,flags,skip)
72 return searchpair('\m'.a:start,'','\m'.a:end,a:flags,a:skip,s:l1,a:skip ==# 's:SkipFunc()' ? 2000 : 200)
Bram Moolenaar68563932017-01-10 13:31:15 +010073 endfunction
74else
Bram Moolenaar37c64c72017-09-19 22:06:03 +020075 function s:GetPair(start,end,flags,skip)
76 return searchpair('\m'.a:start,'','\m'.a:end,a:flags,a:skip,s:l1)
Bram Moolenaar68563932017-01-10 13:31:15 +010077 endfunction
78endif
79
Bram Moolenaar37c64c72017-09-19 22:06:03 +020080function s:SynAt(l,c)
81 let byte = line2byte(a:l) + a:c - 1
82 let pos = index(s:synid_cache[0], byte)
83 if pos == -1
84 let s:synid_cache[:] += [[byte], [synIDattr(synID(a:l, a:c, 0), 'name')]]
Bram Moolenaar68563932017-01-10 13:31:15 +010085 endif
Bram Moolenaar37c64c72017-09-19 22:06:03 +020086 return s:synid_cache[1][pos]
Bram Moolenaar68563932017-01-10 13:31:15 +010087endfunction
Bram Moolenaar09521312016-08-12 22:54:35 +020088
Bram Moolenaar37c64c72017-09-19 22:06:03 +020089function s:ParseCino(f)
90 let [divider, n, cstr] = [0] + matchlist(&cino,
91 \ '\%(.*,\)\=\%(\%d'.char2nr(a:f).'\(-\)\=\([.s0-9]*\)\)\=')[1:2]
92 for c in split(cstr,'\zs')
93 if c == '.' && !divider
94 let divider = 1
95 elseif c ==# 's'
96 if n !~ '\d'
97 return n . s:sw() + 0
98 endif
99 let n = str2nr(n) * s:sw()
100 break
101 else
102 let [n, divider] .= [c, 0]
103 endif
104 endfor
105 return str2nr(n) / max([str2nr(divider),1])
106endfunction
107
108" Optimized {skip} expr, only callable from the search loop which
109" GetJavascriptIndent does to find the containing [[{(] (side-effects)
110function s:SkipFunc()
111 if s:top_col == 1
112 throw 'out of bounds'
Bram Moolenaarf0b03c42017-12-17 17:17:07 +0100113 elseif s:check_in
Bram Moolenaar37c64c72017-09-19 22:06:03 +0200114 if eval(s:skip_expr)
115 return 1
116 endif
117 let s:check_in = 0
118 elseif getline('.') =~ '\%<'.col('.').'c\/.\{-}\/\|\%>'.col('.').'c[''"]\|\\$'
119 if eval(s:skip_expr)
Bram Moolenaar37c64c72017-09-19 22:06:03 +0200120 return 1
121 endif
Bram Moolenaarf0b03c42017-12-17 17:17:07 +0100122 elseif search('\m`\|\${\|\*\/','nW'.s:z,s:looksyn)
123 if eval(s:skip_expr)
124 let s:check_in = 1
125 return 1
126 endif
127 else
128 let s:synid_cache[:] += [[line2byte('.') + col('.') - 1], ['']]
Bram Moolenaar37c64c72017-09-19 22:06:03 +0200129 endif
130 let [s:looksyn, s:top_col] = getpos('.')[1:2]
131endfunction
132
133function s:AlternatePair()
134 let [pat, l:for] = ['[][(){};]', 2]
135 while s:SearchLoop(pat,'bW','s:SkipFunc()')
136 if s:LookingAt() == ';'
137 if !l:for
138 if s:GetPair('{','}','bW','s:SkipFunc()')
139 return
140 endif
Bram Moolenaar3c2881d2017-03-21 19:18:29 +0100141 break
Bram Moolenaar37c64c72017-09-19 22:06:03 +0200142 else
143 let [pat, l:for] = ['[{}();]', l:for - 1]
Bram Moolenaar68563932017-01-10 13:31:15 +0100144 endif
Bram Moolenaar3c2881d2017-03-21 19:18:29 +0100145 else
Bram Moolenaar37c64c72017-09-19 22:06:03 +0200146 let idx = stridx('])}',s:LookingAt())
147 if idx == -1
148 return
149 elseif !s:GetPair(['\[','(','{'][idx],'])}'[idx],'bW','s:SkipFunc()')
150 break
151 endif
Bram Moolenaar68563932017-01-10 13:31:15 +0100152 endif
153 endwhile
Bram Moolenaar37c64c72017-09-19 22:06:03 +0200154 throw 'out of bounds'
Bram Moolenaar68563932017-01-10 13:31:15 +0100155endfunction
156
Bram Moolenaar37c64c72017-09-19 22:06:03 +0200157function s:Nat(int)
158 return a:int * (a:int > 0)
Bram Moolenaar68563932017-01-10 13:31:15 +0100159endfunction
160
Bram Moolenaar37c64c72017-09-19 22:06:03 +0200161function s:LookingAt()
Bram Moolenaar68563932017-01-10 13:31:15 +0100162 return getline('.')[col('.')-1]
163endfunction
164
Bram Moolenaar37c64c72017-09-19 22:06:03 +0200165function s:Token()
166 return s:LookingAt() =~ '\k' ? expand('<cword>') : s:LookingAt()
Bram Moolenaar68563932017-01-10 13:31:15 +0100167endfunction
168
Bram Moolenaarf0b03c42017-12-17 17:17:07 +0100169function s:PreviousToken(...)
170 let [l:pos, tok] = [getpos('.'), '']
Bram Moolenaar37c64c72017-09-19 22:06:03 +0200171 if search('\m\k\{1,}\|\S','ebW')
Bram Moolenaarf0b03c42017-12-17 17:17:07 +0100172 if getline('.')[col('.')-2:col('.')-1] == '*/'
173 if eval(s:in_comm) && !s:SearchLoop('\S\ze\_s*\/[/*]','bW',s:in_comm)
174 call setpos('.',l:pos)
175 else
176 let tok = s:Token()
Bram Moolenaar37c64c72017-09-19 22:06:03 +0200177 endif
Bram Moolenaar68563932017-01-10 13:31:15 +0100178 else
Bram Moolenaarf0b03c42017-12-17 17:17:07 +0100179 let two = a:0 || line('.') != l:pos[1] ? strridx(getline('.')[:col('.')],'//') + 1 : 0
180 if two && eval(s:in_comm)
181 call cursor(0,two)
182 let tok = s:PreviousToken(1)
183 if tok is ''
184 call setpos('.',l:pos)
185 endif
186 else
187 let tok = s:Token()
188 endif
Bram Moolenaar68563932017-01-10 13:31:15 +0100189 endif
Bram Moolenaar3c2881d2017-03-21 19:18:29 +0100190 endif
Bram Moolenaarf0b03c42017-12-17 17:17:07 +0100191 return tok
Bram Moolenaar68563932017-01-10 13:31:15 +0100192endfunction
193
Bram Moolenaar37c64c72017-09-19 22:06:03 +0200194function s:Pure(f,...)
195 return eval("[call(a:f,a:000),cursor(a:firstline,".col('.').")][0]")
196endfunction
197
198function s:SearchLoop(pat,flags,expr)
199 return s:GetPair(a:pat,'\_$.',a:flags,a:expr)
200endfunction
201
202function s:ExprCol()
Bram Moolenaarf0b03c42017-12-17 17:17:07 +0100203 if getline('.')[col('.')-2] == ':'
204 return 1
205 endif
Bram Moolenaar3c2881d2017-03-21 19:18:29 +0100206 let bal = 0
Bram Moolenaarf0b03c42017-12-17 17:17:07 +0100207 while s:SearchLoop('[{}?:]','bW',s:skip_expr)
Bram Moolenaar37c64c72017-09-19 22:06:03 +0200208 if s:LookingAt() == ':'
Bram Moolenaarf0b03c42017-12-17 17:17:07 +0100209 if getline('.')[col('.')-2] == ':'
210 call cursor(0,col('.')-1)
211 continue
212 endif
Bram Moolenaar37c64c72017-09-19 22:06:03 +0200213 let bal -= 1
214 elseif s:LookingAt() == '?'
Bram Moolenaarf0b03c42017-12-17 17:17:07 +0100215 if getline('.')[col('.'):col('.')+1] =~ '^\.\d\@!'
216 continue
217 elseif !bal
218 return 1
Bram Moolenaar37c64c72017-09-19 22:06:03 +0200219 endif
Bram Moolenaarf0b03c42017-12-17 17:17:07 +0100220 let bal += 1
Bram Moolenaar37c64c72017-09-19 22:06:03 +0200221 elseif s:LookingAt() == '{'
Bram Moolenaarf0b03c42017-12-17 17:17:07 +0100222 return !s:IsBlock()
Bram Moolenaar37c64c72017-09-19 22:06:03 +0200223 elseif !s:GetPair('{','}','bW',s:skip_expr)
224 break
225 endif
Bram Moolenaar3c2881d2017-03-21 19:18:29 +0100226 endwhile
Bram Moolenaar68563932017-01-10 13:31:15 +0100227endfunction
Bram Moolenaar09521312016-08-12 22:54:35 +0200228
229" configurable regexes that define continuation lines, not including (, {, or [.
Bram Moolenaar68563932017-01-10 13:31:15 +0100230let s:opfirst = '^' . get(g:,'javascript_opfirst',
Bram Moolenaar37c64c72017-09-19 22:06:03 +0200231 \ '\C\%([<>=,.?^%|/&]\|\([-:+]\)\1\@!\|\*\+\|!=\|in\%(stanceof\)\=\>\)')
Bram Moolenaar68563932017-01-10 13:31:15 +0100232let s:continuation = get(g:,'javascript_continuation',
Bram Moolenaar37c64c72017-09-19 22:06:03 +0200233 \ '\C\%([<=,.~!?/*^%|&:]\|+\@<!+\|-\@<!-\|=\@<!>\|\<\%(typeof\|new\|delete\|void\|in\|instanceof\|await\)\)') . '$'
Bram Moolenaare4a3bcf2016-08-26 19:52:37 +0200234
Bram Moolenaarf0b03c42017-12-17 17:17:07 +0100235function s:Continues()
236 let tok = matchstr(strpart(getline('.'),col('.')-15,15),s:continuation)
Bram Moolenaar37c64c72017-09-19 22:06:03 +0200237 if tok =~ '[a-z:]'
Bram Moolenaar37c64c72017-09-19 22:06:03 +0200238 return tok == ':' ? s:ExprCol() : s:PreviousToken() != '.'
239 elseif tok !~ '[/>]'
240 return tok isnot ''
Bram Moolenaar3c2881d2017-03-21 19:18:29 +0100241 endif
Bram Moolenaarf0b03c42017-12-17 17:17:07 +0100242 return s:SynAt(line('.'),col('.')) !~? (tok == '>' ? 'jsflow\|^html' : 'regex')
Bram Moolenaar09521312016-08-12 22:54:35 +0200243endfunction
244
245" Check if line 'lnum' has a balanced amount of parentheses.
Bram Moolenaarf0b03c42017-12-17 17:17:07 +0100246function s:Balanced(lnum,line)
247 let l:open = 0
248 let pos = match(a:line, '[][(){}]')
Bram Moolenaar09521312016-08-12 22:54:35 +0200249 while pos != -1
Bram Moolenaar37c64c72017-09-19 22:06:03 +0200250 if s:SynAt(a:lnum,pos + 1) !~? b:syng_strcom
Bram Moolenaarf0b03c42017-12-17 17:17:07 +0100251 let l:open += match(' ' . a:line[pos],'[[({]')
Bram Moolenaar68563932017-01-10 13:31:15 +0100252 if l:open < 0
253 return
Bram Moolenaar09521312016-08-12 22:54:35 +0200254 endif
255 endif
Bram Moolenaarf0b03c42017-12-17 17:17:07 +0100256 let pos = match(a:line, !l:open ? '[][(){}]' : '()' =~ a:line[pos] ?
257 \ '[()]' : '{}' =~ a:line[pos] ? '[{}]' : '[][]', pos + 1)
Bram Moolenaar09521312016-08-12 22:54:35 +0200258 endwhile
Bram Moolenaar68563932017-01-10 13:31:15 +0100259 return !l:open
Bram Moolenaar09521312016-08-12 22:54:35 +0200260endfunction
Bram Moolenaar68563932017-01-10 13:31:15 +0100261
Bram Moolenaar37c64c72017-09-19 22:06:03 +0200262function s:OneScope()
263 if s:LookingAt() == ')' && s:GetPair('(', ')', 'bW', s:skip_expr)
264 let tok = s:PreviousToken()
265 return (count(split('for if let while with'),tok) ||
266 \ tok =~# '^await$\|^each$' && s:PreviousToken() ==# 'for') &&
267 \ s:Pure('s:PreviousToken') != '.' && !(tok == 'while' && s:DoWhile())
268 elseif s:Token() =~# '^else$\|^do$'
269 return s:Pure('s:PreviousToken') != '.'
Bram Moolenaarf0b03c42017-12-17 17:17:07 +0100270 elseif strpart(getline('.'),col('.')-2,2) == '=>'
271 call cursor(0,col('.')-1)
272 if s:PreviousToken() == ')'
273 return s:GetPair('(', ')', 'bW', s:skip_expr)
274 endif
275 return 1
Bram Moolenaar68563932017-01-10 13:31:15 +0100276 endif
Bram Moolenaar68563932017-01-10 13:31:15 +0100277endfunction
278
Bram Moolenaar37c64c72017-09-19 22:06:03 +0200279function s:DoWhile()
280 let cpos = searchpos('\m\<','cbW')
Bram Moolenaarf0b03c42017-12-17 17:17:07 +0100281 while s:SearchLoop('\C[{}]\|\<\%(do\|while\)\>','bW',s:skip_expr)
282 if s:LookingAt() =~ '\a'
283 if s:Pure('s:IsBlock')
284 if s:LookingAt() ==# 'd'
285 return 1
286 endif
287 break
288 endif
289 elseif s:LookingAt() != '}' || !s:GetPair('{','}','bW',s:skip_expr)
290 break
Bram Moolenaar37c64c72017-09-19 22:06:03 +0200291 endif
Bram Moolenaarf0b03c42017-12-17 17:17:07 +0100292 endwhile
293 call call('cursor',cpos)
Bram Moolenaar37c64c72017-09-19 22:06:03 +0200294endfunction
295
296" returns total offset from braceless contexts. 'num' is the lineNr which
297" encloses the entire context, 'cont' if whether a:firstline is a continued
298" expression, which could have started in a braceless context
Bram Moolenaarf0b03c42017-12-17 17:17:07 +0100299function s:IsContOne(cont)
300 let [l:num, b_l] = [b:js_cache[1] + !b:js_cache[1], 0]
301 let pind = b:js_cache[1] ? indent(b:js_cache[1]) + s:sw() : 0
Bram Moolenaar37c64c72017-09-19 22:06:03 +0200302 let ind = indent('.') + !a:cont
303 while line('.') > l:num && ind > pind || line('.') == l:num
304 if indent('.') < ind && s:OneScope()
305 let b_l += 1
306 elseif !a:cont || b_l || ind < indent(a:firstline)
307 break
308 else
309 call cursor(0,1)
310 endif
311 let ind = min([ind, indent('.')])
312 if s:PreviousToken() is ''
Bram Moolenaar68563932017-01-10 13:31:15 +0100313 break
314 endif
Bram Moolenaar68563932017-01-10 13:31:15 +0100315 endwhile
Bram Moolenaar37c64c72017-09-19 22:06:03 +0200316 return b_l
317endfunction
318
Bram Moolenaar37c64c72017-09-19 22:06:03 +0200319function s:IsSwitch()
Bram Moolenaarf0b03c42017-12-17 17:17:07 +0100320 call call('cursor',b:js_cache[1:])
321 return search('\m\C\%#.\_s*\%(\%(\/\/.*\_$\|\/\*\_.\{-}\*\/\)\@>\_s*\)*\%(case\|default\)\>','nWc'.s:z)
Bram Moolenaar68563932017-01-10 13:31:15 +0100322endfunction
323
324" https://github.com/sweet-js/sweet.js/wiki/design#give-lookbehind-to-the-reader
325function s:IsBlock()
Bram Moolenaar37c64c72017-09-19 22:06:03 +0200326 let tok = s:PreviousToken()
327 if join(s:stack) =~? 'xml\|jsx' && s:SynAt(line('.'),col('.')-1) =~? 'xml\|jsx'
Bram Moolenaarf0b03c42017-12-17 17:17:07 +0100328 let s:in_jsx = 1
Bram Moolenaar37c64c72017-09-19 22:06:03 +0200329 return tok != '{'
330 elseif tok =~ '\k'
331 if tok ==# 'type'
332 return s:Pure('eval',"s:PreviousToken() !~# '^\\%(im\\|ex\\)port$' || s:PreviousToken() == '.'")
333 elseif tok ==# 'of'
334 return s:Pure('eval',"!s:GetPair('[[({]','[])}]','bW',s:skip_expr) || s:LookingAt() != '(' ||"
335 \ ."s:{s:PreviousToken() ==# 'await' ? 'Previous' : ''}Token() !=# 'for' || s:PreviousToken() == '.'")
Bram Moolenaar68563932017-01-10 13:31:15 +0100336 endif
Bram Moolenaar37c64c72017-09-19 22:06:03 +0200337 return index(split('return const let import export extends yield default delete var await void typeof throw case new in instanceof')
338 \ ,tok) < (line('.') != a:firstline) || s:Pure('s:PreviousToken') == '.'
339 elseif tok == '>'
340 return getline('.')[col('.')-2] == '=' || s:SynAt(line('.'),col('.')) =~? 'jsflow\|^html'
341 elseif tok == '*'
342 return s:Pure('s:PreviousToken') == ':'
343 elseif tok == ':'
344 return s:Pure('eval',"s:PreviousToken() =~ '^\\K\\k*$' && !s:ExprCol()")
345 elseif tok == '/'
346 return s:SynAt(line('.'),col('.')) =~? 'regex'
347 elseif tok !~ '[=~!<,.?^%|&([]'
348 return tok !~ '[-+]' || line('.') != a:firstline && getline('.')[col('.')-2] == tok
Bram Moolenaar68563932017-01-10 13:31:15 +0100349 endif
350endfunction
Bram Moolenaar09521312016-08-12 22:54:35 +0200351
352function GetJavascriptIndent()
Bram Moolenaarf0b03c42017-12-17 17:17:07 +0100353 call s:GetVars()
Bram Moolenaar37c64c72017-09-19 22:06:03 +0200354 let s:synid_cache = [[],[]]
355 let l:line = getline(v:lnum)
Bram Moolenaar3c2881d2017-03-21 19:18:29 +0100356 " use synstack as it validates syn state and works in an empty line
Bram Moolenaar37c64c72017-09-19 22:06:03 +0200357 let s:stack = [''] + map(synstack(v:lnum,1),"synIDattr(v:val,'name')")
Bram Moolenaar09521312016-08-12 22:54:35 +0200358
Bram Moolenaar68563932017-01-10 13:31:15 +0100359 " start with strings,comments,etc.
Bram Moolenaar37c64c72017-09-19 22:06:03 +0200360 if s:stack[-1] =~? 'comment\|doc'
Bram Moolenaar68563932017-01-10 13:31:15 +0100361 if l:line =~ '^\s*\*'
362 return cindent(v:lnum)
363 elseif l:line !~ '^\s*\/[/*]'
364 return -1
365 endif
Bram Moolenaar37c64c72017-09-19 22:06:03 +0200366 elseif s:stack[-1] =~? b:syng_str
Bram Moolenaarf0b03c42017-12-17 17:17:07 +0100367 if b:js_cache[0] == v:lnum - 1 && s:Balanced(v:lnum-1,getline(v:lnum-1))
Bram Moolenaar68563932017-01-10 13:31:15 +0100368 let b:js_cache[0] = v:lnum
369 endif
Bram Moolenaar09521312016-08-12 22:54:35 +0200370 return -1
371 endif
Bram Moolenaar37c64c72017-09-19 22:06:03 +0200372
373 let s:l1 = max([0,prevnonblank(v:lnum) - (s:rel ? 2000 : 1000),
374 \ get(get(b:,'hi_indent',{}),'blocklnr')])
375 call cursor(v:lnum,1)
376 if s:PreviousToken() is ''
Bram Moolenaar68563932017-01-10 13:31:15 +0100377 return
Bram Moolenaar09521312016-08-12 22:54:35 +0200378 endif
Bram Moolenaar37c64c72017-09-19 22:06:03 +0200379 let [l:lnum, pline] = [line('.'), getline('.')[:col('.')-1]]
Bram Moolenaar09521312016-08-12 22:54:35 +0200380
Bram Moolenaar68563932017-01-10 13:31:15 +0100381 let l:line = substitute(l:line,'^\s*','','')
Bram Moolenaar37c64c72017-09-19 22:06:03 +0200382 let l:line_raw = l:line
Bram Moolenaar68563932017-01-10 13:31:15 +0100383 if l:line[:1] == '/*'
384 let l:line = substitute(l:line,'^\%(\/\*.\{-}\*\/\s*\)*','','')
Bram Moolenaar09521312016-08-12 22:54:35 +0200385 endif
Bram Moolenaar68563932017-01-10 13:31:15 +0100386 if l:line =~ '^\/[/*]'
387 let l:line = ''
388 endif
Bram Moolenaar09521312016-08-12 22:54:35 +0200389
Bram Moolenaar68563932017-01-10 13:31:15 +0100390 " the containing paren, bracket, or curly. Many hacks for performance
Bram Moolenaar37c64c72017-09-19 22:06:03 +0200391 call cursor(v:lnum,1)
Bram Moolenaar3c2881d2017-03-21 19:18:29 +0100392 let idx = index([']',')','}'],l:line[0])
Bram Moolenaar37c64c72017-09-19 22:06:03 +0200393 if b:js_cache[0] > l:lnum && b:js_cache[0] < v:lnum ||
Bram Moolenaarf0b03c42017-12-17 17:17:07 +0100394 \ b:js_cache[0] == l:lnum && s:Balanced(l:lnum,pline)
Bram Moolenaar68563932017-01-10 13:31:15 +0100395 call call('cursor',b:js_cache[1:])
Bram Moolenaar09521312016-08-12 22:54:35 +0200396 else
Bram Moolenaar37c64c72017-09-19 22:06:03 +0200397 let [s:looksyn, s:top_col, s:check_in, s:l1] = [v:lnum - 1,0,0,
398 \ max([s:l1, &smc ? search('\m^.\{'.&smc.',}','nbW',s:l1 + 1) + 1 : 0])]
399 try
400 if idx != -1
401 call s:GetPair(['\[','(','{'][idx],'])}'[idx],'bW','s:SkipFunc()')
402 elseif getline(v:lnum) !~ '^\S' && s:stack[-1] =~? 'block\|^jsobject$'
403 call s:GetPair('{','}','bW','s:SkipFunc()')
404 else
405 call s:AlternatePair()
406 endif
407 catch /^\Cout of bounds$/
408 call cursor(v:lnum,1)
409 endtry
410 let b:js_cache[1:] = line('.') == v:lnum ? [0,0] : getpos('.')[1:2]
Bram Moolenaar09521312016-08-12 22:54:35 +0200411 endif
412
Bram Moolenaar37c64c72017-09-19 22:06:03 +0200413 let [b:js_cache[0], num] = [v:lnum, b:js_cache[1]]
Bram Moolenaar68563932017-01-10 13:31:15 +0100414
Bram Moolenaarf0b03c42017-12-17 17:17:07 +0100415 let [num_ind, is_op, b_l, l:switch_offset, s:in_jsx] = [s:Nat(indent(num)),0,0,0,0]
Bram Moolenaar37c64c72017-09-19 22:06:03 +0200416 if !num || s:LookingAt() == '{' && s:IsBlock()
Bram Moolenaar3c2881d2017-03-21 19:18:29 +0100417 let ilnum = line('.')
Bram Moolenaarf0b03c42017-12-17 17:17:07 +0100418 if num && !s:in_jsx && s:LookingAt() == ')' && s:GetPair('(',')','bW',s:skip_expr)
Bram Moolenaar37c64c72017-09-19 22:06:03 +0200419 if ilnum == num
420 let [num, num_ind] = [line('.'), indent('.')]
421 endif
422 if idx == -1 && s:PreviousToken() ==# 'switch' && s:IsSwitch()
423 let l:switch_offset = &cino !~ ':' ? s:sw() : s:ParseCino(':')
Bram Moolenaar68563932017-01-10 13:31:15 +0100424 if pline[-1:] != '.' && l:line =~# '^\%(default\|case\)\>'
Bram Moolenaar37c64c72017-09-19 22:06:03 +0200425 return s:Nat(num_ind + l:switch_offset)
426 elseif &cino =~ '='
427 let l:case_offset = s:ParseCino('=')
Bram Moolenaar68563932017-01-10 13:31:15 +0100428 endif
429 endif
430 endif
Bram Moolenaar37c64c72017-09-19 22:06:03 +0200431 if idx == -1 && pline[-1:] !~ '[{;]'
Bram Moolenaarf0b03c42017-12-17 17:17:07 +0100432 call cursor(l:lnum, len(pline))
Bram Moolenaar37c64c72017-09-19 22:06:03 +0200433 let sol = matchstr(l:line,s:opfirst)
434 if sol is '' || sol == '/' && s:SynAt(v:lnum,
435 \ 1 + len(getline(v:lnum)) - len(l:line)) =~? 'regex'
Bram Moolenaarf0b03c42017-12-17 17:17:07 +0100436 if s:Continues()
Bram Moolenaar37c64c72017-09-19 22:06:03 +0200437 let is_op = s:sw()
438 endif
Bram Moolenaarf0b03c42017-12-17 17:17:07 +0100439 elseif num && sol =~# '^\%(in\%(stanceof\)\=\|\*\)$' &&
440 \ s:LookingAt() == '}' && s:GetPair('{','}','bW',s:skip_expr) &&
441 \ s:PreviousToken() == ')' && s:GetPair('(',')','bW',s:skip_expr) &&
442 \ (s:PreviousToken() == ']' || s:LookingAt() =~ '\k' &&
443 \ s:{s:PreviousToken() == '*' ? 'Previous' : ''}Token() !=# 'function')
444 return num_ind + s:sw()
Bram Moolenaar37c64c72017-09-19 22:06:03 +0200445 else
446 let is_op = s:sw()
447 endif
448 call cursor(l:lnum, len(pline))
Bram Moolenaarf0b03c42017-12-17 17:17:07 +0100449 let b_l = s:Nat(s:IsContOne(is_op) - (!is_op && l:line =~ '^{')) * s:sw()
Bram Moolenaar68563932017-01-10 13:31:15 +0100450 endif
Bram Moolenaar37c64c72017-09-19 22:06:03 +0200451 elseif idx.s:LookingAt().&cino =~ '^-1(.*(' && (search('\m\S','nbW',num) || s:ParseCino('U'))
452 let pval = s:ParseCino('(')
453 if !pval
454 let [Wval, vcol] = [s:ParseCino('W'), virtcol('.')]
455 if search('\m\S','W',num)
456 return s:ParseCino('w') ? vcol : virtcol('.')-1
457 endif
458 return Wval ? s:Nat(num_ind + Wval) : vcol
459 endif
460 return s:Nat(num_ind + pval + searchpair('\m(','','\m)','nbrmW',s:skip_expr,num) * s:sw())
Bram Moolenaar68563932017-01-10 13:31:15 +0100461 endif
462
463 " main return
Bram Moolenaar37c64c72017-09-19 22:06:03 +0200464 if l:line =~ '^[])}]\|^|}'
Bram Moolenaarf0b03c42017-12-17 17:17:07 +0100465 if l:line_raw[0] == ')'
Bram Moolenaar37c64c72017-09-19 22:06:03 +0200466 if s:ParseCino('M')
467 return indent(l:lnum)
Bram Moolenaarf0b03c42017-12-17 17:17:07 +0100468 elseif num && &cino =~# 'm' && !s:ParseCino('m')
Bram Moolenaar37c64c72017-09-19 22:06:03 +0200469 return virtcol('.') - 1
470 endif
471 endif
472 return num_ind
Bram Moolenaar68563932017-01-10 13:31:15 +0100473 elseif num
Bram Moolenaar37c64c72017-09-19 22:06:03 +0200474 return s:Nat(num_ind + get(l:,'case_offset',s:sw()) + l:switch_offset + b_l + is_op)
Bram Moolenaar68563932017-01-10 13:31:15 +0100475 endif
Bram Moolenaar2ecbe532022-07-29 21:36:21 +0100476
477 let nest = get(get(b:, 'hi_indent', {}), 'blocklnr')
478 if nest
479 return indent(nextnonblank(nest + 1)) + b_l + is_op
480 endif
481
Bram Moolenaar37c64c72017-09-19 22:06:03 +0200482 return b_l + is_op
Bram Moolenaar09521312016-08-12 22:54:35 +0200483endfunction
484
Bram Moolenaar09521312016-08-12 22:54:35 +0200485let &cpo = s:cpo_save
486unlet s:cpo_save