1
1
" Vim indent file
2
2
" 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
5
5
" 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
7
7
8
8
" Only load this indent file when no other was loaded.
9
9
if exists (" b:did_indent" )
@@ -29,13 +29,13 @@ set cpo&vim
29
29
30
30
" Get shiftwidth value
31
31
if exists (' *shiftwidth' )
32
- func s: sw ()
32
+ function s: sw ()
33
33
return shiftwidth ()
34
- endfunc
34
+ endfunction
35
35
else
36
- func s: sw ()
36
+ function s: sw ()
37
37
return &sw
38
- endfunc
38
+ endfunction
39
39
endif
40
40
41
41
let s: line_pre = ' ^\s*\%(\/\*.*\*\/\s*\)*'
@@ -49,15 +49,15 @@ let s:syng_comment = '\%(comment\|doc\)\c'
49
49
" Expression used to check whether we should skip a match with searchpair().
50
50
let s: skip_expr = " line('.') < (prevnonblank(v:lnum) - 2000) ? dummy : s:IsSyn(line('.'),col('.'),'')"
51
51
52
- func s: lookForParens (start ,end ,flags,time)
52
+ function s: lookForParens (start ,end ,flags,time)
53
53
try
54
54
return searchpair (a: start ,' ' ,a: end ,a: flags ,s: skip_expr ,0 ,a: time )
55
55
catch /E118/
56
56
return searchpair (a: start ,' ' ,a: end ,a: flags ,0 ,0 )
57
57
endtry
58
- endfunc
58
+ endfunction
59
59
60
- let s: line_term = ' \s*\%(\/\*.*\*\/\s*\)*\%(:\@<!\/\/.*\)\= $'
60
+ let s: line_term = ' \s*\%(\/\*.*\*\/\s*\)*$'
61
61
62
62
" configurable regexes that define continuation lines, not including (, {, or [.
63
63
if ! exists (' g:javascript_opfirst' )
@@ -72,7 +72,8 @@ let g:javascript_continuation .= s:line_term
72
72
73
73
function s: Onescope (lnum,text,add )
74
74
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 ) ||
76
77
\ cursor (a: lnum , match (a: text , ' )' . s: line_term )) > -1 ) &&
77
78
\ s: lookForParens (' (' , ' )' , ' cbW' , 100 ) > 0 &&
78
79
\ search ((a: add ? ' \%(function\*\|[A-Za-z_$][0-9A-Za-z_$]*\)\C' :
@@ -82,6 +83,11 @@ endfunction
82
83
83
84
" Auxiliary Functions {{{2
84
85
86
+ " strip line of comment
87
+ function s: StripLine (c )
88
+ return substitute (a: c , ' \%(:\@<!\/\/.*\)$' , ' ' ,' ' )
89
+ endfunction
90
+
85
91
" Check if the character at lnum:col is inside a string, comment, or is ascii.
86
92
function s: IsSyn (lnum, col , reg )
87
93
return synIDattr (synID (a: lnum , a: col , 1 ), ' name' ) = ~? (a: reg != ' ' ? a: reg : s: syng_strcom )
@@ -96,7 +102,7 @@ function s:PrevCodeLine(lnum)
96
102
endif
97
103
let lnum = prevnonblank (lnum - 1 )
98
104
endwhile
99
- return lnum
105
+ return lnum > 0 ? lnum : -1
100
106
endfunction
101
107
102
108
" Check if line 'lnum' has more opening brackets than closing ones.
@@ -127,7 +133,7 @@ endfunction
127
133
function GetJavascriptIndent ()
128
134
if ! exists (' b:js_cache' )
129
135
let b: js_cache = [0 ,0 ,0 ]
130
- end
136
+ endif
131
137
" Get the current line.
132
138
let line = getline (v: lnum )
133
139
" previous nonblank line number
@@ -142,7 +148,9 @@ function GetJavascriptIndent()
142
148
return cindent (v: lnum )
143
149
endif
144
150
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
146
154
return 0
147
155
endif
148
156
@@ -163,7 +171,7 @@ function GetJavascriptIndent()
163
171
let num = pcounts[0 ][0 ] = ~ ' 1' ? lnum : b: js_cache [1 ]
164
172
if pcounts[0 ][0 ] = ~' 1'
165
173
call cursor (lnum,pcounts[0 ][1 ])
166
- end
174
+ endif
167
175
else
168
176
call cursor (v: lnum ,1 )
169
177
let syns = synIDattr (synID (v: lnum , 1 , 1 ), ' name' )
@@ -173,24 +181,24 @@ function GetJavascriptIndent()
173
181
let num = s: lookForParens (pattern[0 ],pattern[1 ],' bW' ,2000 )
174
182
else
175
183
let num = s: lookForParens (' (\|{\|\[' ,' )\|}\|\]' ,' bW' ,2000 )
176
- end
177
- end
184
+ endif
185
+ endif
178
186
let b: js_cache = [v: lnum ,num,line (' .' ) == v: lnum ? b: js_cache [2 ] : col (' .' )]
179
187
180
188
" most significant part
181
189
if line = ~ s: line_pre . ' [])}]'
182
190
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 )
185
193
let switch_offset = (! inb || num == 0 ) || expand (" <cword>" ) != ' switch' ? 0 : &cino !~ ' :' || ! has (' float' ) ? s: sw () :
186
194
\ float2nr (str2float (matchstr (&cino ,' .*:\zs[-0-9.]*' )) * (match (&cino ,' .*:\zs[^,]*s' ) ? s: sw () : 1 ))
187
195
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 . ' {' )
190
198
return (num > 0 ? indent (num) : - s: sw ()) + (s: sw () * 2 ) + switch_offset
191
199
elseif num > 0
192
200
return indent (num) + s: sw () + switch_offset
193
- end
201
+ endif
194
202
195
203
endfunction
196
204
0 commit comments