3 " Maintainer: Darrick Wiebe <darrick at innatesoftware.com>
4 " URL: http://github.com/pangloss/vim-javascript
6 " Last Change: August 31, 2009
7 " Acknowledgement: Based off of vim-ruby maintained by Nikolai Weibull http://vim-ruby.rubyforge.org
9 " 0. Initialization {{{1
12 " Only load this indent file when no other was loaded.
13 if exists("b:did_indent")
18 setlocal nosmartindent
20 " Now, set up our indentation expression and keys that trigger it.
21 setlocal indentexpr=GetJavascriptIndent()
22 setlocal indentkeys=0{,0},0),0],!^F,o,O,e
24 " Only define the function once.
25 if exists("*GetJavascriptIndent")
35 " Regex of syntax group names that are or delimit string or are comments.
36 let s:syng_strcom = 'javaScript\%(String\|RegexpString\|CommentTodo\|LineComment\|Comment\|DocComment\)'
38 " Regex of syntax group names that are strings.
39 let s:syng_string = 'javaScript\%(RegexpString\)'
41 " Regex of syntax group names that are strings or documentation.
42 let s:syng_stringdoc = 'javaScriptDocComment\|javaScriptComment'
44 " Expression used to check whether we should skip a match with searchpair().
45 let s:skip_expr = "synIDattr(synID(line('.'),col('.'),1),'name') =~ '".s:syng_strcom."'"
47 let s:line_term = '\s*\%(\%(\/\/\).*\)\=$'
49 " Regex that defines continuation lines, not including (, {, or [.
50 let s:continuation_regex = '\%([\\*+/.:]\|\%(<%\)\@<![=-]\|\W[|&?]\|||\|&&\)' . s:line_term
52 " Regex that defines continuation lines.
53 " TODO: this needs to deal with if ...: and so on
54 let s:msl_regex = '\%([\\*+/.:([]\|\%(<%\)\@<![=-]\|\W[|&?]\|||\|&&\)' . s:line_term
56 let s:one_line_scope_regex = '\<\%(if\|else\|for\|while\)\>[^{;]*' . s:line_term
58 " Regex that defines blocks.
59 let s:block_regex = '\%({\)\s*\%(|\%([*@]\=\h\w*,\=\s*\)\%(,\s*[*@]\=\h\w*\)*|\)\=' . s:line_term
61 " 2. Auxiliary Functions {{{1
62 " ======================
64 " Check if the character at lnum:col is inside a string, comment, or is ascii.
65 function s:IsInStringOrComment(lnum, col)
66 return synIDattr(synID(a:lnum, a:col, 1), 'name') =~ s:syng_strcom
69 " Check if the character at lnum:col is inside a string.
70 function s:IsInString(lnum, col)
71 return synIDattr(synID(a:lnum, a:col, 1), 'name') =~ s:syng_string
74 " Check if the character at lnum:col is inside a string or documentation.
75 function s:IsInStringOrDocumentation(lnum, col)
76 return synIDattr(synID(a:lnum, a:col, 1), 'name') =~ s:syng_stringdoc
79 " Find line above 'lnum' that isn't empty, in a comment, or in a string.
80 function s:PrevNonBlankNonString(lnum)
82 let lnum = prevnonblank(a:lnum)
84 " Go in and out of blocks comments as necessary.
85 " If the line isn't empty (with opt. comment) or in a string, end search.
86 let line = getline(lnum)
93 elseif !in_block && line =~ '\*/'
95 elseif !in_block && line !~ '^\s*\%(//\).*$' && !(s:IsInStringOrComment(lnum, 1) && s:IsInStringOrComment(lnum, strlen(line)))
98 let lnum = prevnonblank(lnum - 1)
103 " Find line above 'lnum' that started the continuation 'lnum' may be part of.
104 function s:GetMSL(lnum, in_one_line_scope)
105 " Start on the line we're at and use its indent.
107 let lnum = s:PrevNonBlankNonString(a:lnum - 1)
109 " If we have a continuation line, or we're in a string, use line as MSL.
110 " Otherwise, terminate search as we have found our MSL already.
111 let line = getline(lnum)
112 let col = match(line, s:msl_regex) + 1
113 if (col > 0 && !s:IsInStringOrComment(lnum, col)) || s:IsInString(lnum, strlen(line))
116 " Don't use lines that are part of a one line scope as msl unless the
117 " flag in_one_line_scope is set to 1
119 if a:in_one_line_scope
122 let msl_one_line = s:Match(lnum, s:one_line_scope_regex)
127 let lnum = s:PrevNonBlankNonString(lnum - 1)
132 " Check if line 'lnum' has more opening brackets than closing ones.
133 function s:LineHasOpeningBrackets(lnum)
137 let line = getline(a:lnum)
138 let pos = match(line, '[][(){}]', 0)
140 if !s:IsInStringOrComment(a:lnum, pos + 1)
141 let idx = stridx('(){}[]', line[pos])
143 let open_{idx} = open_{idx} + 1
145 let open_{idx - 1} = open_{idx - 1} - 1
148 let pos = match(line, '[][(){}]', pos + 1)
150 return (open_0 > 0) . (open_2 > 0) . (open_4 > 0)
153 function s:Match(lnum, regex)
154 let col = match(getline(a:lnum), a:regex) + 1
155 return col > 0 && !s:IsInStringOrComment(a:lnum, col) ? col : 0
158 function s:IndentWithContinuation(lnum, ind, width)
159 " Set up variables to use and search for MSL to the previous line.
161 let lnum = s:GetMSL(a:lnum, 1)
162 let line = getline(line)
164 " If the previous line wasn't a MSL and is continuation return its indent.
165 " TODO: the || s:IsInString() thing worries me a bit.
167 if s:Match(p_lnum,s:continuation_regex)||s:IsInString(p_lnum,strlen(line))
172 " Set up more variables now that we know we aren't continuation bound.
173 let msl_ind = indent(lnum)
175 " If the previous line ended with [*+/.-=], start a continuation that
176 " indents an extra level.
177 if s:Match(lnum, s:continuation_regex)
179 return msl_ind + a:width
188 function s:InOneLineScope(lnum)
189 let msl = s:GetMSL(a:lnum, 1)
190 if msl > 0 && s:Match(msl, s:one_line_scope_regex)
196 function s:ExitingOneLineScope(lnum)
197 let msl = s:GetMSL(a:lnum, 1)
199 " if the current line is in a one line scope ..
200 if s:Match(msl, s:one_line_scope_regex)
203 let prev_msl = s:GetMSL(msl - 1, 1)
204 if s:Match(prev_msl, s:one_line_scope_regex)
212 " 3. GetJavascriptIndent Function {{{1
213 " =========================
215 function GetJavascriptIndent()
219 " Set up variables for restoring position in file. Could use v:lnum here.
222 " 3.2. Work on the current line {{{2
223 " -----------------------------
225 " Get the current line.
226 let line = getline(v:lnum)
230 " If we got a closing bracket on an empty line, find its match and indent
231 " according to it. For parentheses we indent to its column - 1, for the
232 " others we indent to the containing line's MSL's level. Return -1 if fail.
233 let col = matchend(line, '^\s*[]})]')
234 if col > 0 && !s:IsInStringOrComment(v:lnum, col)
235 call cursor(v:lnum, col)
236 let bs = strpart('(){}[]', stridx(')}]', line[col - 1]) * 2, 2)
237 if searchpair(escape(bs[0], '\['), '', bs[1], 'bW', s:skip_expr) > 0
238 if line[col-1]==')' && col('.') != col('$') - 1
239 let ind = virtcol('.')-1
241 let ind = indent(s:GetMSL(line('.'), 0))
247 " If we have a /* or */ set indent to first column.
248 if match(line, '^\s*\%(/\*\|\*/\)$') != -1
252 " If we are in a multi-line string or line-comment, don't do anything to it.
253 if s:IsInStringOrDocumentation(v:lnum, matchend(line, '^\s*') + 1)
257 " 3.3. Work on the previous line. {{{2
258 " -------------------------------
260 " Find a non-blank, non-multi-line string line above the current line.
261 let lnum = s:PrevNonBlankNonString(v:lnum - 1)
263 " If the line is empty and inside a string, use the previous line.
264 if line =~ '^\s*$' && lnum != prevnonblank(v:lnum - 1)
265 return indent(prevnonblank(v:lnum))
268 " At the start of the file use zero indent.
273 " Set up variables for current line.
274 let line = getline(lnum)
275 let ind = indent(lnum)
277 " If the previous line ended with a block opening, add a level of indent.
278 if s:Match(lnum, s:block_regex)
279 return indent(s:GetMSL(lnum, 0)) + &sw
282 " If the previous line contained an opening bracket, and we are still in it,
283 " add indent depending on the bracket type.
285 let counts = s:LineHasOpeningBrackets(lnum)
286 if counts[0] == '1' && searchpair('(', '', ')', 'bW', s:skip_expr) > 0
287 if col('.') + 1 == col('$')
292 elseif counts[1] == '1' || counts[2] == '1'
295 call cursor(v:lnum, vcol)
299 " 3.4. Work on the MSL line. {{{2
300 " --------------------------
303 let ind = s:IndentWithContinuation(lnum, ind_con, &sw)
308 let ols = s:InOneLineScope(lnum)
312 let ols = s:ExitingOneLineScope(lnum)
313 while ols > 0 && ind > 0
315 let ols = s:InOneLineScope(ols - 1)
324 let &cpo = s:cpo_save
327 " vim:set sw=2 sts=2 ts=8 noet: