Skip to content

Commit 28b5a87

Browse files
authored
block statement (#596)
this uses the same algorithm from https://github.com/sweet-js/sweet.js/wiki/design#give-lookbehind-to-the-reader which the acorn parser also uses. it actually is basically the same as what i had originally but with support for labeled block statements and plain block statements.
1 parent 0cab456 commit 28b5a87

File tree

1 file changed

+19
-12
lines changed

1 file changed

+19
-12
lines changed

indent/javascript.vim

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
" Language: Javascript
33
" Maintainer: vim-javascript community
44
" URL: https://github.com/pangloss/vim-javascript
5-
" Last Change: August 7, 2016
5+
" Last Change: August 15, 2016
66

77
" Only load this indent file when no other was loaded.
88
if exists('b:did_indent')
@@ -70,13 +70,19 @@ endif
7070
let g:javascript_continuation .= s:line_term
7171

7272
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' :
7474
\ ((a:add && a:text =~ s:line_pre . '$' && search('\%' . s:PrevCodeLine(a:lnum - 1) . 'l.)' . s:line_term)) ||
7575
\ 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()))
8086
endfunction
8187

8288
" Auxiliary Functions {{{2
@@ -170,16 +176,17 @@ function GetJavascriptIndent()
170176
endif
171177

172178
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() :
176183
\ float2nr(str2float(matchstr(&cino,'.*:\zs[-0-9.]*')) * (&cino =~# '.*:[^,]*s' ? s:sw() : 1))
177184

178185
" most significant, find the indent amount
179186
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 . '{')
183190
return (num > 0 ? indent(num) : -s:sw()) + (s:sw() * 2) + switch_offset
184191
elseif num > 0
185192
return indent(num) + s:sw() + switch_offset

0 commit comments

Comments
 (0)