@@ -16,7 +16,7 @@ setlocal nosmartindent
16
16
" Now, set up our indentation expression and keys that trigger it.
17
17
setlocal indentexpr = GetJavascriptIndent ()
18
18
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
20
20
21
21
" Only define the function once.
22
22
if exists (" *GetJavascriptIndent" )
@@ -29,8 +29,8 @@ set cpo&vim
29
29
" 1. Variables {{{1
30
30
" ============
31
31
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* '
34
34
" Regex of syntax group names that are or delimit string or are comments.
35
35
let s: syng_strcom = ' string\|regex\|comment\c'
36
36
@@ -53,7 +53,7 @@ let s:continuation_regex = '\%([\\*+/.:]\|\%(<%\)\@<![=-]\|\W[|&?]\|||\|&&\|[^=]
53
53
54
54
" Regex that defines continuation lines.
55
55
" 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
57
57
58
58
let s: one_line_scope_regex = ' \<\%(if\|else\|for\|while\)\>[^{;]*' . s: line_term
59
59
@@ -68,6 +68,16 @@ let s:comma_last = ',\s*$'
68
68
let s: ternary = ' ^\s\+[?|:]'
69
69
let s: ternary_q = ' ^\s\+?'
70
70
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
71
81
" 2. Auxiliary Functions {{{1
72
82
" ======================
73
83
@@ -300,6 +310,17 @@ function GetJavascriptIndent()
300
310
" previous nonblank line number
301
311
let prevline = prevnonblank (v: lnum - 1 )
302
312
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
303
324
" If we got a closing bracket on an empty line, find its match and indent
304
325
" according to it. For parentheses we indent to its column - 1, for the
305
326
" others we indent to the containing line's MSL's level. Return -1 if fail.
@@ -342,6 +363,9 @@ function GetJavascriptIndent()
342
363
if (getline (prevline) = ~ s: comma_first )
343
364
return indent (prevline) - &sw
344
365
endif
366
+ if (getline (prevline) = ~ s: expr_case )
367
+ return indent (prevline) + s: case_indent_after
368
+ endif
345
369
346
370
if (line = ~ s: ternary )
347
371
if (getline (prevline) = ~ s: ternary_q )
@@ -385,15 +409,19 @@ function GetJavascriptIndent()
385
409
return 0
386
410
endif
387
411
388
- " Set up variables for current line.
389
- let line = getline (lnum)
390
- let ind = indent (lnum)
391
412
392
413
" If the previous line ended with a block opening, add a level of indent.
393
414
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
395
420
endif
396
421
422
+ " Set up variables for current line.
423
+ let line = getline (lnum)
424
+ let ind = indent (lnum)
397
425
" If the previous line contained an opening bracket, and we are still in it,
398
426
" add indent depending on the bracket type.
399
427
if line = ~ ' [[({]'
0 commit comments