diff --git a/indent/javascript.vim b/indent/javascript.vim index 975da3ac..3fac0ab4 100644 --- a/indent/javascript.vim +++ b/indent/javascript.vim @@ -41,8 +41,9 @@ endif " 1. Variables {{{1 " ============ -let s:js_keywords = '^\s*\%(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:expr_case = '^\s*\%(case\s\+[^\:]*\|default\)\s*:\s*\C' +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: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' @@ -50,7 +51,7 @@ let s:syng_strcom = '\%(string\|regex\|comment\|template\)\c' let s:syng_string = 'regex\c' " Regex of syntax group names that are strings or documentation. -let s:syng_multiline = 'comment\c' +let s:syng_multiline = '\%(comment\|doc\)\c' " Regex of syntax group names that are line comment. let s:syng_linecom = 'linecomment\c' @@ -61,7 +62,7 @@ let s:skip_expr = "synIDattr(synID(line('.'),col('.'),1),'name') =~ '".s:syng_st let s:line_term = '\s*\%(\%(\/\/.*\)\=\|\%(\/\*.*\*\/\s*\)*\)$' " Regex that defines continuation lines, not including (, {, or [. -let s:continuation_regex = '\%([\\*/.?:]\|+\@\|=>\)\C' . s:line_term @@ -83,11 +84,11 @@ endfunction " Regex that defines blocks. let s:block_regex = '[{([]' . s:line_term -let s:operator_first = '^\s*\%([*.:?]\|\([-/+]\)\1\@!\|||\|&&\)' +let s:operator_first = s:line_pre . '\%([.:?]\|\([-/+*]\)\%(\1\|\*\|\/\)\@!\|||\|&&\)' -let s:var_stmt = '^\s*\%(const\|let\|var\)\s\+\C' +let s:var_stmt = s:line_pre . '\%(const\|let\|var\)\s\+\C' -let s:comma_first = '^\s*,' +let s:comma_first = s:line_pre . ',' let s:comma_last = ',' . s:line_term " 2. Auxiliary Functions {{{1 @@ -121,15 +122,15 @@ function s:PrevNonBlankNonString(lnum) " Go in and out of blocks comments as necessary. " If the line isn't empty (with opt. comment) or in a string, end search. let line = getline(lnum) - if s:IsInMultilineComment(lnum, matchend(line, '/\*') - 1) + if s:IsInMultilineComment(lnum, matchend(line, '^\s*/\*') - 1) && line !~ s:line_pre . '$' if in_block let in_block = 0 else break endif - elseif !in_block && s:IsInMultilineComment(lnum, matchend(line, '\*/') - 1) + elseif !in_block && s:IsInMultilineComment(lnum, match(line, '\*/\s*$') + 1) && line !~ s:line_pre . '$' let in_block = 1 - elseif !in_block && line !~ '^\s*\%(//\).*$' && !(s:IsInStringOrComment(lnum, 1) && s:IsInStringOrComment(lnum, strlen(line))) + elseif !in_block && line !~ s:line_pre . '\%(//\).*$' && !(s:IsInStringOrComment(lnum, 1) && s:IsInStringOrComment(lnum, strlen(line))) break endif let lnum = prevnonblank(lnum - 1) @@ -191,7 +192,7 @@ function s:InMultiVarStatement(lnum, cont, 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*}') || (a:prev && s:Match(a:prev, s:comma_last))) + while lnum > 0 && (s:Match(lnum, s:comma_last) || (a:cont && getline(lnum) =~ s:line_pre . '}') || (a:prev && s:Match(a:prev, s:comma_last))) " if the line is a js keyword if a:cont call cursor(lnum,1) @@ -341,17 +342,18 @@ function GetJavascriptIndent() let prevline = prevnonblank(v:lnum - 1) " to not change multiline string values - if synIDattr(synID(v:lnum, 1, 1), 'name') =~? 'string\|template' && line !~ '^\s*[''"`]' + if synIDattr(synID(v:lnum, 1, 1), 'name') =~? 'string\|template' && line !~ s:line_pre . '[''"`]' return indent(v:lnum) endif " If we are in a multi-line comment, cindent does the right thing. - if s:IsInMultilineComment(v:lnum, 1) && !s:IsLineComment(v:lnum, 1) + if s:IsInMultilineComment(v:lnum, 1) && !s:IsLineComment(v:lnum, 1) && + \ s:IsInMultilineComment(v:lnum, match(line, '\s*$')) return cindent(v:lnum) endif " single opening bracket will assume you want a c style of indenting - if s:Match(v:lnum, '^\s*{' . s:line_term) + if s:Match(v:lnum, s:line_pre . '{' . s:line_term) return cindent(v:lnum) endif @@ -370,7 +372,7 @@ function GetJavascriptIndent() " 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*[],})]') + let col = matchend(line, s:line_pre . '[],})]') if col > 0 && !s:IsInStringOrComment(v:lnum, col) call cursor(v:lnum, col) @@ -489,13 +491,13 @@ function GetJavascriptIndent() if searchpair('(', '', ')', 'bW', s:skip_expr) > 0 return indent(s:GetMSL(line('.'), 0)) end - elseif counts[1] == '2' && !s:Match(lnum, '^\s*}') + elseif counts[1] == '2' && !s:Match(lnum, s:line_pre . '}') call cursor(lnum, 1) " Search for the opening tag if searchpair('{', '', '}', 'bW', s:skip_expr) > 0 return indent(s:GetMSL(line('.'), 0)) end - elseif counts[2] == '2' && !s:Match(lnum, '^\s*]') + elseif counts[2] == '2' && !s:Match(lnum, s:line_pre . ']') call cursor(lnum, 1) " Search for the opening tag if searchpair('\[', '', '\]', 'bW', s:skip_expr) > 0