Skip to content

Commit 079c5ad

Browse files
committed
Adds fixes for multi var indent for comma first
Also comments out the multi var indent
1 parent 870ffca commit 079c5ad

1 file changed

Lines changed: 33 additions & 24 deletions

File tree

indent/javascript.vim

Lines changed: 33 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ set cpo&vim
2828
" 1. Variables {{{1
2929
" ============
3030

31+
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\)'
32+
3133
" Regex of syntax group names that are or delimit string or are comments.
3234
let s:syng_strcom = 'string\|regex\|comment\c'
3335

@@ -57,10 +59,10 @@ let s:one_line_scope_regex = '\<\%(if\|else\|for\|while\)\>[^{;]*' . s:line_term
5759
" Regex that defines blocks.
5860
let s:block_regex = '\%({\)\s*\%(|\%([*@]\=\h\w*,\=\s*\)\%(,\s*[*@]\=\h\w*\)*|\)\=' . s:line_term
5961

60-
" Var string
61-
let s:var_regex = '\s*var\(.*\),$'
62+
let s:var_stmt = '^\s*var'
6263

6364
let s:comma_first = '^\s*,'
65+
let s:comma_last = ',\s*$'
6466

6567
" 2. Auxiliary Functions {{{1
6668
" ======================
@@ -145,46 +147,53 @@ function s:RemoveTrailingComments(content)
145147
endfunction
146148

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

153+
" let type = synIDattr(synID(lnum, indent(lnum) + 1, 0), 'name')
154+
155+
" loop through previous expressions to find a var statement
151156
while lnum > 0
152157
let line = getline(lnum)
153158

154-
" if line has var statement, return its number
155-
if (line =~ s:var_regex)
156-
return lnum
159+
" if the line is a js keyword
160+
if (line =~ s:js_keywords)
161+
" check if the line is a var stmt
162+
" if the line has a comma first or comma last then we can assume that we
163+
" are in a multiple var statement
164+
if (line =~ s:var_stmt)
165+
return lnum
166+
endif
167+
168+
" other js keywords, not a var
169+
return 0
157170
endif
158171

159172
let lnum = s:PrevNonBlankNonString(lnum - 1)
160173
endwhile
161174

175+
" beginning of program, not a var
162176
return 0
163177
endfunction
164178

165179
" Find line above with beginning of the var statement or returns 0 if it's not
166180
" this statement
167181
function s:GetVarIndent(lnum)
168-
let lvar = s:InVarStatement(a:lnum)
182+
let lvar = s:InMultiVarStatement(a:lnum)
169183
let prev_lnum = s:PrevNonBlankNonString(a:lnum - 1)
170-
let prev_lvar = s:InVarStatement(prev_lnum)
171184

172-
if (lvar)
185+
if lvar
173186
let line = s:RemoveTrailingComments(getline(prev_lnum))
174187

175-
" if the line doesn't end in a comma, return to regular indent
176-
if (line !~ ',\s*$')
177-
return indent(lvar)
188+
" if the previous line doesn't end in a comma, return to regular indent
189+
if (line !~ s:comma_last)
190+
return indent(prev_lnum) - &sw
178191
else
179-
return indent(lvar) + &sw * 2
192+
return indent(lvar) + &sw
180193
endif
181194
endif
182195

183-
if (prev_lvar)
184-
return indent(prev_lvar)
185-
endif
186-
187-
return 'null'
196+
return -1
188197
endfunction
189198

190199

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

297-
let lvar = s:InVarStatement(v:lnum)
306+
let lvar = s:InMultiVarStatement(v:lnum)
298307
if lvar
299308
let prevline_contents = s:RemoveTrailingComments(getline(prevline))
300309

@@ -308,7 +317,7 @@ function GetJavascriptIndent()
308317
return indent(prevline)
309318
" otherwise we indent 1 level
310319
else
311-
return indent(prevline) + &sw
320+
return indent(lvar) + &sw
312321
endif
313322
endif
314323
endif
@@ -336,10 +345,10 @@ function GetJavascriptIndent()
336345
endif
337346

338347
" Check for multiple var assignments
339-
let var_indent = s:GetVarIndent(v:lnum)
340-
if var_indent !~ 'null'
341-
return var_indent
342-
endif
348+
" let var_indent = s:GetVarIndent(v:lnum)
349+
" if var_indent >= 0
350+
" return var_indent
351+
" endif
343352

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

0 commit comments

Comments
 (0)