Skip to content

Commit fc80419

Browse files
authored
trivial indent fixes / updates (pangloss#1007)
trivial indent fixes / non-trivial control flow updates
1 parent 09d7c70 commit fc80419

1 file changed

Lines changed: 43 additions & 41 deletions

File tree

indent/javascript.vim

Lines changed: 43 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -62,13 +62,13 @@ let s:skip_expr = "s:SynAt(line('.'),col('.')) =~? b:syng_strcom"
6262
" searchpair() wrapper
6363
if has('reltime')
6464
function s:GetPair(start,end,flags,skip,time,...)
65-
return s:Nat(searchpair('\m'.(a:start == '[' ? '\[' : a:start),'','\m'.a:end,
66-
\ a:flags,a:skip,max([prevnonblank(v:lnum) - 2000,0] + a:000),a:time))
65+
return searchpair('\m'.(a:start == '[' ? '\[' : a:start),'','\m'.a:end,
66+
\ a:flags,a:skip,max([prevnonblank(v:lnum) - 2000,0] + a:000),a:time)
6767
endfunction
6868
else
6969
function s:GetPair(start,end,flags,skip,...)
70-
return s:Nat(searchpair('\m'.(a:start == '[' ? '\[' : a:start),'','\m'.a:end,
71-
\ a:flags,a:skip,max([prevnonblank(v:lnum) - 1000,0,get(a:000,1)])))
70+
return searchpair('\m'.(a:start == '[' ? '\[' : a:start),'','\m'.a:end,
71+
\ a:flags,a:skip,max([prevnonblank(v:lnum) - 1000,0,get(a:000,1)]))
7272
endfunction
7373
endif
7474

@@ -109,7 +109,7 @@ endfunction
109109
" Optimized {skip} expr, used only once per GetJavascriptIndent() call
110110
function s:SkipFunc()
111111
if s:top_col == 1
112-
return {} " E728, used as limit condition for loops and searchpair()
112+
throw 'out of bounds'
113113
endif
114114
let s:top_col = col('.')
115115
if getline('.') =~ '\%<'.s:top_col.'c\/.\{-}\/\|\%>'.s:top_col.'c[''"]\|\\$'
@@ -128,7 +128,7 @@ function s:SkipFunc()
128128
endfunction
129129

130130
function s:AlternatePair()
131-
let [l:pos, pat, l:for] = [getpos('.'), '[][(){};]', 2]
131+
let [pat, l:for] = ['[][(){};]', 2]
132132
while search('\m'.pat,'bW',s:script_tag)
133133
if s:SkipFunc()
134134
continue
@@ -150,7 +150,7 @@ function s:AlternatePair()
150150
endif
151151
endif
152152
endwhile
153-
call setpos('.',l:pos)
153+
throw 'out of bounds'
154154
endfunction
155155

156156
function s:Nat(int)
@@ -218,23 +218,19 @@ endfunction
218218

219219
" configurable regexes that define continuation lines, not including (, {, or [.
220220
let s:opfirst = '^' . get(g:,'javascript_opfirst',
221-
\ '\C\%([<>=,.?^%|*/&]\|\([-:+]\)\1\@!\|!=\|in\%(stanceof\)\=\>\)')
221+
\ '\C\%([<>=,.?^%|/&]\|\([-:+]\)\1\@!\|\*\+\|!=\|in\%(stanceof\)\=\>\)')
222222
let s:continuation = get(g:,'javascript_continuation',
223223
\ '\C\%([<=,.~!?/*^%|&:]\|+\@<!+\|-\@<!-\|=\@<!>\|\<\%(typeof\|new\|delete\|void\|in\|instanceof\|await\)\)') . '$'
224224

225225
function s:Continues(ln,con)
226226
let tok = matchstr(a:con[-15:],s:continuation)
227-
if tok isnot ''
227+
if tok =~ '[a-z:]'
228228
call cursor(a:ln,strlen(a:con))
229-
if tok =~ '[/>]'
230-
return s:SynAt(a:ln,col('.')) !~? (tok == '>' ? 'jsflow\|^html' : 'regex')
231-
elseif tok =~ '\l'
232-
return s:PreviousToken() != '.'
233-
elseif tok == ':'
234-
return s:ExprCol()
235-
endif
236-
return 1
229+
return tok == ':' ? s:ExprCol() : s:PreviousToken() != '.'
230+
elseif tok !~ '[/>]'
231+
return tok isnot ''
237232
endif
233+
return s:SynAt(a:ln,strlen(a:con)) !~? (tok == '>' ? 'jsflow\|^html' : 'regex')
238234
endfunction
239235

