Skip to content

Commit 2658883

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 Update javascript.vim
1 parent c8ac1c8 commit 2658883

File tree

1 file changed

+27
-8
lines changed

1 file changed

+27
-8
lines changed

Diff for: indent/javascript.vim

+27-8
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
@@ -377,13 +377,32 @@ function GetJavascriptIndent()
377377
if (getline(prevline) =~ s:expr_case)
378378
return indent(prevline) + s:case_indent_after
379379
endif
380-
381-
if (line =~ s:ternary)
382-
if (getline(prevline) =~ s:ternary_q)
380+
381+
" if the line starts with an operator
382+
if (s:Match(v:lnum, s:operator_first))
383+
if (s:Match(prevline, s:operator_first))
384+
" and so does previous line, don't indent
383385
return indent(prevline)
384-
else
386+
end
387+
let counts = s:LineHasOpeningBrackets(prevline)
388+
if counts[0] == '2'
389+
call cursor(prevline, 1)
390+
" Search for the opening tag
391+
let mnum = searchpair('(', '', ')', 'bW', s:skip_expr)
392+
if mnum > 0 && s:Match(mnum, s:operator_first)
393+
return indent(mnum)
394+
end
395+
elseif counts[0] != '1' && counts[1] != '1' && counts[2] != '1'
396+
" otherwise, indent 1 level
385397
return indent(prevline) + s:sw()
386-
endif
398+
end
399+
" If previous line starts with a operator...
400+
elseif s:Match(prevline, s:operator_first) || s:Match(v:lnum, '^\s*[])}]')
401+
let countscur = s:LineHasOpeningBrackets(v:lnum)
402+
let counts = s:LineHasOpeningBrackets(prevline)
403+
if (counts[0] != '1' && counts[1] != '1' || counts[2] != '1') && countscur[0] != '2'
404+
return indent(prevline) - s:sw()
405+
end
387406
endif
388407

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

0 commit comments

Comments
 (0)