@@ -312,6 +312,7 @@ function GetJavascriptIndent()
312
312
" 3.1. Setup {{{1
313
313
" ----------
314
314
" 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
315
316
let vcol = col (' .' )
316
317
317
318
" 3.2. Work on the current line {{{1
@@ -334,7 +335,7 @@ function GetJavascriptIndent()
334
335
if line !~ ' ^\%(\/\*\|\s*\/\/\)' && s: IsInComment (v: lnum , 1 )
335
336
return cindent (v: lnum )
336
337
endif
337
-
338
+
338
339
" single opening bracket will assume you want a c style of indenting
339
340
if s: Match (v: lnum , s: line_pre . ' {' . s: line_term ) && ! s: Match (lnum,s: block_regex ) &&
340
341
\ ! s: Match (lnum,s: comma_last )
@@ -429,16 +430,16 @@ function GetJavascriptIndent()
429
430
return 0
430
431
endif
431
432
432
- " foo('foo',
433
- " bar('bar', function() {
434
- " hi();
435
- " })
436
- " );
433
+ " foo('foo',
434
+ " bar('bar', function() {
435
+ " hi();
436
+ " })
437
+ " );
437
438
438
- " function (a, b, c, d,
439
- " e, f, g) {
440
- " console.log('inner');
441
- " }
439
+ " function (a, b, c, d,
440
+ " e, f, g) {
441
+ " console.log('inner');
442
+ " }
442
443
" If the previous line ended with a block opening, add a level of indent.
443
444
if s: Match (lnum, s: block_regex )
444
445
return s: InMultiVarStatement (lnum, 0 , 0 ) || s: LineHasOpeningBrackets (lnum) !~ ' 2' ?
@@ -450,15 +451,16 @@ function GetJavascriptIndent()
450
451
let ind = indent (lnum)
451
452
" If the previous line contained an opening bracket, and we are still in it,
452
453
" add indent depending on the bracket type.
453
- if s: Match (lnum, ' \%( [[({]\)\|\%([^\t \])}][} )\]]\) ' )
454
+ if s: Match (lnum, ' [[({} )\]]' )
454
455
let counts = s: LineHasOpeningBrackets (lnum)
455
456
if counts = ~ ' 2'
456
- call cursor (lnum,matchend (s: RemoveTrailingComments (line ), ' .\+ \zs[])}]' ))
457
+ call cursor (lnum,matchend (s: RemoveTrailingComments (line ), ' .* \zs[])}]' ))
457
458
while s: lookForParens (' (\|{\|\[' , ' )\|}\|\]' , ' bW' , 0 ) == lnum
458
459
call cursor (lnum, matchend (s: RemoveTrailingComments (strpart (line ,0 ,col (' .' ))), ' .*\zs[])}]' ))
459
460
endwhile
460
- if line (' .' ) < lnum && ! s: InMultiVarStatement (line (' .' ),0 ,0 )
461
- return indent (s: GetMSL (line (' .' ), 0 ))
461
+ let cur = line (' .' )
462
+ if cur < lnum && ! s: InMultiVarStatement (cur,0 ,0 )
463
+ return indent (s: GetMSL (cur, 0 ))
462
464
end
463
465
elseif counts = ~ ' 1' || s: Onescope (lnum)
464
466
return ind + s: sw ()
@@ -501,64 +503,64 @@ let &cpo = s:cpo_save
501
503
unlet s: cpo_save
502
504
" gq{{{2
503
505
function ! Fixedgq (lnum, count )
504
- let l: tw = &tw ? &tw : 80 ;
506
+ let l: tw = &tw ? &tw : 80 ;
505
507
506
- let l: count = a: count
507
- let l: first_char = indent (a: lnum ) + 1
508
+ let l: count = a: count
509
+ let l: first_char = indent (a: lnum ) + 1
508
510
509
- if mode () == ' i' " gq was not pressed, but tw was set
510
- return 1
511
- endif
511
+ if mode () == ' i' " gq was not pressed, but tw was set
512
+ return 1
513
+ endif
512
514
513
- " This gq is only meant to do code with strings, not comments
514
- if s: IsInComment (a: lnum , l: first_char )
515
- return 1
516
- endif
515
+ " This gq is only meant to do code with strings, not comments
516
+ if s: IsInComment (a: lnum , l: first_char )
517
+ return 1
518
+ endif
517
519
518
- if len (getline (a: lnum )) < l: tw && l: count == 1 " No need for gq
519
- return 1
520
- endif
520
+ if len (getline (a: lnum )) < l: tw && l: count == 1 " No need for gq
521
+ return 1
522
+ endif
521
523
522
- " Put all the lines on one line and do normal spliting after that
523
- if l: count > 1
524
- while l: count > 1
525
- let l: count -= 1
526
- normal J
527
- endwhile
528
- endif
524
+ " Put all the lines on one line and do normal spliting after that
525
+ if l: count > 1
526
+ while l: count > 1
527
+ let l: count -= 1
528
+ normal J
529
+ endwhile
530
+ endif
529
531
530
- let l: winview = winsaveview ()
532
+ let l: winview = winsaveview ()
531
533
532
- call cursor (a: lnum , l: tw + 1 )
533
- let orig_breakpoint = searchpairpos (' ' , ' ' , ' \.' , ' bcW' , ' ' , a: lnum )
534
- call cursor (a: lnum , l: tw + 1 )
535
- let breakpoint = searchpairpos (' ' , ' ' , ' \.' , ' bcW' , s: skip_expr , a: lnum )
534
+ call cursor (a: lnum , l: tw + 1 )
535
+ let orig_breakpoint = searchpairpos (' ' , ' ' , ' \.' , ' bcW' , ' ' , a: lnum )
536
+ call cursor (a: lnum , l: tw + 1 )
537
+ let breakpoint = searchpairpos (' ' , ' ' , ' \.' , ' bcW' , s: skip_expr , a: lnum )
536
538
537
- " No need for special treatment, normal gq handles edgecases better
538
- if breakpoint[1 ] == orig_breakpoint[1 ]
539
- call winrestview (l: winview )
540
- return 1
541
- endif
539
+ " No need for special treatment, normal gq handles edgecases better
540
+ if breakpoint[1 ] == orig_breakpoint[1 ]
541
+ call winrestview (l: winview )
542
+ return 1
543
+ endif
542
544
543
- " Try breaking after string
544
- if breakpoint[1 ] <= indent (a: lnum )
545
- call cursor (a: lnum , l: tw + 1 )
546
- let breakpoint = searchpairpos (' \.' , ' ' , ' ' , ' cW' , s: skip_expr , a: lnum )
547
- endif
545
+ " Try breaking after string
546
+ if breakpoint[1 ] <= indent (a: lnum )
547
+ call cursor (a: lnum , l: tw + 1 )
548
+ let breakpoint = searchpairpos (' \.' , ' ' , ' ' , ' cW' , s: skip_expr , a: lnum )
549
+ endif
548
550
549
551
550
- if breakpoint[1 ] != 0
551
- call feedkeys (" r\<CR> " )
552
- else
553
- let l: count = l: count - 1
554
- endif
552
+ if breakpoint[1 ] != 0
553
+ call feedkeys (" r\<CR> " )
554
+ else
555
+ let l: count = l: count - 1
556
+ endif
555
557
556
- " run gq on new lines
557
- if l: count == 1
558
- call feedkeys (" gqq" )
559
- endif
558
+ " run gq on new lines
559
+ if l: count == 1
560
+ call feedkeys (" gqq" )
561
+ endif
560
562
561
- return 0
563
+ return 0
562
564
endfunction
563
565
" }}}
564
566
" vim: foldmethod = marker:foldlevel = 1
0 commit comments