From 61f11499e76ee8bac55df90d5a9bb5943d9a6a69 Mon Sep 17 00:00:00 2001 From: Chris Paul Date: Thu, 13 Oct 2016 21:15:49 -0700 Subject: [PATCH 1/6] do while made accurate can tell the difference between do expressions --- indent/javascript.vim | 58 ++++++++++++++++++++----------------------- 1 file changed, 27 insertions(+), 31 deletions(-) diff --git a/indent/javascript.vim b/indent/javascript.vim index 9fb4f8e6..cbdfd313 100644 --- a/indent/javascript.vim +++ b/indent/javascript.vim @@ -37,8 +37,6 @@ else endfunction endif -let s:line_pre = '^\s*\%(\%(\%(\/\*.\{-}\)\=\*\+\/\s*\)\=\)\@>' - let s:expr_case = '\<\%(\%(case\>\s*\S.\{-}\)\|default\)\s*:\C' " Regex of syntax group names that are or delimit string or are comments. let s:syng_strcom = '\%(s\%(tring\|pecial\)\|comment\|regex\|doc\|template\)' @@ -111,7 +109,7 @@ function s:iscontOne(i,num,cont) if indent(l:i) < ind " first line always true for !a:cont, false for !!a:cont if s:OneScope(l:i,s:Trimline(l:i)) if expand('') ==# 'while' && - \ s:GetPair(s:line_pre . '\C\','\C\','bW',s:skip_expr,100,l:num + !!a:num) > 0 + \ s:GetPair('\C\','\C\','bW',s:skip_expr . '|| !s:IsBlock()',100,l:num + !!a:num) > 0 return 0 endif let bL += 1 @@ -128,38 +126,36 @@ endfunction " https://github.com/sweet-js/sweet.js/wiki/design#give-lookbehind-to-the-reader function s:IsBlock() - if getline(line('.'))[col('.')-1] == '{' - let l:ln = line('.') - if search('\S','bW') + let l:ln = line('.') + if search('\S','bW') + let char = getline(line('.'))[col('.')-1] + let pchar = getline(line('.'))[col('.')-2] + let syn = synIDattr(synID(line('.'),col('.')-1,0),'name') + if pchar . char == '*/' && syn =~? 'comment' + if !search('\/\*','bW') || !search('\S','bW') + return 1 + endif let char = getline(line('.'))[col('.')-1] let pchar = getline(line('.'))[col('.')-2] let syn = synIDattr(synID(line('.'),col('.')-1,0),'name') - if pchar . char == '*/' && syn =~? 'comment' - if !search('\/\*','bW') || !search('\S','bW') - return 1 - endif - let char = getline(line('.'))[col('.')-1] - let pchar = getline(line('.'))[col('.')-2] - let syn = synIDattr(synID(line('.'),col('.')-1,0),'name') - endif - if syn =~? '\%(xml\|jsx\)' - return char != '{' - elseif char =~# '\l' - if line('.') == l:ln && expand('') ==# 'return' - return 0 - endif - return expand('') !~# - \ '^\%(const\|let\|import\|export\|yield\|de\%(fault\|lete\)\|v\%(ar\|oid\)\|t\%(ypeof\|hrow\)\|new\|in\%(stanceof\)\=\)$' - elseif char == '>' - return pchar == '=' || syn =~? '^jsflow' - elseif char == ':' - return strpart(getline(line('.')),0,col('.')) =~# s:expr_case . '$' - else - return char !~# '[-=~!<*+,/?^%|&([]' + endif + if syn =~? '\%(xml\|jsx\)' + return char != '{' + elseif char =~# '\l' + if line('.') == l:ln && expand('') ==# 'return' + return 0 endif + return expand('') !~# + \ '^\%(const\|let\|import\|export\|yield\|de\%(fault\|lete\)\|v\%(ar\|oid\)\|t\%(ypeof\|hrow\)\|new\|in\%(stanceof\)\=\)$' + elseif char == '>' + return pchar == '=' || syn =~? '^jsflow' + elseif char == ':' + return strpart(getline(line('.')),0,col('.')) =~# s:expr_case . '$' else - return 1 + return char !~# '[-=~!<*+,/?^%|&([]' endif + else + return 1 endif endfunction @@ -261,7 +257,7 @@ function GetJavascriptIndent() let b:js_cache = [v:lnum,num,line('.') == v:lnum ? b:js_cache[2] : col('.')] call cursor(v:lnum,1) - if l:line =~# '^while\>' && s:GetPair(s:line_pre . '\C\','\C\','bW',s:skip_expr,100,num + 1) > 0 + if l:line =~# '^while\>' && s:GetPair('\C\','\C\','bW',s:skip_expr . '|| !s:IsBlock()',100,num + 1) > 0 return indent(line('.')) endif @@ -277,7 +273,7 @@ function GetJavascriptIndent() let isOp = l:line =~# g:javascript_opfirst || pline !~# s:expr_case . '$' && pline =~# g:javascript_continuation let bL = s:iscontOne(l:lnum,num,isOp) let bL -= (bL && l:line =~ '^{') * s:W - if isOp && (!num || cursor(b:js_cache[1],b:js_cache[2]) || s:IsBlock()) + if isOp && (!num || cursor(b:js_cache[1],b:js_cache[2]) || (getline(line('.'))[col('.')-1] == '{' && s:IsBlock())) return (num ? indent(num) : -s:W) + (s:W * 2) + switch_offset + bL elseif num return indent(num) + s:W + switch_offset + bL From 9874264cd58ac76677fba7417c8f90aea42ba1f2 Mon Sep 17 00:00:00 2001 From: Chris Paul Date: Fri, 14 Oct 2016 00:43:00 -0700 Subject: [PATCH 2/6] cleanup --- indent/javascript.vim | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/indent/javascript.vim b/indent/javascript.vim index cbdfd313..00f5307a 100644 --- a/indent/javascript.vim +++ b/indent/javascript.vim @@ -145,8 +145,8 @@ function s:IsBlock() if line('.') == l:ln && expand('') ==# 'return' return 0 endif - return expand('') !~# - \ '^\%(const\|let\|import\|export\|yield\|de\%(fault\|lete\)\|v\%(ar\|oid\)\|t\%(ypeof\|hrow\)\|new\|in\%(stanceof\)\=\)$' + return index(split('const let import export yield default delete var void typeof throw new in instanceof') + \ , expand('')) < 0 elseif char == '>' return pchar == '=' || syn =~? '^jsflow' elseif char == ':' From ca1836ab8a599417796142e43f35cf9e9eeb791f Mon Sep 17 00:00:00 2001 From: Chris Paul Date: Fri, 14 Oct 2016 15:06:13 -0700 Subject: [PATCH 3/6] Update javascript.vim --- indent/javascript.vim | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/indent/javascript.vim b/indent/javascript.vim index 00f5307a..691b82c2 100644 --- a/indent/javascript.vim +++ b/indent/javascript.vim @@ -109,7 +109,9 @@ function s:iscontOne(i,num,cont) if indent(l:i) < ind " first line always true for !a:cont, false for !!a:cont if s:OneScope(l:i,s:Trimline(l:i)) if expand('') ==# 'while' && - \ s:GetPair('\C\','\C\','bW',s:skip_expr . '|| !s:IsBlock()',100,l:num + !!a:num) > 0 + \ s:GetPair('\C\','\C\','bW','line2byte(line(".")) + col(".") <' + \ . (line2byte(b:js_cache[1] + !b:js_cache[1]) + b:js_cache[2]) . '||' + \ . s:skip_expr . '|| !s:IsBlock()',100,l:num) > 0 return 0 endif let bL += 1 From a3aee8feab89539aea05f2c187388b12701d5ad9 Mon Sep 17 00:00:00 2001 From: Chris Paul Date: Fri, 14 Oct 2016 15:10:51 -0700 Subject: [PATCH 4/6] 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 691b82c2..f0d1b453 100644 --- a/indent/javascript.vim +++ b/indent/javascript.vim @@ -110,7 +110,7 @@ function s:iscontOne(i,num,cont) if s:OneScope(l:i,s:Trimline(l:i)) if expand('') ==# 'while' && \ s:GetPair('\C\','\C\','bW','line2byte(line(".")) + col(".") <' - \ . (line2byte(b:js_cache[1] + !b:js_cache[1]) + b:js_cache[2]) . '||' + \ . (line2byte(l:num) + b:js_cache[2]) . '||' \ . s:skip_expr . '|| !s:IsBlock()',100,l:num) > 0 return 0 endif From 9aba528902d2ff27fdc5809d66d3b749d2a44ff0 Mon Sep 17 00:00:00 2001 From: Chris Paul Date: Fri, 14 Oct 2016 20:00:59 -0700 Subject: [PATCH 5/6] 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 f0d1b453..748a643c 100644 --- a/indent/javascript.vim +++ b/indent/javascript.vim @@ -256,7 +256,7 @@ function GetJavascriptIndent() if l:line =~ '^[])}]' return !!num * indent(num) endif - let b:js_cache = [v:lnum,num,line('.') == v:lnum ? b:js_cache[2] : col('.')] + let b:js_cache = [v:lnum,num,line('.') == v:lnum && num ? b:js_cache[2] : col('.')] call cursor(v:lnum,1) if l:line =~# '^while\>' && s:GetPair('\C\','\C\','bW',s:skip_expr . '|| !s:IsBlock()',100,num + 1) > 0 From fe45a8195882f0dadf56f5b5c2b12de2f5b74ee2 Mon Sep 17 00:00:00 2001 From: Chris Paul Date: Fri, 14 Oct 2016 21:17:49 -0700 Subject: [PATCH 6/6] 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 748a643c..0de83559 100644 --- a/indent/javascript.vim +++ b/indent/javascript.vim @@ -252,7 +252,7 @@ function GetJavascriptIndent() let num = s:GetPair('[({[]','[])}]','bW',s:skip_expr,200,l:lnum) endif - let num = (num > 0) * num + let num = max([num,0]) if l:line =~ '^[])}]' return !!num * indent(num) endif