22" Language: Javascript
33" Maintainer: Chris Paul ( https://github.com/bounceme )
44" URL: https://github.com/pangloss/vim-javascript
5- " Last Change: October 19 , 2016
5+ " Last Change: October 24 , 2016
66
77" Only load this indent file when no other was loaded.
88if exists (' b:did_indent' )
6363 endfunction
6464endif
6565
66- function s: Trimline (ln )
66+ function s: Trim (ln )
6767 let pline = substitute (getline (a: ln ),' \s*$' ,' ' ,' ' )
6868 let l: max = max ([strridx (pline,' //' ),strridx (pline,' /*' ),0 ])
6969 while l: max && synIDattr (synID (a: ln , strlen (pline), 0 ), ' name' ) = ~? ' \%(comment\|doc\)'
@@ -88,12 +88,12 @@ endfunction
8888
8989function s: iscontOne (i ,num,cont)
9090 let [l: i , l: cont , l: num ] = [a: i , a: cont , a: num + ! a: num ]
91- let pind = a: num ? indent (l: num ) : - s: W
92- let ind = indent (l: i ) + (! l : cont * s: W )
91+ let pind = a: num ? indent (l: num ) + s: W : 0
92+ let ind = indent (l: i ) + (a : cont ? 0 : s: W )
9393 let bL = 0
94- while l: i >= l: num && (! l: cont || ind > pind + s: W )
94+ while l: i >= l: num && (! l: cont || ind > pind)
9595 if indent (l: i ) < ind " first line always true for !a:cont, false for !!a:cont
96- if s: OneScope (l: i ,s: Trimline (l: i ))
96+ if s: OneScope (l: i ,s: Trim (l: i ))
9797 if expand (' <cword>' ) == # ' while' &&
9898 \ s: GetPair (' \C\<do\>' ,' \C\<while\>' ,' bW' ,' line2byte(line(".")) + col(".") <'
9999 \ . (line2byte (l: num ) + b: js_cache [2 ]) . ' ||'
@@ -106,6 +106,8 @@ function s:iscontOne(i,num,cont)
106106 break
107107 endif
108108 let ind = indent (l: i )
109+ elseif ! a: cont
110+ break
109111 endif
110112 let l: i = s: PrevCodeLine (l: i - 1 )
111113 endwhile
@@ -234,32 +236,40 @@ function GetJavascriptIndent()
234236 let b: js_cache = [v: lnum ] + (line (' .' ) == v: lnum ? [0 ,0 ] : [line (' .' ),col (' .' )])
235237 let num = b: js_cache [1 ]
236238
237- call cursor (v: lnum ,1 )
238- if l: line = ~# ' ^while\>' && s: GetPair (' \C\<do\>' ,' \C\<while\>' ,' bW' ,s: skip_expr . ' || !s:IsBlock()' ,100 ,num + 1 ) > 0
239- return indent (line (' .' ))
239+ call call (' cursor' ,b: js_cache [1 :])
240+ let [s: W , pline, isOp, stmt, bL, switch_offset] = [s: sw (), s: Trim (l: lnum ),0 ,0 ,0 ,0 ]
241+ if num
242+ if getline (' .' )[col (' .' )-1 ] == ' {'
243+ if search (' )\_s*\%#' ,' bW' )
244+ let stmt = 1
245+ if s: GetPair (' (' , ' )' , ' bW' , s: skip_expr , 100 ) > 0 && search (' \C\<switch\_s*\%#' ,' bW' )
246+ let switch_offset = &cino !~ ' :' || ! has (' float' ) ? s: W :
247+ \ float2nr (str2float (matchstr (&cino ,' .*:\zs[-0-9.]*' )) * (&cino = ~# ' .*:[^,]*s' ? s: W : 1 ))
248+ if l: line = ~# ' ^' . s: expr_case
249+ return indent (num) + switch_offset
250+ endif
251+ let stmt = pline !~# s: expr_case . ' $'
252+ endif
253+ elseif s: IsBlock ()
254+ let stmt = 1
255+ endif
256+ endif
257+ else
258+ let stmt = 1
240259 endif
241260
242- call call (' cursor' ,b: js_cache [1 :])
243- let s: W = s: sw ()
244- let pline = s: Trimline (l: lnum )
245- let bchar = getline (' .' )[col (' .' )-1 ] == ' {'
246- let switch_offset = 0
247- let in_switch = 0
248- if num && bchar && search (' )\_s*\%#' ,' bW' ) &&
249- \ s: GetPair (' (' , ' )' , ' bW' , s: skip_expr , 100 ) > 0 && search (' \C\<switch\_s*\%#' ,' bW' )
250- let in_switch = 1
251- let switch_offset = &cino !~ ' :' || ! has (' float' ) ? s: W :
252- \ float2nr (str2float (matchstr (&cino ,' .*:\zs[-0-9.]*' )) * (&cino = ~# ' .*:[^,]*s' ? s: W : 1 ))
253- if l: line = ~# ' ^' . s: expr_case
254- return indent (num) + switch_offset
261+ if stmt
262+ call cursor (v: lnum ,1 )
263+ if l: line = ~# ' ^while\>' && s: GetPair (' \C\<do\>' ,' \C\<while\>' ,' bW' ,s: skip_expr . ' || !s:IsBlock()' ,100 ,num + 1 ) > 0
264+ return indent (line (' .' ))
255265 endif
266+ let isOp = l: line = ~# s: opfirst || pline = ~# s: continuation
267+ let bL = s: iscontOne (l: lnum ,num,isOp)
268+ let bL -= (bL && l: line [0 ] == ' {' ) * s: W
256269 endif
257270
258271 " most significant, find the indent amount
259- let isOp = l: line = ~# s: opfirst || (in_switch && pline = ~# s: expr_case . ' $' ? 0 : pline = ~# s: continuation )
260- let bL = s: iscontOne (l: lnum ,num,isOp)
261- let bL -= (bL && l: line [0 ] == ' {' ) * s: W
262- if isOp && (! num || in_switch || bchar && call (' cursor' ,b: js_cache [1 :])+ 1 && s: IsBlock ())
272+ if isOp
263273 return (num ? indent (num) : - s: W ) + (s: W * 2 ) + switch_offset + bL
264274 elseif num
265275 return indent (num) + s: W + switch_offset + bL
0 commit comments