240236
function s:Trim(ln)
@@ -262,19 +258,17 @@ function s:PrevCodeLine(lnum)
262258
let l:pos = getpos('.')
263259
call cursor(l:n,1)
264260
let l:n = search('\m\/\*','bW')
265-
while search('\m\/\*\|\(\*\/\)','bWp') == 1
266-
let br = 0
267-
for l:i in range(l:n,line('.'),-1)
268-
if s:SynAt(l:i,l:i == line('.') ? col('.') : 1) !~? s:syng_com
269-
let br = 1
270-
break
271-
endif
272-
endfor
273-
if br
274-
break
275-
endif
276-
let l:n = line('.')
277-
endwhile
261+
try
262+
while search('\m\/\*\|\(\*\/\)','bWp') == 1
263+
for l:i in range(l:n,line('.'),-1)
264+
if s:SynAt(l:i,l:i == line('.') ? col('.') : 1) !~? s:syng_com
265+
throw 'to main loop'
266+
endif
267+
endfor
268+
let l:n = line('.')
269+
endwhile
270+
catch
271+
endtry
278272
call setpos('.',l:pos)
279273
else
280274
break
@@ -440,13 +434,17 @@ function GetJavascriptIndent()
440434
else
441435
call cursor(v:lnum,1)
442436
let [s:looksyn, s:check_in, s:top_col] = [v:lnum - 1, 0, 0]
443-
if idx != -1
444-
call s:GetPair('[({'[idx],'])}'[idx],'bW','s:SkipFunc()',2000,s:script_tag)
445-
elseif getline(v:lnum) !~ '^\S' && syns =~? 'block'
446-
call s:GetPair('{','}','bW','s:SkipFunc()',2000,s:script_tag)
447-
else
448-
call s:AlternatePair()
449-
endif
437+
try
438+
if idx != -1
439+
call s:GetPair('[({'[idx],'])}'[idx],'bW','s:SkipFunc()',2000,s:script_tag)
440+
elseif getline(v:lnum) !~ '^\S' && syns =~? 'block'
441+
call s:GetPair('{','}','bW','s:SkipFunc()',2000,s:script_tag)
442+
else
443+
call s:AlternatePair()
444+
endif
445+
catch
446+
call cursor(v:lnum,1)
447+
endtry
450448
endif
451449

452450
let b:js_cache = [v:lnum] + (line('.') == v:lnum ? [s:script_tag,0] : getpos('.')[1:2])
@@ -469,14 +467,18 @@ function GetJavascriptIndent()
469467
endif
470468
endif
471469
if idx == -1 && pline[-1:] !~ '[{;]'
472-
if l:line =~# '^\%(in\%(stanceof\)\=\>\|\*\*\@!\)' && pline[-1:] == '}'
470+
let sol = matchstr(l:line,s:opfirst)
471+
if sol =~# '^\%(in\%(stanceof\)\=\|\*\)$'
473472
call cursor(l:lnum,strlen(pline))
474-
if s:GetPair('{','}','bW',s:skip_expr,200) && s:IsBlock()
473+
if pline[-1:] == '}' && s:GetPair('{','}','bW',s:skip_expr,200) && s:IsBlock()
475474
return num_ind + s:sw()
476475
endif
476+
let is_op = s:sw()
477+
elseif sol isnot '' || s:Continues(l:lnum,pline)
478+
let is_op = s:sw()
477479
endif
478-
let is_op = (l:line =~# s:opfirst || s:Continues(l:lnum,pline)) * s:sw()
479-
let b_l = s:Nat(s:IsContOne(l:lnum,b:js_cache[1],is_op) - (l:line =~ '^{')) * s:sw()
480+
let b_l = s:Nat(s:IsContOne(l:lnum,b:js_cache[1],is_op) -
481+
\ (!is_op && l:line =~ '^{')) * s:sw()
480482
endif
481483
elseif idx == -1 && getline(b:js_cache[1])[b:js_cache[2]-1] == '(' && &cino =~ '(' &&
482484
\ (search('\m\S','nbW',num) || s:ParseCino('U'))

0 commit comments

Comments
 (0)