@@ -17,7 +17,7 @@ setlocal nosmartindent
1717setlocal indentexpr = GetJavascriptIndent ()
1818setlocal formatexpr = Fixedgq (v: lnum ,v: count )
1919setlocal indentkeys = 0 {,0 },0 ),0 ],0 \, * <Return> ,:,! ^F,o ,O,e
20- setlocal cinoptions += j1,J1
20+ setlocal cinoptions += j1,J1,c1
2121
2222" Only define the function once.
2323if exists (" *GetJavascriptIndent" )
@@ -140,7 +140,7 @@ function s:GetMSL(lnum, in_one_line_scope)
140140 " Start on the line we're at and use its indent.
141141 let msl = a: lnum
142142 let lnum = s: PrevNonBlankNonString (a: lnum - 1 )
143- while lnum > 0
143+ while lnum > 0 && ! s: Match (msl, s: line_pre . ' [])}] ' )
144144 " If we have a continuation line, or we're in a string, use line as MSL.
145145 " Otherwise, terminate search as we have found our MSL already.
146146 let line = getline (lnum)
@@ -312,6 +312,7 @@ function GetJavascriptIndent()
312312 " 3.1. Setup {{{1
313313 " ----------
314314 " Set up variables for restoring position in file. Could use v:lnum here.
315+ " Avoid use of line('.')/col('.') type functions as the curpos can change
315316 let vcol = col (' .' )
316317
317318 " 3.2. Work on the current line {{{1
@@ -334,7 +335,7 @@ function GetJavascriptIndent()
334335 if line !~ ' ^\%(\/\*\|\s*\/\/\)' && s: IsInComment (v: lnum , 1 )
335336 return cindent (v: lnum )
336337 endif
337-
338+
338339 " single opening bracket will assume you want a c style of indenting
339340 if s: Match (v: lnum , s: line_pre . ' {' . s: line_term ) && ! s: Match (lnum,s: block_regex ) &&
340341 \ ! s: Match (lnum,s: comma_last )
@@ -343,7 +344,11 @@ function GetJavascriptIndent()
343344
344345 " cindent each line which has a switch label
345346 if (line = ~ s: expr_case )
346- return cindent (v: lnum )
347+ let s: cpo_switch = &cpo
348+ set cpo += %
349+ let ind = cindent (v: lnum )
350+ let &cpo = s: cpo_switch
351+ return ind
347352 endif
348353
349354 " If we got a closing bracket on an empty line, find its match and indent
@@ -359,8 +364,8 @@ function GetJavascriptIndent()
359364 continue
360365 end
361366 if parlnum > 0
362- let ind = s: InMultiVarStatement (parlnum, 0 , 0 )|| s: LineHasOpeningBrackets (parlnum) !~ ' 2'
363- \ ? indent (parlnum) : indent (s: GetMSL (parlnum, 0 ))
367+ let ind = s: InMultiVarStatement (parlnum, 0 , 0 )|| s: LineHasOpeningBrackets (parlnum) !~ ' 2' ?
368+ \ indent (parlnum) : indent (s: GetMSL (parlnum, 0 ))
364369 endif
365370 endwhile
366371 return ind
@@ -369,7 +374,8 @@ function GetJavascriptIndent()
369374
370375 " If line starts with an operator...
371376 if (line = ~ s: operator_first )
372- if (s: Match (lnum, s: operator_first ) || s: Match (lnum, s: line_pre . ' [])}]' ))
377+ if (s: Match (lnum, s: operator_first ) || (s: Match (lnum, s: line_pre . ' [])}]' ) &&
378+ \ ! (s: Match (v: lnum ,s: line_pre . ' \.' ) && s: Match (lnum, ' )' . s: line_term ))))
373379 " and so does previous line, don't indent
374380 return indent (lnum)
375381 end
@@ -411,7 +417,7 @@ function GetJavascriptIndent()
411417 " If the line is empty and the previous nonblank line was a multi-line
412418 " comment, use that comment's indent. Deduct one char to account for the
413419 " space in ' */'.
414- if line = ~ ' ^\s*$' && getline (prevline) ! ~ ' \%(\%(^\s*\/\/\|\/\*\).*\)\@<!\*\/' &&
420+ if line = ~ ' ^\s*$' && getline (prevline) = ~ ' \%(\%(^\s*\/\/\|\/\*\).*\)\@<!\*\/' &&
415421 \ s: IsInComment (prevline, 1 )
416422 return indent (prevline) - 1
417423 endif
@@ -428,16 +434,16 @@ function GetJavascriptIndent()
428434 return 0
429435 endif
430436
431- " foo('foo',
432- " bar('bar', function() {
433- " hi();
434- " })
435- " );
437+ " foo('foo',
438+ " bar('bar', function() {
439+ " hi();
440+ " })
441+ " );
436442
437- " function (a, b, c, d,
438- " e, f, g) {
439- " console.log('inner');
440- " }
443+ " function (a, b, c, d,
444+ " e, f, g) {
445+ " console.log('inner');
446+ " }
441447 " If the previous line ended with a block opening, add a level of indent.
442448 if s: Match (lnum, s: block_regex )
443449 return s: InMultiVarStatement (lnum, 0 , 0 ) || s: LineHasOpeningBrackets (lnum) !~ ' 2' ?
@@ -449,15 +455,16 @@ function GetJavascriptIndent()
449455 let ind = indent (lnum)
450456 " If the previous line contained an opening bracket, and we are still in it,
451457 " add indent depending on the bracket type.
452- if s: Match (lnum, ' \%( [[({]\)\|\%([^\t \])}][} )\]]\) ' )
458+ if s: Match (lnum, ' [[({} )\]]' )
453459 let counts = s: LineHasOpeningBrackets (lnum)
454460 if counts = ~ ' 2'
455- call cursor (lnum,matchend (s: RemoveTrailingComments (line ), ' .\+ \zs[])}]' ))
461+ call cursor (lnum,matchend (s: RemoveTrailingComments (line ), ' .* \zs[])}]' ))
456462 while s: lookForParens (' (\|{\|\[' , ' )\|}\|\]' , ' bW' , 0 ) == lnum
457463 call cursor (lnum, matchend (s: RemoveTrailingComments (strpart (line ,0 ,col (' .' ))), ' .*\zs[])}]' ))
458464 endwhile
459- if line (' .' ) < lnum && ! s: InMultiVarStatement (line (' .' ),0 ,0 )
460- return indent (s: GetMSL (line (' .' ), 0 ))
465+ let cur = line (' .' )
466+ if cur < lnum && ! s: InMultiVarStatement (cur,0 ,0 )
467+ return indent (s: GetMSL (cur, 0 ))
461468 end
462469 elseif counts = ~ ' 1' || s: Onescope (lnum)
463470 return ind + s: sw ()
@@ -500,64 +507,64 @@ let &cpo = s:cpo_save
500507unlet s: cpo_save
501508" gq{{{2
502509function ! Fixedgq (lnum, count )
503- let l: tw = &tw ? &tw : 80 ;
510+ let l: tw = &tw ? &tw : 80 ;
504511
505- let l: count = a: count
506- let l: first_char = indent (a: lnum ) + 1
512+ let l: count = a: count
513+ let l: first_char = indent (a: lnum ) + 1
507514
508- if mode () == ' i' " gq was not pressed, but tw was set
509- return 1
510- endif
515+ if mode () == ' i' " gq was not pressed, but tw was set
516+ return 1
517+ endif
511518
512- " This gq is only meant to do code with strings, not comments
513- if s: IsInComment (a: lnum , l: first_char )
514- return 1
515- endif
519+ " This gq is only meant to do code with strings, not comments
520+ if s: IsInComment (a: lnum , l: first_char )
521+ return 1
522+ endif
516523
517- if len (getline (a: lnum )) < l: tw && l: count == 1 " No need for gq
518- return 1
519- endif
524+ if len (getline (a: lnum )) < l: tw && l: count == 1 " No need for gq
525+ return 1
526+ endif
520527
521- " Put all the lines on one line and do normal spliting after that
522- if l: count > 1
523- while l: count > 1
524- let l: count -= 1
525- normal J
526- endwhile
527- endif
528+ " Put all the lines on one line and do normal spliting after that
529+ if l: count > 1
530+ while l: count > 1
531+ let l: count -= 1
532+ normal J
533+ endwhile
534+ endif
528535
529- let l: winview = winsaveview ()
536+ let l: winview = winsaveview ()
530537
531- call cursor (a: lnum , l: tw + 1 )
532- let orig_breakpoint = searchpairpos (' ' , ' ' , ' \.' , ' bcW' , ' ' , a: lnum )
533- call cursor (a: lnum , l: tw + 1 )
534- let breakpoint = searchpairpos (' ' , ' ' , ' \.' , ' bcW' , s: skip_expr , a: lnum )
538+ call cursor (a: lnum , l: tw + 1 )
539+ let orig_breakpoint = searchpairpos (' ' , ' ' , ' \.' , ' bcW' , ' ' , a: lnum )
540+ call cursor (a: lnum , l: tw + 1 )
541+ let breakpoint = searchpairpos (' ' , ' ' , ' \.' , ' bcW' , s: skip_expr , a: lnum )
535542
536- " No need for special treatment, normal gq handles edgecases better
537- if breakpoint[1 ] == orig_breakpoint[1 ]
538- call winrestview (l: winview )
539- return 1
540- endif
543+ " No need for special treatment, normal gq handles edgecases better
544+ if breakpoint[1 ] == orig_breakpoint[1 ]
545+ call winrestview (l: winview )
546+ return 1
547+ endif
541548
542- " Try breaking after string
543- if breakpoint[1 ] <= indent (a: lnum )
544- call cursor (a: lnum , l: tw + 1 )
545- let breakpoint = searchpairpos (' \.' , ' ' , ' ' , ' cW' , s: skip_expr , a: lnum )
546- endif
549+ " Try breaking after string
550+ if breakpoint[1 ] <= indent (a: lnum )
551+ call cursor (a: lnum , l: tw + 1 )
552+ let breakpoint = searchpairpos (' \.' , ' ' , ' ' , ' cW' , s: skip_expr , a: lnum )
553+ endif
547554
548555
549- if breakpoint[1 ] != 0
550- call feedkeys (" r\<CR> " )
551- else
552- let l: count = l: count - 1
553- endif
556+ if breakpoint[1 ] != 0
557+ call feedkeys (" r\<CR> " )
558+ else
559+ let l: count = l: count - 1
560+ endif
554561
555- " run gq on new lines
556- if l: count == 1
557- call feedkeys (" gqq" )
558- endif
562+ " run gq on new lines
563+ if l: count == 1
564+ call feedkeys (" gqq" )
565+ endif
559566
560- return 0
567+ return 0
561568endfunction
562569" }}}
563570" vim: foldmethod = marker:foldlevel = 1
0 commit comments