From 427a7b2fd3b4fc54b7629ee083653035516d357c Mon Sep 17 00:00:00 2001 From: Chris Paul Date: Wed, 21 Sep 2016 21:15:57 -0700 Subject: [PATCH 01/12] optimize top scope indentation speed makes a huge difference when not in a 'scope'. avoids checking the whole file --- indent/javascript.vim | 24 ++++++++++++++---------- 1 file changed, 14 insertions(+), 10 deletions(-) diff --git a/indent/javascript.vim b/indent/javascript.vim index 67006e35..14d157f0 100644 --- a/indent/javascript.vim +++ b/indent/javascript.vim @@ -181,17 +181,21 @@ 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] + 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) + 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 = searchpair('[({[]','','[])}]','bW',s:skip_expr,l:lnum) endif let num = num > 0 ? num : 0 From 4ec37d59c2ab4c941b62e2ac4167e147e16051cd Mon Sep 17 00:00:00 2001 From: Chris Paul Date: Wed, 21 Sep 2016 22:50:28 -0700 Subject: [PATCH 02/12] Update javascript.vim --- indent/javascript.vim | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/indent/javascript.vim b/indent/javascript.vim index 14d157f0..45cf77ef 100644 --- a/indent/javascript.vim +++ b/indent/javascript.vim @@ -203,7 +203,7 @@ function GetJavascriptIndent() let l:line = substitute(l:line,s:line_pre,'','') if l:line =~ '^[])}]' - return indent(num) + return indent(num + !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 From 59658363074cdbf4b62164820b55caab48bfa241 Mon Sep 17 00:00:00 2001 From: Chris Paul Date: Thu, 22 Sep 2016 00:20:25 -0700 Subject: [PATCH 03/12] Update javascript.vim --- indent/javascript.vim | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/indent/javascript.vim b/indent/javascript.vim index 45cf77ef..bb397dfc 100644 --- a/indent/javascript.vim +++ b/indent/javascript.vim @@ -203,7 +203,7 @@ function GetJavascriptIndent() let l:line = substitute(l:line,s:line_pre,'','') if l:line =~ '^[])}]' - return indent(num + !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 From ff9eb9a5d168cded11852bfbfd465558acc4876e Mon Sep 17 00:00:00 2001 From: Chris Paul Date: Thu, 22 Sep 2016 01:46:56 -0700 Subject: [PATCH 04/12] cut ternary to 0/1 --- indent/javascript.vim | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/indent/javascript.vim b/indent/javascript.vim index bb397dfc..f2a51c21 100644 --- a/indent/javascript.vim +++ b/indent/javascript.vim @@ -88,9 +88,9 @@ 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 [l:i, l:cont, l:num] = [a:i, a:cont, a:num + !a:num] let pind = a:num ? indent(l:num) : -s:sw() - let ind = indent(l:i) + (!l:cont ? s:sw() : 0) + let ind = indent(l:i) + (!l:cont * s:sw()) let bL = 0 while l:i >= l:num && (!l:cont || ind > pind + s:sw()) if indent(l:i) < ind " first line always true for !cont, false for !!cont From 6a9ed85aaf8363ba2a79ec73eb2e0ad738254a48 Mon Sep 17 00:00:00 2001 From: Chris Paul Date: Thu, 22 Sep 2016 02:13:13 -0700 Subject: [PATCH 05/12] Update javascript.vim --- indent/javascript.vim | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/indent/javascript.vim b/indent/javascript.vim index f2a51c21..1f316880 100644 --- a/indent/javascript.vim +++ b/indent/javascript.vim @@ -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() @@ -185,7 +185,7 @@ function GetJavascriptIndent() 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) + \ (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' ? ['(',')'] : @@ -198,7 +198,7 @@ function GetJavascriptIndent() let num = searchpair('[({[]','','[])}]','bW',s:skip_expr,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,'','') From a7f4208d20b507e363b602274172218b4d6ef7b1 Mon Sep 17 00:00:00 2001 From: Chris Paul Date: Thu, 22 Sep 2016 05:17:20 -0700 Subject: [PATCH 06/12] Update javascript.vim --- indent/javascript.vim | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/indent/javascript.vim b/indent/javascript.vim index 1f316880..16eee677 100644 --- a/indent/javascript.vim +++ b/indent/javascript.vim @@ -220,7 +220,7 @@ function GetJavascriptIndent() " most significant, find the indent amount let isOp = l:line =~# g:javascript_opfirst || pline =~# g:javascript_continuation let bL = s:iscontOne(l:lnum,num,isOp) - let bL = bL ? bL - (l:line =~ '^{') * s:sw() : bL + let bL -= (bL && l:line =~ '^{') * s:sw() if isOp && (!num || cursor(b:js_cache[1],b:js_cache[2]) || s:IsBlock()) return (num ? indent(num) : -s:sw()) + (s:sw() * 2) + switch_offset + bL elseif num From 1c24bc899f92bd22a31764bc492935d9e97e49da Mon Sep 17 00:00:00 2001 From: Chris Paul Date: Thu, 22 Sep 2016 13:26:21 -0700 Subject: [PATCH 07/12] shift width --- indent/javascript.vim | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/indent/javascript.vim b/indent/javascript.vim index 16eee677..9974aa6d 100644 --- a/indent/javascript.vim +++ b/indent/javascript.vim @@ -89,10 +89,10 @@ endfunction function s:iscontOne(i,num,cont) let [l:i, l:cont, l:num] = [a:i, a:cont, a:num + !a:num] - let pind = a:num ? indent(l:num) : -s:sw() - let ind = indent(l:i) + (!l:cont * s:sw()) + 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 @@ -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 @@ -210,21 +210,22 @@ function GetJavascriptIndent() 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\ Date: Thu, 22 Sep 2016 16:01:42 -0700 Subject: [PATCH 08/12] improve helper --- indent/javascript.vim | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/indent/javascript.vim b/indent/javascript.vim index 9974aa6d..5c6da21e 100644 --- a/indent/javascript.vim +++ b/indent/javascript.vim @@ -58,8 +58,8 @@ 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,...) @@ -95,7 +95,7 @@ function s:iscontOne(i,num,cont) 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 @@ -195,7 +195,7 @@ function GetJavascriptIndent() let num = s:GetPair('[({[]','[])}]','bW','s:skip_func(s:looksyn)',2000) endif else - let num = searchpair('[({[]','','[])}]','bW',s:skip_expr,l:lnum) + let num = s:GetPair('[({[]','[])}]','bW',s:skip_expr,200,l:lnum) endif let num = (num > 0) * num @@ -206,7 +206,7 @@ function GetJavascriptIndent() 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 From bdd41f8fef26185e01b573d4aa310c19be23db16 Mon Sep 17 00:00:00 2001 From: Chris Paul Date: Thu, 22 Sep 2016 20:23:08 -0700 Subject: [PATCH 09/12] silly bug --- indent/javascript.vim | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/indent/javascript.vim b/indent/javascript.vim index 5c6da21e..8c6cb94c 100644 --- a/indent/javascript.vim +++ b/indent/javascript.vim @@ -181,9 +181,9 @@ 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) if getline(l:lnum) !~ '^\S' 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)) let num = b:js_cache[1] From 319a91d84ee02b0f5de3c9495664e13653b94076 Mon Sep 17 00:00:00 2001 From: Chris Paul Date: Thu, 22 Sep 2016 21:06:38 -0700 Subject: [PATCH 10/12] Update javascript.vim --- indent/javascript.vim | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/indent/javascript.vim b/indent/javascript.vim index 8c6cb94c..7212f1a8 100644 --- a/indent/javascript.vim +++ b/indent/javascript.vim @@ -63,7 +63,7 @@ if has('reltime') 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,0,max([prevnonblank(v:lnum) - 2000,0,a:0 == 3 ? a:3 : 0])) endfunction endif From 6444c35918004c5afd3bb93eb75f7b144d8450c2 Mon Sep 17 00:00:00 2001 From: Chris Paul Date: Thu, 22 Sep 2016 21:13:59 -0700 Subject: [PATCH 11/12] Update javascript.vim --- indent/javascript.vim | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/indent/javascript.vim b/indent/javascript.vim index 7212f1a8..f63aab05 100644 --- a/indent/javascript.vim +++ b/indent/javascript.vim @@ -63,7 +63,7 @@ if has('reltime') endfunction else function s:GetPair(start,end,flags,...) - return searchpair(a:start,'',a:end,a:flags,0,max([prevnonblank(v:lnum) - 2000,0,a:0 == 3 ? a:3 : 0])) + return searchpair(a:start,'',a:end,a:flags) endfunction endif From 9b77b4403b0fb8b14993cdba41e6735c6d2d36df Mon Sep 17 00:00:00 2001 From: Chris Paul Date: Thu, 22 Sep 2016 21:23:14 -0700 Subject: [PATCH 12/12] Update javascript.vim --- indent/javascript.vim | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/indent/javascript.vim b/indent/javascript.vim index f63aab05..fb500d7d 100644 --- a/indent/javascript.vim +++ b/indent/javascript.vim @@ -63,7 +63,7 @@ if has('reltime') endfunction else function s:GetPair(start,end,flags,...) - return searchpair(a:start,'',a:end,a:flags) + return searchpair(a:start,'',a:end,a:flags,"line('.') < prevnonblank(v:lnum) - 2000 ? dummy : 0") endfunction endif