From b5841cd3d5352411da4eab2b5b671e5f1482d5b3 Mon Sep 17 00:00:00 2001 From: bounceme Date: Mon, 13 Jun 2016 21:51:53 -0700 Subject: [PATCH 1/8] recursive matching https://github.com/pangloss/vim-javascript/issues/469#issuecomment-225765624 --- indent/javascript.vim | 27 +++++++++++++++++---------- 1 file changed, 17 insertions(+), 10 deletions(-) diff --git a/indent/javascript.vim b/indent/javascript.vim index 84d69690..a647d8d3 100644 --- a/indent/javascript.vim +++ b/indent/javascript.vim @@ -347,15 +347,21 @@ function GetJavascriptIndent() endif " If we got a closing bracket on an empty line, find its match and indent - " according to it. For parentheses we indent to its column - 1, for the - " others we indent to the containing line's MSL's level. Return -1 if fail. - let col = matchend(line, s:line_pre . '[]})]') - if col > 0 && !s:IsInStringOrComment(v:lnum, col) - call cursor(v:lnum, col) - let parlnum = s:lookForParens('(\|{\|\[', ')\|}\|\]', 'nbW', 0) - if parlnum > 0 - let ind = s:InMultiVarStatement(parlnum, 0, 0) ? indent(parlnum) : indent(s:GetMSL(parlnum, 0)) - endif + " according to it. + let col = s:Match(v:lnum, s:line_pre . '[]})]') + if col > 0 + let parlnum = v:lnum + while col + call cursor(parlnum, 1) + let parlnum = s:lookForParens('(\|{\|\[', ')\|}\|\]', 'nbW', 0) + let col = s:Match(parlnum, s:line_pre . '[]})]') + if col + continue + end + if parlnum > 0 + let ind = s:InMultiVarStatement(parlnum, 0, 0) ? indent(parlnum) : indent(s:GetMSL(parlnum, 0)) + endif + endwhile return ind endif @@ -424,7 +430,8 @@ function GetJavascriptIndent() " 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) ? indent(lnum) + s:sw() : indent(s:GetMSL(lnum, 0)) + s:sw() + return s:InMultiVarStatement(lnum, 0, 0) || s:Match(lnum, s:line_pre . '[]})]') ? + \ indent(lnum) + s:sw() : indent(s:GetMSL(lnum, 0)) + s:sw() endif " Set up variables for current line. From d49ec44477b3c07287c88d8e90f0f3c72d8abe79 Mon Sep 17 00:00:00 2001 From: bounceme Date: Mon, 13 Jun 2016 22:18:32 -0700 Subject: [PATCH 2/8] Update javascript.vim --- indent/javascript.vim | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/indent/javascript.vim b/indent/javascript.vim index a647d8d3..0f45da93 100644 --- a/indent/javascript.vim +++ b/indent/javascript.vim @@ -442,9 +442,9 @@ function GetJavascriptIndent() if s:Match(lnum, '[[({})\]]') let counts = s:LineHasOpeningBrackets(lnum) if counts =~ '2' - call cursor(lnum, 1) + call cursor(lnum,match(s:RemoveTrailingComments(line), '.*\zs[])}]') + 1) " Search for the opening tag - let parlnum = s:lookForParens('(\|{\|\[', ')\|}\|\]', 'nbW', 0) + let parlnum = s:lookForParens('(\|{\|\[', ')\|}\|\]', 'nbWz', 0) if parlnum > 0 && !s:InMultiVarStatement(parlnum,0,0) return indent(s:GetMSL(parlnum, 0)) end From 93e5c4d40026dae261566e9711f5ad5416f03a7e Mon Sep 17 00:00:00 2001 From: bounceme Date: Tue, 14 Jun 2016 09:36:37 -0700 Subject: [PATCH 3/8] Update javascript.vim recursion is unnecessary --- indent/javascript.vim | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/indent/javascript.vim b/indent/javascript.vim index 0f45da93..d0bebb05 100644 --- a/indent/javascript.vim +++ b/indent/javascript.vim @@ -443,10 +443,11 @@ function GetJavascriptIndent() let counts = s:LineHasOpeningBrackets(lnum) if counts =~ '2' call cursor(lnum,match(s:RemoveTrailingComments(line), '.*\zs[])}]') + 1) - " Search for the opening tag - let parlnum = s:lookForParens('(\|{\|\[', ')\|}\|\]', 'nbWz', 0) - if parlnum > 0 && !s:InMultiVarStatement(parlnum,0,0) - return indent(s:GetMSL(parlnum, 0)) + while s:lookForParens('(\|{\|\[', ')\|}\|\]', 'bWz', 0) == lnum + call cursor(lnum, match(s:RemoveTrailingComments(strpart(line,0,col('.'))), '.*\zs[])}]') + 1) + endwhile + if line('.') > 0 && !s:InMultiVarStatement(line('.'),0,0) + return indent(s:GetMSL(line('.'), 0)) end elseif counts =~ '1' || s:Onescope(lnum) return ind + s:sw() From 41b9c954142d169922015dba9f7f196471cba788 Mon Sep 17 00:00:00 2001 From: bounceme Date: Tue, 14 Jun 2016 12:34:34 -0700 Subject: [PATCH 4/8] Update javascript.vim --- indent/javascript.vim | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/indent/javascript.vim b/indent/javascript.vim index d0bebb05..b70c8e5a 100644 --- a/indent/javascript.vim +++ b/indent/javascript.vim @@ -147,8 +147,8 @@ function s:GetMSL(lnum, in_one_line_scope) let line2 = getline(msl) if ((s:Match(lnum,s:continuation_regex) || s:Match(lnum, s:comma_last)) && \ !s:Match(lnum, s:expr_case)) || s:IsInString(lnum, strlen(line)) - let msl = lnum - if s:Match(lnum, s:line_pre . '[]})]') && !a:in_one_line_scope + let msl = s:LineHasOpeningBrackets(lnum) =~ '1' ? msl : lnum + if s:Match(lnum, '.*\zs[])}]') && !a:in_one_line_scope call cursor(lnum,1) let parlnum = s:lookForParens('(\|{\|\[', ')\|}\|\]', 'nbW', 0) if parlnum > 0 @@ -442,18 +442,17 @@ function GetJavascriptIndent() if s:Match(lnum, '[[({})\]]') let counts = s:LineHasOpeningBrackets(lnum) if counts =~ '2' - call cursor(lnum,match(s:RemoveTrailingComments(line), '.*\zs[])}]') + 1) - while s:lookForParens('(\|{\|\[', ')\|}\|\]', 'bWz', 0) == lnum - call cursor(lnum, match(s:RemoveTrailingComments(strpart(line,0,col('.'))), '.*\zs[])}]') + 1) + 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('.') > 0 && !s:InMultiVarStatement(line('.'),0,0) + if line('.') < lnum && !s:InMultiVarStatement(line('.'),0,0) return indent(s:GetMSL(line('.'), 0)) end elseif counts =~ '1' || s:Onescope(lnum) return ind + s:sw() - else - call cursor(v:lnum, vcol) end + call cursor(v:lnum, vcol) end " 3.4. Work on the MSL line. {{{1 From 15da60ba0e0fb2c376d31ec8ab741fe36b3eb878 Mon Sep 17 00:00:00 2001 From: bounceme Date: Tue, 14 Jun 2016 13:06:38 -0700 Subject: [PATCH 5/8] Update javascript.vim --- indent/javascript.vim | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/indent/javascript.vim b/indent/javascript.vim index b70c8e5a..db1bcd18 100644 --- a/indent/javascript.vim +++ b/indent/javascript.vim @@ -147,10 +147,10 @@ function s:GetMSL(lnum, in_one_line_scope) let line2 = getline(msl) if ((s:Match(lnum,s:continuation_regex) || s:Match(lnum, s:comma_last)) && \ !s:Match(lnum, s:expr_case)) || s:IsInString(lnum, strlen(line)) - let msl = s:LineHasOpeningBrackets(lnum) =~ '1' ? msl : lnum - if s:Match(lnum, '.*\zs[])}]') && !a:in_one_line_scope + let msl = lnum + if s:Match(lnum, '[^{([]*\zs[])}]') && !a:in_one_line_scope call cursor(lnum,1) - let parlnum = s:lookForParens('(\|{\|\[', ')\|}\|\]', 'nbW', 0) + let parlnum = s:lookForParens('(\|{\|\[', ')\|}\|\]', 'nbWz', 0) if parlnum > 0 let lnum = parlnum continue From 8187f71a264d6da0a1ca8a7b74e5aea7028e7d8f Mon Sep 17 00:00:00 2001 From: bounceme Date: Tue, 14 Jun 2016 17:28:55 -0700 Subject: [PATCH 6/8] working version I'm kind of wary about this being finalized, but from what i see it is working well --- indent/javascript.vim | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/indent/javascript.vim b/indent/javascript.vim index db1bcd18..ee3bc0ae 100644 --- a/indent/javascript.vim +++ b/indent/javascript.vim @@ -45,7 +45,7 @@ let s:line_pre = '^\s*\%(\/\*.*\*\/\s*\)*' let s:js_keywords = s:line_pre . '\%(break\|import\|export\|catch\|const\|continue\|debugger\|delete\|do\|else\|finally\|for\|function\|if\|in\|instanceof\|let\|new\|return\|switch\|this\|throw\|try\|typeof\|var\|void\|while\|with\)\>\C' let s:expr_case = s:line_pre . '\%(case\s\+[^\:]*\|default\)\s*:\s*\C' " Regex of syntax group names that are or delimit string or are comments. -let s:syng_strcom = '\%(string\|regex\|special\|comment\|template\)\c' +let s:syng_strcom = '\%(string\|regex\|special\|doc\|comment\|template\)\c' " Regex of syntax group names that are strings. let s:syng_string = 'regex\c' @@ -150,7 +150,7 @@ function s:GetMSL(lnum, in_one_line_scope) let msl = lnum if s:Match(lnum, '[^{([]*\zs[])}]') && !a:in_one_line_scope call cursor(lnum,1) - let parlnum = s:lookForParens('(\|{\|\[', ')\|}\|\]', 'nbWz', 0) + let parlnum = s:lookForParens('(\|{\|\[', ')\|}\|\]', 'nbW', 0) if parlnum > 0 let lnum = parlnum continue @@ -442,12 +442,12 @@ 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 if line('.') < lnum && !s:InMultiVarStatement(line('.'),0,0) - return indent(s:GetMSL(line('.'), 0)) + return indent(s:GetMSL(line('.'), 0)) end elseif counts =~ '1' || s:Onescope(lnum) return ind + s:sw() From 423715391184e03d24774512b875ba167c771bb4 Mon Sep 17 00:00:00 2001 From: bounceme Date: Tue, 14 Jun 2016 22:45:30 -0700 Subject: [PATCH 7/8] Update javascript.vim --- indent/javascript.vim | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/indent/javascript.vim b/indent/javascript.vim index ee3bc0ae..56d6560a 100644 --- a/indent/javascript.vim +++ b/indent/javascript.vim @@ -120,10 +120,10 @@ function s:PrevNonBlankNonString(lnum) let lnum = prevnonblank(a:lnum) while lnum > 0 let line = getline(lnum) - let com = match(line, '\%(\/\*.*\)\@ 0 let lnum = parlnum end @@ -147,8 +147,8 @@ function s:GetMSL(lnum, in_one_line_scope) let line2 = getline(msl) if ((s:Match(lnum,s:continuation_regex) || s:Match(lnum, s:comma_last)) && \ !s:Match(lnum, s:expr_case)) || s:IsInString(lnum, strlen(line)) - let msl = lnum - if s:Match(lnum, '[^{([]*\zs[])}]') && !a:in_one_line_scope + let msl = s:LineHasOpeningBrackets(lnum) =~ '1' && (!s:Match(lnum,s:comma_last) && !s:Match(lnum, s:continuation_regex)) ? msl : lnum + if s:Match(lnum, s:line_pre . '[]})]') && !a:in_one_line_scope call cursor(lnum,1) let parlnum = s:lookForParens('(\|{\|\[', ')\|}\|\]', 'nbW', 0) if parlnum > 0 @@ -258,7 +258,7 @@ function s:IndentWithContinuation(lnum, ind, width) " If the previous line wasn't a MSL and is continuation return its indent. " TODO: the || s:IsInString() thing worries me a bit. if p_lnum != lnum - if s:Match(p_lnum,s:continuation_regex)||s:IsInString(p_lnum,strlen(line)) + if s:Match(p_lnum,s:continuation_regex)||s:IsInString(p_lnum,strlen(line)) return a:ind endif endif @@ -439,7 +439,7 @@ 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, '[[({})\]]') + if s:Match(lnum, '\%([[({]\)\|\%([^\t \])}][})\]]\)') let counts = s:LineHasOpeningBrackets(lnum) if counts =~ '2' call cursor(lnum,matchend(s:RemoveTrailingComments(line), '.\+\zs[])}]')) @@ -452,7 +452,6 @@ function GetJavascriptIndent() elseif counts =~ '1' || s:Onescope(lnum) return ind + s:sw() end - call cursor(v:lnum, vcol) end " 3.4. Work on the MSL line. {{{1 From 97b6f8bb87affa1b94d9bc7d6d07189aaa35ceae Mon Sep 17 00:00:00 2001 From: bounceme Date: Wed, 15 Jun 2016 00:21:22 -0700 Subject: [PATCH 8/8] possibly finished! --- indent/javascript.vim | 24 +++++++++++++++++------- 1 file changed, 17 insertions(+), 7 deletions(-) diff --git a/indent/javascript.vim b/indent/javascript.vim index 56d6560a..806f079b 100644 --- a/indent/javascript.vim +++ b/indent/javascript.vim @@ -120,10 +120,10 @@ function s:PrevNonBlankNonString(lnum) let lnum = prevnonblank(a:lnum) while lnum > 0 let line = getline(lnum) - let com = match(line, '\%(\/\*.*\)\@ 0 let lnum = parlnum end @@ -147,7 +147,7 @@ function s:GetMSL(lnum, in_one_line_scope) let line2 = getline(msl) if ((s:Match(lnum,s:continuation_regex) || s:Match(lnum, s:comma_last)) && \ !s:Match(lnum, s:expr_case)) || s:IsInString(lnum, strlen(line)) - let msl = s:LineHasOpeningBrackets(lnum) =~ '1' && (!s:Match(lnum,s:comma_last) && !s:Match(lnum, s:continuation_regex)) ? msl : lnum + let msl = lnum if s:Match(lnum, s:line_pre . '[]})]') && !a:in_one_line_scope call cursor(lnum,1) let parlnum = s:lookForParens('(\|{\|\[', ')\|}\|\]', 'nbW', 0) @@ -258,7 +258,7 @@ function s:IndentWithContinuation(lnum, ind, width) " If the previous line wasn't a MSL and is continuation return its indent. " TODO: the || s:IsInString() thing worries me a bit. if p_lnum != lnum - if s:Match(p_lnum,s:continuation_regex)||s:IsInString(p_lnum,strlen(line)) + if s:Match(p_lnum,s:continuation_regex)||s:IsInString(p_lnum,strlen(line)) return a:ind endif endif @@ -359,7 +359,8 @@ function GetJavascriptIndent() continue end if parlnum > 0 - let ind = s:InMultiVarStatement(parlnum, 0, 0) ? indent(parlnum) : indent(s:GetMSL(parlnum, 0)) + let ind = s:InMultiVarStatement(parlnum, 0, 0)|| s:LineHasOpeningBrackets(parlnum) !~ '2' + \ ? indent(parlnum) : indent(s:GetMSL(parlnum, 0)) endif endwhile return ind @@ -368,7 +369,7 @@ function GetJavascriptIndent() " If line starts with an operator... if (line =~ s:operator_first) - if (s:Match(lnum, s:operator_first)) + if (s:Match(lnum, s:operator_first) || s:Match(lnum, s:line_pre . '[])}]')) " and so does previous line, don't indent return indent(lnum) end @@ -427,10 +428,19 @@ function GetJavascriptIndent() return 0 endif +" foo('foo', +" bar('bar', function() { +" hi(); +" }) +" ); +" 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:Match(lnum, s:line_pre . '[]})]') ? + return s:InMultiVarStatement(lnum, 0, 0) || s:LineHasOpeningBrackets(lnum) !~ '2' ? \ indent(lnum) + s:sw() : indent(s:GetMSL(lnum, 0)) + s:sw() endif