Skip to content

Commit 52e8f90

Browse files
authored
incorrect change in cursor pos (#489)
* incorrect change in cursor pos some bizarre problem which made me invent a buggy fix. also reindented the file
1 parent ea58033 commit 52e8f90

File tree

1 file changed

+61
-59
lines changed

1 file changed

+61
-59
lines changed

indent/javascript.vim

+61-59
Original file line numberDiff line numberDiff line change
@@ -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)
@@ -429,16 +430,16 @@ function GetJavascriptIndent()
429430
return 0
430431
endif
431432

432-
" foo('foo',
433-
" bar('bar', function() {
434-
" hi();
435-
" })
436-
" );
433+
" foo('foo',
434+
" bar('bar', function() {
435+
" hi();
436+
" })
437+
" );
437438

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+
" }
442443
" If the previous line ended with a block opening, add a level of indent.
443444
if s:Match(lnum, s:block_regex)
444445
return s:InMultiVarStatement(lnum, 0, 0) || s:LineHasOpeningBrackets(lnum) !~ '2' ?
@@ -450,15 +451,16 @@ function GetJavascriptIndent()
450451
let ind = indent(lnum)
451452
" If the previous line contained an opening bracket, and we are still in it,
452453
" add indent depending on the bracket type.
453-
if s:Match(lnum, '\%([[({]\)\|\%([^\t \])}][})\]]\)')
454+
if s:Match(lnum, '[[({})\]]')
454455
let counts = s:LineHasOpeningBrackets(lnum)
455456
if counts =~ '2'
456-
call cursor(lnum,matchend(s:RemoveTrailingComments(line), '.\+\zs[])}]'))
457+
call cursor(lnum,matchend(s:RemoveTrailingComments(line), '.*\zs[])}]'))
457458
while s:lookForParens('(\|{\|\[', ')\|}\|\]', 'bW', 0) == lnum
458459
call cursor(lnum, matchend(s:RemoveTrailingComments(strpart(line,0,col('.'))), '.*\zs[])}]'))
459460
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))
462464
end
463465
elseif counts =~ '1' || s:Onescope(lnum)
464466
return ind + s:sw()
@@ -501,64 +503,64 @@ let &cpo = s:cpo_save
501503
unlet s:cpo_save
502504
" gq{{{2
503505
function! Fixedgq(lnum, count)
504-
let l:tw = &tw ? &tw : 80;
506+
let l:tw = &tw ? &tw : 80;
505507

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
508510

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
512514

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
517519

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
521523

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
529531

530-
let l:winview = winsaveview()
532+
let l:winview = winsaveview()
531533

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)
536538

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
542544

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
548550

549551

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
555557

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
560562

561-
return 0
563+
return 0
562564
endfunction
563565
"}}}
564566
" vim: foldmethod=marker:foldlevel=1

0 commit comments

Comments
 (0)