Skip to content

Commit 6392e67

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 6392e67

File tree

1 file changed

+43
-9
lines changed

1 file changed

+43
-9
lines changed

Diff for: indent/javascript.vim

+43-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,33 @@ 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+
384+
" If line starts with operator...
385+
386+
if (s:Match(v:lnum, s:operator_first))
387+
if (s:Match(prevline, s:operator_first))
388+
" and so does previous line, don't indent
383389
return indent(prevline)
384-
else
390+
end
391+
let counts = s:LineHasOpeningBrackets(prevline)
392+
if counts[0] == '2'
393+
call cursor(prevline, 1)
394+
" Search for the opening tag
395+
let mnum = searchpair('(', '', ')', 'bW', s:skip_expr)
396+
if mnum > 0 && s:Match(mnum, s:operator_first)
397+
return indent(mnum)
398+
end
399+
elseif counts[0] != '1' && counts[1] != '1' && counts[2] != '1'
400+
" otherwise, indent 1 level
385401
return indent(prevline) + s:sw()
386-
endif
387-
endif
402+
end
403+
" If previous line starts with a operator...
404+
elseif s:Match(prevline, s:operator_first) && !s:Match(prevline, s:comma_last)
405+
let countscur = s:LineHasOpeningBrackets(v:lnum)
406+
if countscur[0] != '2'
407+
return indent(prevline) - s:sw()
408+
end
409+
end
388410

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

453487
" 3.4. Work on the MSL line. {{{2
454488
" --------------------------

0 commit comments

Comments
 (0)