Skip to content

Commit 8c56957

Browse files
committed
Merge pull request pangloss#445 from pangloss/bounceme-patch-1
a comma should be considered an operator
2 parents fe096c0 + b5de6ec commit 8c56957

File tree

1 file changed

+21
-52
lines changed

1 file changed

+21
-52
lines changed

indent/javascript.vim

+21-52
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,9 @@ function s:Onescope(lnum)
7272
end
7373
let mypos = col('.')
7474
call cursor(a:lnum, 1)
75-
if search('\<\%(while\|for\|if\)\>\s*(\C', 'ce', a:lnum) > 0 && searchpair('(', '', ')', 'W', s:skip_expr, a:lnum) > 0 && col('.') == strlen(s:RemoveTrailingComments(getline(a:lnum)))
75+
if search('\<\%(while\|for\|if\)\>\s*(\C', 'ce', a:lnum) > 0 &&
76+
\ searchpair('(', '', ')', 'W', s:skip_expr, a:lnum) > 0 &&
77+
\ col('.') == strlen(s:RemoveTrailingComments(getline(a:lnum)))
7678
call cursor(a:lnum, mypos)
7779
return 1
7880
else
@@ -84,11 +86,10 @@ endfunction
8486
" Regex that defines blocks.
8587
let s:block_regex = '[{([]' . s:line_term
8688

87-
let s:operator_first = s:line_pre . '\%([.:?]\|\([-/+*]\)\%(\1\|\*\|\/\)\@!\|||\|&&\)'
89+
let s:operator_first = s:line_pre . '\%([.,:?]\|\([-/+*]\)\%(\1\|\*\|\/\)\@!\|||\|&&\)'
8890

8991
let s:var_stmt = s:line_pre . '\%(const\|let\|var\)\s\+\C'
9092

91-
let s:comma_first = s:line_pre . ','
9293
let s:comma_last = ',' . s:line_term
9394

9495
" 2. Auxiliary Functions {{{1
@@ -148,9 +149,11 @@ function s:GetMSL(lnum, in_one_line_scope)
148149
" Otherwise, terminate search as we have found our MSL already.
149150
let line = getline(lnum)
150151
let col = match(line, s:continuation_regex) + 1
152+
let coal = match(line, s:comma_last) + 1
151153
let line2 = getline(msl)
152154
let col2 = matchend(line2, ')')
153-
if (col > 0 && !s:IsInStringOrComment(lnum, col) && !s:Match(lnum, s:expr_case)) || s:IsInString(lnum, strlen(line))
155+
if ((col > 0 && !s:IsInStringOrComment(lnum, col) || coal > 0 && !s:IsInStringOrComment(lnum,coal)) &&
156+
\ !s:Match(lnum, s:expr_case)) || s:IsInString(lnum, strlen(line))
154157
let msl = lnum
155158

156159
" if there are more closing brackets, continue from the line which has the matching opening bracket
@@ -192,7 +195,9 @@ function s:InMultiVarStatement(lnum, cont, prev)
192195
" let type = synIDattr(synID(lnum, indent(lnum) + 1, 0), 'name')
193196

194197
" loop through previous expressions to find a var statement
195-
while lnum > 0 && (s:Match(lnum, s:comma_last) || (a:cont && getline(lnum) =~ s:line_pre . '}') || (a:prev && s:Match(a:prev, s:comma_last)))
198+
while lnum > 0 && (s:Match(lnum, s:comma_last) ||(a:cont && getline(lnum) =~ s:line_pre . '}') ||
199+
\ s:Match(lnum,s:continuation_regex)) || (a:prev && (s:Match(a:prev, s:comma_last) ||
200+
\ s:Match(a:prev,s:continuation_regex)))
196201
" if the line is a js keyword
197202
if a:cont
198203
call cursor(lnum,1)
@@ -204,12 +209,12 @@ function s:InMultiVarStatement(lnum, cont, prev)
204209
" check if the line is a var stmt
205210
" if the line has a comma first or comma last then we can assume that we
206211
" are in a multiple var statement
207-
if s:Match(lnum, s:var_stmt) && s:Match(lnum, s:comma_last)
212+
if s:Match(lnum, s:var_stmt) && (s:Match(lnum, s:comma_last)||s:Match(lnum,s:continuation_regex))
208213
return lnum
209214
endif
210215

211216
" other js keywords, not a var
212-
if !s:Match(lnum, s:comma_last)
217+
if !s:Match(lnum, s:comma_last)||!s:Match(lnum,s:continuation_regex)
213218
return 0
214219
end
215220
endif
@@ -348,7 +353,7 @@ function GetJavascriptIndent()
348353

349354
" If we are in a multi-line comment, cindent does the right thing.
350355
if s:IsInMultilineComment(v:lnum, 1) && !s:IsLineComment(v:lnum, 1) &&
351-
\ s:IsInMultilineComment(v:lnum, match(line, '\s*$')) && line !~ '^\/\*'
356+
\ s:IsInMultilineComment(v:lnum, match(line, '\s*$')) && line !~ '^\/\*'
352357
return cindent(v:lnum)
353358
endif
354359

