Skip to content

multi-line var asignment indentation #53

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

Closed
wants to merge 3 commits into from
Closed
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
52 changes: 52 additions & 0 deletions indent/javascript.vim
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,9 @@ let s:one_line_scope_regex = '\<\%(if\|else\|for\|while\)\>[^{;]*' . s:line_term
" Regex that defines blocks.
let s:block_regex = '\%({\)\s*\%(|\%([*@]\=\h\w*,\=\s*\)\%(,\s*[*@]\=\h\w*\)*|\)\=' . s:line_term

" Var string
let s:var_regex = '\s*var\s[^;]\+$'

" 2. Auxiliary Functions {{{1
" ======================

Expand Down Expand Up @@ -129,6 +132,49 @@ function s:GetMSL(lnum, in_one_line_scope)
return msl
endfunction

" Find if the string is inside var statement (but not the first string)
function s:InVarStatement(lnum)
let lnum = s:PrevNonBlankNonString(a:lnum - 1)

while lnum > 0
let line = getline(lnum)

" if line has var statement, return it's number
if ( line =~ s:var_regex )
return lnum
endif

" if line has brackets or semi-colon, return 0
if ( line =~ '[{};]' )
return 0
endif

let lnum = s:PrevNonBlankNonString(lnum - 1)
endwhile

return 0
endfunction

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

if ( lvar )
return indent(lvar) + &sw
endif

if ( prev_lvar )
return indent(prev_lvar)
endif

return 'null'

endfunction


" Check if line 'lnum' has more opening brackets than closing ones.
function s:LineHasOpeningBrackets(lnum)
let open_0 = 0
Expand Down Expand Up @@ -248,6 +294,12 @@ function GetJavascriptIndent()
return cindent(v:lnum)
endif

" Work with var statements' blocks
let var_indent = s:GetVarIndent(v:lnum)
if var_indent != 'null'
return var_indent
endif

" 3.3. Work on the previous line. {{{2
" -------------------------------

Expand Down