From 37badb59f2b769f282a7dd694d733ca16655bf36 Mon Sep 17 00:00:00 2001 From: bounceme Date: Fri, 12 Aug 2016 00:30:38 -0700 Subject: [PATCH 1/9] block statement shouldn't be too difficult --- indent/javascript.vim | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/indent/javascript.vim b/indent/javascript.vim index 32857609..521e1e42 100644 --- a/indent/javascript.vim +++ b/indent/javascript.vim @@ -79,6 +79,12 @@ function s:Onescope(lnum,text,add) \ (a:add || (expand("") ==# 'while' ? !s:lookForParens('\\C', '\\C','bW',100) : 1)) endfunction +" function s:isBlock(lnum,text,add) + " TODO: + " https://github.com/sweet-js/sweet.js/wiki/design#give-lookbehind-to-the-reader + " https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/block +" endfunction + " Auxiliary Functions {{{2 " strip line of comment @@ -172,6 +178,7 @@ function GetJavascriptIndent() let pline = s:StripLine(getline(lnum)) let inb = num == 0 ? 1 : (s:Onescope(num, s:StripLine(strpart(getline(num),0,b:js_cache[2] - 1)),1) || \ (line !~ s:line_pre . ',' && pline !~ ',' . s:line_term)) && num < lnum + " TODO: || isBlock() let switch_offset = (!inb || num == 0) || expand("") !=# 'switch' ? 0 : &cino !~ ':' || !has('float') ? s:sw() : \ float2nr(str2float(matchstr(&cino,'.*:\zs[-0-9.]*')) * (&cino =~# '.*:[^,]*s' ? s:sw() : 1)) From 554ff38038e56243a16999542ffc7b19fb709426 Mon Sep 17 00:00:00 2001 From: bounceme Date: Sun, 14 Aug 2016 12:04:36 -0700 Subject: [PATCH 2/9] Update javascript.vim --- indent/javascript.vim | 84 ++++++++++++++++++++++--------------------- 1 file changed, 43 insertions(+), 41 deletions(-) diff --git a/indent/javascript.vim b/indent/javascript.vim index 521e1e42..08092c3d 100644 --- a/indent/javascript.vim +++ b/indent/javascript.vim @@ -5,7 +5,7 @@ " Last Change: August 7, 2016 " Only load this indent file when no other was loaded. -if exists("b:did_indent") +if exists('b:did_indent') finish endif let b:did_indent = 1 @@ -19,7 +19,7 @@ setlocal cinoptions+=j1,J1 let b:undo_indent = 'setlocal indentexpr< indentkeys< cinoptions<' " Only define the function once. -if exists("*GetJavascriptIndent") +if exists('*GetJavascriptIndent') finish endif @@ -38,7 +38,7 @@ else endif let s:line_pre = '^\s*\%(\/\*.\{-}\*\/\s*\)*' -let s:expr_case = s:line_pre . '\%(\%(case\>.*\)\|default\)\s*:' +let s:expr_case = s:line_pre . '\%(\%(case\>.\+\)\|default\)\s*:' " Regex of syntax group names that are or delimit string or are comments. let s:syng_strcom = '\%(s\%(tring\|pecial\)\|comment\|regex\|doc\|template\)' @@ -70,20 +70,23 @@ endif let g:javascript_continuation .= s:line_term function s:Onescope(lnum,text,add) - return a:text =~# '\%(\' . (a:add ? '\|\\)' . s:line_term ? 'no b' : \ ((a:add && a:text =~ s:line_pre . '$' && search('\%' . s:PrevCodeLine(a:lnum - 1) . 'l.)' . s:line_term)) || \ cursor(a:lnum, match(a:text, ')' . s:line_term)) > -1) && - \ s:lookForParens('(', ')', 'cbW', 100) > 0 && search((a:add ? - \ '\%(function\*\|[[:lower:][:upper:]_$][[:digit:][:lower:][:upper:]_$]*\)' : - \ '\<\%(for\%(\s+each\)\=\|if\|let\|w\%(hile\|ith\)\)') . '\_s*\%#\C','bW') && - \ (a:add || (expand("") ==# 'while' ? !s:lookForParens('\\C', '\\C','bW',100) : 1)) + \ s:lookForParens('(', ')', 'cbW', 100) > 0 && search('\l\+\_s*\%#\C','bW') && + \ (a:add || (expand('') ==# 'while' ? !s:lookForParens('\\C', '\\C','nbW',100) : 1)) ? + \ expand('cword') ==# 'each' ? search('\') : '' endfunction -" function s:isBlock(lnum,text,add) - " TODO: +function s:isBlock() " https://github.com/sweet-js/sweet.js/wiki/design#give-lookbehind-to-the-reader - " https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/block -" endfunction + call cursor(b:js_cache[1:]) + if getline(line('.'))[col('.')-1] == '{' + if !search('\C\%(\%([-=~!<*+,.?^%|&\[(]\|\<\%(yield\|delete\|void\|typeof\|throw\|new\|=\@\|\*\@ 0 - if synIDattr(synID(lnum,matchend(getline(lnum), '^\s*[^''"]'),0),'name') !~? s:syng_strcom + let l:lnum = prevnonblank(a:lnum) + while l:lnum > 0 + if synIDattr(synID(l:lnum,matchend(getline(l:lnum), '^\s*[^''"]'),0),'name') !~? s:syng_strcom break endif - let lnum = prevnonblank(lnum - 1) + let l:lnum = prevnonblank(l:lnum - 1) endwhile - return lnum + return l:lnum endfunction " Check if line 'lnum' has a balanced amount of parentheses. @@ -109,18 +112,18 @@ function s:Balanced(lnum) let open_0 = 0 let open_2 = 0 let open_4 = 0 - let line = getline(a:lnum) - let pos = match(line, '[][(){}]', 0) + let l:line = getline(a:lnum) + let pos = match(l:line, '[][(){}]', 0) while pos != -1 if synIDattr(synID(a:lnum,pos + 1,0),'name') !~? s:syng_strcom - let idx = stridx('(){}[]', line[pos]) + let idx = stridx('(){}[]', l:line[pos]) if idx % 2 == 0 let open_{idx} = open_{idx} + 1 else let open_{idx - 1} = open_{idx - 1} - 1 endif endif - let pos = match(line, '[][(){}]', pos + 1) + let pos = match(l:line, '[][(){}]', pos + 1) endwhile return (!open_4 + !open_2 + !open_0) - 2 endfunction @@ -131,23 +134,23 @@ function GetJavascriptIndent() let b:js_cache = [0,0,0] endif " Get the current line. - let line = getline(v:lnum) + let l:line = getline(v:lnum) let syns = synIDattr(synID(v:lnum, 1, 0), 'name') " start with strings,comments,etc.{{{2 - if (line !~ '^[''"`]' && syns =~? 'string\|template') || - \ (line !~ '^\s*[/*]' && syns =~? s:syng_comment) + if (l:line !~ '^[''"`]' && syns =~? 'string\|template') || + \ (l:line !~ '^\s*[/*]' && syns =~? s:syng_comment) return -1 endif - if line !~ '^\%(\/\*\|\s*\/\/\)' && syns =~? s:syng_comment + if l:line !~ '^\%(\/\*\|\s*\/\/\)' && syns =~? s:syng_comment return cindent(v:lnum) endif - let lnum = s:PrevCodeLine(v:lnum - 1) - if lnum == 0 + let l:lnum = s:PrevCodeLine(v:lnum - 1) + if l:lnum == 0 return 0 endif - if (line =~# s:expr_case) + if (l:line =~# s:expr_case) let cpo_switch = &cpo set cpo+=% let ind = cindent(v:lnum) @@ -159,10 +162,10 @@ 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 b:js_cache[0] >= lnum && b:js_cache[0] <= v:lnum && b:js_cache[0] && - \ (b:js_cache[0] > lnum || s:Balanced(lnum) > 0) + if b:js_cache[0] >= l:lnum && b:js_cache[0] <= v:lnum && b:js_cache[0] && + \ (b:js_cache[0] > l:lnum || s:Balanced(l:lnum) > 0) let num = b:js_cache[1] - elseif syns != '' && line[0] =~ '\s' + elseif syns != '' && l:line[0] =~ '\s' let pattern = syns =~? 'block' ? ['{','}'] : syns =~? 'jsparen' ? ['(',')'] : \ syns =~? 'jsbracket'? ['\[','\]'] : ['[({[]','[])}]'] let num = s:lookForParens(pattern[0],pattern[1],'bW',2000) @@ -171,22 +174,21 @@ function GetJavascriptIndent() endif let b:js_cache = [v:lnum,num,line('.') == v:lnum ? b:js_cache[2] : col('.')] - if line =~ s:line_pre . '[])}]' + if l:line =~ s:line_pre . '[])}]' return indent(num) endif - let pline = s:StripLine(getline(lnum)) - let inb = num == 0 ? 1 : (s:Onescope(num, s:StripLine(strpart(getline(num),0,b:js_cache[2] - 1)),1) || - \ (line !~ s:line_pre . ',' && pline !~ ',' . s:line_term)) && num < lnum - " TODO: || isBlock() - let switch_offset = (!inb || num == 0) || expand("") !=# 'switch' ? 0 : &cino !~ ':' || !has('float') ? s:sw() : + let pline = s:StripLine(getline(l:lnum)) + let inb = num == 0 || (s:isBlock() || (l:line !~ s:line_pre . ',' && pline !~ ',' . s:line_term)) && num < l:lnum + let switch_offset = num == 0 || s:Onescope(num, s:StripLine(strpart(getline(num),0,b:js_cache[2] - 1)),1) !=# 'switch' ? 0 : + \ &cino !~ ':' || !has('float') ? s:sw() : \ float2nr(str2float(matchstr(&cino,'.*:\zs[-0-9.]*')) * (&cino =~# '.*:[^,]*s' ? s:sw() : 1)) " most significant, find the indent amount - if (inb && (line =~# g:javascript_opfirst || - \ (pline =~# g:javascript_continuation && pline !~# s:expr_case && (pline !~ ':' . s:line_term || line !~# - \ s:line_pre . '\%(d\%(o\|ebugger\)\|else\|f\%(or\|inally\)\|if\|let\|switch\|t\%(hrow\|ry\)\|w\%(hile\|ith\)\)\>')))) || - \ (num < lnum && s:Onescope(lnum,pline,0) && line !~ s:line_pre . '{') + if (inb && (l:line =~# g:javascript_opfirst || + \ (pline =~# g:javascript_continuation && pline !~# s:expr_case))) || + \ (num < l:lnum && s:Onescope(l:lnum,pline,0) =~# '\%(for\%(\seach\)\=\|if\|let\|no\sb\|w\%(hile\|ith\)\)' && + \ l:line !~ s:line_pre . '{') return (num > 0 ? indent(num) : -s:sw()) + (s:sw() * 2) + switch_offset elseif num > 0 return indent(num) + s:sw() + switch_offset From 6c7124c501dc58c016ca03b9f60ca637f391ca57 Mon Sep 17 00:00:00 2001 From: bounceme Date: Sun, 14 Aug 2016 13:08:19 -0700 Subject: [PATCH 3/9] Update javascript.vim --- indent/javascript.vim | 21 +++++++++------------ 1 file changed, 9 insertions(+), 12 deletions(-) diff --git a/indent/javascript.vim b/indent/javascript.vim index 08092c3d..d7c07044 100644 --- a/indent/javascript.vim +++ b/indent/javascript.vim @@ -60,12 +60,12 @@ let s:line_term = '\%(\s*\%(\/\*.\{-}\*\/\s*\)\=\)\@>$' " configurable regexes that define continuation lines, not including (, {, or [. if !exists('g:javascript_opfirst') - let g:javascript_opfirst = '\%([<>,:?^%]\|\([-/.+]\)\%(\1\|\*\|\/\)\@!\|\*\/\@!\|=>\@!\||\|&\|in\%(stanceof\)\=\>\)' + let g:javascript_opfirst = '\%([<>,:?^%|&]\|\([-/.+]\)\%(\1\|\*\|\/\)\@!\|\*\/\@!\|=>\@!\|in\%(stanceof\)\=\>\)' endif let g:javascript_opfirst = s:line_pre . g:javascript_opfirst if !exists('g:javascript_continuation') - let g:javascript_continuation = '\%([<*,.?:^%]\|+\@\|\*\@\|\*\@\)' . s:line_term ? 'no b' : \ ((a:add && a:text =~ s:line_pre . '$' && search('\%' . s:PrevCodeLine(a:lnum - 1) . 'l.)' . s:line_term)) || \ cursor(a:lnum, match(a:text, ')' . s:line_term)) > -1) && - \ s:lookForParens('(', ')', 'cbW', 100) > 0 && search('\l\+\_s*\%#\C','bW') && - \ (a:add || (expand('') ==# 'while' ? !s:lookForParens('\\C', '\\C','nbW',100) : 1)) ? - \ expand('cword') ==# 'each' ? search('\') : '' + \ s:lookForParens('(', ')', 'cbW', 100) > 0 && search('\C\l\+\_s*\%#','bW') && + \ (a:add || (expand('') ==# 'while' ? !s:lookForParens('\C\', '\C\','nbW',100) : 1)) ? + \ expand('cword') ==# 'each' ? search('\C\') : '' endfunction function s:isBlock() " https://github.com/sweet-js/sweet.js/wiki/design#give-lookbehind-to-the-reader - call cursor(b:js_cache[1:]) - if getline(line('.'))[col('.')-1] == '{' - if !search('\C\%(\%([-=~!<*+,.?^%|&\[(]\|\<\%(yield\|delete\|void\|typeof\|throw\|new\|=\@\|\*\@ -1 && getline(line('.'))[col('.')-1] == '{' && !search( + \ '\C\%(\%([-=~!<*+,.?^%|&\[(]\|\<\%(yield\|delete\|void\|t\%(ypeof\|hrow\)\|new\|=\@\|\*\@ Date: Sun, 14 Aug 2016 17:01:55 -0700 Subject: [PATCH 4/9] Update javascript.vim --- indent/javascript.vim | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/indent/javascript.vim b/indent/javascript.vim index d7c07044..ff00862f 100644 --- a/indent/javascript.vim +++ b/indent/javascript.vim @@ -74,8 +74,8 @@ function s:Onescope(lnum,text,add) \ ((a:add && a:text =~ s:line_pre . '$' && search('\%' . s:PrevCodeLine(a:lnum - 1) . 'l.)' . s:line_term)) || \ cursor(a:lnum, match(a:text, ')' . s:line_term)) > -1) && \ s:lookForParens('(', ')', 'cbW', 100) > 0 && search('\C\l\+\_s*\%#','bW') && - \ (a:add || (expand('') ==# 'while' ? !s:lookForParens('\C\', '\C\','nbW',100) : 1)) ? - \ expand('cword') ==# 'each' ? search('\C\') : '' + \ (a:add || ((expand('') !=# 'while' || !s:lookForParens('\C\', '\C\','nbW',100)) && + \ expand('cword') !=# 'each' || search('\C\') : '' endfunction function s:isBlock() @@ -184,7 +184,7 @@ function GetJavascriptIndent() " most significant, find the indent amount if (inb && (l:line =~# g:javascript_opfirst || \ (pline =~# g:javascript_continuation && pline !~# s:expr_case))) || - \ (num < l:lnum && s:Onescope(l:lnum,pline,0) =~# '\%(for\%(\seach\)\=\|if\|let\|no\sb\|w\%(hile\|ith\)\)' && + \ (num < l:lnum && s:Onescope(l:lnum,pline,0) =~# '\%(for\|each\|if\|let\|no\sb\|w\%(hile\|ith\)\)' && \ l:line !~ s:line_pre . '{') return (num > 0 ? indent(num) : -s:sw()) + (s:sw() * 2) + switch_offset elseif num > 0 From 94fd61bf7750f929e857cb2eb4cade4d4ea76235 Mon Sep 17 00:00:00 2001 From: bounceme Date: Sun, 14 Aug 2016 18:57:38 -0700 Subject: [PATCH 5/9] Update javascript.vim --- indent/javascript.vim | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/indent/javascript.vim b/indent/javascript.vim index ff00862f..0ad027c5 100644 --- a/indent/javascript.vim +++ b/indent/javascript.vim @@ -80,7 +80,7 @@ endfunction function s:isBlock() " https://github.com/sweet-js/sweet.js/wiki/design#give-lookbehind-to-the-reader - return cursor(b:js_cache[1:]) > -1 && getline(line('.'))[col('.')-1] == '{' && !search( + return getline(line('.'))[col('.')-1] == '{' && !search( \ '\C\%(\%([-=~!<*+,.?^%|&\[(]\|\<\%(yield\|delete\|void\|t\%(ypeof\|hrow\)\|new\|=\@\|\*\@ Date: Sun, 14 Aug 2016 19:42:29 -0700 Subject: [PATCH 6/9] destructuring objects --- indent/javascript.vim | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/indent/javascript.vim b/indent/javascript.vim index 0ad027c5..b8f97ef7 100644 --- a/indent/javascript.vim +++ b/indent/javascript.vim @@ -81,7 +81,7 @@ endfunction function s:isBlock() " https://github.com/sweet-js/sweet.js/wiki/design#give-lookbehind-to-the-reader return getline(line('.'))[col('.')-1] == '{' && !search( - \ '\C\%(\%([-=~!<*+,.?^%|&\[(]\|\<\%(yield\|delete\|void\|t\%(ypeof\|hrow\)\|new\|=\@\|\*\@\|\*\@ Date: Sun, 14 Aug 2016 21:23:24 -0700 Subject: [PATCH 7/9] perf regained `8.679 seconds` how? I don't see why direction should matter if the patterns explicitly match against the cursor position. The `W` flag also helped --- indent/javascript.vim | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/indent/javascript.vim b/indent/javascript.vim index b8f97ef7..7bd06b64 100644 --- a/indent/javascript.vim +++ b/indent/javascript.vim @@ -75,14 +75,14 @@ function s:Onescope(lnum,text,add) \ cursor(a:lnum, match(a:text, ')' . s:line_term)) > -1) && \ s:lookForParens('(', ')', 'cbW', 100) > 0 && search('\C\l\+\_s*\%#','bW') && \ (a:add || ((expand('') !=# 'while' || !s:lookForParens('\C\', '\C\','nbW',100)) && - \ expand('cword') !=# 'each' || search('\C\') : '' + \ expand('cword') !=# 'each' || search('\C\') : '' endfunction function s:isBlock() " https://github.com/sweet-js/sweet.js/wiki/design#give-lookbehind-to-the-reader return getline(line('.'))[col('.')-1] == '{' && !search( - \ '\C\%(\%([-=~!<*+,.?^%|&\[(]\|=\@\|\*\@\|\*\@ Date: Sun, 14 Aug 2016 21:33:58 -0700 Subject: [PATCH 8/9] wouldn't surprise me if this improved performance --- indent/javascript.vim | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/indent/javascript.vim b/indent/javascript.vim index 7bd06b64..67f25a61 100644 --- a/indent/javascript.vim +++ b/indent/javascript.vim @@ -78,8 +78,8 @@ function s:Onescope(lnum,text,add) \ expand('cword') !=# 'each' || search('\C\') : '' endfunction +" https://github.com/sweet-js/sweet.js/wiki/design#give-lookbehind-to-the-reader function s:isBlock() - " https://github.com/sweet-js/sweet.js/wiki/design#give-lookbehind-to-the-reader return getline(line('.'))[col('.')-1] == '{' && !search( \ '\C\%(\%([-=~!<*+,.?^%|&\[(]\|=\@\|\*\@ Date: Mon, 15 Aug 2016 18:44:03 -0700 Subject: [PATCH 9/9] 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 265bea46..38acae0d 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 7, 2016 +" Last Change: August 15, 2016 " Only load this indent file when no other was loaded. if exists('b:did_indent')