Skip to content

Commit 3a3cae8

Browse files
authored
do while made accurate (#671)
Oval loop
1 parent 31b2fff commit 3a3cae8

File tree

1 file changed

+31
-33
lines changed

1 file changed

+31
-33
lines changed

indent/javascript.vim

Lines changed: 31 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,6 @@ else
3737
endfunction
3838
endif
3939

40-
let s:line_pre = '^\s*\%(\%(\%(\/\*.\{-}\)\=\*\+\/\s*\)\=\)\@>'
41-
4240
let s:expr_case = '\<\%(\%(case\>\s*\S.\{-}\)\|default\)\s*:\C'
4341
" Regex of syntax group names that are or delimit string or are comments.
4442
let s:syng_strcom = '\%(s\%(tring\|pecial\)\|comment\|regex\|doc\|template\)'
@@ -111,7 +109,9 @@ function s:iscontOne(i,num,cont)
111109
if indent(l:i) < ind " first line always true for !a:cont, false for !!a:cont
112110
if s:OneScope(l:i,s:Trimline(l:i))
113111
if expand('<cword>') ==# 'while' &&
114-
\ s:GetPair(s:line_pre . '\C\<do\>','\C\<while\>','bW',s:skip_expr,100,l:num + !!a:num) > 0
112+
\ s:GetPair('\C\<do\>','\C\<while\>','bW','line2byte(line(".")) + col(".") <'
113+
\ . (line2byte(l:num) + b:js_cache[2]) . '||'
114+
\ . s:skip_expr . '|| !s:IsBlock()',100,l:num) > 0
115115
return 0
116116
endif
117117
let bL += 1
@@ -128,38 +128,36 @@ endfunction
128128

129129
" https://github.com/sweet-js/sweet.js/wiki/design#give-lookbehind-to-the-reader
130130
function s:IsBlock()
131-
if getline(line('.'))[col('.')-1] == '{'
132-
let l:ln = line('.')
133-
if search('\S','bW')
131+
let l:ln = line('.')
132+
if search('\S','bW')
133+
let char = getline(line('.'))[col('.')-1]
134+
let pchar = getline(line('.'))[col('.')-2]
135+
let syn = synIDattr(synID(line('.'),col('.')-1,0),'name')
136+
if pchar . char == '*/' && syn =~? 'comment'
137+
if !search('\/\*','bW') || !search('\S','bW')
138+
return 1
139+
endif
134140
let char = getline(line('.'))[col('.')-1]
135141
let pchar = getline(line('.'))[col('.')-2]
136142
let syn = synIDattr(synID(line('.'),col('.')-1,0),'name')
137-
if pchar . char == '*/' && syn =~? 'comment'
138-
if !search('\/\*','bW') || !search('\S','bW')
139-
return 1
140-
endif
141-
let char = getline(line('.'))[col('.')-1]
142-
let pchar = getline(line('.'))[col('.')-2]
143-
let syn = synIDattr(synID(line('.'),col('.')-1,0),'name')
144-
endif
145-
if syn =~? '\%(xml\|jsx\)'
146-
return char != '{'
147-
elseif char =~# '\l'
148-
if line('.') == l:ln && expand('<cword>') ==# 'return'
149-
return 0
150-
endif
151-
return expand('<cword>') !~#
152-
\ '^\%(const\|let\|import\|export\|yield\|de\%(fault\|lete\)\|v\%(ar\|oid\)\|t\%(ypeof\|hrow\)\|new\|in\%(stanceof\)\=\)$'
153-
elseif char == '>'
154-
return pchar == '=' || syn =~? '^jsflow'
155-
elseif char == ':'
156-
return strpart(getline(line('.')),0,col('.')) =~# s:expr_case . '$'
157-
else
158-
return char !~# '[-=~!<*+,/?^%|&([]'
143+
endif
144+
if syn =~? '\%(xml\|jsx\)'
145+
return char != '{'
146+
elseif char =~# '\l'
147+
if line('.') == l:ln && expand('<cword>') ==# 'return'
148+
return 0
159149
endif
150+
return index(split('const let import export yield default delete var void typeof throw new in instanceof')
151+
\ , expand('<cword>')) < 0
152+
elseif char == '>'
153+
return pchar == '=' || syn =~? '^jsflow'
154+
elseif char == ':'
155+
return strpart(getline(line('.')),0,col('.')) =~# s:expr_case . '$'
160156
else
161-
return 1
157+
return char !~# '[-=~!<*+,/?^%|&([]'
162158
endif
159+
else
160+
return 1
163161
endif
164162
endfunction
165163

@@ -254,14 +252,14 @@ function GetJavascriptIndent()
254252
let num = s:GetPair('[({[]','[])}]','bW',s:skip_expr,200,l:lnum)
255253
endif
256254

257-
let num = (num > 0) * num
255+
let num = max([num,0])
258256
if l:line =~ '^[])}]'
259257
return !!num * indent(num)
260258
endif
261-
let b:js_cache = [v:lnum,num,line('.') == v:lnum ? b:js_cache[2] : col('.')]
259+
let b:js_cache = [v:lnum,num,line('.') == v:lnum && num ? b:js_cache[2] : col('.')]
262260

263261
call cursor(v:lnum,1)
264-
if l:line =~# '^while\>' && s:GetPair(s:line_pre . '\C\<do\>','\C\<while\>','bW',s:skip_expr,100,num + 1) > 0
262+
if l:line =~# '^while\>' && s:GetPair('\C\<do\>','\C\<while\>','bW',s:skip_expr . '|| !s:IsBlock()',100,num + 1) > 0
265263
return indent(line('.'))
266264
endif
267265

@@ -277,7 +275,7 @@ function GetJavascriptIndent()
277275
let isOp = l:line =~# g:javascript_opfirst || pline !~# s:expr_case . '$' && pline =~# g:javascript_continuation
278276
let bL = s:iscontOne(l:lnum,num,isOp)
279277
let bL -= (bL && l:line =~ '^{') * s:W
280-
if isOp && (!num || cursor(b:js_cache[1],b:js_cache[2]) || s:IsBlock())
278+
if isOp && (!num || cursor(b:js_cache[1],b:js_cache[2]) || (getline(line('.'))[col('.')-1] == '{' && s:IsBlock()))
281279
return (num ? indent(num) : -s:W) + (s:W * 2) + switch_offset + bL
282280
elseif num
283281
return indent(num) + s:W + switch_offset + bL

0 commit comments

Comments
 (0)