Skip to content

Commit 8547784

Browse files
committed
Operator first support,including ternary,dot etc.
the previous pr is being difficult, sorry
1 parent 2adca53 commit 8547784

File tree

1 file changed

+25
-8
lines changed

1 file changed

+25
-8
lines changed

indent/javascript.vim

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -71,14 +71,13 @@ let s:one_line_scope_regex = '\%(\%(\<else\>\|\<\%(if\|for\|while\)\>\s*(.*)\)\|
7171
" Regex that defines blocks.
7272
let s:block_regex = '\%([{([]\)\s*\%(|\%([*@]\=\h\w*,\=\s*\)\%(,\s*[*@]\=\h\w*\)*|\)\=' . s:line_term
7373

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

7678
let s:comma_first = '^\s*,'
7779
let s:comma_last = ',\s*$'
7880

79-
let s:ternary = '^\s\+[?|:]'
80-
let s:ternary_q = '^\s\+?'
81-
8281
let s:case_indent = s:sw()
8382
let s:case_indent_after = s:sw()
8483
let s:m = matchlist(&cinoptions, ':\(.\)')
@@ -378,13 +377,31 @@ function GetJavascriptIndent()
378377
return indent(prevline) + s:case_indent_after
379378
endif
380379

381-
if (line =~ s:ternary)
382-
if (getline(prevline) =~ s:ternary_q)
380+
" If line starts with operator...
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
387-
endif
397+
end
398+
" If previous line starts with a operator...
399+
elseif s:Match(prevline, s:operator_first) && !s:Match(prevline, s:comma_last)
400+
let countscur = s:LineHasOpeningBrackets(v:lnum)
401+
if countscur[0] != '2'
402+
return indent(prevline) - s:sw()
403+
end
404+
end
388405

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

0 commit comments

Comments
 (0)