Skip to content

Commit 81e4e6f

Browse files
authored
small script function changes (#628)
the code is now much less embarassing
1 parent c15142c commit 81e4e6f

File tree

1 file changed

+11
-10
lines changed

1 file changed

+11
-10
lines changed

indent/javascript.vim

+11-10
Original file line numberDiff line numberDiff line change
@@ -71,20 +71,19 @@ endif
7171
let g:javascript_opfirst = s:line_pre . g:javascript_opfirst
7272
let g:javascript_continuation .= s:line_term
7373

74-
function s:OneScope(lnum,text,add)
74+
function s:OneScope(lnum,text)
7575
return a:text =~# '\%(\<else\|\<do\|=>\)' . s:line_term ? 'no b' :
76-
\ ((a:add && a:text =~ s:line_pre . '$' && search('\%' . s:PrevCodeLine(a:lnum - 1) . 'l.)' . s:line_term)) ||
77-
\ cursor(a:lnum, match(a:text, ')' . s:line_term)) > -1) &&
78-
\ s:GetPair('(', ')', 'cbW', 100) > 0 && search('\C\l\+\_s*\%#','bW') &&
79-
\ (a:add || ((expand('<cword>') !=# 'while' || !s:GetPair('\C\<do\>', '\C\<while\>','nbW',100)) &&
80-
\ (expand('<cword>') !=# 'each' || search('\C\<for\_s\+\%#','nbW')))) ? expand('<cword>') : ''
76+
\ cursor(a:lnum, match(' ' . a:text, ')' . s:line_term)) > -1 &&
77+
\ s:GetPair('(', ')', 'bW', 100) > 0 && search('\C\l\+\_s*\%#','bW') &&
78+
\ (expand('<cword>') !=# 'while' || s:GetPair('\C\<do\>', '\C\<while\>','nbW',100) <= 0) &&
79+
\ (expand('<cword>') !=# 'each' || search('\C\<for\_s\+\%#','nbW')) ? expand('<cword>') : ''
8180
endfunction
8281

8382
" https://github.com/sweet-js/sweet.js/wiki/design#give-lookbehind-to-the-reader
8483
function s:IsBlock()
8584
return getline(line('.'))[col('.')-1] == '{' && !search(
8685
\ '\C\%(\<return\s*\|\%([-=~!<*+,.?^%|&\[(]\|=\@<!>\|\*\@<!\/\|\<\%(var\|const\|let\|import\|export\%(\_s\+default\)\=\|yield\|delete\|void\|t\%(ypeof\|hrow\)\|new\|in\%(stanceof\)\=\)\)\_s*\)\%#','bnW') &&
87-
\ (!search('\%({\|:\zs\)\_s*\%#','bW') || search(s:expr_case . '\%#' ,'nbW') || s:IsBlock())
86+
\ (search(s:expr_case . '\_s*\%#','nbW') || !search('[{:]\_s*\%#','bW') || s:IsBlock())
8887
endfunction
8988

9089
" Auxiliary Functions {{{2
@@ -174,14 +173,16 @@ function GetJavascriptIndent()
174173
endif
175174

176175
let pline = substitute(substitute(getline(l:lnum),s:expr_case,'\=repeat(" ",strlen(submatch(0)))',''), '\%(:\@<!\/\/.*\)$', '','')
177-
let switch_offset = num <= 0 || s:OneScope(num, strpart(getline(num),0,b:js_cache[2] - 1),1) !=# 'switch' ? 0 :
178-
\ &cino !~ ':' || !has('float') ? s:sw() :
176+
call cursor(b:js_cache[1],b:js_cache[2])
177+
let switch_offset = num <= 0 || !(search(')\_s*\%#','bW') &&
178+
\ s:GetPair('(', ')', 'bW', 100) > 0 && search('\C\<switch\_s*\%#','bW')) ? 0 :
179+
\ &cino !~ ':' || !has('float') ? s:sw() :
179180
\ float2nr(str2float(matchstr(&cino,'.*:\zs[-0-9.]*')) * (&cino =~# '.*:[^,]*s' ? s:sw() : 1))
180181

181182
" most significant, find the indent amount
182183
let isOp = l:line =~# g:javascript_opfirst || pline =~# g:javascript_continuation
183184
if isOp && (num <= 0 || cursor(b:js_cache[1],b:js_cache[2]) || s:IsBlock()) ||
184-
\ s:OneScope(l:lnum,pline,0) =~# '\<\%(for\|each\|if\|let\|no\sb\|w\%(hile\|ith\)\)\>' &&
185+
\ s:OneScope(l:lnum,pline) =~# '\<\%(for\|each\|if\|let\|no\sb\|w\%(hile\|ith\)\)\>' &&
185186
\ l:line !~ s:line_pre . '{'
186187
return (num > 0 ? indent(num) : -s:sw()) + (s:sw() * 2) + switch_offset
187188
elseif num > 0

0 commit comments

Comments
 (0)