blob: 6537d3f2fd8af8ff72f0966c12db78397b27250e [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
Aliaksei Budavei4e3df442025-04-13 21:00:42 +03005" Last Change: 2025 Apr 13
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)
Aliaksei Budavei4e3df442025-04-13 21:00:42 +030072 " VIM_INDENT_TEST_TRACE_START
Bram Moolenaar37c64c72017-09-19 22:06:03 +020073 return searchpair('\m'.a:start,'','\m'.a:end,a:flags,a:skip,s:l1,a:skip ==# 's:SkipFunc()' ? 2000 : 200)
Aliaksei Budavei4e3df442025-04-13 21:00:42 +030074 " VIM_INDENT_TEST_TRACE_END s:GetPair
Bram Moolenaar68563932017-01-10 13:31:15 +010075 endfunction
76else
Bram Moolenaar37c64c72017-09-19 22:06:03 +020077 function s:GetPair(start,end,flags,skip)
78 return searchpair('\m'.a:start,'','\m'.a:end,a:flags,a:skip,s:l1)
Bram Moolenaar68563932017-01-10 13:31:15 +010079 endfunction
80endif
81
Bram Moolenaar37c64c72017-09-19 22:06:03 +020082function s:SynAt(l,c)
83 let byte = line2byte(a:l) + a:c - 1
84 let pos = index(s:synid_cache[0], byte)
85 if pos == -1
86 let s:synid_cache[:] += [[byte], [synIDattr(synID(a:l, a:c, 0), 'name')]]
Bram Moolenaar68563932017-01-10 13:31:15 +010087 endif
Bram Moolenaar37c64c72017-09-19 22:06:03 +020088 return s:synid_cache[1][pos]
Bram Moolenaar68563932017-01-10 13:31:15 +010089endfunction
Bram Moolenaar09521312016-08-12 22:54:35 +020090
Bram Moolenaar37c64c72017-09-19 22:06:03 +020091function s:ParseCino(f)
92 let [divider, n, cstr] = [0] + matchlist(&cino,
93 \ '\%(.*,\)\=\%(\%d'.char2nr(a:f).'\(-\)\=\([.s0-9]*\)\)\=')[1:2]
94 for c in split(cstr,'\zs')
95 if c == '.' && !divider
96 let divider = 1
97 elseif c ==# 's'
98 if n !~ '\d'
99 return n . s:sw() + 0
100 endif
101 let n = str2nr(n) * s:sw()
102 break
103 else
104 let [n, divider] .= [c, 0]
105 endif
106 endfor
107 return str2nr(n) / max([str2nr(divider),1])
108endfunction
109
110" Optimized {skip} expr, only callable from the search loop which
111" GetJavascriptIndent does to find the containing [[{(] (side-effects)
112function s:SkipFunc()
113 if s:top_col == 1
114 throw 'out of bounds'
Bram Moolenaarf0b03c42017-12-17 17:17:07 +0100115 elseif s:check_in
Bram Moolenaar37c64c72017-09-19 22:06:03 +0200116 if eval(s:skip_expr)
117 return 1
118 endif
119 let s:check_in = 0
120 elseif getline('.') =~ '\%<'.col('.').'c\/.\{-}\/\|\%>'.col('.').'c[''"]\|\\$'
121 if eval(s:skip_expr)
Bram Moolenaar37c64c72017-09-19 22:06:03 +0200122 return 1
123 endif
Bram Moolenaarf0b03c42017-12-17 17:17:07 +0100124 elseif search('\m`\|\${\|\*\/','nW'.s:z,s:looksyn)
125 if eval(s:skip_expr)
126 let s:check_in = 1
127 return 1
128 endif
129 else
130 let s:synid_cache[:] += [[line2byte('.') + col('.') - 1], ['']]
Bram Moolenaar37c64c72017-09-19 22:06:03 +0200131 endif
132 let [s:looksyn, s:top_col] = getpos('.')[1:2]
133endfunction
134
135function s:AlternatePair()
136 let [pat, l:for] = ['[][(){};]', 2]
137 while s:SearchLoop(pat,'bW','s:SkipFunc()')
138 if s:LookingAt() == ';'
139 if !l:for
140 if s:GetPair('{','}','bW','s:SkipFunc()')
141 return
142 endif
Bram Moolenaar3c2881d2017-03-21 19:18:29 +0100143 break
Bram Moolenaar37c64c72017-09-19 22:06:03 +0200144 else
145 let [pat, l:for] = ['[{}();]', l:for - 1]
Bram Moolenaar68563932017-01-10 13:31:15 +0100146 endif
Bram Moolenaar3c2881d2017-03-21 19:18:29 +0100147 else
Bram Moolenaar37c64c72017-09-19 22:06:03 +0200148 let idx = stridx('])}',s:LookingAt())
149 if idx == -1
150 return
151 elseif !s:GetPair(['\[','(','{'][idx],'])}'[idx],'bW','s:SkipFunc()')
152 break
153 endif
Bram Moolenaar68563932017-01-10 13:31:15 +0100154 endif
155 endwhile
Bram Moolenaar37c64c72017-09-19 22:06:03 +0200156 throw 'out of bounds'
Bram Moolenaar68563932017-01-10 13:31:15 +0100157endfunction
158
Bram Moolenaar37c64c72017-09-19 22:06:03 +0200159function s:Nat(int)
160 return a:int * (a:int > 0)
Bram Moolenaar68563932017-01-10 13:31:15 +0100161endfunction
162
Bram Moolenaar37c64c72017-09-19 22:06:03 +0200163function s:LookingAt()
Bram Moolenaar68563932017-01-10 13:31:15 +0100164 return getline('.')[col('.')-1]
165endfunction
166
Bram Moolenaar37c64c72017-09-19 22:06:03 +0200167function s:Token()
168 return s:LookingAt() =~ '\k' ? expand('<cword>') : s:LookingAt()
Bram Moolenaar68563932017-01-10 13:31:15 +0100169endfunction
170
Bram Moolenaarf0b03c42017-12-17 17:17:07 +0100171function s:PreviousToken(...)
172 let [l:pos, tok] = [getpos('.'), '']
Bram Moolenaar37c64c72017-09-19 22:06:03 +0200173 if search('\m\k\{1,}\|\S','ebW')
Bram Moolenaarf0b03c42017-12-17 17:17:07 +0100174 if getline('.')[col('.')-2:col('.')-1] == '*/'
175 if eval(s:in_comm) && !s:SearchLoop('\S\ze\_s*\/[/*]','bW',s:in_comm)
176 call setpos('.',l:pos)
177 else
178 let tok = s:Token()
Bram Moolenaar37c64c72017-09-19 22:06:03 +0200179 endif
Bram Moolenaar68563932017-01-10 13:31:15 +0100180 else
Bram Moolenaarf0b03c42017-12-17 17:17:07 +0100181 let two = a:0 || line('.') != l:pos[1] ? strridx(getline('.')[:col('.')],'//') + 1 : 0
182 if two && eval(s:in_comm)
183 call cursor(0,two)
184 let tok = s:PreviousToken(1)
185 if tok is ''
186 call setpos('.',l:pos)
187 endif
188 else
189 let tok = s:Token()
190 endif
Bram Moolenaar68563932017-01-10 13:31:15 +0100191 endif
Bram Moolenaar3c2881d2017-03-21 19:18:29 +0100192 endif
Bram Moolenaarf0b03c42017-12-17 17:17:07 +0100193 return tok
Bram Moolenaar68563932017-01-10 13:31:15 +0100194endfunction
195
Bram Moolenaar37c64c72017-09-19 22:06:03 +0200196function s:Pure(f,...)
197 return eval("[call(a:f,a:000),cursor(a:firstline,".col('.').")][0]")
198endfunction
199
200function s:SearchLoop(pat,flags,expr)
201 return s:GetPair(a:pat,'\_$.',a:flags,a:expr)
202endfunction
203
204function s:ExprCol()
Bram Moolenaarf0b03c42017-12-17 17:17:07 +0100205 if getline('.')[col('.')-2] == ':'
206 return 1
207 endif
Bram Moolenaar3c2881d2017-03-21 19:18:29 +0100208 let bal = 0
Bram Moolenaarf0b03c42017-12-17 17:17:07 +0100209 while s:SearchLoop('[{}?:]','bW',s:skip_expr)
Bram Moolenaar37c64c72017-09-19 22:06:03 +0200210 if s:LookingAt() == ':'
Bram Moolenaarf0b03c42017-12-17 17:17:07 +0100211 if getline('.')[col('.')-2] == ':'
212 call cursor(0,col('.')-1)
213 continue
214 endif
Bram Moolenaar37c64c72017-09-19 22:06:03 +0200215 let bal -= 1
216 elseif s:LookingAt() == '?'
Bram Moolenaarf0b03c42017-12-17 17:17:07 +0100217 if getline('.')[col('.'):col('.')+1] =~ '^\.\d\@!'
218 continue
219 elseif !bal
220 return 1
Bram Moolenaar37c64c72017-09-19 22:06:03 +0200221 endif
Bram Moolenaarf0b03c42017-12-17 17:17:07 +0100222 let bal += 1
Bram Moolenaar37c64c72017-09-19 22:06:03 +0200223 elseif s:LookingAt() == '{'
Bram Moolenaarf0b03c42017-12-17 17:17:07 +0100224 return !s:IsBlock()
Bram Moolenaar37c64c72017-09-19 22:06:03 +0200225 elseif !s:GetPair('{','}','bW',s:skip_expr)
226 break
227 endif
Bram Moolenaar3c2881d2017-03-21 19:18:29 +0100228 endwhile
Bram Moolenaar68563932017-01-10 13:31:15 +0100229endfunction
Bram Moolenaar09521312016-08-12 22:54:35 +0200230
231" configurable regexes that define continuation lines, not including (, {, or [.
Bram Moolenaar68563932017-01-10 13:31:15 +0100232let s:opfirst = '^' . get(g:,'javascript_opfirst',
Bram Moolenaar37c64c72017-09-19 22:06:03 +0200233 \ '\C\%([<>=,.?^%|/&]\|\([-:+]\)\1\@!\|\*\+\|!=\|in\%(stanceof\)\=\>\)')
Bram Moolenaar68563932017-01-10 13:31:15 +0100234let s:continuation = get(g:,'javascript_continuation',
Bram Moolenaar37c64c72017-09-19 22:06:03 +0200235 \ '\C\%([<=,.~!?/*^%|&:]\|+\@<!+\|-\@<!-\|=\@<!>\|\<\%(typeof\|new\|delete\|void\|in\|instanceof\|await\)\)') . '$'
Bram Moolenaare4a3bcf2016-08-26 19:52:37 +0200236
Bram Moolenaarf0b03c42017-12-17 17:17:07 +0100237function s:Continues()
238 let tok = matchstr(strpart(getline('.'),col('.')-15,15),s:continuation)
Bram Moolenaar37c64c72017-09-19 22:06:03 +0200239 if tok =~ '[a-z:]'
Bram Moolenaar37c64c72017-09-19 22:06:03 +0200240 return tok == ':' ? s:ExprCol() : s:PreviousToken() != '.'
241 elseif tok !~ '[/>]'
242 return tok isnot ''
Bram Moolenaar3c2881d2017-03-21 19:18:29 +0100243 endif
Bram Moolenaarf0b03c42017-12-17 17:17:07 +0100244 return s:SynAt(line('.'),col('.')) !~? (tok == '>' ? 'jsflow\|^html' : 'regex')
Bram Moolenaar09521312016-08-12 22:54:35 +0200245endfunction
246
247" Check if line 'lnum' has a balanced amount of parentheses.
Bram Moolenaarf0b03c42017-12-17 17:17:07 +0100248function s:Balanced(lnum,line)
249 let l:open = 0
250 let pos = match(a:line, '[][(){}]')
Bram Moolenaar09521312016-08-12 22:54:35 +0200251 while pos != -1
Bram Moolenaar37c64c72017-09-19 22:06:03 +0200252 if s:SynAt(a:lnum,pos + 1) !~? b:syng_strcom
Bram Moolenaarf0b03c42017-12-17 17:17:07 +0100253 let l:open += match(' ' . a:line[pos],'[[({]')
Bram Moolenaar68563932017-01-10 13:31:15 +0100254 if l:open < 0
255 return
Bram Moolenaar09521312016-08-12 22:54:35 +0200256 endif
257 endif
Bram Moolenaarf0b03c42017-12-17 17:17:07 +0100258 let pos = match(a:line, !l:open ? '[][(){}]' : '()' =~ a:line[pos] ?
259 \ '[()]' : '{}' =~ a:line[pos] ? '[{}]' : '[][]', pos + 1)
Bram Moolenaar09521312016-08-12 22:54:35 +0200260 endwhile
Bram Moolenaar68563932017-01-10 13:31:15 +0100261 return !l:open
Bram Moolenaar09521312016-08-12 22:54:35 +0200262endfunction
Bram Moolenaar68563932017-01-10 13:31:15 +0100263
Bram Moolenaar37c64c72017-09-19 22:06:03 +0200264function s:OneScope()
265 if s:LookingAt() == ')' && s:GetPair('(', ')', 'bW', s:skip_expr)
266 let tok = s:PreviousToken()
267 return (count(split('for if let while with'),tok) ||
268 \ tok =~# '^await$\|^each$' && s:PreviousToken() ==# 'for') &&
269 \ s:Pure('s:PreviousToken') != '.' && !(tok == 'while' && s:DoWhile())
270 elseif s:Token() =~# '^else$\|^do$'
271 return s:Pure('s:PreviousToken') != '.'
Bram Moolenaarf0b03c42017-12-17 17:17:07 +0100272 elseif strpart(getline('.'),col('.')-2,2) == '=>'
273 call cursor(0,col('.')-1)
274 if s:PreviousToken() == ')'
275 return s:GetPair('(', ')', 'bW', s:skip_expr)
276 endif
277 return 1
Bram Moolenaar68563932017-01-10 13:31:15 +0100278 endif
Bram Moolenaar68563932017-01-10 13:31:15 +0100279endfunction
280
Bram Moolenaar37c64c72017-09-19 22:06:03 +0200281function s:DoWhile()
282 let cpos = searchpos('\m\<','cbW')
Bram Moolenaarf0b03c42017-12-17 17:17:07 +0100283 while s:SearchLoop('\C[{}]\|\<\%(do\|while\)\>','bW',s:skip_expr)
284 if s:LookingAt() =~ '\a'
285 if s:Pure('s:IsBlock')
286 if s:LookingAt() ==# 'd'
287 return 1
288 endif
289 break
290 endif
291 elseif s:LookingAt() != '}' || !s:GetPair('{','}','bW',s:skip_expr)
292 break
Bram Moolenaar37c64c72017-09-19 22:06:03 +0200293 endif
Bram Moolenaarf0b03c42017-12-17 17:17:07 +0100294 endwhile
295 call call('cursor',cpos)
Bram Moolenaar37c64c72017-09-19 22:06:03 +0200296endfunction
297
298" returns total offset from braceless contexts. 'num' is the lineNr which
299" encloses the entire context, 'cont' if whether a:firstline is a continued
300" expression, which could have started in a braceless context
Bram Moolenaarf0b03c42017-12-17 17:17:07 +0100301function s:IsContOne(cont)
302 let [l:num, b_l] = [b:js_cache[1] + !b:js_cache[1], 0]
303 let pind = b:js_cache[1] ? indent(b:js_cache[1]) + s:sw() : 0
Bram Moolenaar37c64c72017-09-19 22:06:03 +0200304 let ind = indent('.') + !a:cont
305 while line('.') > l:num && ind > pind || line('.') == l:num
306 if indent('.') < ind && s:OneScope()
307 let b_l += 1
308 elseif !a:cont || b_l || ind < indent(a:firstline)
309 break
310 else
311 call cursor(0,1)
312 endif
313 let ind = min([ind, indent('.')])
314 if s:PreviousToken() is ''
Bram Moolenaar68563932017-01-10 13:31:15 +0100315 break
316 endif
Bram Moolenaar68563932017-01-10 13:31:15 +0100317 endwhile
Bram Moolenaar37c64c72017-09-19 22:06:03 +0200318 return b_l
319endfunction
320
Bram Moolenaar37c64c72017-09-19 22:06:03 +0200321function s:IsSwitch()
Bram Moolenaarf0b03c42017-12-17 17:17:07 +0100322 call call('cursor',b:js_cache[1:])
323 return search('\m\C\%#.\_s*\%(\%(\/\/.*\_$\|\/\*\_.\{-}\*\/\)\@>\_s*\)*\%(case\|default\)\>','nWc'.s:z)
Bram Moolenaar68563932017-01-10 13:31:15 +0100324endfunction
325
326" https://github.com/sweet-js/sweet.js/wiki/design#give-lookbehind-to-the-reader
327function s:IsBlock()
Bram Moolenaar37c64c72017-09-19 22:06:03 +0200328 let tok = s:PreviousToken()
329 if join(s:stack) =~? 'xml\|jsx' && s:SynAt(line('.'),col('.')-1) =~? 'xml\|jsx'
Bram Moolenaarf0b03c42017-12-17 17:17:07 +0100330 let s:in_jsx = 1
Bram Moolenaar37c64c72017-09-19 22:06:03 +0200331 return tok != '{'
332 elseif tok =~ '\k'
333 if tok ==# 'type'
334 return s:Pure('eval',"s:PreviousToken() !~# '^\\%(im\\|ex\\)port$' || s:PreviousToken() == '.'")
335 elseif tok ==# 'of'
336 return s:Pure('eval',"!s:GetPair('[[({]','[])}]','bW',s:skip_expr) || s:LookingAt() != '(' ||"
337 \ ."s:{s:PreviousToken() ==# 'await' ? 'Previous' : ''}Token() !=# 'for' || s:PreviousToken() == '.'")
Bram Moolenaar68563932017-01-10 13:31:15 +0100338 endif
Bram Moolenaar37c64c72017-09-19 22:06:03 +0200339 return index(split('return const let import export extends yield default delete var await void typeof throw case new in instanceof')
340 \ ,tok) < (line('.') != a:firstline) || s:Pure('s:PreviousToken') == '.'
341 elseif tok == '>'
342 return getline('.')[col('.')-2] == '=' || s:SynAt(line('.'),col('.')) =~? 'jsflow\|^html'
343 elseif tok == '*'
344 return s:Pure('s:PreviousToken') == ':'
345 elseif tok == ':'
346 return s:Pure('eval',"s:PreviousToken() =~ '^\\K\\k*$' && !s:ExprCol()")
347 elseif tok == '/'
348 return s:SynAt(line('.'),col('.')) =~? 'regex'
349 elseif tok !~ '[=~!<,.?^%|&([]'
350 return tok !~ '[-+]' || line('.') != a:firstline && getline('.')[col('.')-2] == tok
Bram Moolenaar68563932017-01-10 13:31:15 +0100351 endif
352endfunction
Bram Moolenaar09521312016-08-12 22:54:35 +0200353
354function GetJavascriptIndent()
Bram Moolenaarf0b03c42017-12-17 17:17:07 +0100355 call s:GetVars()
Bram Moolenaar37c64c72017-09-19 22:06:03 +0200356 let s:synid_cache = [[],[]]
357 let l:line = getline(v:lnum)
Bram Moolenaar3c2881d2017-03-21 19:18:29 +0100358 " use synstack as it validates syn state and works in an empty line
Bram Moolenaar37c64c72017-09-19 22:06:03 +0200359 let s:stack = [''] + map(synstack(v:lnum,1),"synIDattr(v:val,'name')")
Bram Moolenaar09521312016-08-12 22:54:35 +0200360
Bram Moolenaar68563932017-01-10 13:31:15 +0100361 " start with strings,comments,etc.
Bram Moolenaar37c64c72017-09-19 22:06:03 +0200362 if s:stack[-1] =~? 'comment\|doc'
Bram Moolenaar68563932017-01-10 13:31:15 +0100363 if l:line =~ '^\s*\*'
364 return cindent(v:lnum)
365 elseif l:line !~ '^\s*\/[/*]'
366 return -1
367 endif
Bram Moolenaar37c64c72017-09-19 22:06:03 +0200368 elseif s:stack[-1] =~? b:syng_str
Bram Moolenaarf0b03c42017-12-17 17:17:07 +0100369 if b:js_cache[0] == v:lnum - 1 && s:Balanced(v:lnum-1,getline(v:lnum-1))
Bram Moolenaar68563932017-01-10 13:31:15 +0100370 let b:js_cache[0] = v:lnum
371 endif
Bram Moolenaar09521312016-08-12 22:54:35 +0200372 return -1
373 endif
Bram Moolenaar37c64c72017-09-19 22:06:03 +0200374
375 let s:l1 = max([0,prevnonblank(v:lnum) - (s:rel ? 2000 : 1000),
376 \ get(get(b:,'hi_indent',{}),'blocklnr')])
377 call cursor(v:lnum,1)
378 if s:PreviousToken() is ''
Bram Moolenaar68563932017-01-10 13:31:15 +0100379 return
Bram Moolenaar09521312016-08-12 22:54:35 +0200380 endif
Bram Moolenaar37c64c72017-09-19 22:06:03 +0200381 let [l:lnum, pline] = [line('.'), getline('.')[:col('.')-1]]
Bram Moolenaar09521312016-08-12 22:54:35 +0200382
Bram Moolenaar68563932017-01-10 13:31:15 +0100383 let l:line = substitute(l:line,'^\s*','','')
Bram Moolenaar37c64c72017-09-19 22:06:03 +0200384 let l:line_raw = l:line
Bram Moolenaar68563932017-01-10 13:31:15 +0100385 if l:line[:1] == '/*'
386 let l:line = substitute(l:line,'^\%(\/\*.\{-}\*\/\s*\)*','','')
Bram Moolenaar09521312016-08-12 22:54:35 +0200387 endif
Bram Moolenaar68563932017-01-10 13:31:15 +0100388 if l:line =~ '^\/[/*]'
389 let l:line = ''
390 endif
Bram Moolenaar09521312016-08-12 22:54:35 +0200391
Bram Moolenaar68563932017-01-10 13:31:15 +0100392 " the containing paren, bracket, or curly. Many hacks for performance
Bram Moolenaar37c64c72017-09-19 22:06:03 +0200393 call cursor(v:lnum,1)
Bram Moolenaar3c2881d2017-03-21 19:18:29 +0100394 let idx = index([']',')','}'],l:line[0])
Bram Moolenaar37c64c72017-09-19 22:06:03 +0200395 if b:js_cache[0] > l:lnum && b:js_cache[0] < v:lnum ||
Bram Moolenaarf0b03c42017-12-17 17:17:07 +0100396 \ b:js_cache[0] == l:lnum && s:Balanced(l:lnum,pline)
Bram Moolenaar68563932017-01-10 13:31:15 +0100397 call call('cursor',b:js_cache[1:])
Bram Moolenaar09521312016-08-12 22:54:35 +0200398 else
Bram Moolenaar37c64c72017-09-19 22:06:03 +0200399 let [s:looksyn, s:top_col, s:check_in, s:l1] = [v:lnum - 1,0,0,
400 \ max([s:l1, &smc ? search('\m^.\{'.&smc.',}','nbW',s:l1 + 1) + 1 : 0])]
401 try
402 if idx != -1
403 call s:GetPair(['\[','(','{'][idx],'])}'[idx],'bW','s:SkipFunc()')
404 elseif getline(v:lnum) !~ '^\S' && s:stack[-1] =~? 'block\|^jsobject$'
405 call s:GetPair('{','}','bW','s:SkipFunc()')
406 else
407 call s:AlternatePair()
408 endif
409 catch /^\Cout of bounds$/
410 call cursor(v:lnum,1)
411 endtry
412 let b:js_cache[1:] = line('.') == v:lnum ? [0,0] : getpos('.')[1:2]
Bram Moolenaar09521312016-08-12 22:54:35 +0200413 endif
414
Bram Moolenaar37c64c72017-09-19 22:06:03 +0200415 let [b:js_cache[0], num] = [v:lnum, b:js_cache[1]]
Bram Moolenaar68563932017-01-10 13:31:15 +0100416
Bram Moolenaarf0b03c42017-12-17 17:17:07 +0100417 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 +0200418 if !num || s:LookingAt() == '{' && s:IsBlock()
Bram Moolenaar3c2881d2017-03-21 19:18:29 +0100419 let ilnum = line('.')
Bram Moolenaarf0b03c42017-12-17 17:17:07 +0100420 if num && !s:in_jsx && s:LookingAt() == ')' && s:GetPair('(',')','bW',s:skip_expr)
Bram Moolenaar37c64c72017-09-19 22:06:03 +0200421 if ilnum == num
422 let [num, num_ind] = [line('.'), indent('.')]
423 endif
424 if idx == -1 && s:PreviousToken() ==# 'switch' && s:IsSwitch()
425 let l:switch_offset = &cino !~ ':' ? s:sw() : s:ParseCino(':')
Bram Moolenaar68563932017-01-10 13:31:15 +0100426 if pline[-1:] != '.' && l:line =~# '^\%(default\|case\)\>'
Bram Moolenaar37c64c72017-09-19 22:06:03 +0200427 return s:Nat(num_ind + l:switch_offset)
428 elseif &cino =~ '='
429 let l:case_offset = s:ParseCino('=')
Bram Moolenaar68563932017-01-10 13:31:15 +0100430 endif
431 endif
432 endif
Bram Moolenaar37c64c72017-09-19 22:06:03 +0200433 if idx == -1 && pline[-1:] !~ '[{;]'
Bram Moolenaarf0b03c42017-12-17 17:17:07 +0100434 call cursor(l:lnum, len(pline))
Bram Moolenaar37c64c72017-09-19 22:06:03 +0200435 let sol = matchstr(l:line,s:opfirst)
436 if sol is '' || sol == '/' && s:SynAt(v:lnum,
437 \ 1 + len(getline(v:lnum)) - len(l:line)) =~? 'regex'
Bram Moolenaarf0b03c42017-12-17 17:17:07 +0100438 if s:Continues()
Bram Moolenaar37c64c72017-09-19 22:06:03 +0200439 let is_op = s:sw()
440 endif
Bram Moolenaarf0b03c42017-12-17 17:17:07 +0100441 elseif num && sol =~# '^\%(in\%(stanceof\)\=\|\*\)$' &&
442 \ s:LookingAt() == '}' && s:GetPair('{','}','bW',s:skip_expr) &&
443 \ s:PreviousToken() == ')' && s:GetPair('(',')','bW',s:skip_expr) &&
444 \ (s:PreviousToken() == ']' || s:LookingAt() =~ '\k' &&
445 \ s:{s:PreviousToken() == '*' ? 'Previous' : ''}Token() !=# 'function')
446 return num_ind + s:sw()
Bram Moolenaar37c64c72017-09-19 22:06:03 +0200447 else
448 let is_op = s:sw()
449 endif
450 call cursor(l:lnum, len(pline))
Bram Moolenaarf0b03c42017-12-17 17:17:07 +0100451 let b_l = s:Nat(s:IsContOne(is_op) - (!is_op && l:line =~ '^{')) * s:sw()
Bram Moolenaar68563932017-01-10 13:31:15 +0100452 endif
Bram Moolenaar37c64c72017-09-19 22:06:03 +0200453 elseif idx.s:LookingAt().&cino =~ '^-1(.*(' && (search('\m\S','nbW',num) || s:ParseCino('U'))
454 let pval = s:ParseCino('(')
455 if !pval
456 let [Wval, vcol] = [s:ParseCino('W'), virtcol('.')]
457 if search('\m\S','W',num)
458 return s:ParseCino('w') ? vcol : virtcol('.')-1
459 endif
460 return Wval ? s:Nat(num_ind + Wval) : vcol
461 endif
462 return s:Nat(num_ind + pval + searchpair('\m(','','\m)','nbrmW',s:skip_expr,num) * s:sw())
Bram Moolenaar68563932017-01-10 13:31:15 +0100463 endif
464
465 " main return
Bram Moolenaar37c64c72017-09-19 22:06:03 +0200466 if l:line =~ '^[])}]\|^|}'
Bram Moolenaarf0b03c42017-12-17 17:17:07 +0100467 if l:line_raw[0] == ')'
Bram Moolenaar37c64c72017-09-19 22:06:03 +0200468 if s:ParseCino('M')
469 return indent(l:lnum)
Bram Moolenaarf0b03c42017-12-17 17:17:07 +0100470 elseif num && &cino =~# 'm' && !s:ParseCino('m')
Bram Moolenaar37c64c72017-09-19 22:06:03 +0200471 return virtcol('.') - 1
472 endif
473 endif
474 return num_ind
Bram Moolenaar68563932017-01-10 13:31:15 +0100475 elseif num
Bram Moolenaar37c64c72017-09-19 22:06:03 +0200476 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 +0100477 endif
Bram Moolenaar2ecbe532022-07-29 21:36:21 +0100478
479 let nest = get(get(b:, 'hi_indent', {}), 'blocklnr')
480 if nest
481 return indent(nextnonblank(nest + 1)) + b_l + is_op
482 endif
483
Bram Moolenaar37c64c72017-09-19 22:06:03 +0200484 return b_l + is_op
Bram Moolenaar09521312016-08-12 22:54:35 +0200485endfunction
486
Bram Moolenaar09521312016-08-12 22:54:35 +0200487let &cpo = s:cpo_save
488unlet s:cpo_save