diff --git a/indent/javascript.vim b/indent/javascript.vim index f591a0f1..e30e272a 100644 --- a/indent/javascript.vim +++ b/indent/javascript.vim @@ -2,7 +2,7 @@ " Language: Javascript " Maintainer: vim-javascript community " URL: https://github.com/pangloss/vim-javascript -" Last Change: August 2, 2016 +" Last Change: August 6, 2016 " Only load this indent file when no other was loaded. if exists("b:did_indent") @@ -88,7 +88,7 @@ endfunction " Check if the character at lnum:col is inside a string, comment, or is ascii. function s:IsSyn(lnum, col, reg) - return synIDattr(synID(a:lnum, a:col, 1), 'name') =~? (a:reg != '' ? a:reg : s:syng_strcom) + return synIDattr(synID(a:lnum, a:col, 0), 'name') =~? (a:reg != '' ? a:reg : s:syng_strcom) endfunction " Find line above 'lnum' that isn't empty, in a comment, or in a string. @@ -104,7 +104,7 @@ function s:PrevCodeLine(lnum) endfunction " Check if line 'lnum' has more opening brackets than closing ones. -function s:LineHasOpeningBrackets(lnum) +function s:Balanced(lnum) let open_0 = 0 let open_2 = 0 let open_4 = 0 @@ -121,8 +121,7 @@ function s:LineHasOpeningBrackets(lnum) endif let pos = match(line, '[][(){}]', pos + 1) endwhile - return (open_0 > 0 ? 1 : (open_0 == 0 ? 0 : 2)) . (open_2 > 0 ? 1 : (open_2 == 0 ? 0 : 2)) . - \ (open_4 > 0 ? 1 : (open_4 == 0 ? 0 : 2)) + return (!open_4 + !open_2 + !open_0) - 2 endfunction " }}} @@ -160,19 +159,16 @@ 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. call cursor(v:lnum,1) - let pcounts = [0] if b:js_cache[0] >= lnum && b:js_cache[0] <= v:lnum && b:js_cache[0] && - \ (b:js_cache[0] > lnum || map(pcounts,'s:LineHasOpeningBrackets(lnum)')[0] !~ '2') - let num = pcounts[0] =~ '1' ? s:lookForParens('[({[]','[])}]','bW',2000) : b:js_cache[1] - else + \ (b:js_cache[0] > lnum || s:Balanced(lnum) > 0) + let num = b:js_cache[1] + elseif line[0] =~ '\s' let syns = synIDattr(synID(v:lnum, 1, 1), 'name') - if line[0] =~ '\s' && syns != '' - let pattern = syns =~? 'funcblock' ? ['{','}'] : syns =~? 'jsparen' ? ['(',')'] : syns =~? 'jsbracket'? ['\[','\]'] : - \ ['[({[]','[])}]'] - let num = s:lookForParens(pattern[0],pattern[1],'bW',2000) - else - let num = s:lookForParens('[({[]','[])}]','bW',2000) - endif + let pattern = syns =~? 'block' ? ['{','}'] : syns =~? 'jsparen' ? ['(',')'] : + \ syns =~? 'jsbracket'? ['\[','\]'] : ['[({[]','[])}]'] + let num = s:lookForParens(pattern[0],pattern[1],'bW',2000) + else + let num = s:lookForParens('[({[]','[])}]','bW',2000) endif let b:js_cache = [v:lnum,num,line('.') == v:lnum ? b:js_cache[2] : col('.')]