Skip to content

Commit 8fb5fa8

Browse files
committed
parentheses regex is thankfully gone (#412)
proper asi for control statements
1 parent 617ccb5 commit 8fb5fa8

File tree

1 file changed

+21
-6
lines changed

1 file changed

+21
-6
lines changed

indent/javascript.vim

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,22 @@ let s:line_term = '\s*\%(\%(\/\/\).*\)\=$'
6565
" Regex that defines continuation lines, not including (, {, or [.
6666
let s:continuation_regex = '\%([\\*/.:]\|+\@<!+\|-\@<!-\|\%(<%\)\@<!=\|\W[|&?]\|||\|&&\|[^=]=[^=>].*,\)' . s:line_term
6767

68-
let s:one_line_scope_regex = '\%(\%(\<else\>\|\<\%(if\|for\|while\)\>\s*(\%([^()]*\|[^()]*(\%([^()]*\|[^()]*(\%([^()]*\|[^()]*([^()]*)[^()]*\))[^()]*\))[^()]*\))\)\|=>\)' . s:line_term
68+
let s:one_line_scope_regex = '\%(\<else\>\|=>\)' . s:line_term
69+
70+
function s:Onescope(lnum)
71+
if getline(a:lnum) =~ s:one_line_scope_regex
72+
return 1
73+
end
74+
let mypos = col('.')
75+
call cursor(a:lnum, 1)
76+
if search('\<\%(while\|for\|if\)\>\s*(', 'ce', a:lnum) > 0 && searchpair('(', '', ')', 'W', s:skip_expr, a:lnum) > 0 && col('.') + 1 == col('$')
77+
call cursor(a:lnum, mypos)
78+
return 1
79+
else
80+
call cursor(a:lnum, mypos)
81+
return 0
82+
end
83+
endfunction
6984

7085
" Regex that defines blocks.
7186
let s:block_regex = '\%([{([]\)\s*\%(|\%([*@]\=\h\w*,\=\s*\)\%(,\s*[*@]\=\h\w*\)*|\)\=' . s:line_term
@@ -160,7 +175,7 @@ function s:GetMSL(lnum, in_one_line_scope)
160175
if a:in_one_line_scope
161176
break
162177
end
163-
let msl_one_line = s:Match(lnum, s:one_line_scope_regex)
178+
let msl_one_line = s:Onescope(lnum)
164179
if msl_one_line == 0
165180
break
166181
endif
@@ -285,7 +300,7 @@ endfunction
285300

286301
function s:InOneLineScope(lnum)
287302
let msl = s:GetMSL(a:lnum, 1)
288-
if msl > 0 && s:Match(msl, s:one_line_scope_regex)
303+
if msl > 0 && s:Onescope(msl)
289304
return msl
290305
endif
291306
return 0
@@ -295,11 +310,11 @@ function s:ExitingOneLineScope(lnum)
295310
let msl = s:GetMSL(a:lnum, 1)
296311
if msl > 0
297312
" if the current line is in a one line scope ..
298-
if s:Match(msl, s:one_line_scope_regex)
313+
if s:Onescope(msl)
299314
return 0
300315
else
301316
let prev_msl = s:GetMSL(msl - 1, 1)
302-
if s:Match(prev_msl, s:one_line_scope_regex)
317+
if s:Onescope(prev_msl)
303318
return prev_msl
304319
endif
305320
endif
@@ -472,7 +487,7 @@ function GetJavascriptIndent()
472487
if line =~ '[[({})\]]'
473488
let counts = s:LineHasOpeningBrackets(lnum)
474489
if counts[0] == '1' && searchpair('(', '', ')', 'bW', s:skip_expr) > 0
475-
if col('.') + 1 == col('$') || line =~ s:one_line_scope_regex
490+
if col('.') + 1 == col('$') || s:Onescope(lnum)
476491
return ind + s:sw()
477492
else
478493
return virtcol('.')

0 commit comments

Comments
 (0)