blob: 286171628738188298f3888a0b40cc4b4a51d490 [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 Moolenaar37c64c72017-09-19 22:06:03 +02005" Last Change: September 18, 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 Moolenaar37c64c72017-09-19 22:06:03 +020013" indent correctly if inside <script>
14" vim/vim@690afe1 for the switch from cindent
15let b:html_indent_script1 = 'inc'
16
Bram Moolenaar09521312016-08-12 22:54:35 +020017" Now, set up our indentation expression and keys that trigger it.
18setlocal indentexpr=GetJavascriptIndent()
Bram Moolenaar68563932017-01-10 13:31:15 +010019setlocal autoindent nolisp nosmartindent
20setlocal indentkeys+=0],0)
Bram Moolenaar3c2881d2017-03-21 19:18:29 +010021" Testable with something like:
22" vim -eNs "+filetype plugin indent on" "+syntax on" "+set ft=javascript" \
23" "+norm! gg=G" '+%print' '+:q!' testfile.js \
24" | diff -uBZ testfile.js -
Bram Moolenaardd2a3cd2007-05-05 17:10:09 +000025
Bram Moolenaar68563932017-01-10 13:31:15 +010026let b:undo_indent = 'setlocal indentexpr< smartindent< autoindent< indentkeys<'
Bram Moolenaar09521312016-08-12 22:54:35 +020027
Bram Moolenaar37c64c72017-09-19 22:06:03 +020028" Regex of syntax group names that are or delimit string or are comments.
29let b:syng_strcom = get(b:,'syng_strcom','string\|comment\|regex\|special\|doc\|template\%(braces\)\@!')
30let b:syng_str = get(b:,'syng_str','string\|template\|special')
31" template strings may want to be excluded when editing graphql:
32" au! Filetype javascript let b:syng_str = '^\%(.*template\)\@!.*string\|special'
33" au! Filetype javascript let b:syng_strcom = '^\%(.*template\)\@!.*string\|comment\|regex\|special\|doc'
34
Bram Moolenaar09521312016-08-12 22:54:35 +020035" Only define the function once.
36if exists('*GetJavascriptIndent')
37 finish
38endif
39
40let s:cpo_save = &cpo
41set cpo&vim
42
43" Get shiftwidth value
44if exists('*shiftwidth')
45 function s:sw()
46 return shiftwidth()
47 endfunction
48else
49 function s:sw()
Bram Moolenaar37c64c72017-09-19 22:06:03 +020050 return &l:shiftwidth ? &l:shiftwidth : &l:tabstop
Bram Moolenaar09521312016-08-12 22:54:35 +020051 endfunction
52endif
53
Bram Moolenaar3c2881d2017-03-21 19:18:29 +010054" Performance for forwards search(): start search at pos rather than masking
55" matches before pos.
56let s:z = has('patch-7.4.984') ? 'z' : ''
57
Bram Moolenaar37c64c72017-09-19 22:06:03 +020058" Expression used to check whether we should skip a match with searchpair().
59let s:skip_expr = "s:SynAt(line('.'),col('.')) =~? b:syng_strcom"
60let s:in_comm = s:skip_expr[:-14] . "'comment\\|doc'"
61
62let s:rel = has('reltime')
Bram Moolenaar68563932017-01-10 13:31:15 +010063" searchpair() wrapper
Bram Moolenaar37c64c72017-09-19 22:06:03 +020064if s:rel
65 function s:GetPair(start,end,flags,skip)
66 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 +010067 endfunction
68else
Bram Moolenaar37c64c72017-09-19 22:06:03 +020069 function s:GetPair(start,end,flags,skip)
70 return searchpair('\m'.a:start,'','\m'.a:end,a:flags,a:skip,s:l1)
Bram Moolenaar68563932017-01-10 13:31:15 +010071 endfunction
72endif
73
Bram Moolenaar37c64c72017-09-19 22:06:03 +020074function s:SynAt(l,c)
75 let byte = line2byte(a:l) + a:c - 1
76 let pos = index(s:synid_cache[0], byte)
77 if pos == -1
78 let s:synid_cache[:] += [[byte], [synIDattr(synID(a:l, a:c, 0), 'name')]]
Bram Moolenaar68563932017-01-10 13:31:15 +010079 endif
Bram Moolenaar37c64c72017-09-19 22:06:03 +020080 return s:synid_cache[1][pos]
Bram Moolenaar68563932017-01-10 13:31:15 +010081endfunction
Bram Moolenaar09521312016-08-12 22:54:35 +020082
Bram Moolenaar37c64c72017-09-19 22:06:03 +020083function s:ParseCino(f)
84 let [divider, n, cstr] = [0] + matchlist(&cino,
85 \ '\%(.*,\)\=\%(\%d'.char2nr(a:f).'\(-\)\=\([.s0-9]*\)\)\=')[1:2]
86 for c in split(cstr,'\zs')
87 if c == '.' && !divider
88 let divider = 1
89 elseif c ==# 's'
90 if n !~ '\d'
91 return n . s:sw() + 0
92 endif
93 let n = str2nr(n) * s:sw()
94 break
95 else
96 let [n, divider] .= [c, 0]
97 endif
98 endfor
99 return str2nr(n) / max([str2nr(divider),1])
100endfunction
101
102" Optimized {skip} expr, only callable from the search loop which
103" GetJavascriptIndent does to find the containing [[{(] (side-effects)
104function s:SkipFunc()
105 if s:top_col == 1
106 throw 'out of bounds'
107 endif
108 let s:top_col = 0
109 if s:check_in
110 if eval(s:skip_expr)
111 return 1
112 endif
113 let s:check_in = 0
114 elseif getline('.') =~ '\%<'.col('.').'c\/.\{-}\/\|\%>'.col('.').'c[''"]\|\\$'
115 if eval(s:skip_expr)
116 let s:looksyn = a:firstline
117 return 1
118 endif
119 elseif search('\m`\|\${\|\*\/','nW'.s:z,s:looksyn) && eval(s:skip_expr)
120 let s:check_in = 1
121 return 1
122 endif
123 let [s:looksyn, s:top_col] = getpos('.')[1:2]
124endfunction
125
126function s:AlternatePair()
127 let [pat, l:for] = ['[][(){};]', 2]
128 while s:SearchLoop(pat,'bW','s:SkipFunc()')
129 if s:LookingAt() == ';'
130 if !l:for
131 if s:GetPair('{','}','bW','s:SkipFunc()')
132 return
133 endif
Bram Moolenaar3c2881d2017-03-21 19:18:29 +0100134 break
Bram Moolenaar37c64c72017-09-19 22:06:03 +0200135 else
136 let [pat, l:for] = ['[{}();]', l:for - 1]
Bram Moolenaar68563932017-01-10 13:31:15 +0100137 endif
Bram Moolenaar3c2881d2017-03-21 19:18:29 +0100138 else
Bram Moolenaar37c64c72017-09-19 22:06:03 +0200139 let idx = stridx('])}',s:LookingAt())
140 if idx == -1
141 return
142 elseif !s:GetPair(['\[','(','{'][idx],'])}'[idx],'bW','s:SkipFunc()')
143 break
144 endif
Bram Moolenaar68563932017-01-10 13:31:15 +0100145 endif
146 endwhile
Bram Moolenaar37c64c72017-09-19 22:06:03 +0200147 throw 'out of bounds'
Bram Moolenaar68563932017-01-10 13:31:15 +0100148endfunction
149
Bram Moolenaar37c64c72017-09-19 22:06:03 +0200150function s:Nat(int)
151 return a:int * (a:int > 0)
Bram Moolenaar68563932017-01-10 13:31:15 +0100152endfunction
153
Bram Moolenaar37c64c72017-09-19 22:06:03 +0200154function s:LookingAt()
Bram Moolenaar68563932017-01-10 13:31:15 +0100155 return getline('.')[col('.')-1]
156endfunction
157
Bram Moolenaar37c64c72017-09-19 22:06:03 +0200158function s:Token()
159 return s:LookingAt() =~ '\k' ? expand('<cword>') : s:LookingAt()
Bram Moolenaar68563932017-01-10 13:31:15 +0100160endfunction
161
Bram Moolenaar37c64c72017-09-19 22:06:03 +0200162function s:PreviousToken()
163 let l:col = col('.')
164 if search('\m\k\{1,}\|\S','ebW')
165 if search('\m\*\%#\/\|\/\/\%<'.a:firstline.'l','nbW',line('.')) && eval(s:in_comm)
166 if s:SearchLoop('\S\ze\_s*\/[/*]','bW',s:in_comm)
167 return s:Token()
168 endif
169 call cursor(a:firstline, l:col)
Bram Moolenaar68563932017-01-10 13:31:15 +0100170 else
Bram Moolenaar37c64c72017-09-19 22:06:03 +0200171 return s:Token()
Bram Moolenaar68563932017-01-10 13:31:15 +0100172 endif
Bram Moolenaar3c2881d2017-03-21 19:18:29 +0100173 endif
Bram Moolenaar68563932017-01-10 13:31:15 +0100174 return ''
175endfunction
176
Bram Moolenaar37c64c72017-09-19 22:06:03 +0200177function s:Pure(f,...)
178 return eval("[call(a:f,a:000),cursor(a:firstline,".col('.').")][0]")
179endfunction
180
181function s:SearchLoop(pat,flags,expr)
182 return s:GetPair(a:pat,'\_$.',a:flags,a:expr)
183endfunction
184
185function s:ExprCol()
Bram Moolenaar3c2881d2017-03-21 19:18:29 +0100186 let bal = 0
Bram Moolenaar37c64c72017-09-19 22:06:03 +0200187 while s:SearchLoop('[{}?]\|\_[^:]\zs::\@!','bW',s:skip_expr)
188 if s:LookingAt() == ':'
189 let bal -= 1
190 elseif s:LookingAt() == '?'
191 let bal += 1
192 if bal == 1
193 break
194 endif
195 elseif s:LookingAt() == '{'
196 let bal = !s:IsBlock()
197 break
198 elseif !s:GetPair('{','}','bW',s:skip_expr)
199 break
200 endif
Bram Moolenaar3c2881d2017-03-21 19:18:29 +0100201 endwhile
Bram Moolenaar37c64c72017-09-19 22:06:03 +0200202 return s:Nat(bal)
Bram Moolenaar68563932017-01-10 13:31:15 +0100203endfunction
Bram Moolenaar09521312016-08-12 22:54:35 +0200204
205" configurable regexes that define continuation lines, not including (, {, or [.
Bram Moolenaar68563932017-01-10 13:31:15 +0100206let s:opfirst = '^' . get(g:,'javascript_opfirst',
Bram Moolenaar37c64c72017-09-19 22:06:03 +0200207 \ '\C\%([<>=,.?^%|/&]\|\([-:+]\)\1\@!\|\*\+\|!=\|in\%(stanceof\)\=\>\)')
Bram Moolenaar68563932017-01-10 13:31:15 +0100208let s:continuation = get(g:,'javascript_continuation',
Bram Moolenaar37c64c72017-09-19 22:06:03 +0200209 \ '\C\%([<=,.~!?/*^%|&:]\|+\@<!+\|-\@<!-\|=\@<!>\|\<\%(typeof\|new\|delete\|void\|in\|instanceof\|await\)\)') . '$'
Bram Moolenaare4a3bcf2016-08-26 19:52:37 +0200210
Bram Moolenaar37c64c72017-09-19 22:06:03 +0200211function s:Continues(ln,con)
212 let tok = matchstr(a:con[-15:],s:continuation)
213 if tok =~ '[a-z:]'
214 call cursor(a:ln, len(a:con))
215 return tok == ':' ? s:ExprCol() : s:PreviousToken() != '.'
216 elseif tok !~ '[/>]'
217 return tok isnot ''
Bram Moolenaar3c2881d2017-03-21 19:18:29 +0100218 endif
Bram Moolenaar37c64c72017-09-19 22:06:03 +0200219 return s:SynAt(a:ln, len(a:con)) !~? (tok == '>' ? 'jsflow\|^html' : 'regex')
Bram Moolenaar09521312016-08-12 22:54:35 +0200220endfunction
221
222" Check if line 'lnum' has a balanced amount of parentheses.
223function s:Balanced(lnum)
Bram Moolenaar37c64c72017-09-19 22:06:03 +0200224 let [l:open, l:line] = [0, getline(a:lnum)]
225 let pos = match(l:line, '[][(){}]')
Bram Moolenaar09521312016-08-12 22:54:35 +0200226 while pos != -1
Bram Moolenaar37c64c72017-09-19 22:06:03 +0200227 if s:SynAt(a:lnum,pos + 1) !~? b:syng_strcom
Bram Moolenaar68563932017-01-10 13:31:15 +0100228 let l:open += match(' ' . l:line[pos],'[[({]')
229 if l:open < 0
230 return
Bram Moolenaar09521312016-08-12 22:54:35 +0200231 endif
232 endif
Bram Moolenaar37c64c72017-09-19 22:06:03 +0200233 let pos = match(l:line, !l:open ? '[][(){}]' : '()' =~ l:line[pos] ?
234 \ '[()]' : '{}' =~ l:line[pos] ? '[{}]' : '[][]', pos + 1)
Bram Moolenaar09521312016-08-12 22:54:35 +0200235 endwhile
Bram Moolenaar68563932017-01-10 13:31:15 +0100236 return !l:open
Bram Moolenaar09521312016-08-12 22:54:35 +0200237endfunction
Bram Moolenaar68563932017-01-10 13:31:15 +0100238
Bram Moolenaar37c64c72017-09-19 22:06:03 +0200239function s:OneScope()
240 if s:LookingAt() == ')' && s:GetPair('(', ')', 'bW', s:skip_expr)
241 let tok = s:PreviousToken()
242 return (count(split('for if let while with'),tok) ||
243 \ tok =~# '^await$\|^each$' && s:PreviousToken() ==# 'for') &&
244 \ s:Pure('s:PreviousToken') != '.' && !(tok == 'while' && s:DoWhile())
245 elseif s:Token() =~# '^else$\|^do$'
246 return s:Pure('s:PreviousToken') != '.'
Bram Moolenaar68563932017-01-10 13:31:15 +0100247 endif
Bram Moolenaar37c64c72017-09-19 22:06:03 +0200248 return strpart(getline('.'),col('.')-2,2) == '=>'
Bram Moolenaar68563932017-01-10 13:31:15 +0100249endfunction
250
Bram Moolenaar37c64c72017-09-19 22:06:03 +0200251function s:DoWhile()
252 let cpos = searchpos('\m\<','cbW')
253 if s:SearchLoop('\C[{}]\|\<\%(do\|while\)\>','bW',s:skip_expr)
254 if s:{s:LookingAt() == '}' && s:GetPair('{','}','bW',s:skip_expr) ?
255 \ 'Previous' : ''}Token() ==# 'do' && s:IsBlock()
256 return 1
257 endif
258 call call('cursor',cpos)
259 endif
260endfunction
261
262" returns total offset from braceless contexts. 'num' is the lineNr which
263" encloses the entire context, 'cont' if whether a:firstline is a continued
264" expression, which could have started in a braceless context
265function s:IsContOne(num,cont)
266 let [l:num, b_l] = [a:num + !a:num, 0]
267 let pind = a:num ? indent(a:num) + s:sw() : 0
268 let ind = indent('.') + !a:cont
269 while line('.') > l:num && ind > pind || line('.') == l:num
270 if indent('.') < ind && s:OneScope()
271 let b_l += 1
272 elseif !a:cont || b_l || ind < indent(a:firstline)
273 break
274 else
275 call cursor(0,1)
276 endif
277 let ind = min([ind, indent('.')])
278 if s:PreviousToken() is ''
Bram Moolenaar68563932017-01-10 13:31:15 +0100279 break
280 endif
Bram Moolenaar68563932017-01-10 13:31:15 +0100281 endwhile
Bram Moolenaar37c64c72017-09-19 22:06:03 +0200282 return b_l
283endfunction
284
285function s:Class()
286 return (s:Token() ==# 'class' || s:PreviousToken() =~# '^class$\|^extends$') &&
287 \ s:PreviousToken() != '.'
288endfunction
289
290function s:IsSwitch()
291 return s:PreviousToken() !~ '[.*]' &&
292 \ (!s:GetPair('{','}','cbW',s:skip_expr) || s:IsBlock() && !s:Class())
Bram Moolenaar68563932017-01-10 13:31:15 +0100293endfunction
294
295" https://github.com/sweet-js/sweet.js/wiki/design#give-lookbehind-to-the-reader
296function s:IsBlock()
Bram Moolenaar37c64c72017-09-19 22:06:03 +0200297 let tok = s:PreviousToken()
298 if join(s:stack) =~? 'xml\|jsx' && s:SynAt(line('.'),col('.')-1) =~? 'xml\|jsx'
299 return tok != '{'
300 elseif tok =~ '\k'
301 if tok ==# 'type'
302 return s:Pure('eval',"s:PreviousToken() !~# '^\\%(im\\|ex\\)port$' || s:PreviousToken() == '.'")
303 elseif tok ==# 'of'
304 return s:Pure('eval',"!s:GetPair('[[({]','[])}]','bW',s:skip_expr) || s:LookingAt() != '(' ||"
305 \ ."s:{s:PreviousToken() ==# 'await' ? 'Previous' : ''}Token() !=# 'for' || s:PreviousToken() == '.'")
Bram Moolenaar68563932017-01-10 13:31:15 +0100306 endif
Bram Moolenaar37c64c72017-09-19 22:06:03 +0200307 return index(split('return const let import export extends yield default delete var await void typeof throw case new in instanceof')
308 \ ,tok) < (line('.') != a:firstline) || s:Pure('s:PreviousToken') == '.'
309 elseif tok == '>'
310 return getline('.')[col('.')-2] == '=' || s:SynAt(line('.'),col('.')) =~? 'jsflow\|^html'
311 elseif tok == '*'
312 return s:Pure('s:PreviousToken') == ':'
313 elseif tok == ':'
314 return s:Pure('eval',"s:PreviousToken() =~ '^\\K\\k*$' && !s:ExprCol()")
315 elseif tok == '/'
316 return s:SynAt(line('.'),col('.')) =~? 'regex'
317 elseif tok !~ '[=~!<,.?^%|&([]'
318 return tok !~ '[-+]' || line('.') != a:firstline && getline('.')[col('.')-2] == tok
Bram Moolenaar68563932017-01-10 13:31:15 +0100319 endif
320endfunction
Bram Moolenaar09521312016-08-12 22:54:35 +0200321
322function GetJavascriptIndent()
Bram Moolenaar68563932017-01-10 13:31:15 +0100323 let b:js_cache = get(b:,'js_cache',[0,0,0])
Bram Moolenaar37c64c72017-09-19 22:06:03 +0200324 let s:synid_cache = [[],[]]
325 let l:line = getline(v:lnum)
Bram Moolenaar3c2881d2017-03-21 19:18:29 +0100326 " use synstack as it validates syn state and works in an empty line
Bram Moolenaar37c64c72017-09-19 22:06:03 +0200327 let s:stack = [''] + map(synstack(v:lnum,1),"synIDattr(v:val,'name')")
Bram Moolenaar09521312016-08-12 22:54:35 +0200328
Bram Moolenaar68563932017-01-10 13:31:15 +0100329 " start with strings,comments,etc.
Bram Moolenaar37c64c72017-09-19 22:06:03 +0200330 if s:stack[-1] =~? 'comment\|doc'
Bram Moolenaar68563932017-01-10 13:31:15 +0100331 if l:line =~ '^\s*\*'
332 return cindent(v:lnum)
333 elseif l:line !~ '^\s*\/[/*]'
334 return -1
335 endif
Bram Moolenaar37c64c72017-09-19 22:06:03 +0200336 elseif s:stack[-1] =~? b:syng_str
Bram Moolenaar68563932017-01-10 13:31:15 +0100337 if b:js_cache[0] == v:lnum - 1 && s:Balanced(v:lnum-1)
338 let b:js_cache[0] = v:lnum
339 endif
Bram Moolenaar09521312016-08-12 22:54:35 +0200340 return -1
341 endif
Bram Moolenaar37c64c72017-09-19 22:06:03 +0200342
343 let s:l1 = max([0,prevnonblank(v:lnum) - (s:rel ? 2000 : 1000),
344 \ get(get(b:,'hi_indent',{}),'blocklnr')])
345 call cursor(v:lnum,1)
346 if s:PreviousToken() is ''
Bram Moolenaar68563932017-01-10 13:31:15 +0100347 return
Bram Moolenaar09521312016-08-12 22:54:35 +0200348 endif
Bram Moolenaar37c64c72017-09-19 22:06:03 +0200349 let [l:lnum, pline] = [line('.'), getline('.')[:col('.')-1]]
Bram Moolenaar09521312016-08-12 22:54:35 +0200350
Bram Moolenaar68563932017-01-10 13:31:15 +0100351 let l:line = substitute(l:line,'^\s*','','')
Bram Moolenaar37c64c72017-09-19 22:06:03 +0200352 let l:line_raw = l:line
Bram Moolenaar68563932017-01-10 13:31:15 +0100353 if l:line[:1] == '/*'
354 let l:line = substitute(l:line,'^\%(\/\*.\{-}\*\/\s*\)*','','')
Bram Moolenaar09521312016-08-12 22:54:35 +0200355 endif
Bram Moolenaar68563932017-01-10 13:31:15 +0100356 if l:line =~ '^\/[/*]'
357 let l:line = ''
358 endif
Bram Moolenaar09521312016-08-12 22:54:35 +0200359
Bram Moolenaar68563932017-01-10 13:31:15 +0100360 " the containing paren, bracket, or curly. Many hacks for performance
Bram Moolenaar37c64c72017-09-19 22:06:03 +0200361 call cursor(v:lnum,1)
Bram Moolenaar3c2881d2017-03-21 19:18:29 +0100362 let idx = index([']',')','}'],l:line[0])
Bram Moolenaar37c64c72017-09-19 22:06:03 +0200363 if b:js_cache[0] > l:lnum && b:js_cache[0] < v:lnum ||
364 \ b:js_cache[0] == l:lnum && s:Balanced(l:lnum)
Bram Moolenaar68563932017-01-10 13:31:15 +0100365 call call('cursor',b:js_cache[1:])
Bram Moolenaar09521312016-08-12 22:54:35 +0200366 else
Bram Moolenaar37c64c72017-09-19 22:06:03 +0200367 let [s:looksyn, s:top_col, s:check_in, s:l1] = [v:lnum - 1,0,0,
368 \ max([s:l1, &smc ? search('\m^.\{'.&smc.',}','nbW',s:l1 + 1) + 1 : 0])]
369 try
370 if idx != -1
371 call s:GetPair(['\[','(','{'][idx],'])}'[idx],'bW','s:SkipFunc()')
372 elseif getline(v:lnum) !~ '^\S' && s:stack[-1] =~? 'block\|^jsobject$'
373 call s:GetPair('{','}','bW','s:SkipFunc()')
374 else
375 call s:AlternatePair()
376 endif
377 catch /^\Cout of bounds$/
378 call cursor(v:lnum,1)
379 endtry
380 let b:js_cache[1:] = line('.') == v:lnum ? [0,0] : getpos('.')[1:2]
Bram Moolenaar09521312016-08-12 22:54:35 +0200381 endif
382
Bram Moolenaar37c64c72017-09-19 22:06:03 +0200383 let [b:js_cache[0], num] = [v:lnum, b:js_cache[1]]
Bram Moolenaar68563932017-01-10 13:31:15 +0100384
Bram Moolenaar37c64c72017-09-19 22:06:03 +0200385 let [num_ind, is_op, b_l, l:switch_offset] = [s:Nat(indent(num)),0,0,0]
386 if !num || s:LookingAt() == '{' && s:IsBlock()
Bram Moolenaar3c2881d2017-03-21 19:18:29 +0100387 let ilnum = line('.')
Bram Moolenaar37c64c72017-09-19 22:06:03 +0200388 if num && s:LookingAt() == ')' && s:GetPair('(',')','bW',s:skip_expr)
389 if ilnum == num
390 let [num, num_ind] = [line('.'), indent('.')]
391 endif
392 if idx == -1 && s:PreviousToken() ==# 'switch' && s:IsSwitch()
393 let l:switch_offset = &cino !~ ':' ? s:sw() : s:ParseCino(':')
Bram Moolenaar68563932017-01-10 13:31:15 +0100394 if pline[-1:] != '.' && l:line =~# '^\%(default\|case\)\>'
Bram Moolenaar37c64c72017-09-19 22:06:03 +0200395 return s:Nat(num_ind + l:switch_offset)
396 elseif &cino =~ '='
397 let l:case_offset = s:ParseCino('=')
Bram Moolenaar68563932017-01-10 13:31:15 +0100398 endif
399 endif
400 endif
Bram Moolenaar37c64c72017-09-19 22:06:03 +0200401 if idx == -1 && pline[-1:] !~ '[{;]'
402 let sol = matchstr(l:line,s:opfirst)
403 if sol is '' || sol == '/' && s:SynAt(v:lnum,
404 \ 1 + len(getline(v:lnum)) - len(l:line)) =~? 'regex'
405 if s:Continues(l:lnum,pline)
406 let is_op = s:sw()
407 endif
408 elseif num && sol =~# '^\%(in\%(stanceof\)\=\|\*\)$'
409 call call('cursor',b:js_cache[1:])
410 if s:PreviousToken() =~ '\k' && s:Class()
411 return num_ind + s:sw()
412 endif
413 let is_op = s:sw()
414 else
415 let is_op = s:sw()
416 endif
417 call cursor(l:lnum, len(pline))
418 let b_l = s:Nat(s:IsContOne(b:js_cache[1],is_op) - (!is_op && l:line =~ '^{')) * s:sw()
Bram Moolenaar68563932017-01-10 13:31:15 +0100419 endif
Bram Moolenaar37c64c72017-09-19 22:06:03 +0200420 elseif idx.s:LookingAt().&cino =~ '^-1(.*(' && (search('\m\S','nbW',num) || s:ParseCino('U'))
421 let pval = s:ParseCino('(')
422 if !pval
423 let [Wval, vcol] = [s:ParseCino('W'), virtcol('.')]
424 if search('\m\S','W',num)
425 return s:ParseCino('w') ? vcol : virtcol('.')-1
426 endif
427 return Wval ? s:Nat(num_ind + Wval) : vcol
428 endif
429 return s:Nat(num_ind + pval + searchpair('\m(','','\m)','nbrmW',s:skip_expr,num) * s:sw())
Bram Moolenaar68563932017-01-10 13:31:15 +0100430 endif
431
432 " main return
Bram Moolenaar37c64c72017-09-19 22:06:03 +0200433 if l:line =~ '^[])}]\|^|}'
434 if l:line_raw[0] == ')' && getline(num)[b:js_cache[2]-1] == '('
435 if s:ParseCino('M')
436 return indent(l:lnum)
437 elseif &cino =~# 'm' && !s:ParseCino('m')
438 return virtcol('.') - 1
439 endif
440 endif
441 return num_ind
Bram Moolenaar68563932017-01-10 13:31:15 +0100442 elseif num
Bram Moolenaar37c64c72017-09-19 22:06:03 +0200443 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 +0100444 endif
Bram Moolenaar37c64c72017-09-19 22:06:03 +0200445 return b_l + is_op
Bram Moolenaar09521312016-08-12 22:54:35 +0200446endfunction
447
Bram Moolenaar09521312016-08-12 22:54:35 +0200448let &cpo = s:cpo_save
449unlet s:cpo_save