From df2780953b863de5f066fb1f0e4a53e49c8f809d Mon Sep 17 00:00:00 2001 From: bounceme Date: Thu, 16 Jun 2016 22:45:41 -0700 Subject: [PATCH 1/3] incorrect change in cursor pos some bizarre problem which made me invent a buggy fix. also reindented the file --- indent/javascript.vim | 118 +++++++++++++++++++++--------------------- 1 file changed, 60 insertions(+), 58 deletions(-) diff --git a/indent/javascript.vim b/indent/javascript.vim index 9cb3048b..b96ecda9 100644 --- a/indent/javascript.vim +++ b/indent/javascript.vim @@ -334,7 +334,7 @@ function GetJavascriptIndent() if line !~ '^\%(\/\*\|\s*\/\/\)' && s:IsInComment(v:lnum, 1) return cindent(v:lnum) endif - + " single opening bracket will assume you want a c style of indenting if s:Match(v:lnum, s:line_pre . '{' . s:line_term) && !s:Match(lnum,s:block_regex) && \ !s:Match(lnum,s:comma_last) @@ -429,16 +429,16 @@ function GetJavascriptIndent() return 0 endif -" foo('foo', -" bar('bar', function() { -" hi(); -" }) -" ); + " foo('foo', + " bar('bar', function() { + " hi(); + " }) + " ); -" function (a, b, c, d, -" e, f, g) { -" console.log('inner'); -" } + " function (a, b, c, d, + " e, f, g) { + " console.log('inner'); + " } " If the previous line ended with a block opening, add a level of indent. if s:Match(lnum, s:block_regex) return s:InMultiVarStatement(lnum, 0, 0) || s:LineHasOpeningBrackets(lnum) !~ '2' ? @@ -450,15 +450,17 @@ function GetJavascriptIndent() let ind = indent(lnum) " If the previous line contained an opening bracket, and we are still in it, " add indent depending on the bracket type. - if s:Match(lnum, '\%([[({]\)\|\%([^\t \])}][})\]]\)') + if s:Match(lnum, '[[({})\]]') let counts = s:LineHasOpeningBrackets(lnum) if counts =~ '2' call cursor(lnum,matchend(s:RemoveTrailingComments(line), '.\+\zs[])}]')) while s:lookForParens('(\|{\|\[', ')\|}\|\]', 'bW', 0) == lnum call cursor(lnum, matchend(s:RemoveTrailingComments(strpart(line,0,col('.'))), '.*\zs[])}]')) endwhile - if line('.') < lnum && !s:InMultiVarStatement(line('.'),0,0) - return indent(s:GetMSL(line('.'), 0)) + let cur = line('.') + if cur < lnum && !s:InMultiVarStatement(cur,0,0) + call cursor(v:lnum,1) + return indent(s:GetMSL(cur, 0)) end elseif counts =~ '1' || s:Onescope(lnum) return ind + s:sw() @@ -501,64 +503,64 @@ let &cpo = s:cpo_save unlet s:cpo_save " gq{{{2 function! Fixedgq(lnum, count) - let l:tw = &tw ? &tw : 80; + let l:tw = &tw ? &tw : 80; - let l:count = a:count - let l:first_char = indent(a:lnum) + 1 + let l:count = a:count + let l:first_char = indent(a:lnum) + 1 - if mode() == 'i' " gq was not pressed, but tw was set - return 1 - endif + if mode() == 'i' " gq was not pressed, but tw was set + return 1 + endif - " This gq is only meant to do code with strings, not comments - if s:IsInComment(a:lnum, l:first_char) - return 1 - endif + " This gq is only meant to do code with strings, not comments + if s:IsInComment(a:lnum, l:first_char) + return 1 + endif - if len(getline(a:lnum)) < l:tw && l:count == 1 " No need for gq - return 1 - endif + if len(getline(a:lnum)) < l:tw && l:count == 1 " No need for gq + return 1 + endif - " Put all the lines on one line and do normal spliting after that - if l:count > 1 - while l:count > 1 - let l:count -= 1 - normal J - endwhile - endif + " Put all the lines on one line and do normal spliting after that + if l:count > 1 + while l:count > 1 + let l:count -= 1 + normal J + endwhile + endif - let l:winview = winsaveview() + let l:winview = winsaveview() - call cursor(a:lnum, l:tw + 1) - let orig_breakpoint = searchpairpos(' ', '', '\.', 'bcW', '', a:lnum) - call cursor(a:lnum, l:tw + 1) - let breakpoint = searchpairpos(' ', '', '\.', 'bcW', s:skip_expr, a:lnum) + call cursor(a:lnum, l:tw + 1) + let orig_breakpoint = searchpairpos(' ', '', '\.', 'bcW', '', a:lnum) + call cursor(a:lnum, l:tw + 1) + let breakpoint = searchpairpos(' ', '', '\.', 'bcW', s:skip_expr, a:lnum) - " No need for special treatment, normal gq handles edgecases better - if breakpoint[1] == orig_breakpoint[1] - call winrestview(l:winview) - return 1 - endif + " No need for special treatment, normal gq handles edgecases better + if breakpoint[1] == orig_breakpoint[1] + call winrestview(l:winview) + return 1 + endif - " Try breaking after string - if breakpoint[1] <= indent(a:lnum) - call cursor(a:lnum, l:tw + 1) - let breakpoint = searchpairpos('\.', '', ' ', 'cW', s:skip_expr, a:lnum) - endif + " Try breaking after string + if breakpoint[1] <= indent(a:lnum) + call cursor(a:lnum, l:tw + 1) + let breakpoint = searchpairpos('\.', '', ' ', 'cW', s:skip_expr, a:lnum) + endif - if breakpoint[1] != 0 - call feedkeys("r\") - else - let l:count = l:count - 1 - endif + if breakpoint[1] != 0 + call feedkeys("r\") + else + let l:count = l:count - 1 + endif - " run gq on new lines - if l:count == 1 - call feedkeys("gqq") - endif + " run gq on new lines + if l:count == 1 + call feedkeys("gqq") + endif - return 0 + return 0 endfunction "}}} " vim: foldmethod=marker:foldlevel=1 From 36c996f1b22e93955d3f8e3eb3a87f8bb47e9bdf Mon Sep 17 00:00:00 2001 From: bounceme Date: Thu, 16 Jun 2016 22:56:49 -0700 Subject: [PATCH 2/3] Update javascript.vim --- indent/javascript.vim | 1 - 1 file changed, 1 deletion(-) diff --git a/indent/javascript.vim b/indent/javascript.vim index b96ecda9..c6e1f7e9 100644 --- a/indent/javascript.vim +++ b/indent/javascript.vim @@ -459,7 +459,6 @@ function GetJavascriptIndent() endwhile let cur = line('.') if cur < lnum && !s:InMultiVarStatement(cur,0,0) - call cursor(v:lnum,1) return indent(s:GetMSL(cur, 0)) end elseif counts =~ '1' || s:Onescope(lnum) From 55828eaf50b38d5acff5cb2a2f822b74782b58b8 Mon Sep 17 00:00:00 2001 From: bounceme Date: Fri, 17 Jun 2016 00:01:21 -0700 Subject: [PATCH 3/3] Update javascript.vim --- indent/javascript.vim | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/indent/javascript.vim b/indent/javascript.vim index c6e1f7e9..4b8b1f94 100644 --- a/indent/javascript.vim +++ b/indent/javascript.vim @@ -312,6 +312,7 @@ function GetJavascriptIndent() " 3.1. Setup {{{1 " ---------- " Set up variables for restoring position in file. Could use v:lnum here. + " Avoid use of line('.')/col('.') type functions as the curpos can change let vcol = col('.') " 3.2. Work on the current line {{{1 @@ -453,7 +454,7 @@ function GetJavascriptIndent() if s:Match(lnum, '[[({})\]]') let counts = s:LineHasOpeningBrackets(lnum) if counts =~ '2' - call cursor(lnum,matchend(s:RemoveTrailingComments(line), '.\+\zs[])}]')) + call cursor(lnum,matchend(s:RemoveTrailingComments(line), '.*\zs[])}]')) while s:lookForParens('(\|{\|\[', ')\|}\|\]', 'bW', 0) == lnum call cursor(lnum, matchend(s:RemoveTrailingComments(strpart(line,0,col('.'))), '.*\zs[])}]')) endwhile