11" Vim indent file
22" Language: Javascript
3- " Maintainer: vim-javascript community
4- " URL: https://github.com/pangloss/vim-javascript
3+ " Maintainer: vim-javascript community
4+ " URL: https://github.com/pangloss/vim-javascript
55" Acknowledgement: Based off of vim-ruby maintained by Nikolai Weibull http://vim-ruby.rubyforge.org
6- " Last Change: July 29 , 2016
6+ " Last Change: July 30 , 2016
77
88" Only load this indent file when no other was loaded.
99if exists (" b:did_indent" )
@@ -29,13 +29,13 @@ set cpo&vim
2929
3030" Get shiftwidth value
3131if exists (' *shiftwidth' )
32- func s: sw ()
32+ function s: sw ()
3333 return shiftwidth ()
34- endfunc
34+ endfunction
3535else
36- func s: sw ()
36+ function s: sw ()
3737 return &sw
38- endfunc
38+ endfunction
3939endif
4040
4141let s: line_pre = ' ^\s*\%(\/\*.*\*\/\s*\)*'
@@ -49,15 +49,15 @@ let s:syng_comment = '\%(comment\|doc\)\c'
4949" Expression used to check whether we should skip a match with searchpair().
5050let s: skip_expr = " line('.') < (prevnonblank(v:lnum) - 2000) ? dummy : s:IsSyn(line('.'),col('.'),'')"
5151
52- func s: lookForParens (start ,end ,flags,time)
52+ function s: lookForParens (start ,end ,flags,time)
5353 try
5454 return searchpair (a: start ,' ' ,a: end ,a: flags ,s: skip_expr ,0 ,a: time )
5555 catch /E118/
5656 return searchpair (a: start ,' ' ,a: end ,a: flags ,0 ,0 )
5757 endtry
58- endfunc
58+ endfunction
5959
60- let s: line_term = ' \s*\%(\/\*.*\*\/\s*\)*\%(:\@<!\/\/.*\)\= $'
60+ let s: line_term = ' \s*\%(\/\*.*\*\/\s*\)*$'
6161
6262" configurable regexes that define continuation lines, not including (, {, or [.
6363if ! exists (' g:javascript_opfirst' )
@@ -72,7 +72,8 @@ let g:javascript_continuation .= s:line_term
7272
7373function s: Onescope (lnum,text,add )
7474 return a: text = ~ ' \%(\<else\|\<do\|=>' . (a: add ? ' \|\<try\|\<finally' : ' ' ) . ' \)\C' . s: line_term ||
75- \ ((a: add && a: text = ~ s: line_pre . s: line_term && search (' \%' . s: PrevCodeLine (a: lnum - 1 ) . ' l.)' . s: line_term )) ||
75+ \ ((a: add && a: text = ~ s: line_pre . s: line_term && cursor (s: PrevCodeLine (a: lnum - 1 ),1 ) > -1 &&
76+ \ cursor (line (' .' ),match (s: Stripline (getline (line (' .' ))), ' )' . s: line_term ))>-1 ) ||
7677 \ cursor (a: lnum , match (a: text , ' )' . s: line_term )) > -1 ) &&
7778 \ s: lookForParens (' (' , ' )' , ' cbW' , 100 ) > 0 &&
7879 \ search ((a: add ? ' \%(function\*\|[A-Za-z_$][0-9A-Za-z_$]*\)\C' :
@@ -82,6 +83,11 @@ endfunction
8283
8384" Auxiliary Functions {{{2
8485
86+ " strip line of comment
87+ function s: StripLine (c )
88+ return substitute (a: c , ' \%(:\@<!\/\/.*\)$' , ' ' ,' ' )
89+ endfunction
90+
8591" Check if the character at lnum:col is inside a string, comment, or is ascii.
8692function s: IsSyn (lnum, col , reg )
8793 return synIDattr (synID (a: lnum , a: col , 1 ), ' name' ) = ~? (a: reg != ' ' ? a: reg : s: syng_strcom )
@@ -96,7 +102,7 @@ function s:PrevCodeLine(lnum)
96102 endif
97103 let lnum = prevnonblank (lnum - 1 )
98104 endwhile
99- return lnum
105+ return lnum > 0 ? lnum : -1
100106endfunction
101107
102108" Check if line 'lnum' has more opening brackets than closing ones.
@@ -127,7 +133,7 @@ endfunction
127133function GetJavascriptIndent ()
128134 if ! exists (' b:js_cache' )
129135 let b: js_cache = [0 ,0 ,0 ]
130- end
136+ endif
131137 " Get the current line.
132138 let line = getline (v: lnum )
133139 " previous nonblank line number
@@ -142,7 +148,9 @@ function GetJavascriptIndent()
142148 return cindent (v: lnum )
143149 endif
144150 let lnum = s: PrevCodeLine (v: lnum - 1 )
145- if lnum == 0
151+ let pline = s: StripLine (getline (lnum))
152+ let line = s: StripLine (line )
153+ if lnum <= 0
146154 return 0
147155 endif
148156
@@ -163,7 +171,7 @@ function GetJavascriptIndent()
163171 let num = pcounts[0 ][0 ] = ~ ' 1' ? lnum : b: js_cache [1 ]
164172 if pcounts[0 ][0 ] = ~' 1'
165173 call cursor (lnum,pcounts[0 ][1 ])
166- end
174+ endif
167175 else
168176 call cursor (v: lnum ,1 )
169177 let syns = synIDattr (synID (v: lnum , 1 , 1 ), ' name' )
@@ -173,24 +181,24 @@ function GetJavascriptIndent()
173181 let num = s: lookForParens (pattern[0 ],pattern[1 ],' bW' ,2000 )
174182 else
175183 let num = s: lookForParens (' (\|{\|\[' ,' )\|}\|\]' ,' bW' ,2000 )
176- end
177- end
184+ endif
185+ endif
178186 let b: js_cache = [v: lnum ,num,line (' .' ) == v: lnum ? b: js_cache [2 ] : col (' .' )]
179187
180188 " most significant part
181189 if line = ~ s: line_pre . ' [])}]'
182190 return indent (num)
183- end
184- let inb = num == 0 ? 1 : s: Onescope (num, strpart (getline (num),0 ,b: js_cache [2 ] - 1 ),1 )
191+ endif
192+ let inb = num == 0 ? 1 : s: Onescope (num, s: StripLine ( strpart (getline (num),0 ,b: js_cache [2 ] - 1 ) ),1 )
185193 let switch_offset = (! inb || num == 0 ) || expand (" <cword>" ) != ' switch' ? 0 : &cino !~ ' :' || ! has (' float' ) ? s: sw () :
186194 \ float2nr (str2float (matchstr (&cino ,' .*:\zs[-0-9.]*' )) * (match (&cino ,' .*:\zs[^,]*s' ) ? s: sw () : 1 ))
187195 if ((line = ~ g: javascript_opfirst ||
188- \ (getline (lnum) = ~ g: javascript_continuation && getline (lnum) !~ s: expr_case )) &&
189- \ inb) || (s: Onescope (lnum,getline (lnum) ,0 ) && line !~ s: line_pre . ' {' )
196+ \ (pline = ~ g: javascript_continuation && pline !~ s: expr_case )) &&
197+ \ inb) || (s: Onescope (lnum,pline ,0 ) && line !~ s: line_pre . ' {' )
190198 return (num > 0 ? indent (num) : - s: sw ()) + (s: sw () * 2 ) + switch_offset
191199 elseif num > 0
192200 return indent (num) + s: sw () + switch_offset
193- end
201+ endif
194202
195203endfunction
196204
0 commit comments