diff --git a/indent/javascript.vim b/indent/javascript.vim index 67006e35..fb500d7d 100644 --- a/indent/javascript.vim +++ b/indent/javascript.vim @@ -58,12 +58,12 @@ function s:skip_func(lnum) endfunction if has('reltime') - function s:GetPair(start,end,flags,skip,time) - return searchpair(a:start,'',a:end,a:flags,a:skip,max([prevnonblank(v:lnum) - 2000,0]),a:time) + function s:GetPair(start,end,flags,skip,time,...) + return searchpair(a:start,'',a:end,a:flags,a:skip,max([prevnonblank(v:lnum) - 2000,0] + a:000),a:time) endfunction else function s:GetPair(start,end,flags,...) - return searchpair(a:start,'',a:end,a:flags,0,max([prevnonblank(v:lnum) - 2000,0])) + return searchpair(a:start,'',a:end,a:flags,"line('.') < prevnonblank(v:lnum) - 2000 ? dummy : 0") endfunction endif @@ -88,14 +88,14 @@ function s:OneScope(lnum,text) endfunction function s:iscontOne(i,num,cont) - let [l:i, l:cont, l:num] = [a:i, a:cont, a:num ? a:num : 1] - let pind = a:num ? indent(l:num) : -s:sw() - let ind = indent(l:i) + (!l:cont ? s:sw() : 0) + let [l:i, l:cont, l:num] = [a:i, a:cont, a:num + !a:num] + let pind = a:num ? indent(l:num) : -s:W + let ind = indent(l:i) + (!l:cont * s:W) let bL = 0 - while l:i >= l:num && (!l:cont || ind > pind + s:sw()) + while l:i >= l:num && (!l:cont || ind > pind + s:W) if indent(l:i) < ind " first line always true for !cont, false for !!cont if s:OneScope(l:i,substitute(getline(l:i),':\@') ==# 'while' && s:GetPair(s:line_pre . '\C\\%>'.(l:num-1).'l','\C\','bW',s:skip_expr,100) > 0 + if expand('') ==# 'while' && s:GetPair(s:line_pre . '\C\','\C\','bW',s:skip_expr,100,l:num) > 0 return 0 endif let bL += 1 @@ -107,7 +107,7 @@ function s:iscontOne(i,num,cont) endif let l:i = s:PrevCodeLine(l:i - 1) endwhile - return bL * s:sw() + return bL * s:W endfunction " https://github.com/sweet-js/sweet.js/wiki/design#give-lookbehind-to-the-reader @@ -136,7 +136,7 @@ function s:Balanced(lnum) while pos != -1 if synIDattr(synID(a:lnum,pos + 1,0),'name') !~? s:syng_strcom let idx = stridx('(){}[]', l:line[pos]) - if idx % 2 == 0 + if !(idx % 2) let open_{idx} += 1 else let open_{idx - 1} -= 1 @@ -147,7 +147,7 @@ function s:Balanced(lnum) endif let pos = match(l:line, '[][(){}]', pos + 1) endwhile - return (!open_4 + !open_2 + !open_0) - 2 + return !(open_4 || open_2 || open_0) endfunction function GetJavascriptIndent() @@ -181,46 +181,51 @@ function GetJavascriptIndent() " the containing paren, bracket, curly. Memoize, last lineNr either has the " same scope or starts a new one, unless if it closed a scope. - let [s:looksyn,s:free] = [v:lnum - 1,1] call cursor(v:lnum,1) - if b:js_cache[0] < v:lnum && b:js_cache[0] >= l:lnum && - \ (b:js_cache[0] > l:lnum || s:Balanced(l:lnum) > 0) - let num = b:js_cache[1] - elseif syns != '' && l:line[0] =~ '\s' - let pattern = syns =~? 'block' ? ['{','}'] : syns =~? 'jsparen' ? ['(',')'] : - \ syns =~? 'jsbracket'? ['\[','\]'] : ['[({[]','[])}]'] - let num = s:GetPair(pattern[0],pattern[1],'bW','s:skip_func(s:looksyn)',2000) + if getline(l:lnum) !~ '^\S' + let [s:looksyn,s:free] = [v:lnum - 1,1] + if b:js_cache[0] < v:lnum && b:js_cache[0] >= l:lnum && + \ (b:js_cache[0] > l:lnum || s:Balanced(l:lnum)) + let num = b:js_cache[1] + elseif syns != '' && l:line[0] =~ '\s' + let pattern = syns =~? 'block' ? ['{','}'] : syns =~? 'jsparen' ? ['(',')'] : + \ syns =~? 'jsbracket'? ['\[','\]'] : ['[({[]','[])}]'] + let num = s:GetPair(pattern[0],pattern[1],'bW','s:skip_func(s:looksyn)',2000) + else + let num = s:GetPair('[({[]','[])}]','bW','s:skip_func(s:looksyn)',2000) + endif else - let num = s:GetPair('[({[]','[])}]','bW','s:skip_func(s:looksyn)',2000) + let num = s:GetPair('[({[]','[])}]','bW',s:skip_expr,200,l:lnum) endif - let num = num > 0 ? num : 0 + let num = (num > 0) * num let b:js_cache = [v:lnum,num,line('.') == v:lnum ? b:js_cache[2] : col('.')] let l:line = substitute(l:line,s:line_pre,'','') if l:line =~ '^[])}]' - return indent(num) + return !!num * indent(num) endif call cursor(v:lnum,1) - if l:line =~# '^while\>' && s:GetPair(s:line_pre . '\C\\%>'.(num-!!num).'l','\C\','bW',s:skip_expr,100) > 0 + if l:line =~# '^while\>' && s:GetPair(s:line_pre . '\C\','\C\','bW',s:skip_expr,100,num) > 0 return indent(line('.')) endif + let s:W = s:sw() let pline = substitute(substitute(getline(l:lnum),s:expr_case,'\=repeat(" ",strlen(submatch(0)))',''), ':\@ 0 && search('\C\