Skip to content

Commit 233b6c3

Browse files
authored
paren matching (#474)
#469 (comment)
1 parent be55064 commit 233b6c3

File tree

1 file changed

+36
-20
lines changed

1 file changed

+36
-20
lines changed

indent/javascript.vim

Lines changed: 36 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ let s:line_pre = '^\s*\%(\/\*.*\*\/\s*\)*'
4545
let s:js_keywords = s:line_pre . '\%(break\|import\|export\|catch\|const\|continue\|debugger\|delete\|do\|else\|finally\|for\|function\|if\|in\|instanceof\|let\|new\|return\|switch\|this\|throw\|try\|typeof\|var\|void\|while\|with\)\>\C'
4646
let s:expr_case = s:line_pre . '\%(case\s\+[^\:]*\|default\)\s*:\s*\C'
4747
" Regex of syntax group names that are or delimit string or are comments.
48-
let s:syng_strcom = '\%(string\|regex\|special\|comment\|template\)\c'
48+
let s:syng_strcom = '\%(string\|regex\|special\|doc\|comment\|template\)\c'
4949

5050
" Regex of syntax group names that are strings.
5151
let s:syng_string = 'regex\c'
@@ -347,22 +347,29 @@ function GetJavascriptIndent()
347347
endif
348348

349349
" If we got a closing bracket on an empty line, find its match and indent
350-
" according to it. For parentheses we indent to its column - 1, for the
351-
" others we indent to the containing line's MSL's level. Return -1 if fail.
352-
let col = matchend(line, s:line_pre . '[]})]')
353-
if col > 0 && !s:IsInStringOrComment(v:lnum, col)
354-
call cursor(v:lnum, col)
355-
let parlnum = s:lookForParens('(\|{\|\[', ')\|}\|\]', 'nbW', 0)
356-
if parlnum > 0
357-
let ind = s:InMultiVarStatement(parlnum, 0, 0) ? indent(parlnum) : indent(s:GetMSL(parlnum, 0))
358-
endif
350+
" according to it.
351+
let col = s:Match(v:lnum, s:line_pre . '[]})]')
352+
if col > 0
353+
let parlnum = v:lnum
354+
while col
355+
call cursor(parlnum, 1)
356+
let parlnum = s:lookForParens('(\|{\|\[', ')\|}\|\]', 'nbW', 0)
357+
let col = s:Match(parlnum, s:line_pre . '[]})]')
358+
if col
359+
continue
360+
end
361+
if parlnum > 0
362+
let ind = s:InMultiVarStatement(parlnum, 0, 0)|| s:LineHasOpeningBrackets(parlnum) !~ '2'
363+
\ ? indent(parlnum) : indent(s:GetMSL(parlnum, 0))
364+
endif
365+
endwhile
359366
return ind
360367
endif
361368

362369

363370
" If line starts with an operator...
364371
if (line =~ s:operator_first)
365-
if (s:Match(lnum, s:operator_first))
372+
if (s:Match(lnum, s:operator_first) || s:Match(lnum, s:line_pre . '[])}]'))
366373
" and so does previous line, don't indent
367374
return indent(lnum)
368375
end
@@ -421,30 +428,39 @@ function GetJavascriptIndent()
421428
return 0
422429
endif
423430

431+
" foo('foo',
432+
" bar('bar', function() {
433+
" hi();
434+
" })
435+
" );
424436

437+
" function (a, b, c, d,
438+
" e, f, g) {
439+
" console.log('inner');
440+
" }
425441
" If the previous line ended with a block opening, add a level of indent.
426442
if s:Match(lnum, s:block_regex)
427-
return s:InMultiVarStatement(lnum, 0, 0) ? indent(lnum) + s:sw() : indent(s:GetMSL(lnum, 0)) + s:sw()
443+
return s:InMultiVarStatement(lnum, 0, 0) || s:LineHasOpeningBrackets(lnum) !~ '2' ?
444+
\ indent(lnum) + s:sw() : indent(s:GetMSL(lnum, 0)) + s:sw()
428445
endif
429446

430447
" Set up variables for current line.
431448
let line = getline(lnum)
432449
let ind = indent(lnum)
433450
" If the previous line contained an opening bracket, and we are still in it,
434451
" add indent depending on the bracket type.
435-
if s:Match(lnum, '[[({})\]]')
452+
if s:Match(lnum, '\%([[({]\)\|\%([^\t \])}][})\]]\)')
436453
let counts = s:LineHasOpeningBrackets(lnum)
437454
if counts =~ '2'
438-
call cursor(lnum, 1)
439-
" Search for the opening tag
440-
let parlnum = s:lookForParens('(\|{\|\[', ')\|}\|\]', 'nbW', 0)
441-
if parlnum > 0 && !s:InMultiVarStatement(parlnum,0,0)
442-
return indent(s:GetMSL(parlnum, 0))
455+
call cursor(lnum,matchend(s:RemoveTrailingComments(line), '.\+\zs[])}]'))
456+
while s:lookForParens('(\|{\|\[', ')\|}\|\]', 'bW', 0) == lnum
457+
call cursor(lnum, matchend(s:RemoveTrailingComments(strpart(line,0,col('.'))), '.*\zs[])}]'))
458+
endwhile
459+
if line('.') < lnum && !s:InMultiVarStatement(line('.'),0,0)
460+
return indent(s:GetMSL(line('.'), 0))
443461
end
444462
elseif counts =~ '1' || s:Onescope(lnum)
445463
return ind + s:sw()
446-
else
447-
call cursor(v:lnum, vcol)
448464
end
449465
end
450466

0 commit comments

Comments
 (0)