Skip to content

Commit ab5d96b

Browse files
authored
possible reuse of logic (simple lexing) (#570)
reusing of logic
1 parent e895bd1 commit ab5d96b

File tree

1 file changed

+28
-29
lines changed

1 file changed

+28
-29
lines changed

indent/javascript.vim

Lines changed: 28 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ let s:syng_strcom = '\%(string\|regex\|special\|doc\|comment\|template\)\c'
4444
let s:syng_comment = '\%(comment\|doc\)\c'
4545

4646
" Expression used to check whether we should skip a match with searchpair().
47-
let s:skip_expr = "synIDattr(synID(line('.'),col('.'),1),'name') =~ '".s:syng_strcom."'"
47+
let s:skip_expr = "s:IsInStringOrComment(line('.'),col('.'))"
4848

4949
func s:lookForParens(start,end,flags,time)
5050
try
@@ -69,13 +69,14 @@ if !exists('g:javascript_continuation')
6969
endif
7070
let g:javascript_continuation .= s:line_term
7171

72-
function s:Onescope(lnum)
73-
return getline(a:lnum) =~ '\%(\<else\|\<do\|=>\)\C' . s:line_term ||
74-
\ (cursor(a:lnum, match(getline(a:lnum),')' . s:line_term)) > -1 &&
72+
function s:Onescope(lnum,text,add)
73+
return a:text =~ '\%(\<else\|\<do\|=>' . (a:add ? '\|try\|finally' : '' ) . '\)\C' . s:line_term ||
74+
\ (cursor(a:lnum, match(a:text, ')' . s:line_term)) > -1 &&
7575
\ s:lookForParens('(', ')', 'cbW', 100) > 0 &&
7676
\ cursor(line('.'),match( ' ' . strpart(getline(line('.')),0,col('.') - 1),
77-
\ '\<\%(else\|for\%(\s+each\)\=\|if\|let\|while\|with\)\C' . s:line_term)) > -1) &&
78-
\ (expand("<cword>") =~ 'while\C' ? !s:lookForParens('\<do\>\C', '\<while\>\C','bw',100) : 1)
77+
\ (a:add ? '\K\k*' :
78+
\ '\<\%(else\|for\%(\s+each\)\=\|function\*\=\%(\s\+\K\k*\)\=\|if\|let\|switch\|while\|with\)\C') . s:line_term)) > -1) &&
79+
\ (a:add || (expand("<cword>") =~ 'while\C' ? !s:lookForParens('\<do\>\C', '\<while\>\C','bW',100) : 1))
7980
endfunction
8081

8182
" Auxiliary Functions {{{2
@@ -86,11 +87,6 @@ function s:IsInStringOrComment(lnum, col)
8687
return synIDattr(synID(a:lnum, a:col, 1), 'name') =~ s:syng_strcom
8788
endfunction
8889

89-
" Check if the character at lnum:col is inside a multi-line comment.
90-
function s:IsInComment(lnum, col)
91-
return synIDattr(synID(a:lnum, a:col, 1), 'name') =~ s:syng_comment
92-
endfunction
93-
9490
" Find line above 'lnum' that isn't empty, in a comment, or in a string.
9591
function s:PrevNonBlankNonString(lnum)
9692
let lnum = prevnonblank(a:lnum)
@@ -110,26 +106,29 @@ function s:LineHasOpeningBrackets(lnum)
110106
let open_4 = 0
111107
let line = getline(a:lnum)
112108
let pos = match(line, '[][(){}]', 0)
109+
let last = 0
113110
while pos != -1
114111
if !s:IsInStringOrComment(a:lnum, pos + 1)
115112
let idx = stridx('(){}[]', line[pos])
116113
if idx % 2 == 0
117114
let open_{idx} = open_{idx} + 1
115+
let last = pos
118116
else
119117
let open_{idx - 1} = open_{idx - 1} - 1
120118
endif
121119
endif
122120
let pos = match(line, '[][(){}]', pos + 1)
123121
endwhile
124-
return (open_0 > 0 ? 1 : (open_0 == 0 ? 0 : 2)) . (open_2 > 0 ? 1 : (open_2 == 0 ? 0 : 2)) . (open_4 > 0 ? 1 : (open_4 == 0 ? 0 : 2))
122+
return [(open_0 > 0 ? 1 : (open_0 == 0 ? 0 : 2)) . (open_2 > 0 ? 1 : (open_2 == 0 ? 0 : 2)) .
123+
\ (open_4 > 0 ? 1 : (open_4 == 0 ? 0 : 2)), last]
125124
endfunction
126125
" }}}
127126

128127
" GetJavascriptIndent Function
129128
" =========================
130129
function GetJavascriptIndent()
131130
if !exists('b:js_cache')
132-
let b:js_cache = [0,0]
131+
let b:js_cache = [0,0,0]
133132
end
134133
" Get the current line.
135134
let line = getline(v:lnum)
@@ -143,10 +142,10 @@ function GetJavascriptIndent()
143142

