Skip to content

Commit be0b8eb

Browse files
committed
simplifies the parentheses handling
also a possible fix for the last example in pangloss#329 and pangloss#315
1 parent 2881bba commit be0b8eb

File tree

1 file changed

+18
-5
lines changed

1 file changed

+18
-5
lines changed

indent/javascript.vim

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ 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

7474
let s:var_stmt = '^\s*(const\|let\|var)'
7575

@@ -240,7 +240,7 @@ function s:LineHasOpeningBrackets(lnum)
240240
endif
241241
let pos = match(line, '[][(){}]', pos + 1)
242242
endwhile
243-
return (open_0 > 0) . (open_2 > 0) . (open_4 > 0)
243+
return (open_0 > 0?1:(open_0 == 0 ? 0 : 2)) . (open_2 > 0) . (open_4 > 0)
244244
endfunction
245245

246246
function s:Match(lnum, regex)
@@ -302,6 +302,7 @@ function s:ExitingOneLineScope(lnum)
302302
return 0
303303
endfunction
304304

305+
305306
" 3. GetJavascriptIndent Function {{{1
306307
" =========================
307308

@@ -438,9 +439,7 @@ function GetJavascriptIndent()
438439
if line =~ '[[({]'
439440
let counts = s:LineHasOpeningBrackets(lnum)
440441
if counts[0] == '1' && searchpair('(', '', ')', 'bW', s:skip_expr) > 0
441-
if col('.') + 1 == col('$')
442-
return ind + s:sw()
443-
else
442+
if col('.') + 1 != col('$')
444443
return virtcol('.')
445444
endif
446445
elseif counts[1] == '1' || counts[2] == '1'
@@ -450,6 +449,20 @@ function GetJavascriptIndent()
450449
end
451450
endif
452451

452+
if line =~ ')'
453+
let counts = s:LineHasOpeningBrackets(lnum)
454+
if counts[0] == '2'
455+
let curpos = getpos(".")
456+
call cursor(lnum, 1)
457+
" Search for the opening tag
458+
let mnum = searchpair('(', '', ')', 'bW', s:skip_expr)
459+
"Restore the cursor position
460+
call cursor(curpos)
461+
if mnum > 0
462+
return indent(mnum)
463+
end
464+
end
465+
endif
453466
" 3.4. Work on the MSL line. {{{2
454467
" --------------------------
455468

0 commit comments

Comments
 (0)