Skip to content

Commit 920873e

Browse files
authored
simplify balance func (#672)
1 parent c516241 commit 920873e

File tree

1 file changed

+12
-14
lines changed

1 file changed

+12
-14
lines changed

indent/javascript.vim

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -174,24 +174,23 @@ endfunction
174174

175175
" Check if line 'lnum' has a balanced amount of parentheses.
176176
function s:Balanced(lnum)
177-
let [open_0,open_2,open_4] = [0,0,0]
177+
let l:open = 0
178178
let l:line = getline(a:lnum)
179179
let pos = match(l:line, '[][(){}]', 0)
180180
while pos != -1
181181
if synIDattr(synID(a:lnum,pos + 1,0),'name') !~? s:syng_strcom
182-
let idx = stridx('(){}[]', l:line[pos])
183-
if !(idx % 2)
184-
let open_{idx} += 1
182+
if l:line[pos] =~ '[{([]'
183+
let l:open += 1
185184
else
186-
let open_{idx - 1} -= 1
187-
if open_{idx - 1} < 0
185+
let l:open -= 1
186+
if l:open < 0
188187
return 0
189188
endif
190189
endif
191190
endif
192191
let pos = match(l:line, '[][(){}]', pos + 1)
193192
endwhile
194-
return !(open_4 || open_2 || open_0)
193+
return !l:open
195194
endfunction
196195

197196
function GetJavascriptIndent()
@@ -230,18 +229,17 @@ function GetJavascriptIndent()
230229
return ind
231230
endif
232231

233-
" the containing paren, bracket, curly. Memoize, last lineNr either has the
234-
" same scope or starts a new one, unless if it closed a scope.
232+
" the containing paren, bracket, curly. Many hacks for performance
235233
call cursor(v:lnum,1)
236234
let fclose = l:line =~ '^[])}]'
237235
if indent(l:lnum)
238236
let [s:looksyn,s:free] = [v:lnum - 1,1]
239237
if b:js_cache[0] >= l:lnum && b:js_cache[0] < v:lnum &&
240-
\ (b:js_cache[0] > l:lnum || s:Balanced(l:lnum))
238+
\ (b:js_cache[0] > l:lnum || !fclose && s:Balanced(l:lnum))
241239
let num = b:js_cache[1]
242240
elseif fclose
243241
let id = stridx('])}',l:line[0])
244-
let num = s:GetPair(escape('[({'[id],'['), escape('])}'[id],']'),'bW','s:skip_func(s:looksyn)',2000)
242+
return indent(s:GetPair(escape('[({'[id],'['), '])}'[id],'bW','s:skip_func(s:looksyn)',2000))
245243
elseif indent(v:lnum) && syns =~? 'block'
246244
let num = s:GetPair('{','}','bW','s:skip_func(s:looksyn)',2000)
247245
else
@@ -251,10 +249,10 @@ function GetJavascriptIndent()
251249
let num = s:GetPair('[({[]','[])}]','bW',s:skip_expr,200,l:lnum)
252250
endif
253251

254-
let num = max([num,0])
255252
if fclose
256-
return !!num * indent(num)
253+
return indent(num)
257254
endif
255+
let num = max([num,0])
258256
let b:js_cache = [v:lnum,num,line('.') == v:lnum && num ? b:js_cache[2] : col('.')]
259257

260258
call cursor(v:lnum,1)
@@ -281,11 +279,11 @@ function GetJavascriptIndent()
281279
return indent(num) + s:W + switch_offset + bL
282280
endif
283281
return bL
282+
284283
finally
285284
let &magic = save_magic
286285
endtry
287286
endfunction
288287

289-
290288
let &cpo = s:cpo_save
291289
unlet s:cpo_save

0 commit comments

Comments
 (0)