Skip to content

Commit da43967

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 da43967

File tree

1 file changed

+41
-9
lines changed

1 file changed

+41
-9
lines changed

Diff for: indent/javascript.vim

+41-9
Original file line numberDiff line numberDiff line change
@@ -69,9 +69,11 @@ let s:msl_regex = s:continuation_regex.'\|'.s:expr_case
6969
let s:one_line_scope_regex = '\%(\%(\<else\>\|\<\%(if\|for\|while\)\>\s*(.*)\)\|=>\)' . s:line_term
7070

7171
" Regex that defines blocks.
72-
let s:block_regex = '\%([{[]\)\s*\%(|\%([*@]\=\h\w*,\=\s*\)\%(,\s*[*@]\=\h\w*\)*|\)\=' . s:line_term
72+
let s:block_regex = '\%([{([]\)\s*\%(|\%([*@]\=\h\w*,\=\s*\)\%(,\s*[*@]\=\h\w*\)*|\)\=' . s:line_term
7373

74-
let s:var_stmt = '^\s*(const\|let\|var)'
74+
let s:operator_first = '^\s*\%([-*/+.:?]\|||\|&&\)'
75+
76+
let s:var_stmt = '^\s*\%(const\|let\|var\)'
7577

7678
let s:comma_first = '^\s*,'
7779
let s:comma_last = ',\s*$'
@@ -240,7 +242,7 @@ function s:LineHasOpeningBrackets(lnum)
240242
endif
241243
let pos = match(line, '[][(){}]', pos + 1)
242244
endwhile
243-
return (open_0 > 0) . (open_2 > 0) . (open_4 > 0)
245+
return (open_0 > 0 ? 1 : (open_0 == 0 ? 0 : 2)) . (open_2 > 0) . (open_4 > 0)
244246
endfunction
245247

246248
function s:Match(lnum, regex)
@@ -378,13 +380,31 @@ function GetJavascriptIndent()
378380
return indent(prevline) + s:case_indent_after
379381
endif
380382

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

389409
" If we are in a multi-line comment, cindent does the right thing.
390410
if s:IsInMultilineComment(v:lnum, 1) && !s:IsLineComment(v:lnum, 1)
@@ -448,7 +468,19 @@ function GetJavascriptIndent()
448468
else
449469
call cursor(v:lnum, vcol)
450470
end
451-
endif
471+
elseif line =~ ')' || line =~ s:comma_last
472+
let counts = s:LineHasOpeningBrackets(lnum)
473+
if counts[0] == '2'
474+
call cursor(lnum, 1)
475+
" Search for the opening tag
476+
let mnum = searchpair('(', '', ')', 'bW', s:skip_expr)
477+
if mnum > 0
478+
return indent(s:GetMSL(mnum, 0))
479+
end
480+
elseif line !~ s:var_stmt
481+
return indent(prevline)
482+
end
483+
end
452484

453485
" 3.4. Work on the MSL line. {{{2
454486
" --------------------------

0 commit comments

Comments
 (0)