Skip to content

Commit

Permalink
Adds fixes for multi var indent for comma first
Browse files Browse the repository at this point in the history
Also comments out the multi var indent
  • Loading branch information
goatslacker committed Mar 12, 2013
1 parent 870ffca commit 079c5ad
Showing 1 changed file with 33 additions and 24 deletions.
57 changes: 33 additions & 24 deletions indent/javascript.vim
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ set cpo&vim
" 1. Variables {{{1
" ============

let s:js_keywords = '^\s*\(break\|case\|catch\|continue\|debugger\|default\|delete\|do\|else\|finally\|for\|function\|if\|in\|instanceof\|new\|return\|switch\|this\|throw\|try\|typeof\|var\|void\|while\|with\)'

" Regex of syntax group names that are or delimit string or are comments.
let s:syng_strcom = 'string\|regex\|comment\c'

Expand Down Expand Up @@ -57,10 +59,10 @@ 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\(.*\),$'
let s:var_stmt = '^\s*var'

let s:comma_first = '^\s*,'
let s:comma_last = ',\s*$'

" 2. Auxiliary Functions {{{1
" ======================
Expand Down Expand Up @@ -145,46 +147,53 @@ function s:RemoveTrailingComments(content)
endfunction

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

" let type = synIDattr(synID(lnum, indent(lnum) + 1, 0), 'name')

" loop through previous expressions to find a var statement
while lnum > 0
let line = getline(lnum)

" if line has var statement, return its number
if (line =~ s:var_regex)
return lnum
" if the line is a js keyword
if (line =~ 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)
return lnum
endif

" other js keywords, not a var
return 0
endif

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
" this statement
function s:GetVarIndent(lnum)
let lvar = s:InVarStatement(a:lnum)
let lvar = s:InMultiVarStatement(a:lnum)
let prev_lnum = s:PrevNonBlankNonString(a:lnum - 1)
let prev_lvar = s:InVarStatement(prev_lnum)

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

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

if (prev_lvar)
return indent(prev_lvar)
endif

return 'null'
return -1
endfunction


Expand Down Expand Up @@ -294,7 +303,7 @@ function GetJavascriptIndent()
if col > 0 && !s:IsInStringOrComment(v:lnum, col)
call cursor(v:lnum, col)

let lvar = s:InVarStatement(v:lnum)
let lvar = s:InMultiVarStatement(v:lnum)
if lvar
let prevline_contents = s:RemoveTrailingComments(getline(prevline))

Expand All @@ -308,7 +317,7 @@ function GetJavascriptIndent()
return indent(prevline)
" otherwise we indent 1 level
else
return indent(prevline) + &sw
return indent(lvar) + &sw
endif
endif
endif
Expand Down Expand Up @@ -336,10 +345,10 @@ function GetJavascriptIndent()
endif

" Check for multiple var assignments
let var_indent = s:GetVarIndent(v:lnum)
if var_indent !~ 'null'
return var_indent
endif
" let var_indent = s:GetVarIndent(v:lnum)
" if var_indent >= 0
" return var_indent
" endif

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

3 comments on commit 079c5ad

@bcrescimanno
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

After applying this commit, multi-line var statements ONLY work with comma-first style.
See: https://gist.github.com/bcrescimanno/5144215

Edit: It did, however, fix the insanity I was seeing around nested returns and functions.

@goatslacker
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I intentionally commented out the multi-var statement indentation but left the comma-first style indent.

There are lot of edge cases with the multi-var indent

function x() {
  var x;
}
  // was being indented incorrectly

var a = 1,
  b = [
1, // indented incorrectly
  ]

I want to refine the syntax file and add plenty of unit tests before I turn this on again so it can work as expected without any limitations.

The comma-first patch, for now, seems to work great and I haven't found any cases where it works incorrectly yet so I kept that in.

@bcrescimanno
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Heh...I see it now in the commit notes. Sorry for the noise.

Please sign in to comment.