6363 endfunction
6464endif
6565
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+
6679function s: Trim (ln )
6780 let pline = substitute (getline (a: ln ),' \s*$' ,' ' ,' ' )
6881 let l: max = max ([strridx (pline,' //' ),strridx (pline,' /*' ),0 ])
@@ -80,10 +93,15 @@ let s:continuation = get(g:,'javascript_continuation',
8093 \ ' \%([<=,.?/*^%|&:]\|+\@<!+\|-\@<!-\|=\@<!>\|\<in\%(stanceof\)\=\)' ) . ' $'
8194
8295function 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
87105endfunction
88106
89107function s: iscontOne (i ,num,cont)
@@ -94,7 +112,7 @@ function s:iscontOne(i,num,cont)
94112 while l: i >= l: num && (! l: cont || ind > pind)
95113 if indent (l: i ) < ind " first line always true for !a:cont, false for !!a:cont
96114 if s: OneScope (l: i ,s: Trim (l: i ))
97- if expand ( ' <cword> ' ) == # ' while' &&
115+ if s: token ( ) == # ' while' &&
98116 \ s: GetPair (' \C\<do\>' ,' \C\<while\>' ,' bW' ,' line2byte(line(".")) + col(".") <'
99117 \ . (line2byte (l: num ) + b: js_cache [2 ]) . ' ||'
100118 \ . s: skip_expr . ' || !s:IsBlock()' ,100 ,l: num ) > 0
@@ -105,10 +123,10 @@ function s:iscontOne(i,num,cont)
105123 elseif ! l: cont
106124 break
107125 endif
108- let ind = indent (l: i )
109126 elseif ! a: cont
110127 break
111128 endif
129+ let ind = min ([ind, indent (l: i )])
112130 let l: i = s: PrevCodeLine (l: i - 1 )
113131 endwhile
114132 return bL
@@ -118,22 +136,20 @@ endfunction
118136function s: IsBlock (... )
119137 let l: ln = get (a: 000 ,0 ,line (' .' ))
120138 if search (' \S' ,' bW' )
121- let char = getline ( ' . ' )[ col ( ' . ' ) -1 ]
139+ let char = s: token ()
122140 let syn = synIDattr (synID (line (' .' ),col (' .' )- (char == ' {' ),0 ),' name' )
123141 if syn = ~? ' \%(xml\|jsx\)'
124142 return char != ' {'
125143 elseif syn = ~? ' comment'
126144 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 ))
130145 elseif char == ' >'
131146 return getline (' .' )[col (' .' )-2 ] == ' =' || syn = ~? ' ^jsflow'
132147 elseif char == ' :'
133148 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 () !~ ' [,{]' )
135150 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 ))
137153 endif
138154 return 1
139155endfunction
@@ -215,7 +231,7 @@ function GetJavascriptIndent()
215231 endif
216232
217233 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 () == ' )'
219235 call s: GetPair (' (' ,' )' ,' bW' ,s: skip_expr ,200 )
220236 endif
221237 return indent (line (' .' ))
@@ -226,10 +242,10 @@ function GetJavascriptIndent()
226242
227243 let [s: W , pline, isOp, stmt, bL, switch_offset] = [s: sw (), s: Trim (l: lnum ),0 ,0 ,0 ,0 ]
228244 if num
229- if getline ( ' . ' )[ col ( ' . ' ) -1 ] == ' {'
245+ if s: current_char () == ' {'
230246 if search (' )\_s*\%#' ,' bW' )
231247 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'
233249 let switch_offset = &cino !~ ' :' || ! has (' float' ) ? s: W :
234250 \ float2nr (str2float (matchstr (&cino ,' .*:\zs[-0-9.]*' )) * (&cino = ~# ' .*:[^,]*s' ? s: W : 1 ))
235251 if l: line = ~# ' ^' . s: expr_case
0 commit comments