Skip to content

Commit 3d34e4f

Browse files
bouncemebounceme
authored and
bounceme
committed
Operator first support,including ternary,dot etc.
generally this will give us fairly accurate indents without having specific code for each operator.Thanks to pangloss#139 (comment) which this is based on
1 parent c8ac1c8 commit 3d34e4f

File tree

1 file changed

+25
-7
lines changed

1 file changed

+25
-7
lines changed

Diff for: indent/javascript.vim

+25-7
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,7 @@ let s:var_stmt = '^\s*(const\|let\|var)'
7676
let s:comma_first = '^\s*,'
7777
let s:comma_last = ',\s*$'
7878

79-
let s:ternary = '^\s\+[?|:]'
80-
let s:ternary_q = '^\s\+?'
79+
let s:operator_first = '^\s*\%([-*/+.:?]\|||\|&&\)'
8180

8281
let s:case_indent = s:sw()
8382
let s:case_indent_after = s:sw()
@@ -146,7 +145,8 @@ function s:GetMSL(lnum, in_one_line_scope)
146145
" Otherwise, terminate search as we have found our MSL already.
147146
let line = getline(lnum)
148147
let col = match(line, s:msl_regex) + 1
149-
if (col > 0 && !s:IsInStringOrComment(lnum, col)) || s:IsInString(lnum, strlen(line))
148+
let colprev = s:Match(msl, s:operator_first)
149+
if ((col > 0 && !s:IsInStringOrComment(lnum, col)) || s:IsInString(lnum, strlen(line)) || colprev)
150150
let msl = lnum
151151
else
152152
" Don't use lines that are part of a one line scope as msl unless the
@@ -378,12 +378,30 @@ function GetJavascriptIndent()
378378
return indent(prevline) + s:case_indent_after
379379
endif
380380

381-
if (line =~ s:ternary)
382-
if (getline(prevline) =~ s:ternary_q)
381+
if (s:Match(v:lnum, s:operator_first))
382+
if (s:Match(prevline, s:operator_first))
383+
" and so does previous line, don't indent
383384
return indent(prevline)
384-
else
385+
end
386+
let counts = s:LineHasOpeningBrackets(prevline)
387+
if counts[0] == '2'
388+
call cursor(prevline, 1)
389+
" Search for the opening tag
390+
let mnum = searchpair('(', '', ')', 'bW', s:skip_expr)
391+
if mnum > 0 && s:Match(mnum, s:operator_first)
392+
return indent(mnum)
393+
end
394+
elseif counts[0] != '1' && counts[1] != '1' && counts[2] != '1'
395+
" otherwise, indent 1 level
385396
return indent(prevline) + s:sw()
386-
endif
397+
end
398+
" If previous line starts with a operator...
399+
elseif (s:Match(prevline, s:operator_first))
400+
let counts = s:LineHasOpeningBrackets(prevline)
401+
if counts[0] != '1' && counts[1] != '1' && counts[2] != '1'
402+
" dedent 1 level
403+
return indent(prevline) - s:sw()
404+
end
387405
endif
388406

389407
" If we are in a multi-line comment, cindent does the right thing.

0 commit comments

Comments
 (0)