63
63
endfunction
64
64
endif
65
65
66
+ function s: current_char ()
67
+ return getline (' .' )[col (' .' )-1 ]
68
+ endfunction
69
+
70
+ function s: token ()
71
+ return s: current_char () = ~ ' \w' ? expand (' <cword>' ) : s: current_char ()
72
+ endfunction
73
+
74
+ " NOTE: moves the cursor
75
+ function s: previous_token ()
76
+ return search (' \<\|[^[:alnum:]_$[:space:]]' ,' bW' ) ? s: token () : ' '
77
+ endfunction
78
+
66
79
function s: Trim (ln )
67
80
let pline = substitute (getline (a: ln ),' \s*$' ,' ' ,' ' )
68
81
let l: max = max ([strridx (pline,' //' ),strridx (pline,' /*' ),0 ])
@@ -80,10 +93,15 @@ let s:continuation = get(g:,'javascript_continuation',
80
93
\ ' \%([<=,.?/*^%|&:]\|+\@<!+\|-\@<!-\|=\@<!>\|\<in\%(stanceof\)\=\)' ) . ' $'
81
94
82
95
function s: OneScope (lnum,text)
83
- return cursor (a: lnum , match (' ' . a: text , ' \%(\<else\|\<do\|=>\)$' )) + 1 ||
84
- \ cursor (a: lnum , match (' ' . a: text , ' )$' )) + 1 &&
85
- \ s: GetPair (' (' , ' )' , ' bW' , s: skip_expr , 100 ) > 0 &&
86
- \ search (' \C\<\%(for\%(\_s\+\%(await\|each\)\)\=\|if\|let\|w\%(hile\|ith\)\)\_s*\%#' ,' bW' )
96
+ if cursor (a: lnum , match (' ' . a: text , ' )$' )) + 1 &&
97
+ \ s: GetPair (' (' , ' )' , ' bW' , s: skip_expr , 100 ) > 0
98
+ let token = s: previous_token ()
99
+ if index (split (' await each' ),token) + 1
100
+ return s: previous_token () == # ' for'
101
+ endif
102
+ return index (split (' for if let while with' ),token) + 1
103
+ endif
104
+ return cursor (a: lnum , match (' ' . a: text , ' \%(\<else\|\<do\|=>\)$\C' )) + 1
87
105
endfunction
88
106
89
107
function s: iscontOne (i ,num,cont)
@@ -94,7 +112,7 @@ function s:iscontOne(i,num,cont)
94
112
while l: i >= l: num && (! l: cont || ind > pind)
95
113
if indent (l: i ) < ind " first line always true for !a:cont, false for !!a:cont
96
114
if s: OneScope (l: i ,s: Trim (l: i ))
97
- if expand ( ' <cword> ' ) == # ' while' &&
115
+ if s: token ( ) == # ' while' &&
98
116
\ s: GetPair (' \C\<do\>' ,' \C\<while\>' ,' bW' ,' line2byte(line(".")) + col(".") <'
99
117
\ . (line2byte (l: num ) + b: js_cache [2 ]) . ' ||'
100
118
\ . s: skip_expr . ' || !s:IsBlock()' ,100 ,l: num ) > 0
@@ -105,10 +123,10 @@ function s:iscontOne(i,num,cont)
105
123
elseif ! l: cont
106
124
break
107
125
endif
108
- let ind = indent (l: i )
109
126
elseif ! a: cont
110
127
break
111
128
endif
129
+ let ind = min ([ind, indent (l: i )])
112
130
let l: i = s: PrevCodeLine (l: i - 1 )
113
131
endwhile
114
132
return bL
@@ -118,22 +136,20 @@ endfunction
118
136
function s: IsBlock (... )
119
137
let l: ln = get (a: 000 ,0 ,line (' .' ))
120
138
if search (' \S' ,' bW' )
121
- let char = getline ( ' . ' )[ col ( ' . ' ) -1 ]
139
+ let char = s: token ()
122
140
let syn = synIDattr (synID (line (' .' ),col (' .' )- (char == ' {' ),0 ),' name' )
123
141
if syn = ~? ' \%(xml\|jsx\)'
124
142
return char != ' {'
125
143
elseif syn = ~? ' comment'
126
144
return search (' \/[/*]' ,' bW' ) && s: IsBlock (l: ln )
127
- elseif char = ~# ' \a'
128
- return index (split (' return const let import export yield default delete var void typeof throw new in instanceof' )
129
- \ , expand (' <cword>' )) < (0 + (line (' .' ) != l: ln ))
130
145
elseif char == ' >'
131
146
return getline (' .' )[col (' .' )-2 ] == ' =' || syn = ~? ' ^jsflow'
132
147
elseif char == ' :'
133
148
return cursor (0 ,match (' ' . strpart (getline (' .' ),0 ,col (' .' )),' .*\zs' . s: expr_case . ' $' )) + 1 &&
134
- \ (expand (' <cword>' ) !=# ' default' || ! search ( ' \S ' , ' bW ' ) || getline ( ' . ' )[ col ( ' . ' ) -1 ] !~ ' [,{]' )
149
+ \ (expand (' <cword>' ) !=# ' default' || s: previous_token () !~ ' [,{]' )
135
150
endif
136
- return stridx (' -=~!<*+,/?^%|&([' ,char) < 0
151
+ return index (split (' return const let import export yield default delete var void typeof throw new in instanceof'
152
+ \ . ' - = ~ ! < * + , / ? ^ % | & ( [' ), char) < (0 + (line (' .' ) != l: ln ))
137
153
endif
138
154
return 1
139
155
endfunction
@@ -215,7 +231,7 @@ function GetJavascriptIndent()
215
231
endif
216
232
217
233
if idx + 1
218
- if idx == 2 && search (' \S' ,' bW' ,line (' .' )) && getline ( ' . ' )[ col ( ' . ' ) -1 ] == ' )'
234
+ if idx == 2 && search (' \S' ,' bW' ,line (' .' )) && s: current_char () == ' )'
219
235
call s: GetPair (' (' ,' )' ,' bW' ,s: skip_expr ,200 )
220
236
endif
221
237
return indent (line (' .' ))
@@ -226,10 +242,10 @@ function GetJavascriptIndent()
226
242
227
243
let [s: W , pline, isOp, stmt, bL, switch_offset] = [s: sw (), s: Trim (l: lnum ),0 ,0 ,0 ,0 ]
228
244
if num
229
- if getline ( ' . ' )[ col ( ' . ' ) -1 ] == ' {'
245
+ if s: current_char () == ' {'
230
246
if search (' )\_s*\%#' ,' bW' )
231
247
let stmt = 1
232
- if s: GetPair (' (' , ' )' , ' bW' , s: skip_expr , 100 ) > 0 && search ( ' \C\< switch\_s*\%# ' , ' bW ' )
248
+ if s: GetPair (' (' , ' )' , ' bW' , s: skip_expr , 100 ) > 0 && s: previous_token () == # ' switch'
233
249
let switch_offset = &cino !~ ' :' || ! has (' float' ) ? s: W :
234
250
\ float2nr (str2float (matchstr (&cino ,' .*:\zs[-0-9.]*' )) * (&cino = ~# ' .*:[^,]*s' ? s: W : 1 ))
235
251
if l: line = ~# ' ^' . s: expr_case
0 commit comments