@@ -44,7 +44,7 @@ let s:syng_strcom = '\%(string\|regex\|special\|doc\|comment\|template\)\c'
44
44
let s: syng_comment = ' \%(comment\|doc\)\c'
45
45
46
46
" Expression used to check whether we should skip a match with searchpair().
47
- let s: skip_expr = " synIDattr(synID( line('.'),col('.'),1),'name') =~ ' " . s: syng_strcom . " ' "
47
+ let s: skip_expr = " s:IsInStringOrComment( line('.'),col('.')) "
48
48
49
49
func s: lookForParens (start ,end ,flags,time)
50
50
try
@@ -69,13 +69,14 @@ if !exists('g:javascript_continuation')
69
69
endif
70
70
let g: javascript_continuation .= s: line_term
71
71
72
- function s: Onescope (lnum)
73
- return getline ( a: lnum ) = ~ ' \%(\<else\|\<do\|=>\)\C' . s: line_term ||
74
- \ (cursor (a: lnum , match (getline ( a: lnum ), ' )' . s: line_term )) > -1 &&
72
+ function s: Onescope (lnum,text, add )
73
+ return a: text = ~ ' \%(\<else\|\<do\|=>' . ( a: add ? ' \|try\|finally ' : ' ' ) . ' \)\C' . s: line_term ||
74
+ \ (cursor (a: lnum , match (a: text , ' )' . s: line_term )) > -1 &&
75
75
\ s: lookForParens (' (' , ' )' , ' cbW' , 100 ) > 0 &&
76
76
\ cursor (line (' .' ),match ( ' ' . strpart (getline (line (' .' )),0 ,col (' .' ) - 1 ),
77
- \ ' \<\%(else\|for\%(\s+each\)\=\|if\|let\|while\|with\)\C' . s: line_term )) > -1 ) &&
78
- \ (expand (" <cword>" ) = ~ ' while\C' ? ! s: lookForParens (' \<do\>\C' , ' \<while\>\C' ,' bw' ,100 ) : 1 )
77
+ \ (a: add ? ' \K\k*' :
78
+ \ ' \<\%(else\|for\%(\s+each\)\=\|function\*\=\%(\s\+\K\k*\)\=\|if\|let\|switch\|while\|with\)\C' ) . s: line_term )) > -1 ) &&
79
+ \ (a: add || (expand (" <cword>" ) = ~ ' while\C' ? ! s: lookForParens (' \<do\>\C' , ' \<while\>\C' ,' bW' ,100 ) : 1 ))
79
80
endfunction
80
81
81
82
" Auxiliary Functions {{{2
@@ -86,11 +87,6 @@ function s:IsInStringOrComment(lnum, col)
86
87
return synIDattr (synID (a: lnum , a: col , 1 ), ' name' ) = ~ s: syng_strcom
87
88
endfunction
88
89
89
- " Check if the character at lnum:col is inside a multi-line comment.
90
- function s: IsInComment (lnum, col )
91
- return synIDattr (synID (a: lnum , a: col , 1 ), ' name' ) = ~ s: syng_comment
92
- endfunction
93
-
94
90
" Find line above 'lnum' that isn't empty, in a comment, or in a string.
95
91
function s: PrevNonBlankNonString (lnum)
96
92
let lnum = prevnonblank (a: lnum )
@@ -110,26 +106,29 @@ function s:LineHasOpeningBrackets(lnum)
110
106
let open_4 = 0
111
107
let line = getline (a: lnum )
112
108
let pos = match (line , ' [][(){}]' , 0 )
109
+ let last = 0
113
110
while pos != -1
114
111
if ! s: IsInStringOrComment (a: lnum , pos + 1 )
115
112
let idx = stridx (' (){}[]' , line [pos])
116
113
if idx % 2 == 0
117
114
let open_{idx} = open_{idx} + 1
115
+ let last = pos
118
116
else
119
117
let open_{idx - 1 } = open_{idx - 1 } - 1
120
118
endif
121
119
endif
122
120
let pos = match (line , ' [][(){}]' , pos + 1 )
123
121
endwhile
124
- return (open_0 > 0 ? 1 : (open_0 == 0 ? 0 : 2 )) . (open_2 > 0 ? 1 : (open_2 == 0 ? 0 : 2 )) . (open_4 > 0 ? 1 : (open_4 == 0 ? 0 : 2 ))
122
+ return [(open_0 > 0 ? 1 : (open_0 == 0 ? 0 : 2 )) . (open_2 > 0 ? 1 : (open_2 == 0 ? 0 : 2 )) .
123
+ \ (open_4 > 0 ? 1 : (open_4 == 0 ? 0 : 2 )), last ]
125
124
endfunction
126
125
" }}}
127
126
128
127
" GetJavascriptIndent Function
129
128
" =========================
130
129
function GetJavascriptIndent ()
131
130
if ! exists (' b:js_cache' )
132
- let b: js_cache = [0 ,0 ]
131
+ let b: js_cache = [0 ,0 , 0 ]
133
132
end
134
133
" Get the current line.
135
134
let line = getline (v: lnum )
@@ -143,10 +142,10 @@ function GetJavascriptIndent()
143
142
144
143
" start with strings,comments,etc.{{{2
145
144
if (line !~ ' ^['' "`]' && synIDattr (synID (v: lnum , 1 , 1 ), ' name' ) = ~? ' string\|template' ) ||
146
- \ (line !~ ' ^\s*[/*]' && synIDattr (synID (v: lnum , 1 , 1 ), ' name' ) = ~? ' comment ' )
145
+ \ (line !~ ' ^\s*[/*]' && synIDattr (synID (v: lnum , 1 , 1 ), ' name' ) = ~? s: syng_comment )
147
146
return -1
148
147
endif
149
- if line !~ ' ^\%(\/\*\|\s*\/\/\)' && s: IsInComment ( v: lnum , 1 )
148
+ if line !~ ' ^\%(\/\*\|\s*\/\/\)' && synIDattr ( synID ( v: lnum , 1 , 1 ), ' name ' ) = ~? s: syng_comment )
150
149
return cindent (v: lnum )
151
150
endif
152
151
@@ -161,39 +160,39 @@ function GetJavascriptIndent()
161
160
162
161
" the containing paren, bracket, curly
163
162
let pcounts = [0 ]
164
- if b: js_cache [0 ] >= lnum && b: js_cache [0 ] < v: lnum && b: js_cache [0 ] &&
165
- \ (b: js_cache [0 ] > lnum || map (pcounts,' s:LineHasOpeningBrackets(lnum)' )[0 ] !~ ' 2' )
166
- let num = pcounts[0 ] = ~ ' 1' ? lnum : b: js_cache [1 ]
163
+ if b: js_cache [0 ] >= lnum && b: js_cache [0 ] <= v: lnum && b: js_cache [0 ] &&
164
+ \ (b: js_cache [0 ] > lnum || map (pcounts,' s:LineHasOpeningBrackets(lnum)' )[0 ][0 ] !~ ' 2' )
165
+ let num = pcounts[0 ][0 ] = ~ ' 1' ? lnum : b: js_cache [1 ]
166
+ if pcounts[0 ][0 ] = ~' 1'
167
+ call cursor (lnum,pcounts[0 ][1 ])
168
+ end
167
169
else
168
170
call cursor (v: lnum ,1 )
169
171
let syns = synIDattr (synID (v: lnum , 1 , 1 ), ' name' )
170
172
if line [0 ] = ~ ' \s' && syns != ' '
171
173
let pattern = syns = ~? ' funcblock' ? [' {' ,' }' ] : syns = ~? ' jsparen' ? [' (' ,' )' ] : syns = ~? ' jsbracket' ? [' \[' ,' \]' ] :
172
174
\ [' (\|{\|\[' ,' )\|}\|\]' ]
173
- let num = s: lookForParens (pattern[0 ],pattern[1 ],' nbw' ,2000 )
174
- elseif syns != ' '
175
- let num = s: lookForParens (' (\|{\|\[' ,' )\|}\|\]' ,' nbW' ,2000 )
175
+ let num = s: lookForParens (pattern[0 ],pattern[1 ],' bW' ,2000 )
176
176
else
177
- let num = 0
177
+ let num = s: lookForParens ( ' (\|{\|\[ ' , ' )\|}\|\] ' , ' bW ' , 2000 )
178
178
end
179
179
end
180
- let b: js_cache = [v: lnum , num]
180
+ let b: js_cache = [v: lnum ,num, line ( ' . ' ) == v: lnum ? b: js_cache [ 2 ] : col ( ' . ' ) ]
181
181
182
182
" most significant part
183
183
if line = ~ s: line_pre . ' [])}]'
184
184
return indent (num)
185
185
end
186
186
let switch_offset = 0
187
187
if index (map (synstack (v: lnum , 1 ), ' synIDattr( v:val, "name")' ),' jsSwitchBlock' ) > -1
188
- let bnum = search (' \<switch\s*(' ,' nbw ' )
188
+ let bnum = search (' \<switch\s*(' ,' nbW ' )
189
189
let switch_offset = bnum < num || bnum == lnum ? 0 : &cino !~ ' :' || ! has (' float' ) ? s: sw () :
190
190
\ float2nr (str2float (matchstr (&cino ,' .*:\zs[-0-9.]*' )) * (match (&cino ,' .*:\zs[^,]*s' ) ? s: sw () : 1 ))
191
191
endif
192
- if (line = ~ g: javascript_opfirst ||
193
- \ (getline (lnum) = ~ g: javascript_continuation && getline (lnum) !~ s: expr_case ) ||
194
- \ (s: Onescope (lnum) && line !~ s: line_pre . ' {' )) &&
195
- \ (num != lnum &&
196
- \ synIDattr (synID (v: lnum , 1 , 1 ), ' name' ) !~? ' jsdestructuringblock\|args\|jsbracket\|jsparen\|jsobject' )
192
+ if ((line = ~ g: javascript_opfirst ||
193
+ \ (getline (lnum) = ~ g: javascript_continuation && getline (lnum) !~ s: expr_case )) &&
194
+ \ (num == 0 || s: Onescope (num, strpart (getline (num),0 ,b: js_cache [2 ] - 1 ),1 ))) ||
195
+ \ (s: Onescope (lnum,getline (lnum),0 ) && line !~ s: line_pre . ' {' )
197
196
return (num > 0 ? indent (num) : - s: sw ()) + (s: sw () * 2 ) + switch_offset
198
197
elseif num > 0
199
198
return indent (num) + s: sw () + switch_offset
0 commit comments