Skip to content

Commit b4b3936

Browse files
committed
De-dent after multiline ternary statement
Handles statements like: var test = a ? 'b' : 'c';
1 parent 1d8c267 commit b4b3936

File tree

1 file changed

+7
-0
lines changed

1 file changed

+7
-0
lines changed

indent/javascript.vim

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ let s:comma_last = ',\s*$'
6767

6868
let s:ternary = '^\s\+[?|:]'
6969
let s:ternary_q = '^\s\+?'
70+
let s:ternary_s = '^\s\+:'
7071

7172
let s:case_indent = &sw
7273
let s:case_indent_after = &sw
@@ -367,13 +368,19 @@ function GetJavascriptIndent()
367368
return indent(prevline) + s:case_indent_after
368369
endif
369370

371+
" If in a multi line ternary, indent the next line
370372
if (line =~ s:ternary)
371373
if (getline(prevline) =~ s:ternary_q)
372374
return indent(prevline)
373375
else
374376
return indent(prevline) + &sw
375377
endif
376378
endif
379+
" If the previous line was the last part of a multiline ternary, dedent
380+
if (getline(prevline) =~ s:ternary_s)
381+
return indent(prevline) - &sw
382+
endif
383+
377384

378385
" If we are in a multi-line comment, cindent does the right thing.
379386
if s:IsInMultilineComment(v:lnum, 1) && !s:IsLineComment(v:lnum, 1)

0 commit comments

Comments
 (0)