Skip to content

Commit 77387ef

Browse files
committed
Update javascript.vim (#437)
regexes have been cleaned and no longer have ruby specifics. all of the regex matching has been either refactored to use the s:match function if it is needed to look at the syntax of the characters, or changed to be a simple match to save performance.the current file has none of the bugs i talked about that were caused by the last commit
1 parent 2767155 commit 77387ef

1 file changed

Lines changed: 48 additions & 44 deletions

File tree

indent/javascript.vim

Lines changed: 48 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -58,10 +58,10 @@ let s:syng_linecom = 'linecomment\c'
5858
" Expression used to check whether we should skip a match with searchpair().
5959
let s:skip_expr = "synIDattr(synID(line('.'),col('.'),1),'name') =~ '".s:syng_strcom."'"
6060

61-
let s:line_term = '\s*\%(\%(\/\/\).*\)\=$'
61+
let s:line_term = '\s*\%(\%(\/\/.*\)\=\|\%(\/\*.*\*\/\s*\)*\)$'
6262

6363
" Regex that defines continuation lines, not including (, {, or [.
64-
let s:continuation_regex = '\%([\\*/.?:]\|+\@<!+\|-\@<!-\|=\|||\|&&\|\%(=>.*\)\@<!=[^=>],\)' . s:line_term
64+
let s:continuation_regex = '\%([\\*/.?:]\|+\@<!+\|-\@<!-\|=\|||\|&&\)' . s:line_term
6565

6666
let s:one_line_scope_regex = '\%(\<else\>\|=>\)\C' . s:line_term
6767

@@ -81,7 +81,7 @@ function s:Onescope(lnum)
8181
endfunction
8282

8383
" Regex that defines blocks.
84-
let s:block_regex = '\%([{([]\)\s*\%(|\%([*@]\=\h\w*,\=\s*\)\%(,\s*[*@]\=\h\w*\)*|\)\=' . s:line_term
84+
let s:block_regex = '[{([]' . s:line_term
8585

8686
let s:operator_first = '^\s*\%([*.:?]\|\([-/+]\)\1\@!\|||\|&&\)'
8787

@@ -179,8 +179,8 @@ function s:GetMSL(lnum, in_one_line_scope)
179179
endfunction
180180

181181
function s:RemoveTrailingComments(content)
182-
let single = '\/\/\(.*\)\s*$'
183-
let multi = '\/\*\(.*\)\*\/\s*$'
182+
let single = '\/\/\%(.*\)\s*$'
183+
let multi = '\/\*\%(.*\)\*\/\s*$'
184184
return substitute(substitute(substitute(a:content, single, '', ''), multi, '', ''), '\s\+$', '', '')
185185
endfunction
186186

@@ -192,54 +192,52 @@ function s:InMultiVarStatement(lnum, cont, prev)
192192

193193
" loop through previous expressions to find a var statement
194194
while lnum > 0 && (s:Match(lnum, s:comma_last) || (a:cont && getline(lnum) =~ '^\s*}') || (a:prev && s:Match(a:prev, s:comma_last)))
195-
let line = getline(lnum)
196195
" if the line is a js keyword
197-
if (line =~ s:js_keywords)
196+
if a:cont
197+
call cursor(lnum,1)
198+
if searchpair('{', '', '}', 'bW', s:skip_expr) > 0
199+
let lnum = line('.')
200+
end
201+
end
202+
if s:Match(lnum, s:js_keywords)
198203
" check if the line is a var stmt
199204
" if the line has a comma first or comma last then we can assume that we
200205
" are in a multiple var statement
201-
if (line =~ s:var_stmt) && line =~ s:comma_last
206+
if s:Match(lnum, s:var_stmt) && s:Match(lnum, s:comma_last)
202207
return lnum
203208
endif
204209

205210
" other js keywords, not a var
206-
if line !~ s:comma_last
211+
if !s:Match(lnum, s:comma_last)
207212
return 0
208213
end
209214
endif
210-
if a:cont
211-
call cursor(lnum,1)
212-
if searchpair('{', '', '}', 'bW', s:skip_expr) > 0
213-
let lnum = line('.')
214-
continue
215-
end
216-
end
217215
let lnum = s:PrevNonBlankNonString(lnum - 1)
218216
endwhile
219217

220218
" beginning of program, not a var
221219
return 0
222220
endfunction
223221

224-
" Find line above with beginning of the var statement or returns 0 if it's not
222+
" Find line above with beginning of the var statement or returns 0 if it's not"{{{2
225223
" this statement
226-
function s:GetVarIndent(lnum)
227-
let lvar = s:InMultiVarStatement(a:lnum, 0,0)
228-
let prev_lnum = s:PrevNonBlankNonString(a:lnum - 1)
224+
" function s:GetVarIndent(lnum)
225+
" let lvar = s:InMultiVarStatement(a:lnum, 0,0)
226+
" let prev_lnum = s:PrevNonBlankNonString(a:lnum - 1)
229227

230-
if lvar
231-
let line = s:RemoveTrailingComments(getline(prev_lnum))
228+
" if lvar
229+
" let line = s:RemoveTrailingComments(getline(prev_lnum))
232230

233-
" if the previous line doesn't end in a comma, return to regular indent
234-
if (line !~ s:comma_last)
235-
return indent(prev_lnum) - s:sw()
236-
else
237-
return indent(lvar) + s:sw()
238-
endif
239-
endif
231+
" " if the previous line doesn't end in a comma, return to regular indent
232+
" if (line !~ s:comma_last)
233+
" return indent(prev_lnum) - s:sw()
234+
" else
235+
" return indent(lvar) + s:sw()
236+
" endif
237+
" endif
240238

241-
return -1
242-
endfunction
239+
" return -1
240+
" endfunction"}}}
243241

244242

245243
" Check if line 'lnum' has more opening brackets than closing ones.
@@ -361,8 +359,13 @@ function GetJavascriptIndent()
361359
if (line =~ s:expr_case)
362360
return cindent(v:lnum)
363361
endif
364-
if s:Match(v:lnum, s:comma_first) && s:Match(prevline,s:var_stmt)
365-
return indent(prevline) + s:sw()
362+
363+
" the part first where we deal with comma first
364+
if line =~ s:comma_first
365+
let counts = s:LineHasOpeningBrackets(prevline)
366+
if (s:Match(prevline, s:var_stmt) || counts[0] == '1' || counts[1] == '1' || counts[2] == '1')
367+
return indent(prevline) + s:sw()
368+
end
366369
end
367370
" If we got a closing bracket on an empty line, find its match and indent
368371
" according to it. For parentheses we indent to its column - 1, for the
@@ -373,15 +376,14 @@ function GetJavascriptIndent()
373376

374377
let lvar = s:InMultiVarStatement(v:lnum, 0, 0) || line =~ s:comma_first
375378
if lvar || line =~ s:comma_first
376-
let prevline_contents = s:RemoveTrailingComments(getline(prevline))
377379

378380
" check for comma first
379381
if (line[col - 1] =~ ',')
380382
" if the previous line ends in comma or semicolon don't indent
381-
if (prevline_contents =~ '[;,]\s*$')
383+
if (getline(prevline) =~ '[;,]' . s:line_term)
382384
return indent(s:GetMSL(line('.'), 0))
383385
" get previous line indent, if it's comma first return prevline indent
384-
elseif (prevline_contents =~ s:comma_first)
386+
elseif s:Match(prevline, s:comma_first)
385387
return indent(prevline)
386388
" otherwise we indent 1 level
387389
else
@@ -399,12 +401,12 @@ function GetJavascriptIndent()
399401
endif
400402

401403
" If the line is comma first, dedent 1 level
402-
if (getline(prevline) =~ s:comma_first)
404+
if s:Match(prevline, s:comma_first)
403405
return indent(prevline) - s:sw()
404406
end
405407

406408
" If line starts with an operator...
407-
if (s:Match(v:lnum, s:operator_first))
409+
if (line =~ s:operator_first)
408410
if (s:Match(prevline, s:operator_first))
409411
" and so does previous line, don't indent
410412
return indent(prevline)
@@ -422,7 +424,7 @@ function GetJavascriptIndent()
422424
return indent(prevline) + s:sw()
423425
end
424426
" If previous line starts with an operator...
425-
elseif (s:Match(prevline, s:operator_first) && !s:Match(prevline, s:comma_last) && !s:Match(prevline, '};\=' . s:line_term)) || s:Match(prevline, ');\=' . s:line_term)
427+
elseif (s:Match(prevline, s:operator_first) && getline(prevline) !~ s:comma_last && getline(prevline) !~ '};\=' . s:line_term) || getline(prevline) =~ ');\=' . s:line_term
426428
let counts = s:LineHasOpeningBrackets(prevline)
427429
if counts[0] == '2' && !s:Match(prevline, s:operator_first)
428430
call cursor(prevline, 1)
@@ -487,13 +489,13 @@ function GetJavascriptIndent()
487489
if searchpair('(', '', ')', 'bW', s:skip_expr) > 0
488490
return indent(s:GetMSL(line('.'), 0))
489491
end
490-
elseif counts[1] == '2' && line !~ '^\s*}'
492+
elseif counts[1] == '2' && !s:Match(lnum, '^\s*}')
491493
call cursor(lnum, 1)
492494
" Search for the opening tag
493495
if searchpair('{', '', '}', 'bW', s:skip_expr) > 0
494496
return indent(s:GetMSL(line('.'), 0))
495497
end
496-
elseif counts[2] == '2' && line !~ '^\s*]'
498+
elseif counts[2] == '2' && !s:Match(lnum, '^\s*]')
497499
call cursor(lnum, 1)
498500
" Search for the opening tag
499501
if searchpair('\[', '', '\]', 'bW', s:skip_expr) > 0
@@ -509,10 +511,12 @@ function GetJavascriptIndent()
509511
" 3.4. Work on the MSL line. {{{1
510512
" --------------------------
511513
if s:Match(lnum, s:comma_last) && !s:Match(lnum, s:continuation_regex)
512-
return line =~ s:var_stmt ? indent(lnum) + s:sw() : indent(lnum)
514+
return s:Match(lnum, s:var_stmt) ? indent(lnum) + s:sw() : indent(lnum)
513515

514-
elseif s:InMultiVarStatement(lnum, 1, v:lnum) && line !~ s:var_stmt
515-
return indent(lnum) - s:sw()
516+
elseif s:Match(s:PrevNonBlankNonString(lnum - 1), s:comma_last)
517+
if !s:Match(lnum, s:comma_last) && s:InMultiVarStatement(lnum,1,0)
518+
return indent(lnum) - s:sw()
519+
end
516520
end
517521
let ind_con = ind
518522
let ind = s:IndentWithContinuation(lnum, ind_con, s:sw())

0 commit comments

Comments
 (0)