Skip to content

Update javascript.vim #437

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
May 21, 2016
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
92 changes: 48 additions & 44 deletions indent/javascript.vim
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,10 @@ let s:syng_linecom = 'linecomment\c'
" Expression used to check whether we should skip a match with searchpair().
let s:skip_expr = "synIDattr(synID(line('.'),col('.'),1),'name') =~ '".s:syng_strcom."'"

let s:line_term = '\s*\%(\%(\/\/\).*\)\=$'
let s:line_term = '\s*\%(\%(\/\/.*\)\=\|\%(\/\*.*\*\/\s*\)*\)$'

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

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

Expand All @@ -81,7 +81,7 @@ function s:Onescope(lnum)
endfunction

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

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

Expand Down Expand Up @@ -179,8 +179,8 @@ function s:GetMSL(lnum, in_one_line_scope)
endfunction

function s:RemoveTrailingComments(content)
let single = '\/\/\(.*\)\s*$'
let multi = '\/\*\(.*\)\*\/\s*$'
let single = '\/\/\%(.*\)\s*$'
let multi = '\/\*\%(.*\)\*\/\s*$'
return substitute(substitute(substitute(a:content, single, '', ''), multi, '', ''), '\s\+$', '', '')
endfunction

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

" loop through previous expressions to find a var statement
while lnum > 0 && (s:Match(lnum, s:comma_last) || (a:cont && getline(lnum) =~ '^\s*}') || (a:prev && s:Match(a:prev, s:comma_last)))
let line = getline(lnum)
" if the line is a js keyword
if (line =~ s:js_keywords)
if a:cont
call cursor(lnum,1)
if searchpair('{', '', '}', 'bW', s:skip_expr) > 0
let lnum = line('.')
end
end
if s:Match(lnum, s:js_keywords)
" check if the line is a var stmt
" if the line has a comma first or comma last then we can assume that we
" are in a multiple var statement
if (line =~ s:var_stmt) && line =~ s:comma_last
if s:Match(lnum, s:var_stmt) && s:Match(lnum, s:comma_last)
return lnum
endif

" other js keywords, not a var
if line !~ s:comma_last
if !s:Match(lnum, s:comma_last)
return 0
end
endif
if a:cont
call cursor(lnum,1)
if searchpair('{', '', '}', 'bW', s:skip_expr) > 0
let lnum = line('.')
continue
end
end
let lnum = s:PrevNonBlankNonString(lnum - 1)
endwhile

" beginning of program, not a var
return 0
endfunction

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

if lvar
let line = s:RemoveTrailingComments(getline(prev_lnum))
" if lvar
" let line = s:RemoveTrailingComments(getline(prev_lnum))

" if the previous line doesn't end in a comma, return to regular indent
if (line !~ s:comma_last)
return indent(prev_lnum) - s:sw()
else
return indent(lvar) + s:sw()
endif
endif
" " if the previous line doesn't end in a comma, return to regular indent
" if (line !~ s:comma_last)
" return indent(prev_lnum) - s:sw()
" else
" return indent(lvar) + s:sw()
" endif
" endif

return -1
endfunction
" return -1
" endfunction"}}}


" Check if line 'lnum' has more opening brackets than closing ones.
Expand Down Expand Up @@ -361,8 +359,13 @@ function GetJavascriptIndent()
if (line =~ s:expr_case)
return cindent(v:lnum)
endif
if s:Match(v:lnum, s:comma_first) && s:Match(prevline,s:var_stmt)
return indent(prevline) + s:sw()

" the part first where we deal with comma first
if line =~ s:comma_first
let counts = s:LineHasOpeningBrackets(prevline)
if (s:Match(prevline, s:var_stmt) || counts[0] == '1' || counts[1] == '1' || counts[2] == '1')
return indent(prevline) + s:sw()
end
end
" If we got a closing bracket on an empty line, find its match and indent
" according to it. For parentheses we indent to its column - 1, for the
Expand All @@ -373,15 +376,14 @@ function GetJavascriptIndent()

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

" check for comma first
if (line[col - 1] =~ ',')
" if the previous line ends in comma or semicolon don't indent
if (prevline_contents =~ '[;,]\s*$')
if (getline(prevline) =~ '[;,]' . s:line_term)
return indent(s:GetMSL(line('.'), 0))
" get previous line indent, if it's comma first return prevline indent
elseif (prevline_contents =~ s:comma_first)
elseif s:Match(prevline, s:comma_first)
return indent(prevline)
" otherwise we indent 1 level
else
Expand All @@ -399,12 +401,12 @@ function GetJavascriptIndent()
endif

" If the line is comma first, dedent 1 level
if (getline(prevline) =~ s:comma_first)
if s:Match(prevline, s:comma_first)
return indent(prevline) - s:sw()
end

" If line starts with an operator...
if (s:Match(v:lnum, s:operator_first))
if (line =~ s:operator_first)
if (s:Match(prevline, s:operator_first))
" and so does previous line, don't indent
return indent(prevline)
Expand All @@ -422,7 +424,7 @@ function GetJavascriptIndent()
return indent(prevline) + s:sw()
end
" If previous line starts with an operator...
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)
elseif (s:Match(prevline, s:operator_first) && getline(prevline) !~ s:comma_last && getline(prevline) !~ '};\=' . s:line_term) || getline(prevline) =~ ');\=' . s:line_term
let counts = s:LineHasOpeningBrackets(prevline)
if counts[0] == '2' && !s:Match(prevline, s:operator_first)
call cursor(prevline, 1)
Expand Down Expand Up @@ -487,13 +489,13 @@ function GetJavascriptIndent()
if searchpair('(', '', ')', 'bW', s:skip_expr) > 0
return indent(s:GetMSL(line('.'), 0))
end
elseif counts[1] == '2' && line !~ '^\s*}'
elseif counts[1] == '2' && !s:Match(lnum, '^\s*}')
call cursor(lnum, 1)
" Search for the opening tag
if searchpair('{', '', '}', 'bW', s:skip_expr) > 0
return indent(s:GetMSL(line('.'), 0))
end
elseif counts[2] == '2' && line !~ '^\s*]'
elseif counts[2] == '2' && !s:Match(lnum, '^\s*]')
call cursor(lnum, 1)
" Search for the opening tag
if searchpair('\[', '', '\]', 'bW', s:skip_expr) > 0
Expand All @@ -509,10 +511,12 @@ function GetJavascriptIndent()
" 3.4. Work on the MSL line. {{{1
" --------------------------
if s:Match(lnum, s:comma_last) && !s:Match(lnum, s:continuation_regex)
return line =~ s:var_stmt ? indent(lnum) + s:sw() : indent(lnum)
return s:Match(lnum, s:var_stmt) ? indent(lnum) + s:sw() : indent(lnum)

elseif s:InMultiVarStatement(lnum, 1, v:lnum) && line !~ s:var_stmt
return indent(lnum) - s:sw()
elseif s:Match(s:PrevNonBlankNonString(lnum - 1), s:comma_last)
if !s:Match(lnum, s:comma_last) && s:InMultiVarStatement(lnum,1,0)
return indent(lnum) - s:sw()
end
end
let ind_con = ind
let ind = s:IndentWithContinuation(lnum, ind_con, s:sw())
Expand Down