2
2
" Language: Javascript
3
3
" Maintainer: Chris Paul ( https://github.com/bounceme )
4
4
" URL: https://github.com/pangloss/vim-javascript
5
- " Last Change: October 19 , 2016
5
+ " Last Change: October 24 , 2016
6
6
7
7
" Only load this indent file when no other was loaded.
8
8
if exists (' b:did_indent' )
63
63
endfunction
64
64
endif
65
65
66
- function s: Trimline (ln )
66
+ function s: Trim (ln )
67
67
let pline = substitute (getline (a: ln ),' \s*$' ,' ' ,' ' )
68
68
let l: max = max ([strridx (pline,' //' ),strridx (pline,' /*' ),0 ])
69
69
while l: max && synIDattr (synID (a: ln , strlen (pline), 0 ), ' name' ) = ~? ' \%(comment\|doc\)'
@@ -88,12 +88,12 @@ endfunction
88
88
89
89
function s: iscontOne (i ,num,cont)
90
90
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 )
93
93
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)
95
95
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 ))
97
97
if expand (' <cword>' ) == # ' while' &&
98
98
\ s: GetPair (' \C\<do\>' ,' \C\<while\>' ,' bW' ,' line2byte(line(".")) + col(".") <'
99
99
\ . (line2byte (l: num ) + b: js_cache [2 ]) . ' ||'
@@ -106,6 +106,8 @@ function s:iscontOne(i,num,cont)
106
106
break
107
107
endif
108
108
let ind = indent (l: i )
109
+ elseif ! a: cont
110
+ break
109
111
endif
110
112
let l: i = s: PrevCodeLine (l: i - 1 )
111
113
endwhile
@@ -234,32 +236,40 @@ function GetJavascriptIndent()
234
236
let b: js_cache = [v: lnum ] + (line (' .' ) == v: lnum ? [0 ,0 ] : [line (' .' ),col (' .' )])
235
237
let num = b: js_cache [1 ]
236
238
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
240
259
endif
241
260
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 (' .' ))
255
265
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
256
269
endif
257
270
258
271
" 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
263
273
return (num ? indent (num) : - s: W ) + (s: W * 2 ) + switch_offset + bL
264
274
elseif num
265
275
return indent (num) + s: W + switch_offset + bL
0 commit comments