Skip to content

Commit d3e952c

Browse files
authored
multi-braceless statements (#636)
* multi-braceless statements This changes: ``` if (1) for (;;) i += l ``` to: ``` if (1) for (;;) i += l ```
1 parent d5dbecb commit d3e952c

File tree

1 file changed

+44
-16
lines changed

1 file changed

+44
-16
lines changed

indent/javascript.vim

Lines changed: 44 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -75,15 +75,38 @@ if !exists('g:javascript_continuation')
7575
let g:javascript_continuation = '\%([<=,.?/*^%|&:]\|+\@<!+\|-\@<!-\|=\@<!>\|\<in\%(stanceof\)\=\)'
7676
endif
7777

78-
let g:javascript_opfirst = s:line_pre . g:javascript_opfirst
78+
let g:javascript_opfirst = '^' . g:javascript_opfirst
7979
let g:javascript_continuation .= s:line_term
8080

8181
function s:OneScope(lnum,text)
82-
return a:text =~# '\%(\<else\|\<do\|=>\)' . s:line_term ? 'no b' :
82+
return cursor(a:lnum, match(' ' . a:text, '\%(\<else\|\<do\|=>\)' . s:line_term)) > -1 ||
8383
\ cursor(a:lnum, match(' ' . a:text, ')' . s:line_term)) > -1 &&
84-
\ s:GetPair('(', ')', 'bW', s:skip_expr, 100) > 0 && search('\C\l\+\_s*\%#','bW') &&
85-
\ (expand('<cword>') !=# 'while' || s:GetPair('\C\<do\>', '\C\<while\>','nbW',s:skip_expr,100) <= 0) &&
86-
\ (expand('<cword>') !=# 'each' || search('\C\<for\_s\+\%#','nbW')) ? expand('<cword>') : ''
84+
\ s:GetPair('(', ')', 'bW', s:skip_expr, 100) > 0 &&
85+
\ search('\C\<\%(for\%(\_s\+each\)\=\|if\|let\|w\%(hile\|ith\)\)\_s*\%#','bW')
86+
endfunction
87+
88+
function s:iscontOne(i,num,cont)
89+
let [l:i, l:cont, l:num] = [a:i, a:cont, a:num > 0 ? a:num : 1]
90+
let pind = a:num > 0 ? indent(l:num) : -s:sw()
91+
let ind = indent(l:i) + (!l:cont ? s:sw() : 0)
92+
let bL = 0
93+
while l:i >= l:num && (!l:cont || ind > pind + s:sw())
94+
if indent(l:i) < ind " first line always true for !cont, false for !!cont
95+
if s:OneScope(l:i,substitute(getline(l:i),':\@<!\/\/.*','',''))
96+
if expand('<cword>') ==# 'while' && searchpair(s:line_pre . '\C\<do\>','','\C\<while\>','bW',s:skip_expr,l:num,100) > 0
97+
return 0
98+
endif
99+
let bL += 1
100+
let l:cont = 0
101+
let l:i = line('.')
102+
elseif !l:cont
103+
break
104+
endif
105+
let ind = indent(l:i)
106+
endif
107+
let l:i = s:PrevCodeLine(l:i - 1)
108+
endwhile
109+
return bL * s:sw()
87110
endfunction
88111

89112
" https://github.com/sweet-js/sweet.js/wiki/design#give-lookbehind-to-the-reader
@@ -138,8 +161,8 @@ function GetJavascriptIndent()
138161
let syns = synIDattr(synID(v:lnum, 1, 0), 'name')
139162

140163
" start with strings,comments,etc.{{{2
141-
if (l:line !~ '^[''"]' && syns =~? '\%(string\|template\)') ||
142-
\ (l:line !~ '^\s*[/*]' && syns =~? s:syng_comment)
164+
if l:line !~ '^[''"]' && syns =~? '\%(string\|template\)' ||
165+
\ l:line !~ '^\s*[/*]' && syns =~? s:syng_comment
143166
return -1
144167
endif
145168
if l:line !~ '^\%(\/\*\|\s*\/\/\)' && syns =~? s:syng_comment
@@ -150,7 +173,7 @@ function GetJavascriptIndent()
150173
return 0
151174
endif
152175

153-
if (l:line =~# s:expr_case)
176+
if l:line =~# s:expr_case
154177
let cpo_switch = &cpo
155178
set cpo+=%
156179
let ind = cindent(v:lnum)
@@ -176,11 +199,16 @@ function GetJavascriptIndent()
176199

177200
let b:js_cache = [v:lnum,num,line('.') == v:lnum ? b:js_cache[2] : col('.')]
178201

179-
if l:line =~ s:line_pre . '[])}]'
202+
let l:line = substitute(l:line,s:line_pre,'','')
203+
if l:line =~ '^[])}]'
180204
return indent(num)
181205
endif
206+
call cursor(v:lnum,1)
207+
if l:line =~# '^while\>' && searchpair(s:line_pre . '\C\<do\>','','\C\<while\>','bW',s:skip_expr,num,100) > 0
208+
return indent(line('.'))
209+
endif
182210

183-
let pline = substitute(substitute(getline(l:lnum),s:expr_case,'\=repeat(" ",strlen(submatch(0)))',''), '\%(:\@<!\/\/.*\)$', '','')
211+
let pline = substitute(substitute(getline(l:lnum),s:expr_case,'\=repeat(" ",strlen(submatch(0)))',''), ':\@<!\/\/.*', '','')
184212
call cursor(b:js_cache[1],b:js_cache[2])
185213
let switch_offset = num <= 0 || !(search(')\_s*\%#','bW') &&
186214
\ s:GetPair('(', ')', 'bW', s:skip_expr, 100) > 0 && search('\C\<switch\_s*\%#','bW')) ? 0 :
@@ -189,14 +217,14 @@ function GetJavascriptIndent()
189217

190218
" most significant, find the indent amount
191219
let isOp = l:line =~# g:javascript_opfirst || pline =~# g:javascript_continuation
192-
if isOp && (num <= 0 || cursor(b:js_cache[1],b:js_cache[2]) || s:IsBlock()) ||
193-
\ s:OneScope(l:lnum,pline) =~# '\<\%(for\|each\|if\|let\|no\sb\|w\%(hile\|ith\)\)\>' &&
194-
\ l:line !~ s:line_pre . '{'
195-
return (num > 0 ? indent(num) : -s:sw()) + (s:sw() * 2) + switch_offset
220+
let bL = s:iscontOne(l:lnum,num,isOp)
221+
let bL = bL ? bL - (l:line =~ '^{') * s:sw() : bL
222+
if isOp && (num <= 0 || cursor(b:js_cache[1],b:js_cache[2]) || s:IsBlock())
223+
return (num > 0 ? indent(num) : -s:sw()) + (s:sw() * 2) + switch_offset + bL
196224
elseif num > 0
197-
return indent(num) + s:sw() + switch_offset
225+
return indent(num) + s:sw() + switch_offset + bL
198226
endif
199-
227+
return bL
200228
endfunction
201229

202230

0 commit comments

Comments
 (0)