144143
" start with strings,comments,etc.{{{2
145144
if (line !~ '^[''"`]' && synIDattr(synID(v:lnum, 1, 1), 'name') =~? 'string\|template') ||
146-
\ (line !~ '^\s*[/*]' && synIDattr(synID(v:lnum, 1, 1), 'name') =~? 'comment')
145+
\ (line !~ '^\s*[/*]' && synIDattr(synID(v:lnum, 1, 1), 'name') =~? s:syng_comment)
147146
return -1
148147
endif
149-
if line !~ '^\%(\/\*\|\s*\/\/\)' && s:IsInComment(v:lnum, 1)
148+
if line !~ '^\%(\/\*\|\s*\/\/\)' && synIDattr(synID(v:lnum, 1, 1), 'name') =~? s:syng_comment)
150149
return cindent(v:lnum)
151150
endif
152151

@@ -161,39 +160,39 @@ function GetJavascriptIndent()
161160

162161
" the containing paren, bracket, curly
163162
let pcounts = [0]
164-
if b:js_cache[0] >= lnum && b:js_cache[0] < v:lnum && b:js_cache[0] &&
165-
\ (b:js_cache[0] > lnum || map(pcounts,'s:LineHasOpeningBrackets(lnum)')[0] !~ '2')
166-
let num = pcounts[0] =~ '1' ? lnum : b:js_cache[1]
163+
if b:js_cache[0] >= lnum && b:js_cache[0] <= v:lnum && b:js_cache[0] &&
164+
\ (b:js_cache[0] > lnum || map(pcounts,'s:LineHasOpeningBrackets(lnum)')[0][0] !~ '2')
165+
let num = pcounts[0][0] =~ '1' ? lnum : b:js_cache[1]
166+
if pcounts[0][0] =~'1'
167+
call cursor(lnum,pcounts[0][1])
168+
end
167169
else
168170
call cursor(v:lnum,1)
169171
let syns = synIDattr(synID(v:lnum, 1, 1), 'name')
170172
if line[0] =~ '\s' && syns != ''
171173
let pattern = syns =~? 'funcblock' ? ['{','}'] : syns =~? 'jsparen' ? ['(',')'] : syns =~? 'jsbracket'? ['\[','\]'] :
172174
\ ['(\|{\|\[',')\|}\|\]']
173-
let num = s:lookForParens(pattern[0],pattern[1],'nbw',2000)
174-
elseif syns != ''
175-
let num = s:lookForParens('(\|{\|\[',')\|}\|\]','nbW',2000)
175+
let num = s:lookForParens(pattern[0],pattern[1],'bW',2000)
176176
else
177-
let num = 0
177+
let num = s:lookForParens('(\|{\|\[',')\|}\|\]','bW',2000)
178178
end
179179
end
180-
let b:js_cache = [v:lnum, num]
180+
let b:js_cache = [v:lnum,num,line('.') == v:lnum ? b:js_cache[2] : col('.')]
181181

182182
" most significant part
183183
if line =~ s:line_pre . '[])}]'
184184
return indent(num)
185185
end
186186
let switch_offset = 0
187187
if index(map(synstack(v:lnum, 1), 'synIDattr( v:val, "name")'),'jsSwitchBlock') > -1
188-
let bnum = search('\<switch\s*(','nbw')
188+
let bnum = search('\<switch\s*(','nbW')
189189
let switch_offset = bnum < num || bnum == lnum ? 0 : &cino !~ ':' || !has('float') ? s:sw() :
190190
\ float2nr(str2float(matchstr(&cino,'.*:\zs[-0-9.]*')) * (match(&cino,'.*:\zs[^,]*s') ? s:sw() : 1))
191191
endif
192-
if (line =~ g:javascript_opfirst ||
193-
\ (getline(lnum) =~ g:javascript_continuation && getline(lnum) !~ s:expr_case) ||
194-
\ (s:Onescope(lnum) && line !~ s:line_pre . '{')) &&
195-
\ (num != lnum &&
196-
\ synIDattr(synID(v:lnum, 1, 1), 'name') !~? 'jsdestructuringblock\|args\|jsbracket\|jsparen\|jsobject')
192+
if ((line =~ g:javascript_opfirst ||
193+
\ (getline(lnum) =~ g:javascript_continuation && getline(lnum) !~ s:expr_case)) &&
194+
\ (num == 0 || s:Onescope(num, strpart(getline(num),0,b:js_cache[2] - 1),1))) ||
195+
\ (s:Onescope(lnum,getline(lnum),0) && line !~ s:line_pre . '{')
197196
return (num > 0 ? indent(num) : -s:sw()) + (s:sw() * 2) + switch_offset
198197
elseif num > 0
199198
return indent(num) + s:sw() + switch_offset

0 commit comments

Comments
 (0)