Skip to content

Commit 3d91f81

Browse files
committed
Merge pull request #250 from utix/develop
Manage case indent
2 parents c378730 + 5cbbdd4 commit 3d91f81

File tree

1 file changed

+36
-8
lines changed

1 file changed

+36
-8
lines changed

indent/javascript.vim

Lines changed: 36 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ setlocal nosmartindent
1616
" Now, set up our indentation expression and keys that trigger it.
1717
setlocal indentexpr=GetJavascriptIndent()
1818
setlocal formatexpr=Fixedgq(v:lnum,v:count)
19-
setlocal indentkeys=0{,0},0),0],0\,,!^F,o,O,e
19+
setlocal indentkeys=0{,0},0),0],0\,:,!^F,o,O,e
2020

2121
" Only define the function once.
2222
if exists("*GetJavascriptIndent")
@@ -29,8 +29,8 @@ set cpo&vim
2929
" 1. Variables {{{1
3030
" ============
3131

32-
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\)'
33-
32+
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\)'
33+
let s:expr_case = '^\s*\(case\s\+[^\:]*\|default\)\s*:\s*'
3434
" Regex of syntax group names that are or delimit string or are comments.
3535
let s:syng_strcom = 'string\|regex\|comment\c'
3636

@@ -53,7 +53,7 @@ let s:continuation_regex = '\%([\\*+/.:]\|\%(<%\)\@<![=-]\|\W[|&?]\|||\|&&\|[^=]
5353

5454
" Regex that defines continuation lines.
5555
" TODO: this needs to deal with if ...: and so on
56-
let s:msl_regex = s:continuation_regex
56+
let s:msl_regex = s:continuation_regex.'|'.s:expr_case
5757

5858
let s:one_line_scope_regex = '\<\%(if\|else\|for\|while\)\>[^{;]*' . s:line_term
5959

@@ -68,6 +68,16 @@ let s:comma_last = ',\s*$'
6868
let s:ternary = '^\s\+[?|:]'
6969
let s:ternary_q = '^\s\+?'
7070

71+
let s:case_indent = &sw
72+
let s:case_indent_after = &sw
73+
let m = matchlist(&cinoptions, ':\(.\)')
74+
if (len(m) > 2)
75+
let s:case_indent = m[1]
76+
endif
77+
let m = matchlist(&cinoptions, '=\(.\)')
78+
if (len(m) > 2)
79+
let s:case_indent_after = m[1]
80+
endif
7181
" 2. Auxiliary Functions {{{1
7282
" ======================
7383

@@ -300,6 +310,17 @@ function GetJavascriptIndent()
300310
" previous nonblank line number
301311
let prevline = prevnonblank(v:lnum - 1)
302312

313+
if (line =~ s:expr_case)
314+
if (getline(prevline) =~ s:expr_case)
315+
return indent(prevline)
316+
else
317+
if (getline(prevline) =~ s:block_regex)
318+
return indent(prevline) + s:case_indent
319+
else
320+
return indent(prevline) - s:case_indent_after
321+
endif
322+
endif
323+
endif
303324
" If we got a closing bracket on an empty line, find its match and indent
304325
" according to it. For parentheses we indent to its column - 1, for the
305326
" others we indent to the containing line's MSL's level. Return -1 if fail.
@@ -342,6 +363,9 @@ function GetJavascriptIndent()
342363
if (getline(prevline) =~ s:comma_first)
343364
return indent(prevline) - &sw
344365
endif
366+
if (getline(prevline) =~ s:expr_case)
367+
return indent(prevline) + s:case_indent_after
368+
endif
345369

346370
if (line =~ s:ternary)
347371
if (getline(prevline) =~ s:ternary_q)
@@ -385,15 +409,19 @@ function GetJavascriptIndent()
385409
return 0
386410
endif
387411

388-
" Set up variables for current line.
389-
let line = getline(lnum)
390-
let ind = indent(lnum)
391412

392413
" If the previous line ended with a block opening, add a level of indent.
393414
if s:Match(lnum, s:block_regex)
394-
return indent(s:GetMSL(lnum, 0)) + &sw
415+
if (line =~ s:expr_case)
416+
return indent(s:GetMSL(lnum, 0)) + &sw/2
417+
else
418+
return indent(s:GetMSL(lnum, 0)) + &sw
419+
endif
395420
endif
396421

422+
" Set up variables for current line.
423+
let line = getline(lnum)
424+
let ind = indent(lnum)
397425
" If the previous line contained an opening bracket, and we are still in it,
398426
" add indent depending on the bracket type.
399427
if line =~ '[[({]'

0 commit comments

Comments
 (0)