Skip to content

Commit 0b6aad4

Browse files
authored
speed increase attempt (#629)
1 parent 81e4e6f commit 0b6aad4

File tree

1 file changed

+20
-9
lines changed

1 file changed

+20
-9
lines changed

indent/javascript.vim

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -47,13 +47,23 @@ let s:syng_comment = '\%(comment\|doc\)'
4747

4848
" Expression used to check whether we should skip a match with searchpair().
4949
let s:skip_expr = "synIDattr(synID(line('.'),col('.'),0),'name') =~? '".s:syng_strcom."'"
50+
function s:skip_func(lnum)
51+
if !s:free || getline(line('.')) =~ '[''/"\\]'
52+
return eval(s:skip_expr)
53+
endif
54+
if search('`','nW',a:lnum) || search('\*\/','nW',a:lnum)
55+
let s:free = !eval(s:skip_expr)
56+
endif
57+
let s:looksyn = s:free ? line('.') : s:looksyn
58+
return !s:free
59+
endfunction
5060

5161
if has('reltime')
52-
function s:GetPair(start,end,flags,time)
53-
return searchpair(a:start,'',a:end,a:flags,s:skip_expr,max([prevnonblank(v:lnum) - 2000,0]),a:time)
62+
function s:GetPair(start,end,flags,skip,time)
63+
return searchpair(a:start,'',a:end,a:flags,a:skip,max([prevnonblank(v:lnum) - 2000,0]),a:time)
5464
endfunction
5565
else
56-
function s:GetPair(start,end,flags,n)
66+
function s:GetPair(start,end,flags,...)
5767
return searchpair(a:start,'',a:end,a:flags,0,max([prevnonblank(v:lnum) - 2000,0]))
5868
endfunction
5969
endif
@@ -74,8 +84,8 @@ let g:javascript_continuation .= s:line_term
7484
function s:OneScope(lnum,text)
7585
return a:text =~# '\%(\<else\|\<do\|=>\)' . s:line_term ? 'no b' :
7686
\ cursor(a:lnum, match(' ' . a:text, ')' . s:line_term)) > -1 &&
77-
\ s:GetPair('(', ')', 'bW', 100) > 0 && search('\C\l\+\_s*\%#','bW') &&
78-
\ (expand('<cword>') !=# 'while' || s:GetPair('\C\<do\>', '\C\<while\>','nbW',100) <= 0) &&
87+
\ s:GetPair('(', ')', 'bW', s:skip_expr, 100) > 0 && search('\C\l\+\_s*\%#','bW') &&
88+
\ (expand('<cword>') !=# 'while' || s:GetPair('\C\<do\>', '\C\<while\>','nbW',s:skip_expr,100) <= 0) &&
7989
\ (expand('<cword>') !=# 'each' || search('\C\<for\_s\+\%#','nbW')) ? expand('<cword>') : ''
8090
endfunction
8191

@@ -131,7 +141,7 @@ function GetJavascriptIndent()
131141
let syns = synIDattr(synID(v:lnum, 1, 0), 'name')
132142

133143
" start with strings,comments,etc.{{{2
134-
if (l:line !~ '^[''"`]' && syns =~? '\%(string\|template\)') ||
144+
if (l:line !~ '^[''"]' && syns =~? '\%(string\|template\)') ||
135145
\ (l:line !~ '^\s*[/*]' && syns =~? s:syng_comment)
136146
return -1
137147
endif
@@ -154,16 +164,17 @@ function GetJavascriptIndent()
154164

155165
" the containing paren, bracket, curly. Memoize, last lineNr either has the
156166
" same scope or starts a new one, unless if it closed a scope.
167+
let [s:looksyn,s:free] = [v:lnum - 1,1]
157168
call cursor(v:lnum,1)
158169
if b:js_cache[0] < v:lnum && b:js_cache[0] >= l:lnum &&
159170
\ (b:js_cache[0] > l:lnum || s:Balanced(l:lnum) > 0)
160171
let num = b:js_cache[1]
161172
elseif syns != '' && l:line[0] =~ '\s'
162173
let pattern = syns =~? 'block' ? ['{','}'] : syns =~? 'jsparen' ? ['(',')'] :
163174
\ syns =~? 'jsbracket'? ['\[','\]'] : ['[({[]','[])}]']
164-
let num = s:GetPair(pattern[0],pattern[1],'bW',2000)
175+
let num = s:GetPair(pattern[0],pattern[1],'bW','s:skip_func(s:looksyn)',2000)
165176
else
166-
let num = s:GetPair('[({[]','[])}]','bW',2000)
177+
let num = s:GetPair('[({[]','[])}]','bW','s:skip_func(s:looksyn)',2000)
167178
endif
168179

169180
let b:js_cache = [v:lnum,num,line('.') == v:lnum ? b:js_cache[2] : col('.')]
@@ -175,7 +186,7 @@ function GetJavascriptIndent()
175186
let pline = substitute(substitute(getline(l:lnum),s:expr_case,'\=repeat(" ",strlen(submatch(0)))',''), '\%(:\@<!\/\/.*\)$', '','')
176187
call cursor(b:js_cache[1],b:js_cache[2])
177188
let switch_offset = num <= 0 || !(search(')\_s*\%#','bW') &&
178-
\ s:GetPair('(', ')', 'bW', 100) > 0 && search('\C\<switch\_s*\%#','bW')) ? 0 :
189+
\ s:GetPair('(', ')', 'bW', s:skip_expr, 100) > 0 && search('\C\<switch\_s*\%#','bW')) ? 0 :
179190
\ &cino !~ ':' || !has('float') ? s:sw() :
180191
\ float2nr(str2float(matchstr(&cino,'.*:\zs[-0-9.]*')) * (&cino =~# '.*:[^,]*s' ? s:sw() : 1))
181192

0 commit comments

Comments
 (0)