Skip to content

Commit ae278ef

Browse files
committed
adding var statement block indentation
1 parent 827c47c commit ae278ef

1 file changed

Lines changed: 52 additions & 0 deletions

File tree

indent/javascript.vim

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,9 @@ let s:one_line_scope_regex = '\<\%(if\|else\|for\|while\)\>[^{;]*' . s:line_term
5858
" Regex that defines blocks.
5959
let s:block_regex = '\%({\)\s*\%(|\%([*@]\=\h\w*,\=\s*\)\%(,\s*[*@]\=\h\w*\)*|\)\=' . s:line_term
6060

61+
" Var string
62+
let s:var_regex = '\s*var\s.\+[^;]$'
63+
6164
" 2. Auxiliary Functions {{{1
6265
" ======================
6366

@@ -129,6 +132,49 @@ function s:GetMSL(lnum, in_one_line_scope)
129132
return msl
130133
endfunction
131134

135+
" Find if the string is inside var statement (but not the first string)
136+
function s:InVarStatement(lnum)
137+
let lnum = s:PrevNonBlankNonString(a:lnum - 1)
138+
139+
while lnum > 0
140+
let line = getline(lnum)
141+
142+
" if line has var statement, return it's number
143+
if ( line =~ s:var_regex )
144+
return lnum
145+
endif
146+
147+
" if line has brackets or semi-colon, return 0
148+
if ( line =~ '[{};]' )
149+
return 0
150+
endif
151+
152+
let lnum = s:PrevNonBlankNonString(lnum - 1)
153+
endwhile
154+
155+
return 0
156+
endfunction
157+
158+
" Find line above with beginning of the var statement or returns 0 if it's not
159+
" this statement
160+
function s:GetVarIndent(lnum)
161+
let lvar = s:InVarStatement(a:lnum)
162+
let prev_lnum = s:PrevNonBlankNonString(a:lnum - 1)
163+
let prev_lvar = s:InVarStatement(prev_lnum)
164+
165+
if ( lvar )
166+
return indent(lvar) + &sw
167+
endif
168+
169+
if ( prev_lvar )
170+
return indent(prev_lvar)
171+
endif
172+
173+
return 'null'
174+
175+
endfunction
176+
177+
132178
" Check if line 'lnum' has more opening brackets than closing ones.
133179
function s:LineHasOpeningBrackets(lnum)
134180
let open_0 = 0
@@ -248,6 +294,12 @@ function GetJavascriptIndent()
248294
return cindent(v:lnum)
249295
endif
250296

297+
" Work with var statements' blocks
298+
let var_indent = s:GetVarIndent(v:lnum)
299+
if var_indent != 'null'
300+
return var_indent
301+
endif
302+
251303
" 3.3. Work on the previous line. {{{2
252304
" -------------------------------
253305

0 commit comments

Comments
 (0)