From b571da74aeba60277ff1503d82d90a7436f82a05 Mon Sep 17 00:00:00 2001 From: bounceme Date: Mon, 30 May 2016 22:11:53 -0700 Subject: [PATCH 1/5] timeout the paren searching the search pair function almost stalls my decently powerful macbook pro on really long lines, could be annoying because of minification etc.this function just makes it backwards compatible in old vim versions or builds without this feature --- indent/javascript.vim | 22 +++++++++++++++------- 1 file changed, 15 insertions(+), 7 deletions(-) diff --git a/indent/javascript.vim b/indent/javascript.vim index 78d5d4e5..52701758 100644 --- a/indent/javascript.vim +++ b/indent/javascript.vim @@ -59,6 +59,14 @@ let s:syng_linecom = 'linecomment\c' " Expression used to check whether we should skip a match with searchpair(). let s:skip_expr = "synIDattr(synID(line('.'),col('.'),1),'name') =~ '".s:syng_strcom."'" +func s:searchpar(start,mid,end,flags,stop) + try + return searchpair(a:start,a:mid,a:end,a:flags,s:skip_expr,a:stop,300) + catch /E118/ + return searchpair(a:start,a:mid,a:end,a:flags,0,a:stop) + endtry +endfunc + let s:line_term = '\s*\%(\%(\/\/.*\)\=\|\%(\/\*.*\*\/\s*\)*\)$' " Regex that defines continuation lines, not including (, {, or [. @@ -73,7 +81,7 @@ function s:Onescope(lnum) let mypos = col('.') call cursor(a:lnum, 1) if search('\<\%(while\|for\|if\)\>\s*(\C', 'ce', a:lnum) > 0 && - \ searchpair('(', '', ')', 'W', s:skip_expr, a:lnum) > 0 && + \ s:searchpar('(', '', ')', 'W', a:lnum) > 0 && \ col('.') == strlen(s:RemoveTrailingComments(getline(a:lnum))) call cursor(a:lnum, mypos) return 1 @@ -153,7 +161,7 @@ function s:GetMSL(lnum, in_one_line_scope) " if there are more closing brackets, continue from the line which has the matching opening bracket elseif col2 > 0 && !s:IsInStringOrComment(msl, col2) && s:LineHasOpeningBrackets(msl)[0] == '2' && !a:in_one_line_scope call cursor(msl, 1) - if searchpair('(', '', ')', 'bW', s:skip_expr) > 0 + if s:searchpar('(', '', ')', 'bW', 0) > 0 let lnum = line('.') let msl = lnum endif @@ -195,7 +203,7 @@ function s:InMultiVarStatement(lnum, cont, prev) " if the line is a js keyword if a:cont call cursor(lnum,1) - let parlnum = searchpair('(\|{\|\[', '', ')\|}\|\]', 'nbW', s:skip_expr) + let parlnum = s:searchpar('(\|{\|\[', '', ')\|}\|\]', 'nbW', 0) if parlnum > 0 let lnum = parlnum end @@ -348,7 +356,7 @@ function GetJavascriptIndent() call cursor(v:lnum, col) - let parlnum = searchpair('(\|{\|\[', '', ')\|}\|\]', 'nbW', s:skip_expr) + let parlnum = s:searchpar('(\|{\|\[', '', ')\|}\|\]', 'nbW', 0) if parlnum > 0 let ind = s:InMultiVarStatement(parlnum, 0, 0) ? indent(parlnum) : indent(s:GetMSL(parlnum, 0)) endif @@ -367,7 +375,7 @@ function GetJavascriptIndent() if counts[0] == '2' || counts[1] == '2' || counts[2] == '2' call cursor(lnum, 1) " Search for the opening tag - let parlnum = searchpair('(\|{\|\[', '', ')\|}\|\]', 'nbW', s:skip_expr) + let parlnum = s:searchpar('(\|{\|\[', '', ')\|}\|\]', 'nbW', 0) if parlnum > 0 && s:Match(parlnum, s:operator_first) return indent(parlnum) end @@ -382,7 +390,7 @@ function GetJavascriptIndent() if counts[0] == '2' && !s:Match(lnum, s:operator_first) call cursor(lnum, 1) " Search for the opening tag - let mnum = searchpair('(', '', ')', 'nbW', s:skip_expr) + let mnum = s:searchpar('(', '', ')', 'nbW', 0) if mnum > 0 && s:Match(mnum, s:operator_first) return indent(mnum) - s:sw() end @@ -432,7 +440,7 @@ function GetJavascriptIndent() \ (counts[2] == '2' && !s:Match(lnum, s:line_pre . ']')) call cursor(lnum, 1) " Search for the opening tag - let parlnum = searchpair('(\|{\|\[', '', ')\|}\|\]', 'nbW', s:skip_expr) + let parlnum = s:searchpar('(\|{\|\[', '', ')\|}\|\]', 'nbW', 0) if parlnum > 0 return indent(s:GetMSL(parlnum, 0)) end From a4aa4c79b79e3c315c0a56577fe1c3041cd07785 Mon Sep 17 00:00:00 2001 From: bounceme Date: Mon, 30 May 2016 22:25:44 -0700 Subject: [PATCH 2/5] Update javascript.vim --- indent/javascript.vim | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/indent/javascript.vim b/indent/javascript.vim index 52701758..973a491b 100644 --- a/indent/javascript.vim +++ b/indent/javascript.vim @@ -59,7 +59,7 @@ let s:syng_linecom = 'linecomment\c' " Expression used to check whether we should skip a match with searchpair(). let s:skip_expr = "synIDattr(synID(line('.'),col('.'),1),'name') =~ '".s:syng_strcom."'" -func s:searchpar(start,mid,end,flags,stop) +func s:lookForParens(start,mid,end,flags,stop) try return searchpair(a:start,a:mid,a:end,a:flags,s:skip_expr,a:stop,300) catch /E118/ @@ -81,7 +81,7 @@ function s:Onescope(lnum) let mypos = col('.') call cursor(a:lnum, 1) if search('\<\%(while\|for\|if\)\>\s*(\C', 'ce', a:lnum) > 0 && - \ s:searchpar('(', '', ')', 'W', a:lnum) > 0 && + \ s:lookForParens('(', '', ')', 'W', a:lnum) > 0 && \ col('.') == strlen(s:RemoveTrailingComments(getline(a:lnum))) call cursor(a:lnum, mypos) return 1 @@ -161,7 +161,7 @@ function s:GetMSL(lnum, in_one_line_scope) " if there are more closing brackets, continue from the line which has the matching opening bracket elseif col2 > 0 && !s:IsInStringOrComment(msl, col2) && s:LineHasOpeningBrackets(msl)[0] == '2' && !a:in_one_line_scope call cursor(msl, 1) - if s:searchpar('(', '', ')', 'bW', 0) > 0 + if s:lookForParens('(', '', ')', 'bW', 0) > 0 let lnum = line('.') let msl = lnum endif @@ -203,7 +203,7 @@ function s:InMultiVarStatement(lnum, cont, prev) " if the line is a js keyword if a:cont call cursor(lnum,1) - let parlnum = s:searchpar('(\|{\|\[', '', ')\|}\|\]', 'nbW', 0) + let parlnum = s:lookForParens('(\|{\|\[', '', ')\|}\|\]', 'nbW', 0) if parlnum > 0 let lnum = parlnum end @@ -356,7 +356,7 @@ function GetJavascriptIndent() call cursor(v:lnum, col) - let parlnum = s:searchpar('(\|{\|\[', '', ')\|}\|\]', 'nbW', 0) + let parlnum = s:lookForParens('(\|{\|\[', '', ')\|}\|\]', 'nbW', 0) if parlnum > 0 let ind = s:InMultiVarStatement(parlnum, 0, 0) ? indent(parlnum) : indent(s:GetMSL(parlnum, 0)) endif @@ -375,7 +375,7 @@ function GetJavascriptIndent() if counts[0] == '2' || counts[1] == '2' || counts[2] == '2' call cursor(lnum, 1) " Search for the opening tag - let parlnum = s:searchpar('(\|{\|\[', '', ')\|}\|\]', 'nbW', 0) + let parlnum = s:lookForParens('(\|{\|\[', '', ')\|}\|\]', 'nbW', 0) if parlnum > 0 && s:Match(parlnum, s:operator_first) return indent(parlnum) end @@ -390,7 +390,7 @@ function GetJavascriptIndent() if counts[0] == '2' && !s:Match(lnum, s:operator_first) call cursor(lnum, 1) " Search for the opening tag - let mnum = s:searchpar('(', '', ')', 'nbW', 0) + let mnum = s:lookForParens('(', '', ')', 'nbW', 0) if mnum > 0 && s:Match(mnum, s:operator_first) return indent(mnum) - s:sw() end @@ -440,7 +440,7 @@ function GetJavascriptIndent() \ (counts[2] == '2' && !s:Match(lnum, s:line_pre . ']')) call cursor(lnum, 1) " Search for the opening tag - let parlnum = s:searchpar('(\|{\|\[', '', ')\|}\|\]', 'nbW', 0) + let parlnum = s:lookForParens('(\|{\|\[', '', ')\|}\|\]', 'nbW', 0) if parlnum > 0 return indent(s:GetMSL(parlnum, 0)) end From dbc613df8928c607542cca4a8dfe497fc8aaf392 Mon Sep 17 00:00:00 2001 From: bounceme Date: Mon, 30 May 2016 22:43:15 -0700 Subject: [PATCH 3/5] Update javascript.vim --- indent/javascript.vim | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/indent/javascript.vim b/indent/javascript.vim index 973a491b..3997b0ad 100644 --- a/indent/javascript.vim +++ b/indent/javascript.vim @@ -59,11 +59,11 @@ let s:syng_linecom = 'linecomment\c' " Expression used to check whether we should skip a match with searchpair(). let s:skip_expr = "synIDattr(synID(line('.'),col('.'),1),'name') =~ '".s:syng_strcom."'" -func s:lookForParens(start,mid,end,flags,stop) +func s:lookForParens(start,end,flags,stop) try - return searchpair(a:start,a:mid,a:end,a:flags,s:skip_expr,a:stop,300) + return searchpair(a:start,'',a:end,a:flags,s:skip_expr,a:stop,300) catch /E118/ - return searchpair(a:start,a:mid,a:end,a:flags,0,a:stop) + return searchpair(a:start,'',a:end,a:flags,0,a:stop) endtry endfunc @@ -81,7 +81,7 @@ function s:Onescope(lnum) let mypos = col('.') call cursor(a:lnum, 1) if search('\<\%(while\|for\|if\)\>\s*(\C', 'ce', a:lnum) > 0 && - \ s:lookForParens('(', '', ')', 'W', a:lnum) > 0 && + \ s:lookForParens('(', ')', 'W', a:lnum) > 0 && \ col('.') == strlen(s:RemoveTrailingComments(getline(a:lnum))) call cursor(a:lnum, mypos) return 1 @@ -161,7 +161,7 @@ function s:GetMSL(lnum, in_one_line_scope) " if there are more closing brackets, continue from the line which has the matching opening bracket elseif col2 > 0 && !s:IsInStringOrComment(msl, col2) && s:LineHasOpeningBrackets(msl)[0] == '2' && !a:in_one_line_scope call cursor(msl, 1) - if s:lookForParens('(', '', ')', 'bW', 0) > 0 + if s:lookForParens('(', ')', 'bW', 0) > 0 let lnum = line('.') let msl = lnum endif @@ -203,7 +203,7 @@ function s:InMultiVarStatement(lnum, cont, prev) " if the line is a js keyword if a:cont call cursor(lnum,1) - let parlnum = s:lookForParens('(\|{\|\[', '', ')\|}\|\]', 'nbW', 0) + let parlnum = s:lookForParens('(\|{\|\[', ')\|}\|\]', 'nbW', 0) if parlnum > 0 let lnum = parlnum end @@ -356,7 +356,7 @@ function GetJavascriptIndent() call cursor(v:lnum, col) - let parlnum = s:lookForParens('(\|{\|\[', '', ')\|}\|\]', 'nbW', 0) + let parlnum = s:lookForParens('(\|{\|\[', ')\|}\|\]', 'nbW', 0) if parlnum > 0 let ind = s:InMultiVarStatement(parlnum, 0, 0) ? indent(parlnum) : indent(s:GetMSL(parlnum, 0)) endif @@ -375,7 +375,7 @@ function GetJavascriptIndent() if counts[0] == '2' || counts[1] == '2' || counts[2] == '2' call cursor(lnum, 1) " Search for the opening tag - let parlnum = s:lookForParens('(\|{\|\[', '', ')\|}\|\]', 'nbW', 0) + let parlnum = s:lookForParens('(\|{\|\[', ')\|}\|\]', 'nbW', 0) if parlnum > 0 && s:Match(parlnum, s:operator_first) return indent(parlnum) end @@ -390,7 +390,7 @@ function GetJavascriptIndent() if counts[0] == '2' && !s:Match(lnum, s:operator_first) call cursor(lnum, 1) " Search for the opening tag - let mnum = s:lookForParens('(', '', ')', 'nbW', 0) + let mnum = s:lookForParens('(', ')', 'nbW', 0) if mnum > 0 && s:Match(mnum, s:operator_first) return indent(mnum) - s:sw() end @@ -440,7 +440,7 @@ function GetJavascriptIndent() \ (counts[2] == '2' && !s:Match(lnum, s:line_pre . ']')) call cursor(lnum, 1) " Search for the opening tag - let parlnum = s:lookForParens('(\|{\|\[', '', ')\|}\|\]', 'nbW', 0) + let parlnum = s:lookForParens('(\|{\|\[', ')\|}\|\]', 'nbW', 0) if parlnum > 0 return indent(s:GetMSL(parlnum, 0)) end From ddcd125e1904163cb4465f09f73292f40e21b430 Mon Sep 17 00:00:00 2001 From: bounceme Date: Tue, 31 May 2016 00:54:06 -0700 Subject: [PATCH 4/5] Update javascript.vim --- indent/javascript.vim | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/indent/javascript.vim b/indent/javascript.vim index 3997b0ad..4be7458b 100644 --- a/indent/javascript.vim +++ b/indent/javascript.vim @@ -42,7 +42,7 @@ endif " ============ let s:line_pre = '^\s*\%(\/\*.*\*\/\s*\)*' -let s:js_keywords = s:line_pre . '\%(break\|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:js_keywords = s:line_pre . '\%(break\|import\|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\|comment\|template\)\c' From 4a5d2238913cc204fc07993ea45d3f6a362ee26d Mon Sep 17 00:00:00 2001 From: bounceme Date: Tue, 31 May 2016 11:32:30 -0700 Subject: [PATCH 5/5] 454 continued --- indent/javascript.vim | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/indent/javascript.vim b/indent/javascript.vim index 4be7458b..85badf8a 100644 --- a/indent/javascript.vim +++ b/indent/javascript.vim @@ -42,7 +42,7 @@ endif " ============ let s:line_pre = '^\s*\%(\/\*.*\*\/\s*\)*' -let s:js_keywords = s:line_pre . '\%(break\|import\|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: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\|comment\|template\)\c' @@ -193,13 +193,14 @@ endfunction " Find if the string is inside var statement (but not the first string) function s:InMultiVarStatement(lnum, cont, prev) let lnum = s:PrevNonBlankNonString(a:lnum - 1) + let prev = a:prev " let type = synIDattr(synID(lnum, indent(lnum) + 1, 0), 'name') " loop through previous expressions to find a var statement while lnum > 0 && (s:Match(lnum, s:comma_last) ||(a:cont && getline(lnum) =~ s:line_pre . '}') || - \ s:Match(lnum,s:continuation_regex)) || (a:prev && (s:Match(a:prev, s:comma_last) || - \ s:Match(a:prev,s:continuation_regex))) + \ s:Match(lnum,s:continuation_regex)) || (prev && (s:Match(prev, s:comma_last) || + \ s:Match(prev,s:continuation_regex))) " if the line is a js keyword if a:cont call cursor(lnum,1) @@ -222,6 +223,7 @@ function s:InMultiVarStatement(lnum, cont, prev) end endif let lnum = s:PrevNonBlankNonString(lnum - 1) + let prev = prev && lnum > 0 ? prev : 0 endwhile " beginning of program, not a var