|
2 | 2 | " Language: Javascript
|
3 | 3 | " Maintainer: vim-javascript community
|
4 | 4 | " URL: https://github.com/pangloss/vim-javascript
|
5 |
| -" Last Change: August 7, 2016 |
| 5 | +" Last Change: August 15, 2016 |
6 | 6 |
|
7 | 7 | " Only load this indent file when no other was loaded.
|
8 | 8 | if exists('b:did_indent')
|
@@ -70,13 +70,19 @@ endif
|
70 | 70 | let g:javascript_continuation .= s:line_term
|
71 | 71 |
|
72 | 72 | function s:Onescope(lnum,text,add)
|
73 |
| - return a:text =~# '\%(\<else\|\<do\|=>' . (a:add ? '\|\<try\|\<finally' : '' ) . '\)' . s:line_term || |
| 73 | + return a:text =~# '\%(\<else\|\<do\|=>\)' . s:line_term ? 'no b' : |
74 | 74 | \ ((a:add && a:text =~ s:line_pre . '$' && search('\%' . s:PrevCodeLine(a:lnum - 1) . 'l.)' . s:line_term)) ||
|
75 | 75 | \ cursor(a:lnum, match(a:text, ')' . s:line_term)) > -1) &&
|
76 |
| - \ s:lookForParens('(', ')', 'cbW', 100) > 0 && search((a:add ? |
77 |
| - \ '\%(function\*\|[[:lower:][:upper:]_$][[:digit:][:lower:][:upper:]_$]*\)' : |
78 |
| - \ '\<\%(for\%(\s\+each\)\=\|if\|let\|w\%(hile\|ith\)\)') . '\_s*\%#\C','bW') && |
79 |
| - \ (a:add || (expand('<cword>') ==# 'while' ? !s:lookForParens('\<do\>\C', '\<while\>\C','bW',100) : 1)) |
| 76 | + \ s:lookForParens('(', ')', 'cbW', 100) > 0 && search('\C\l\+\_s*\%#','bW') && |
| 77 | + \ (a:add || ((expand('<cword>') !=# 'while' || !s:lookForParens('\C\<do\>', '\C\<while\>','nbW',100)) && |
| 78 | + \ expand('cword') !=# 'each' || search('\C\<for\_s\+\%#','nbW'))) ? expand('<cword>') : '' |
| 79 | +endfunction |
| 80 | + |
| 81 | +" https://github.com/sweet-js/sweet.js/wiki/design#give-lookbehind-to-the-reader |
| 82 | +function s:isBlock() |
| 83 | + return getline(line('.'))[col('.')-1] == '{' && !search( |
| 84 | + \ '\C\%(\%([-=~!<*+,.?^%|&\[(]\|=\@<!>\|\*\@<!\/\|\<\%(var\|const\|let\|yield\|delete\|void\|t\%(ypeof\|hrow\)\|new\|\<in\%(stanceof\)\=\)\)\_s*\|\<return\s*\)\%#','bnW') && |
| 85 | + \ (!search(':\_s*\%#','bW') || (!s:lookForParens('[({[]','[])}]','bW',200) || s:isBlock())) |
80 | 86 | endfunction
|
81 | 87 |
|
82 | 88 | " Auxiliary Functions {{{2
|
@@ -170,16 +176,17 @@ function GetJavascriptIndent()
|
170 | 176 | endif
|
171 | 177 |
|
172 | 178 | let pline = s:StripLine(getline(l:lnum))
|
173 |
| - let inb = num == 0 ? 1 : (s:Onescope(num, s:StripLine(strpart(getline(num),0,b:js_cache[2] - 1)),1) || |
174 |
| - \ (l:line !~ s:line_pre . ',' && pline !~ ',' . s:line_term)) && num < l:lnum |
175 |
| - let switch_offset = (!inb || num == 0) || expand("<cword>") !=# 'switch' ? 0 : &cino !~ ':' || !has('float') ? s:sw() : |
| 179 | + call cursor(b:js_cache[1:]) |
| 180 | + let inb = num == 0 || num < l:lnum && ((l:line !~ s:line_pre . ',' && pline !~ ',' . s:line_term) || s:isBlock()) |
| 181 | + let switch_offset = num == 0 || s:Onescope(num, s:StripLine(strpart(getline(num),0,b:js_cache[2] - 1)),1) !=# 'switch' ? 0 : |
| 182 | + \ &cino !~ ':' || !has('float') ? s:sw() : |
176 | 183 | \ float2nr(str2float(matchstr(&cino,'.*:\zs[-0-9.]*')) * (&cino =~# '.*:[^,]*s' ? s:sw() : 1))
|
177 | 184 |
|
178 | 185 | " most significant, find the indent amount
|
179 | 186 | if (inb && (l:line =~# g:javascript_opfirst ||
|
180 |
| - \ (pline =~# g:javascript_continuation && pline !~# s:expr_case && (pline !~ ':' . s:line_term || l:line !~# |
181 |
| - \ s:line_pre . '\%(d\%(o\|ebugger\)\|else\|f\%(or\|inally\)\|if\|let\|switch\|t\%(hrow\|ry\)\|w\%(hile\|ith\)\)\>')))) || |
182 |
| - \ (num < l:lnum && s:Onescope(l:lnum,pline,0) && l:line !~ s:line_pre . '{') |
| 187 | + \ (pline =~# g:javascript_continuation && pline !~# s:expr_case))) || |
| 188 | + \ (num < l:lnum && s:Onescope(l:lnum,pline,0) =~# '\%(for\|each\|if\|let\|no\sb\|w\%(hile\|ith\)\)' && |
| 189 | + \ l:line !~ s:line_pre . '{') |
183 | 190 | return (num > 0 ? indent(num) : -s:sw()) + (s:sw() * 2) + switch_offset
|
184 | 191 | elseif num > 0
|
185 | 192 | return indent(num) + s:sw() + switch_offset
|
|
0 commit comments