22" Language: Javascript
33" Maintainer: Chris Paul ( https://github.com/bounceme )
44" URL: https://github.com/pangloss/vim-javascript
5- " Last Change: July 2 , 2017
5+ " Last Change: August 12 , 2017
66
77" Only load this indent file when no other was loaded.
88if exists (' b:did_indent' )
@@ -132,10 +132,8 @@ endfunction
132132
133133function s: AlternatePair ()
134134 let [pat, l: for ] = [' [][(){};]' , 2 ]
135- while search (' \m' .pat,' bW' ,s: script_tag )
136- if s: SkipFunc ()
137- continue
138- elseif s: LookingAt () == ' ;'
135+ while s: SearchLoop (' \m' .pat,' bW' ,s: script_tag ,' s:SkipFunc()' )
136+ if s: LookingAt () == ' ;'
139137 if ! l: for
140138 if s: GetPair (' {' ,' }' ,' bW' ,' s:SkipFunc()' ,2000 ,s: script_tag )
141139 return
@@ -173,35 +171,41 @@ function s:PreviousToken()
173171 if search (' \m\k\{1,}\|\S' ,' ebW' )
174172 if (strpart (getline (' .' ),col (' .' )-2 ,2 ) == ' */' || line (' .' ) != l: pos [1 ] &&
175173 \ getline (' .' )[: col (' .' )-1 ] = ~ ' \/\/' ) && s: SynAt (line (' .' ),col (' .' )) = ~? s: syng_com
176- while search (' \m\S\ze\_s*\/[/*]' ,' bW' )
177- if s: SynAt (line (' .' ),col (' .' )) !~? s: syng_com
178- return s: Token ()
179- endif
180- endwhile
174+ if s: SearchLoop (' \m\S\ze\_s*\/[/*]' ,' bW' ," s:SynAt(line('.'),col('.')) =~? s:syng_com" )
175+ return s: Token ()
176+ endif
177+ call setpos (' .' ,l: pos )
181178 else
182179 return s: Token ()
183180 endif
184- call setpos (' .' ,l: pos )
185181 endif
186182 return ' '
187183endfunction
188184
189- function s: __PreviousToken ( )
185+ function s: Pure ( f , ... )
190186 let l: pos = getpos (' .' )
191- let ret = s: PreviousToken ( )
187+ let ret = call ( a: f , a: 000 )
192188 call setpos (' .' ,l: pos )
193189 return ret
194190endfunction
195191
192+ function s: SearchLoop (... )
193+ let l: pos = getpos (' .' )
194+ while call (' search' ,a: 000 [:-2 ]) " search flags [^nc]
195+ if ! eval (a: 000 [-1 ])
196+ return line (' .' )
197+ endif
198+ endwhile
199+ call setpos (' .' ,l: pos )
200+ endfunction
201+
196202function s: ExprCol ()
197203 if getline (' .' )[col (' .' )-2 ] == ' :'
198204 return 1
199205 endif
200206 let [bal, l: pos ] = [0 , getpos (' .' )]
201- while search (' \m[{}?:]' ,' bW' ,s: script_tag )
202- if eval (s: skip_expr )
203- continue
204- elseif s: LookingAt () == ' :'
207+ while s: SearchLoop (' \m[{}?:]' ,' bW' ,s: script_tag ,s: skip_expr )
208+ if s: LookingAt () == ' :'
205209 let bal -= strpart (getline (' .' ),col (' .' )-2 ,3 ) !~ ' ::'
206210 elseif s: LookingAt () == ' ?'
207211 let bal += 1
@@ -287,34 +291,25 @@ function s:Balanced(lnum)
287291endfunction
288292
289293function s: OneScope (lnum)
290- let [pline, kw] = [s: Trim (a: lnum ), ' else do' ]
291- call cursor (a: lnum ,strlen (pline))
292- if pline[-1 :] == ' )' && s: GetPair (' (' , ' )' , ' bW' , s: skip_expr , 100 )
293- if s: PreviousToken () = ~# ' ^\%(await\|each\)$'
294- call s: PreviousToken ()
295- let kw = ' for'
296- else
297- let kw = ' for if let while with'
298- endif
299- elseif pline[-2 :] == ' =>'
300- return 1
294+ call cursor (a: lnum ,strlen (s: Trim (a: lnum )))
295+ if s: LookingAt () == ' )' && s: GetPair (' (' , ' )' , ' bW' , s: skip_expr , 100 )
296+ let tok = s: PreviousToken ()
297+ return (tok = ~# ' ^\%(for\|if\|let\|while\|with\)$' ||
298+ \ tok = ~# ' ^await$\|^each$' && s: PreviousToken () == # ' for' ) &&
299+ \ s: Pure (' s:PreviousToken' ) != ' .' && ! (tok == ' while' && s: DoWhile ())
300+ elseif s: Token () = ~# ' ^else$\|^do$'
301+ return s: Pure (' s:PreviousToken' ) != ' .'
301302 endif
302- return count (split (kw),s: Token ()) &&
303- \ s: __PreviousToken () != ' .' && ! s: DoWhile ()
303+ return strpart (getline (' .' ),col (' .' )-2 ,2 ) == ' =>'
304304endfunction
305305
306306function s: DoWhile ()
307- if expand (' <cword>' ) == # ' while'
308- let cpos = searchpos (' \m\<' ,' cbW' )
309- while search (' \m\C[{}]\|\<\%(do\|while\)\>' ,' bW' )
310- if ! eval (s: skip_expr )
311- if (s: LookingAt () == ' }' && s: GetPair (' {' ,' }' ,' bW' ,s: skip_expr ,200 ) ?
312- \ s: PreviousToken () : s: Token ()) == # ' do' && s: IsBlock ()
313- return 1
314- endif
315- break
316- endif
317- endwhile
307+ let cpos = searchpos (' \m\<' ,' cbW' )
308+ if s: SearchLoop (' \m\C[{}]\|\<\%(do\|while\)\>' ,' bW' ,s: skip_expr )
309+ if s: {s: LookingAt () == ' }' && s: GetPair (' {' ,' }' ,' bW' ,s: skip_expr ,200 ) ?
310+ \ ' Previous' : ' ' }Token () == # ' do' && s: IsBlock ()
311+ return 1
312+ endif
318313 call call (' cursor' ,cpos)
319314 endif
320315endfunction
@@ -340,13 +335,10 @@ function s:IsContOne(i,num,cont)
340335endfunction
341336
342337function s: IsSwitch ()
343- if s: PreviousToken () !~ ' [.*]'
344- if s: GetPair (' {' ,' }' ,' cbW' ,s: skip_expr ,100 )
345- return s: IsBlock () && (s: Token () !~ ' ^\K\k*$' || expand (' <cword>' ) !=# ' class' &&
346- \ s: PreviousToken () !~# ' ^class$\|^extends$' || s: PreviousToken () == ' .' )
347- endif
348- return 1
349- endif
338+ return s: PreviousToken () !~ ' [.*]' &&
339+ \ (! s: GetPair (' {' ,' }' ,' cbW' ,s: skip_expr ,100 ) || s: IsBlock () &&
340+ \ (s: Token () !~ ' ^\K\k*$' || expand (' <cword>' ) !=# ' class' &&
341+ \ s: PreviousToken () !~# ' ^class$\|^extends$' || s: PreviousToken () == ' .' ))
350342endfunction
351343
352344" https://github.com/sweet-js/sweet.js/wiki/design#give-lookbehind-to-the-reader
@@ -357,26 +349,21 @@ function s:IsBlock()
357349 return tok != ' {'
358350 elseif tok = ~ ' \k'
359351 if tok == # ' type'
360- let l: pos = getpos (' .' )
361- try
362- return s: PreviousToken () !~# ' ^\%(im\|ex\)port$' || s: PreviousToken () == ' .'
363- finally
364- call setpos (' .' ,l: pos )
365- endtry
352+ return s: Pure (' eval' ," s:PreviousToken() !~# '^\\ %(im\\ |ex\\ )port$' || s:PreviousToken() == '.'" )
366353 endif
367354 return index (split (' return const let import export extends yield default delete var await void typeof throw case new of in instanceof' )
368- \ ,tok) < (line (' .' ) != l: n ) || s: __PreviousToken ( ) == ' .'
355+ \ ,tok) < (line (' .' ) != l: n ) || s: Pure ( ' s:PreviousToken ' ) == ' .'
369356 elseif tok == ' >'
370357 return getline (' .' )[col (' .' )-2 ] == ' =' || s: SynAt (line (' .' ),col (' .' )) = ~? ' jsflow\|^html'
371358 elseif tok == ' *'
372- return s: __PreviousToken ( ) == ' :'
359+ return s: Pure ( ' s:PreviousToken ' ) == ' :'
373360 elseif tok == ' :'
374361 return ! s: ExprCol ()
375362 elseif tok == ' /'
376363 return s: SynAt (line (' .' ),col (' .' )) = ~? ' regex'
364+ elseif tok !~ ' [=~!<,.?^%|&([]'
365+ return tok !~ ' [-+]' || l: n != line (' .' ) && getline (' .' )[col (' .' )-2 ] == tok
377366 endif
378- return tok !~ ' [=~!<,.?^%|&([]' &&
379- \ (tok !~ ' [-+]' || l: n != line (' .' ) && getline (' .' )[col (' .' )-2 ] == tok)
380367endfunction
381368
382369function GetJavascriptIndent ()
0 commit comments