diff --git a/indent/javascript.vim b/indent/javascript.vim index e30580c3..0f526db9 100644 --- a/indent/javascript.vim +++ b/indent/javascript.vim @@ -16,7 +16,7 @@ setlocal nosmartindent " Now, set up our indentation expression and keys that trigger it. setlocal indentexpr=GetJavascriptIndent() setlocal formatexpr=Fixedgq(v:lnum,v:count) -setlocal indentkeys=0{,0},0),0],0\,,!^F,o,O,e +setlocal indentkeys=0{,0},0),0],0\,:,!^F,o,O,e " Only define the function once. if exists("*GetJavascriptIndent") @@ -29,8 +29,8 @@ set cpo&vim " 1. Variables {{{1 " ============ -let s:js_keywords = '^\s*\(break\|case\|catch\|const\|continue\|debugger\|default\|delete\|do\|else\|finally\|for\|function\|if\|in\|instanceof\|let\|new\|return\|switch\|this\|throw\|try\|typeof\|var\|void\|while\|with\)' - +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\)' +let s:expr_case = '^\s*\(case\s\+[^\:]*\|default\)\s*:\s*' " Regex of syntax group names that are or delimit string or are comments. let s:syng_strcom = 'string\|regex\|comment\c' @@ -53,7 +53,7 @@ let s:continuation_regex = '\%([\\*+/.:]\|\%(<%\)\@[^{;]*' . s:line_term @@ -68,6 +68,16 @@ let s:comma_last = ',\s*$' let s:ternary = '^\s\+[?|:]' let s:ternary_q = '^\s\+?' +let s:case_indent = &sw +let s:case_indent_after = &sw +let m = matchlist(&cinoptions, ':\(.\)') +if (len(m) > 2) + let s:case_indent = m[1] +endif +let m = matchlist(&cinoptions, '=\(.\)') +if (len(m) > 2) + let s:case_indent_after = m[1] +endif " 2. Auxiliary Functions {{{1 " ====================== @@ -300,6 +310,17 @@ function GetJavascriptIndent() " previous nonblank line number let prevline = prevnonblank(v:lnum - 1) + if (line =~ s:expr_case) + if (getline(prevline) =~ s:expr_case) + return indent(prevline) + else + if (getline(prevline) =~ s:block_regex) + return indent(prevline) + s:case_indent + else + return indent(prevline) - s:case_indent_after + endif + endif + 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. @@ -342,6 +363,9 @@ function GetJavascriptIndent() if (getline(prevline) =~ s:comma_first) return indent(prevline) - &sw endif + if (getline(prevline) =~ s:expr_case) + return indent(prevline) + s:case_indent_after + endif if (line =~ s:ternary) if (getline(prevline) =~ s:ternary_q) @@ -385,15 +409,19 @@ function GetJavascriptIndent() return 0 endif - " Set up variables for current line. - let line = getline(lnum) - let ind = indent(lnum) " If the previous line ended with a block opening, add a level of indent. if s:Match(lnum, s:block_regex) - return indent(s:GetMSL(lnum, 0)) + &sw + if (line =~ s:expr_case) + return indent(s:GetMSL(lnum, 0)) + &sw/2 + else + return indent(s:GetMSL(lnum, 0)) + &sw + endif endif + " Set up variables for current line. + let line = getline(lnum) + 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 line =~ '[[({]'