Skip to content

Commit 69f2c87

Browse files
authored
small minification and perf (#675)
1 parent e643047 commit 69f2c87

File tree

1 file changed

+15
-15
lines changed

1 file changed

+15
-15
lines changed

indent/javascript.vim

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
" Language: Javascript
33
" Maintainer: Chris Paul ( https://github.com/bounceme )
44
" URL: https://github.com/pangloss/vim-javascript
5-
" Last Change: October 15, 2016
5+
" Last Change: October 19, 2016
66

77
" Only load this indent file when no other was loaded.
88
if exists('b:did_indent')
@@ -50,7 +50,7 @@ function s:skip_func(lnum)
5050
return !s:free
5151
endif
5252
let s:looksyn = line('.')
53-
return (search('\/','nbW',line('.')) || search('[''"\\]','nW',line('.'))) && eval(s:skip_expr)
53+
return (strridx(getline('.'),'/',col('.')-1) + 1 || search('[''"\\]','nW',s:looksyn)) && eval(s:skip_expr)
5454
endfunction
5555

5656
if has('reltime')
@@ -65,10 +65,10 @@ endif
6565

6666
function s:Trimline(ln)
6767
let pline = substitute(getline(a:ln),'\s*$','','')
68-
let max = max([strridx(pline,'//'),strridx(pline,'/*'),0])
69-
while max && synIDattr(synID(a:ln, strlen(pline), 0), 'name') =~? '\%(comment\|doc\)'
70-
let pline = substitute(strpart(pline, 0, max),'\s*$','','')
71-
let max = max([strridx(pline,'//'),strridx(pline,'/*'),0])
68+
let l:max = max([strridx(pline,'//'),strridx(pline,'/*'),0])
69+
while l:max && synIDattr(synID(a:ln, strlen(pline), 0), 'name') =~? '\%(comment\|doc\)'
70+
let pline = substitute(strpart(pline, 0, l:max),'\s*$','','')
71+
let l:max = max([strridx(pline,'//'),strridx(pline,'/*'),0])
7272
endwhile
7373
return pline
7474
endfunction
@@ -85,8 +85,8 @@ let g:javascript_opfirst = '^' . g:javascript_opfirst
8585
let g:javascript_continuation .= '$'
8686

8787
function s:OneScope(lnum,text)
88-
return cursor(a:lnum, match(' ' . a:text, '\%(\<else\|\<do\|=>\)$')) > -1 ||
89-
\ cursor(a:lnum, match(' ' . a:text, ')$')) > -1 &&
88+
return cursor(a:lnum, match(' ' . a:text, '\%(\<else\|\<do\|=>\)$')) + 1 ||
89+
\ cursor(a:lnum, match(' ' . a:text, ')$')) + 1 &&
9090
\ s:GetPair('(', ')', 'bW', s:skip_expr, 100) > 0 &&
9191
\ search('\C\<\%(for\%(\_s\+\%(await\|each\)\)\=\|if\|let\|w\%(hile\|ith\)\)\_s*\%#','bW')
9292
endfunction
@@ -121,15 +121,15 @@ endfunction
121121
function s:IsBlock()
122122
let l:ln = line('.')
123123
if search('\S','bW')
124-
let char = getline(line('.'))[col('.')-1]
125-
let pchar = getline(line('.'))[col('.')-2]
124+
let char = getline('.')[col('.')-1]
125+
let pchar = getline('.')[col('.')-2]
126126
let syn = synIDattr(synID(line('.'),col('.')-1,0),'name')
127127
if pchar . char == '*/' && syn =~? 'comment'
128128
if !search('\/\*','bW') || !search('\S','bW')
129129
return 1
130130
endif
131-
let char = getline(line('.'))[col('.')-1]
132-
let pchar = getline(line('.'))[col('.')-2]
131+
let char = getline('.')[col('.')-1]
132+
let pchar = getline('.')[col('.')-2]
133133
let syn = synIDattr(synID(line('.'),col('.')-1,0),'name')
134134
endif
135135
if syn =~? '\%(xml\|jsx\)'
@@ -143,7 +143,7 @@ function s:IsBlock()
143143
elseif char == '>'
144144
return pchar == '=' || syn =~? '^jsflow'
145145
elseif char == ':'
146-
return strpart(getline(line('.')),0,col('.')) =~# s:expr_case . '$'
146+
return strpart(getline('.'),0,col('.')) =~# s:expr_case . '$'
147147
else
148148
return char !~# '[-=~!<*+,/?^%|&([]'
149149
endif
@@ -254,7 +254,7 @@ function GetJavascriptIndent()
254254
let s:W = s:sw()
255255
let pline = s:Trimline(l:lnum)
256256
call cursor(b:js_cache[1],b:js_cache[2])
257-
let bchar = getline(b:js_cache[1])[b:js_cache[2]-1] == '{'
257+
let bchar = getline('.')[col('.')-1] == '{'
258258
let switch_offset = !num || !bchar || !(search(')\_s*\%#','bW') &&
259259
\ s:GetPair('(', ')', 'bW', s:skip_expr, 100) > 0 && search('\C\<switch\_s*\%#','bW')) ? 0 :
260260
\ &cino !~ ':' || !has('float') ? s:W :
@@ -264,7 +264,7 @@ function GetJavascriptIndent()
264264
let isOp = l:line =~# g:javascript_opfirst || pline !~# s:expr_case . '$' && pline =~# g:javascript_continuation
265265
let bL = s:iscontOne(l:lnum,num,isOp)
266266
let bL -= (bL && l:line =~ '^{') * s:W
267-
if isOp && (!num || cursor(b:js_cache[1],b:js_cache[2]) || (bchar && s:IsBlock()))
267+
if isOp && (!num || bchar && cursor(b:js_cache[1],b:js_cache[2])+1 && s:IsBlock())
268268
return (num ? indent(num) : -s:W) + (s:W * 2) + switch_offset + bL
269269
elseif num
270270
return indent(num) + s:W + switch_offset + bL

0 commit comments

Comments
 (0)