Skip to content

Commit 487e0c6

Browse files
committed
Manage case indent
* Multi case * half indent for case * indent when typing : in case case is now use as a block delimiter Before: switch(name) { case 1: break; case 2: a = b; foo(); bar(); break; case 4: case 3: if (true) { foo(); } bar(); c = d; break; default: foo(); break; } After: switch(name) { case 1: break; case 2: a = b; foo(); bar(); break; case 4: case 3: if (true) { foo(); } bar(); c = d; break; default: foo(); break; }
1 parent c378730 commit 487e0c6

File tree

1 file changed

+26
-8
lines changed

1 file changed

+26
-8
lines changed

indent/javascript.vim

Lines changed: 26 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

@@ -300,6 +300,17 @@ function GetJavascriptIndent()
300300
" previous nonblank line number
301301
let prevline = prevnonblank(v:lnum - 1)
302302

303+
if (line =~ s:expr_case)
304+
if (getline(prevline) =~ s:expr_case)
305+
return indent(prevline)
306+
else
307+
if (getline(prevline) =~ s:block_regex)
308+
return indent(prevline) + &sw/2
309+
else
310+
return indent(prevline) - &sw/2
311+
endif
312+
endif
313+
endif
303314
" If we got a closing bracket on an empty line, find its match and indent
304315
" according to it. For parentheses we indent to its column - 1, for the
305316
" others we indent to the containing line's MSL's level. Return -1 if fail.
@@ -342,6 +353,9 @@ function GetJavascriptIndent()
342353
if (getline(prevline) =~ s:comma_first)
343354
return indent(prevline) - &sw
344355
endif
356+
if (getline(prevline) =~ s:expr_case)
357+
return indent(prevline) + &sw/2
358+
endif
345359

346360
if (line =~ s:ternary)
347361
if (getline(prevline) =~ s:ternary_q)
@@ -385,15 +399,19 @@ function GetJavascriptIndent()
385399
return 0
386400
endif
387401

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

392403
" If the previous line ended with a block opening, add a level of indent.
393404
if s:Match(lnum, s:block_regex)
394-
return indent(s:GetMSL(lnum, 0)) + &sw
405+
if (line =~ s:expr_case)
406+
return indent(s:GetMSL(lnum, 0)) + &sw/2
407+
else
408+
return indent(s:GetMSL(lnum, 0)) + &sw
409+
endif
395410
endif
396411

412+
" Set up variables for current line.
413+
let line = getline(lnum)
414+
let ind = indent(lnum)
397415
" If the previous line contained an opening bracket, and we are still in it,
398416
" add indent depending on the bracket type.
399417
if line =~ '[[({]'

0 commit comments

Comments
 (0)