@@ -75,15 +75,38 @@ if !exists('g:javascript_continuation')
75
75
let g: javascript_continuation = ' \%([<=,.?/*^%|&:]\|+\@<!+\|-\@<!-\|=\@<!>\|\<in\%(stanceof\)\=\)'
76
76
endif
77
77
78
- let g: javascript_opfirst = s: line_pre . g: javascript_opfirst
78
+ let g: javascript_opfirst = ' ^ ' . g: javascript_opfirst
79
79
let g: javascript_continuation .= s: line_term
80
80
81
81
function s: OneScope (lnum,text)
82
- return a: text = ~# ' \%(\<else\|\<do\|=>\)' . s: line_term ? ' no b ' :
82
+ return cursor ( a: lnum , match ( ' ' . a: text , ' \%(\<else\|\<do\|=>\)' . s: line_term)) > -1 ||
83
83
\ cursor (a: lnum , match (' ' . a: text , ' )' . s: line_term )) > -1 &&
84
- \ s: GetPair (' (' , ' )' , ' bW' , s: skip_expr , 100 ) > 0 && search (' \C\l\+\_s*\%#' ,' bW' ) &&
85
- \ (expand (' <cword>' ) !=# ' while' || s: GetPair (' \C\<do\>' , ' \C\<while\>' ,' nbW' ,s: skip_expr ,100 ) <= 0 ) &&
86
- \ (expand (' <cword>' ) !=# ' each' || search (' \C\<for\_s\+\%#' ,' nbW' )) ? expand (' <cword>' ) : ' '
84
+ \ s: GetPair (' (' , ' )' , ' bW' , s: skip_expr , 100 ) > 0 &&
85
+ \ search (' \C\<\%(for\%(\_s\+each\)\=\|if\|let\|w\%(hile\|ith\)\)\_s*\%#' ,' bW' )
86
+ endfunction
87
+
88
+ function s: iscontOne (i ,num,cont)
89
+ let [l: i , l: cont , l: num ] = [a: i , a: cont , a: num > 0 ? a: num : 1 ]
90
+ let pind = a: num > 0 ? indent (l: num ) : - s: sw ()
91
+ let ind = indent (l: i ) + (! l: cont ? s: sw () : 0 )
92
+ let bL = 0
93
+ while l: i >= l: num && (! l: cont || ind > pind + s: sw ())
94
+ if indent (l: i ) < ind " first line always true for !cont, false for !!cont
95
+ if s: OneScope (l: i ,substitute (getline (l: i ),' :\@<!\/\/.*' ,' ' ,' ' ))
96
+ if expand (' <cword>' ) == # ' while' && searchpair (s: line_pre . ' \C\<do\>' ,' ' ,' \C\<while\>' ,' bW' ,s: skip_expr ,l: num ,100 ) > 0
97
+ return 0
98
+ endif
99
+ let bL += 1
100
+ let l: cont = 0
101
+ let l: i = line (' .' )
102
+ elseif ! l: cont
103
+ break
104
+ endif
105
+ let ind = indent (l: i )
106
+ endif
107
+ let l: i = s: PrevCodeLine (l: i - 1 )
108
+ endwhile
109
+ return bL * s: sw ()
87
110
endfunction
88
111
89
112
" https://github.com/sweet-js/sweet.js/wiki/design#give-lookbehind-to-the-reader
@@ -138,8 +161,8 @@ function GetJavascriptIndent()
138
161
let syns = synIDattr (synID (v: lnum , 1 , 0 ), ' name' )
139
162
140
163
" start with strings,comments,etc.{{{2
141
- if ( l: line !~ ' ^['' "]' && syns = ~? ' \%(string\|template\)' ) ||
142
- \ ( l: line !~ ' ^\s*[/*]' && syns = ~? s: syng_comment)
164
+ if l: line !~ ' ^['' "]' && syns = ~? ' \%(string\|template\)' ||
165
+ \ l: line !~ ' ^\s*[/*]' && syns = ~? s: syng_comment
143
166
return -1
144
167
endif
145
168
if l: line !~ ' ^\%(\/\*\|\s*\/\/\)' && syns = ~? s: syng_comment
@@ -150,7 +173,7 @@ function GetJavascriptIndent()
150
173
return 0
151
174
endif
152
175
153
- if ( l: line = ~# s: expr_case)
176
+ if l: line = ~# s: expr_case
154
177
let cpo_switch = &cpo
155
178
set cpo += %
156
179
let ind = cindent (v: lnum )
@@ -176,11 +199,16 @@ function GetJavascriptIndent()
176
199
177
200
let b: js_cache = [v: lnum ,num,line (' .' ) == v: lnum ? b: js_cache [2 ] : col (' .' )]
178
201
179
- if l: line = ~ s: line_pre . ' [])}]'
202
+ let l: line = substitute (l: line ,s: line_pre ,' ' ,' ' )
203
+ if l: line = ~ ' ^[])}]'
180
204
return indent (num)
181
205
endif
206
+ call cursor (v: lnum ,1 )
207
+ if l: line = ~# ' ^while\>' && searchpair (s: line_pre . ' \C\<do\>' ,' ' ,' \C\<while\>' ,' bW' ,s: skip_expr ,num,100 ) > 0
208
+ return indent (line (' .' ))
209
+ endif
182
210
183
- let pline = substitute (substitute (getline (l: lnum ),s: expr_case ,' \=repeat(" ",strlen(submatch(0)))' ,' ' ), ' \%( :\@<!\/\/.*\)$ ' , ' ' ,' ' )
211
+ let pline = substitute (substitute (getline (l: lnum ),s: expr_case ,' \=repeat(" ",strlen(submatch(0)))' ,' ' ), ' :\@<!\/\/.*' , ' ' ,' ' )
184
212
call cursor (b: js_cache [1 ],b: js_cache [2 ])
185
213
let switch_offset = num <= 0 || ! (search (' )\_s*\%#' ,' bW' ) &&
186
214
\ s: GetPair (' (' , ' )' , ' bW' , s: skip_expr , 100 ) > 0 && search (' \C\<switch\_s*\%#' ,' bW' )) ? 0 :
@@ -189,14 +217,14 @@ function GetJavascriptIndent()
189
217
190
218
" most significant, find the indent amount
191
219
let isOp = l: line = ~# g: javascript_opfirst || pline = ~# g: javascript_continuation
192
- if isOp && (num <= 0 || cursor ( b: js_cache [ 1 ], b: js_cache [ 2 ]) || s: IsBlock ()) ||
193
- \ s: OneScope (l: lnum ,pline) = ~# ' \<\%(for\|each\|if\|let\|no\sb\|w\%(hile\|ith\)\)\> ' &&
194
- \ l: line !~ s: line_pre . ' { '
195
- return (num > 0 ? indent (num) : - s: sw ()) + (s: sw () * 2 ) + switch_offset
220
+ let bL = s: iscontOne ( l: lnum ,num,isOp)
221
+ let bL = bL ? bL - (l: line = ~ ' ^{ ' ) * s: sw () : bL
222
+ if isOp && (num <= 0 || cursor ( b: js_cache [ 1 ], b: js_cache [ 2 ]) || s: IsBlock ())
223
+ return (num > 0 ? indent (num) : - s: sw ()) + (s: sw () * 2 ) + switch_offset + bL
196
224
elseif num > 0
197
- return indent (num) + s: sw () + switch_offset
225
+ return indent (num) + s: sw () + switch_offset + bL
198
226
endif
199
-
227
+ return bL
200
228
endfunction
201
229
202
230
0 commit comments