@@ -362,38 +367,13 @@ function GetJavascriptIndent()
362367
return cindent(v:lnum)
363368
endif
364369

365-
" the part first where we deal with comma first
366-
if line =~ s:comma_first
367-
let counts = s:LineHasOpeningBrackets(prevline)
368-
if (s:Match(prevline, s:var_stmt) || counts[0] == '1' || counts[1] == '1' || counts[2] == '1')
369-
return indent(prevline) + s:sw()
370-
end
371-
end
372370
" If we got a closing bracket on an empty line, find its match and indent
373371
" according to it. For parentheses we indent to its column - 1, for the
374372
" others we indent to the containing line's MSL's level. Return -1 if fail.
375-
let col = matchend(line, s:line_pre . '[],})]')
373+
let col = matchend(line, s:line_pre . '[]})]')
376374
if col > 0 && !s:IsInStringOrComment(v:lnum, col)
377375
call cursor(v:lnum, col)
378376

379-
let lvar = s:InMultiVarStatement(v:lnum, 0, 0) || line =~ s:comma_first
380-
if lvar || line =~ s:comma_first
381-
382-
" check for comma first
383-
if (line[col - 1] =~ ',')
384-
" if the previous line ends in comma or semicolon don't indent
385-
if (getline(prevline) =~ '[;,]' . s:line_term)
386-
return indent(s:GetMSL(line('.'), 0))
387-
" get previous line indent, if it's comma first return prevline indent
388-
elseif s:Match(prevline, s:comma_first)
389-
return indent(prevline)
390-
" otherwise we indent 1 level
391-
else
392-
return indent(lvar) + s:sw()
393-
endif
394-
endif
395-
endif
396-
397377

398378
let bs = strpart('(){}[]', stridx(')}]', line[col - 1]) * 2, 2)
399379
if searchpair(escape(bs[0], '\['), '', bs[1], 'bW', s:skip_expr) > 0
@@ -402,31 +382,27 @@ function GetJavascriptIndent()
402382
return ind
403383
endif
404384

405-
" If the line is comma first, dedent 1 level
406-
if s:Match(prevline, s:comma_first)
407-
return indent(prevline) - s:sw()
408-
end
409-
410385
" If line starts with an operator...
411386
if (line =~ s:operator_first)
412387
if (s:Match(prevline, s:operator_first))
413388
" and so does previous line, don't indent
414389
return indent(prevline)
415390
end
416391
let counts = s:LineHasOpeningBrackets(prevline)
417-
if counts[0] == '2'
392+
if counts[0] == '2' || counts[1] == '2' || counts[2] == '2'
418393
call cursor(prevline, 1)
419394
" Search for the opening tag
420-
let mnum = searchpair('(', '', ')', 'bW', s:skip_expr)
421-
if mnum > 0 && s:Match(mnum, s:operator_first)
422-
return indent(mnum)
395+
let bs = strpart('(){}[]', stridx(')}]', line[col - 1]) * 2, 2)
396+
if searchpair(escape(bs[0], '\['), '', bs[1], 'bW', s:skip_expr) > 0 && s:Match(line('.'), s:operator_first)
397+
return indent(line('.'))
423398
end
424399
elseif counts[0] != '1' && counts[1] != '1' && counts[2] != '1'
425400
" otherwise, indent 1 level
426401
return indent(prevline) + s:sw()
427402
end
403+
428404
" If previous line starts with an operator...
429-
elseif (s:Match(prevline, s:operator_first) && getline(prevline) !~ s:comma_last && getline(prevline) !~ '};\=' . s:line_term) || getline(prevline) =~ ');\=' . s:line_term
405+
elseif (s:Match(prevline, s:operator_first) && !s:Match(prevline,s:continuation_regex))||getline(prevline) =~ ');\=' . s:line_term
430406
let counts = s:LineHasOpeningBrackets(prevline)
431407
if counts[0] == '2' && !s:Match(prevline, s:operator_first)
432408
call cursor(prevline, 1)
@@ -436,14 +412,7 @@ function GetJavascriptIndent()
436412
return indent(mnum) - s:sw()
437413
end
438414
elseif s:Match(prevline, s:operator_first)
439-
if counts[0] == '2' && counts[1] == '1'
440-
call cursor(prevline, 1)
441-
" Search for the opening tag
442-
let mnum = searchpair('(', '', ')', 'bW', s:skip_expr)
443-
if mnum > 0 && !s:Match(mnum, s:operator_first)
444-
return indent(mnum) + s:sw()
445-
end
446-
elseif counts[0] != '1' && counts[1] != '1' && counts[2] != '1'
415+
if counts[0] != '1' && counts[1] != '1' && counts[2] != '1'
447416
return indent(prevline) - s:sw()
448417
end
449418
end

0 commit comments

Comments
 (0)