Skip to content

Commit 12bbaba

Browse files
bouncemeamadeus
authored andcommitted
timeout the paren searching (#453)
the search pair function almost stalls my decently powerful macbook pro on really long lines, could be annoying because of minification etc.this function just makes it backwards compatible in old vim versions or builds without this feature
1 parent 120352b commit 12bbaba

File tree

1 file changed

+20
-10
lines changed

1 file changed

+20
-10
lines changed

indent/javascript.vim

+20-10
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ endif
4242
" ============
4343

4444
let s:line_pre = '^\s*\%(\/\*.*\*\/\s*\)*'
45-
let s:js_keywords = s:line_pre . '\%(break\|catch\|const\|continue\|debugger\|delete\|do\|else\|finally\|for\|function\|if\|in\|instanceof\|let\|new\|return\|switch\|this\|throw\|try\|typeof\|var\|void\|while\|with\)\>\C'
45+
let s:js_keywords = s:line_pre . '\%(break\|import\|export\|catch\|const\|continue\|debugger\|delete\|do\|else\|finally\|for\|function\|if\|in\|instanceof\|let\|new\|return\|switch\|this\|throw\|try\|typeof\|var\|void\|while\|with\)\>\C'
4646
let s:expr_case = s:line_pre . '\%(case\s\+[^\:]*\|default\)\s*:\s*\C'
4747
" Regex of syntax group names that are or delimit string or are comments.
4848
let s:syng_strcom = '\%(string\|regex\|comment\|template\)\c'
@@ -59,6 +59,14 @@ let s:syng_linecom = 'linecomment\c'
5959
" Expression used to check whether we should skip a match with searchpair().
6060
let s:skip_expr = "synIDattr(synID(line('.'),col('.'),1),'name') =~ '".s:syng_strcom."'"
6161

62+
func s:lookForParens(start,end,flags,stop)
63+
try
64+
return searchpair(a:start,'',a:end,a:flags,s:skip_expr,a:stop,300)
65+
catch /E118/
66+
return searchpair(a:start,'',a:end,a:flags,0,a:stop)
67+
endtry
68+
endfunc
69+
6270
let s:line_term = '\s*\%(\%(\/\/.*\)\=\|\%(\/\*.*\*\/\s*\)*\)$'
6371

6472
" Regex that defines continuation lines, not including (, {, or [.
@@ -73,7 +81,7 @@ function s:Onescope(lnum)
7381
let mypos = col('.')
7482
call cursor(a:lnum, 1)
7583
if search('\<\%(while\|for\|if\)\>\s*(\C', 'ce', a:lnum) > 0 &&
76-
\ searchpair('(', '', ')', 'W', s:skip_expr, a:lnum) > 0 &&
84+
\ s:lookForParens('(', ')', 'W', a:lnum) > 0 &&
7785
\ col('.') == strlen(s:RemoveTrailingComments(getline(a:lnum)))
7886
call cursor(a:lnum, mypos)
7987
return 1
@@ -153,7 +161,7 @@ function s:GetMSL(lnum, in_one_line_scope)
153161
" if there are more closing brackets, continue from the line which has the matching opening bracket
154162
elseif col2 > 0 && !s:IsInStringOrComment(msl, col2) && s:LineHasOpeningBrackets(msl)[0] == '2' && !a:in_one_line_scope
155163
call cursor(msl, 1)
156-
if searchpair('(', '', ')', 'bW', s:skip_expr) > 0
164+
if s:lookForParens('(', ')', 'bW', 0) > 0
157165
let lnum = line('.')
158166
let msl = lnum
159167
endif
@@ -185,17 +193,18 @@ endfunction
185193
" Find if the string is inside var statement (but not the first string)
186194
function s:InMultiVarStatement(lnum, cont, prev)
187195
let lnum = s:PrevNonBlankNonString(a:lnum - 1)
196+
let prev = a:prev
188197

189198
" let type = synIDattr(synID(lnum, indent(lnum) + 1, 0), 'name')
190199

191200
" loop through previous expressions to find a var statement
192201
while lnum > 0 && (s:Match(lnum, s:comma_last) ||(a:cont && getline(lnum) =~ s:line_pre . '}') ||
193-
\ s:Match(lnum,s:continuation_regex)) || (a:prev && (s:Match(a:prev, s:comma_last) ||
194-
\ s:Match(a:prev,s:continuation_regex)))
202+
\ s:Match(lnum,s:continuation_regex)) || (prev && (s:Match(prev, s:comma_last) ||
203+
\ s:Match(prev,s:continuation_regex)))
195204
" if the line is a js keyword
196205
if a:cont
197206
call cursor(lnum,1)
198-
let parlnum = searchpair('(\|{\|\[', '', ')\|}\|\]', 'nbW', s:skip_expr)
207+
let parlnum = s:lookForParens('(\|{\|\[', ')\|}\|\]', 'nbW', 0)
199208
if parlnum > 0
200209
let lnum = parlnum
201210
end
@@ -214,6 +223,7 @@ function s:InMultiVarStatement(lnum, cont, prev)
214223
end
215224
endif
216225
let lnum = s:PrevNonBlankNonString(lnum - 1)
226+
let prev = prev && lnum > 0 ? prev : 0
217227
endwhile
218228

219229
" beginning of program, not a var
@@ -348,7 +358,7 @@ function GetJavascriptIndent()
348358
call cursor(v:lnum, col)
349359

350360

351-
let parlnum = searchpair('(\|{\|\[', '', ')\|}\|\]', 'nbW', s:skip_expr)
361+
let parlnum = s:lookForParens('(\|{\|\[', ')\|}\|\]', 'nbW', 0)
352362
if parlnum > 0
353363
let ind = s:InMultiVarStatement(parlnum, 0, 0) ? indent(parlnum) : indent(s:GetMSL(parlnum, 0))
354364
endif
@@ -367,7 +377,7 @@ function GetJavascriptIndent()
367377
if counts[0] == '2' || counts[1] == '2' || counts[2] == '2'
368378
call cursor(lnum, 1)
369379
" Search for the opening tag
370-
let parlnum = searchpair('(\|{\|\[', '', ')\|}\|\]', 'nbW', s:skip_expr)
380+
let parlnum = s:lookForParens('(\|{\|\[', ')\|}\|\]', 'nbW', 0)
371381
if parlnum > 0 && s:Match(parlnum, s:operator_first)
372382
return indent(parlnum)
373383
end
@@ -382,7 +392,7 @@ function GetJavascriptIndent()
382392
if counts[0] == '2' && !s:Match(lnum, s:operator_first)
383393
call cursor(lnum, 1)
384394
" Search for the opening tag
385-
let mnum = searchpair('(', '', ')', 'nbW', s:skip_expr)
395+
let mnum = s:lookForParens('(', ')', 'nbW', 0)
386396
if mnum > 0 && s:Match(mnum, s:operator_first)
387397
return indent(mnum) - s:sw()
388398
end
@@ -432,7 +442,7 @@ function GetJavascriptIndent()
432442
\ (counts[2] == '2' && !s:Match(lnum, s:line_pre . ']'))
433443
call cursor(lnum, 1)
434444
" Search for the opening tag
435-
let parlnum = searchpair('(\|{\|\[', '', ')\|}\|\]', 'nbW', s:skip_expr)
445+
let parlnum = s:lookForParens('(\|{\|\[', ')\|}\|\]', 'nbW', 0)
436446
if parlnum > 0
437447
return indent(s:GetMSL(parlnum, 0))
438448
end

0 commit comments

Comments
 (0)