Mercurial > vim
comparison runtime/indent/javascript.vim @ 12499:d91cf2e26ef0
Update runtime files.
commit https://github.com/vim/vim/commit/37c64c78fd87e086b5a945ad7032787c274e2dcb
Author: Bram Moolenaar <Bram@vim.org>
Date: Tue Sep 19 22:06:03 2017 +0200
Update runtime files.
author | Christian Brabandt <cb@256bit.org> |
---|---|
date | Tue, 19 Sep 2017 22:15:06 +0200 |
parents | 63b0b7b79b25 |
children | a6d3e2081544 |
comparison
equal
deleted
inserted
replaced
12498:bf98d339b568 | 12499:d91cf2e26ef0 |
---|---|
1 " Vim indent file | 1 " Vim indent file |
2 " Language: Javascript | 2 " Language: Javascript |
3 " Maintainer: Chris Paul ( https://github.com/bounceme ) | 3 " Maintainer: Chris Paul ( https://github.com/bounceme ) |
4 " URL: https://github.com/pangloss/vim-javascript | 4 " URL: https://github.com/pangloss/vim-javascript |
5 " Last Change: March 21, 2017 | 5 " Last Change: September 18, 2017 |
6 | 6 |
7 " Only load this indent file when no other was loaded. | 7 " Only load this indent file when no other was loaded. |
8 if exists('b:did_indent') | 8 if exists('b:did_indent') |
9 finish | 9 finish |
10 endif | 10 endif |
11 let b:did_indent = 1 | 11 let b:did_indent = 1 |
12 | |
13 " indent correctly if inside <script> | |
14 " vim/vim@690afe1 for the switch from cindent | |
15 let b:html_indent_script1 = 'inc' | |
12 | 16 |
13 " Now, set up our indentation expression and keys that trigger it. | 17 " Now, set up our indentation expression and keys that trigger it. |
14 setlocal indentexpr=GetJavascriptIndent() | 18 setlocal indentexpr=GetJavascriptIndent() |
15 setlocal autoindent nolisp nosmartindent | 19 setlocal autoindent nolisp nosmartindent |
16 setlocal indentkeys+=0],0) | 20 setlocal indentkeys+=0],0) |
19 " "+norm! gg=G" '+%print' '+:q!' testfile.js \ | 23 " "+norm! gg=G" '+%print' '+:q!' testfile.js \ |
20 " | diff -uBZ testfile.js - | 24 " | diff -uBZ testfile.js - |
21 | 25 |
22 let b:undo_indent = 'setlocal indentexpr< smartindent< autoindent< indentkeys<' | 26 let b:undo_indent = 'setlocal indentexpr< smartindent< autoindent< indentkeys<' |
23 | 27 |
28 " Regex of syntax group names that are or delimit string or are comments. | |
29 let b:syng_strcom = get(b:,'syng_strcom','string\|comment\|regex\|special\|doc\|template\%(braces\)\@!') | |
30 let b:syng_str = get(b:,'syng_str','string\|template\|special') | |
31 " template strings may want to be excluded when editing graphql: | |
32 " au! Filetype javascript let b:syng_str = '^\%(.*template\)\@!.*string\|special' | |
33 " au! Filetype javascript let b:syng_strcom = '^\%(.*template\)\@!.*string\|comment\|regex\|special\|doc' | |
34 | |
24 " Only define the function once. | 35 " Only define the function once. |
25 if exists('*GetJavascriptIndent') | 36 if exists('*GetJavascriptIndent') |
26 finish | 37 finish |
27 endif | 38 endif |
28 | 39 |
34 function s:sw() | 45 function s:sw() |
35 return shiftwidth() | 46 return shiftwidth() |
36 endfunction | 47 endfunction |
37 else | 48 else |
38 function s:sw() | 49 function s:sw() |
39 return &l:shiftwidth == 0 ? &l:tabstop : &l:shiftwidth | 50 return &l:shiftwidth ? &l:shiftwidth : &l:tabstop |
40 endfunction | 51 endfunction |
41 endif | 52 endif |
42 | 53 |
43 " Performance for forwards search(): start search at pos rather than masking | 54 " Performance for forwards search(): start search at pos rather than masking |
44 " matches before pos. | 55 " matches before pos. |
45 let s:z = has('patch-7.4.984') ? 'z' : '' | 56 let s:z = has('patch-7.4.984') ? 'z' : '' |
46 | 57 |
58 " Expression used to check whether we should skip a match with searchpair(). | |
59 let s:skip_expr = "s:SynAt(line('.'),col('.')) =~? b:syng_strcom" | |
60 let s:in_comm = s:skip_expr[:-14] . "'comment\\|doc'" | |
61 | |
62 let s:rel = has('reltime') | |
47 " searchpair() wrapper | 63 " searchpair() wrapper |
48 if has('reltime') | 64 if s:rel |
49 function s:GetPair(start,end,flags,skip,time,...) | 65 function s:GetPair(start,end,flags,skip) |
50 return searchpair('\m'.a:start,'','\m'.a:end,a:flags,a:skip,max([prevnonblank(v:lnum) - 2000,0] + a:000),a:time) | 66 return searchpair('\m'.a:start,'','\m'.a:end,a:flags,a:skip,s:l1,a:skip ==# 's:SkipFunc()' ? 2000 : 200) |
51 endfunction | 67 endfunction |
52 else | 68 else |
53 function s:GetPair(start,end,flags,skip,...) | 69 function s:GetPair(start,end,flags,skip) |
54 return searchpair('\m'.a:start,'','\m'.a:end,a:flags,a:skip,max([prevnonblank(v:lnum) - 1000,get(a:000,1)])) | 70 return searchpair('\m'.a:start,'','\m'.a:end,a:flags,a:skip,s:l1) |
55 endfunction | 71 endfunction |
56 endif | 72 endif |
57 | 73 |
58 " Regex of syntax group names that are or delimit string or are comments. | 74 function s:SynAt(l,c) |
59 let s:syng_strcom = 'string\|comment\|regex\|special\|doc\|template\%(braces\)\@!' | 75 let byte = line2byte(a:l) + a:c - 1 |
60 let s:syng_str = 'string\|template\|special' | 76 let pos = index(s:synid_cache[0], byte) |
61 let s:syng_com = 'comment\|doc' | 77 if pos == -1 |
62 " Expression used to check whether we should skip a match with searchpair(). | 78 let s:synid_cache[:] += [[byte], [synIDattr(synID(a:l, a:c, 0), 'name')]] |
63 let s:skip_expr = "synIDattr(synID(line('.'),col('.'),0),'name') =~? '".s:syng_strcom."'" | 79 endif |
64 | 80 return s:synid_cache[1][pos] |
65 function s:parse_cino(f) abort | 81 endfunction |
66 return float2nr(eval(substitute(substitute(join(split( | 82 |
67 \ matchstr(&cino,'.*'.a:f.'\zs[^,]*'), 's',1), '*'.s:W) | 83 function s:ParseCino(f) |
68 \ , '^-\=\zs\*','',''), '^-\=\zs\.','0.',''))) | 84 let [divider, n, cstr] = [0] + matchlist(&cino, |
69 endfunction | 85 \ '\%(.*,\)\=\%(\%d'.char2nr(a:f).'\(-\)\=\([.s0-9]*\)\)\=')[1:2] |
70 | 86 for c in split(cstr,'\zs') |
71 function s:skip_func() | 87 if c == '.' && !divider |
72 if getline('.') =~ '\%<'.col('.').'c\/.\{-}\/\|\%>'.col('.').'c[''"]\|\\$' | 88 let divider = 1 |
73 return eval(s:skip_expr) | 89 elseif c ==# 's' |
74 elseif s:checkIn || search('\m`\|\${\|\*\/','nW'.s:z,s:looksyn) | 90 if n !~ '\d' |
75 let s:checkIn = eval(s:skip_expr) | 91 return n . s:sw() + 0 |
76 endif | 92 endif |
77 let s:looksyn = line('.') | 93 let n = str2nr(n) * s:sw() |
78 return s:checkIn | 94 break |
79 endfunction | 95 else |
80 | 96 let [n, divider] .= [c, 0] |
81 function s:alternatePair(stop) | 97 endif |
82 let pos = getpos('.')[1:2] | 98 endfor |
83 let pat = '[][(){};]' | 99 return str2nr(n) / max([str2nr(divider),1]) |
84 while search('\m'.pat,'bW',a:stop) | 100 endfunction |
85 if s:skip_func() | continue | endif | 101 |
86 let idx = stridx('])};',s:looking_at()) | 102 " Optimized {skip} expr, only callable from the search loop which |
87 if idx is 3 | let pat = '[{}()]' | continue | endif | 103 " GetJavascriptIndent does to find the containing [[{(] (side-effects) |
88 if idx + 1 | 104 function s:SkipFunc() |
89 if s:GetPair(['\[','(','{'][idx], '])}'[idx],'bW','s:skip_func()',2000,a:stop) <= 0 | 105 if s:top_col == 1 |
106 throw 'out of bounds' | |
107 endif | |
108 let s:top_col = 0 | |
109 if s:check_in | |
110 if eval(s:skip_expr) | |
111 return 1 | |
112 endif | |
113 let s:check_in = 0 | |
114 elseif getline('.') =~ '\%<'.col('.').'c\/.\{-}\/\|\%>'.col('.').'c[''"]\|\\$' | |
115 if eval(s:skip_expr) | |
116 let s:looksyn = a:firstline | |
117 return 1 | |
118 endif | |
119 elseif search('\m`\|\${\|\*\/','nW'.s:z,s:looksyn) && eval(s:skip_expr) | |
120 let s:check_in = 1 | |
121 return 1 | |
122 endif | |
123 let [s:looksyn, s:top_col] = getpos('.')[1:2] | |
124 endfunction | |
125 | |
126 function s:AlternatePair() | |
127 let [pat, l:for] = ['[][(){};]', 2] | |
128 while s:SearchLoop(pat,'bW','s:SkipFunc()') | |
129 if s:LookingAt() == ';' | |
130 if !l:for | |
131 if s:GetPair('{','}','bW','s:SkipFunc()') | |
132 return | |
133 endif | |
90 break | 134 break |
135 else | |
136 let [pat, l:for] = ['[{}();]', l:for - 1] | |
91 endif | 137 endif |
92 else | 138 else |
93 return | 139 let idx = stridx('])}',s:LookingAt()) |
140 if idx == -1 | |
141 return | |
142 elseif !s:GetPair(['\[','(','{'][idx],'])}'[idx],'bW','s:SkipFunc()') | |
143 break | |
144 endif | |
94 endif | 145 endif |
95 endwhile | 146 endwhile |
96 call call('cursor',pos) | 147 throw 'out of bounds' |
97 endfunction | 148 endfunction |
98 | 149 |
99 function s:save_pos(f,...) | 150 function s:Nat(int) |
100 let l:pos = getpos('.')[1:2] | 151 return a:int * (a:int > 0) |
101 let ret = call(a:f,a:000) | 152 endfunction |
102 call call('cursor',l:pos) | 153 |
103 return ret | 154 function s:LookingAt() |
104 endfunction | |
105 | |
106 function s:syn_at(l,c) | |
107 return synIDattr(synID(a:l,a:c,0),'name') | |
108 endfunction | |
109 | |
110 function s:looking_at() | |
111 return getline('.')[col('.')-1] | 155 return getline('.')[col('.')-1] |
112 endfunction | 156 endfunction |
113 | 157 |
114 function s:token() | 158 function s:Token() |
115 return s:looking_at() =~ '\k' ? expand('<cword>') : s:looking_at() | 159 return s:LookingAt() =~ '\k' ? expand('<cword>') : s:LookingAt() |
116 endfunction | 160 endfunction |
117 | 161 |
118 function s:previous_token() | 162 function s:PreviousToken() |
119 let l:pos = getpos('.')[1:2] | 163 let l:col = col('.') |
120 if search('\m\k\{1,}\zs\k\|\S','bW') | 164 if search('\m\k\{1,}\|\S','ebW') |
121 if (getline('.')[col('.')-2:col('.')-1] == '*/' || line('.') != l:pos[0] && | 165 if search('\m\*\%#\/\|\/\/\%<'.a:firstline.'l','nbW',line('.')) && eval(s:in_comm) |
122 \ getline('.') =~ '\%<'.col('.').'c\/\/') && s:syn_at(line('.'),col('.')) =~? s:syng_com | 166 if s:SearchLoop('\S\ze\_s*\/[/*]','bW',s:in_comm) |
123 while search('\m\S\ze\_s*\/[/*]','bW') | 167 return s:Token() |
124 if s:syn_at(line('.'),col('.')) !~? s:syng_com | 168 endif |
125 return s:token() | 169 call cursor(a:firstline, l:col) |
126 endif | |
127 endwhile | |
128 else | 170 else |
129 return s:token() | 171 return s:Token() |
130 endif | 172 endif |
131 endif | 173 endif |
132 call call('cursor',l:pos) | |
133 return '' | 174 return '' |
134 endfunction | 175 endfunction |
135 | 176 |
136 function s:expr_col() | 177 function s:Pure(f,...) |
137 if getline('.')[col('.')-2] == ':' | 178 return eval("[call(a:f,a:000),cursor(a:firstline,".col('.').")][0]") |
138 return 1 | 179 endfunction |
139 endif | 180 |
181 function s:SearchLoop(pat,flags,expr) | |
182 return s:GetPair(a:pat,'\_$.',a:flags,a:expr) | |
183 endfunction | |
184 | |
185 function s:ExprCol() | |
140 let bal = 0 | 186 let bal = 0 |
141 while search('\m[{}?:;]','bW') | 187 while s:SearchLoop('[{}?]\|\_[^:]\zs::\@!','bW',s:skip_expr) |
142 if eval(s:skip_expr) | continue | endif | 188 if s:LookingAt() == ':' |
143 " switch (looking_at()) | 189 let bal -= 1 |
144 exe { '}': "if s:GetPair('{','}','bW',s:skip_expr,200) <= 0 | return | endif", | 190 elseif s:LookingAt() == '?' |
145 \ ';': "return", | 191 let bal += 1 |
146 \ '{': "return getpos('.')[1:2] != b:js_cache[1:] && !s:IsBlock()", | 192 if bal == 1 |
147 \ ':': "let bal -= getline('.')[max([col('.')-2,0]):col('.')] !~ '::'", | 193 break |
148 \ '?': "let bal += 1 | if bal > 0 | return 1 | endif" }[s:looking_at()] | 194 endif |
195 elseif s:LookingAt() == '{' | |
196 let bal = !s:IsBlock() | |
197 break | |
198 elseif !s:GetPair('{','}','bW',s:skip_expr) | |
199 break | |
200 endif | |
149 endwhile | 201 endwhile |
202 return s:Nat(bal) | |
150 endfunction | 203 endfunction |
151 | 204 |
152 " configurable regexes that define continuation lines, not including (, {, or [. | 205 " configurable regexes that define continuation lines, not including (, {, or [. |
153 let s:opfirst = '^' . get(g:,'javascript_opfirst', | 206 let s:opfirst = '^' . get(g:,'javascript_opfirst', |
154 \ '\C\%([<>=,?^%|*/&]\|\([-.:+]\)\1\@!\|!=\|in\%(stanceof\)\=\>\)') | 207 \ '\C\%([<>=,.?^%|/&]\|\([-:+]\)\1\@!\|\*\+\|!=\|in\%(stanceof\)\=\>\)') |
155 let s:continuation = get(g:,'javascript_continuation', | 208 let s:continuation = get(g:,'javascript_continuation', |
156 \ '\C\%([-+<>=,.~!?/*^%|&:]\|\<\%(typeof\|new\|delete\|void\|in\|instanceof\|await\)\)') . '$' | 209 \ '\C\%([<=,.~!?/*^%|&:]\|+\@<!+\|-\@<!-\|=\@<!>\|\<\%(typeof\|new\|delete\|void\|in\|instanceof\|await\)\)') . '$' |
157 | 210 |
158 function s:continues(ln,con) | 211 function s:Continues(ln,con) |
159 if !cursor(a:ln, match(' '.a:con,s:continuation)) | 212 let tok = matchstr(a:con[-15:],s:continuation) |
160 let teol = s:looking_at() | 213 if tok =~ '[a-z:]' |
161 if teol == '/' | 214 call cursor(a:ln, len(a:con)) |
162 return s:syn_at(line('.'),col('.')) !~? 'regex' | 215 return tok == ':' ? s:ExprCol() : s:PreviousToken() != '.' |
163 elseif teol =~ '[-+>]' | 216 elseif tok !~ '[/>]' |
164 return getline('.')[col('.')-2] != tr(teol,'>','=') | 217 return tok isnot '' |
165 elseif teol =~ '\l' | 218 endif |
166 return s:previous_token() != '.' | 219 return s:SynAt(a:ln, len(a:con)) !~? (tok == '>' ? 'jsflow\|^html' : 'regex') |
167 elseif teol == ':' | |
168 return s:expr_col() | |
169 endif | |
170 return 1 | |
171 endif | |
172 endfunction | |
173 | |
174 " get the line of code stripped of comments and move cursor to the last | |
175 " non-comment char. | |
176 function s:Trim(ln) | |
177 let pline = substitute(getline(a:ln),'\s*$','','') | |
178 let l:max = max([strridx(pline,'//'), strridx(pline,'/*')]) | |
179 while l:max != -1 && s:syn_at(a:ln, strlen(pline)) =~? s:syng_com | |
180 let pline = pline[: l:max] | |
181 let l:max = max([strridx(pline,'//'), strridx(pline,'/*')]) | |
182 let pline = substitute(pline[:-2],'\s*$','','') | |
183 endwhile | |
184 return pline is '' || cursor(a:ln,strlen(pline)) ? pline : pline | |
185 endfunction | |
186 | |
187 " Find line above 'lnum' that isn't empty or in a comment | |
188 function s:PrevCodeLine(lnum) | |
189 let [l:pos, l:n] = [getpos('.')[1:2], prevnonblank(a:lnum)] | |
190 while l:n | |
191 if getline(l:n) =~ '^\s*\/[/*]' | |
192 let l:n = prevnonblank(l:n-1) | |
193 elseif stridx(getline(l:n), '*/') + 1 && s:syn_at(l:n,1) =~? s:syng_com | |
194 call cursor(l:n,1) | |
195 keepjumps norm! [* | |
196 let l:n = search('\m\S','nbW') | |
197 else | |
198 break | |
199 endif | |
200 endwhile | |
201 call call('cursor',l:pos) | |
202 return l:n | |
203 endfunction | 220 endfunction |
204 | 221 |
205 " Check if line 'lnum' has a balanced amount of parentheses. | 222 " Check if line 'lnum' has a balanced amount of parentheses. |
206 function s:Balanced(lnum) | 223 function s:Balanced(lnum) |
207 let l:open = 0 | 224 let [l:open, l:line] = [0, getline(a:lnum)] |
208 let l:line = getline(a:lnum) | 225 let pos = match(l:line, '[][(){}]') |
209 let pos = match(l:line, '[][(){}]', 0) | |
210 while pos != -1 | 226 while pos != -1 |
211 if s:syn_at(a:lnum,pos + 1) !~? s:syng_strcom | 227 if s:SynAt(a:lnum,pos + 1) !~? b:syng_strcom |
212 let l:open += match(' ' . l:line[pos],'[[({]') | 228 let l:open += match(' ' . l:line[pos],'[[({]') |
213 if l:open < 0 | 229 if l:open < 0 |
214 return | 230 return |
215 endif | 231 endif |
216 endif | 232 endif |
217 let pos = match(l:line, (l:open ? | 233 let pos = match(l:line, !l:open ? '[][(){}]' : '()' =~ l:line[pos] ? |
218 \ '['.escape(tr(l:line[pos],'({[]})',')}][{(').l:line[pos],']').']' : | 234 \ '[()]' : '{}' =~ l:line[pos] ? '[{}]' : '[][]', pos + 1) |
219 \ '[][(){}]'), pos + 1) | |
220 endwhile | 235 endwhile |
221 return !l:open | 236 return !l:open |
222 endfunction | 237 endfunction |
223 | 238 |
224 function s:OneScope(lnum) | 239 function s:OneScope() |
225 let pline = s:Trim(a:lnum) | 240 if s:LookingAt() == ')' && s:GetPair('(', ')', 'bW', s:skip_expr) |
226 let kw = 'else do' | 241 let tok = s:PreviousToken() |
227 if pline[-1:] == ')' && s:GetPair('(', ')', 'bW', s:skip_expr, 100) > 0 | 242 return (count(split('for if let while with'),tok) || |
228 if s:previous_token() =~# '^\%(await\|each\)$' | 243 \ tok =~# '^await$\|^each$' && s:PreviousToken() ==# 'for') && |
229 call s:previous_token() | 244 \ s:Pure('s:PreviousToken') != '.' && !(tok == 'while' && s:DoWhile()) |
230 let kw = 'for' | 245 elseif s:Token() =~# '^else$\|^do$' |
246 return s:Pure('s:PreviousToken') != '.' | |
247 endif | |
248 return strpart(getline('.'),col('.')-2,2) == '=>' | |
249 endfunction | |
250 | |
251 function s:DoWhile() | |
252 let cpos = searchpos('\m\<','cbW') | |
253 if s:SearchLoop('\C[{}]\|\<\%(do\|while\)\>','bW',s:skip_expr) | |
254 if s:{s:LookingAt() == '}' && s:GetPair('{','}','bW',s:skip_expr) ? | |
255 \ 'Previous' : ''}Token() ==# 'do' && s:IsBlock() | |
256 return 1 | |
257 endif | |
258 call call('cursor',cpos) | |
259 endif | |
260 endfunction | |
261 | |
262 " returns total offset from braceless contexts. 'num' is the lineNr which | |
263 " encloses the entire context, 'cont' if whether a:firstline is a continued | |
264 " expression, which could have started in a braceless context | |
265 function s:IsContOne(num,cont) | |
266 let [l:num, b_l] = [a:num + !a:num, 0] | |
267 let pind = a:num ? indent(a:num) + s:sw() : 0 | |
268 let ind = indent('.') + !a:cont | |
269 while line('.') > l:num && ind > pind || line('.') == l:num | |
270 if indent('.') < ind && s:OneScope() | |
271 let b_l += 1 | |
272 elseif !a:cont || b_l || ind < indent(a:firstline) | |
273 break | |
231 else | 274 else |
232 let kw = 'for if let while with' | 275 call cursor(0,1) |
233 endif | 276 endif |
234 endif | 277 let ind = min([ind, indent('.')]) |
235 return pline[-2:] == '=>' || index(split(kw),s:token()) + 1 && | 278 if s:PreviousToken() is '' |
236 \ s:save_pos('s:previous_token') != '.' | |
237 endfunction | |
238 | |
239 " returns braceless levels started by 'i' and above lines * shiftwidth(). | |
240 " 'num' is the lineNr which encloses the entire context, 'cont' if whether | |
241 " line 'i' + 1 is a continued expression, which could have started in a | |
242 " braceless context | |
243 function s:iscontOne(i,num,cont) | |
244 let [l:i, l:num, bL] = [a:i, a:num + !a:num, 0] | |
245 let pind = a:num ? indent(l:num) + s:W : 0 | |
246 let ind = indent(l:i) + (a:cont ? 0 : s:W) | |
247 while l:i >= l:num && (ind > pind || l:i == l:num) | |
248 if indent(l:i) < ind && s:OneScope(l:i) | |
249 let bL += s:W | |
250 let l:i = line('.') | |
251 elseif !a:cont || bL || ind < indent(a:i) | |
252 break | 279 break |
253 endif | 280 endif |
254 let ind = min([ind, indent(l:i)]) | |
255 let l:i = s:PrevCodeLine(l:i - 1) | |
256 endwhile | 281 endwhile |
257 return bL | 282 return b_l |
283 endfunction | |
284 | |
285 function s:Class() | |
286 return (s:Token() ==# 'class' || s:PreviousToken() =~# '^class$\|^extends$') && | |
287 \ s:PreviousToken() != '.' | |
288 endfunction | |
289 | |
290 function s:IsSwitch() | |
291 return s:PreviousToken() !~ '[.*]' && | |
292 \ (!s:GetPair('{','}','cbW',s:skip_expr) || s:IsBlock() && !s:Class()) | |
258 endfunction | 293 endfunction |
259 | 294 |
260 " https://github.com/sweet-js/sweet.js/wiki/design#give-lookbehind-to-the-reader | 295 " https://github.com/sweet-js/sweet.js/wiki/design#give-lookbehind-to-the-reader |
261 function s:IsBlock() | 296 function s:IsBlock() |
262 if s:looking_at() == '{' | 297 let tok = s:PreviousToken() |
263 let l:n = line('.') | 298 if join(s:stack) =~? 'xml\|jsx' && s:SynAt(line('.'),col('.')-1) =~? 'xml\|jsx' |
264 let char = s:previous_token() | 299 return tok != '{' |
265 if match(s:stack,'\cxml\|jsx') + 1 && s:syn_at(line('.'),col('.')-1) =~? 'xml\|jsx' | 300 elseif tok =~ '\k' |
266 return char != '{' | 301 if tok ==# 'type' |
267 elseif char =~ '\k' | 302 return s:Pure('eval',"s:PreviousToken() !~# '^\\%(im\\|ex\\)port$' || s:PreviousToken() == '.'") |
268 if char ==# 'type' | 303 elseif tok ==# 'of' |
269 return s:previous_token() !~# '^\%(im\|ex\)port$' | 304 return s:Pure('eval',"!s:GetPair('[[({]','[])}]','bW',s:skip_expr) || s:LookingAt() != '(' ||" |
270 endif | 305 \ ."s:{s:PreviousToken() ==# 'await' ? 'Previous' : ''}Token() !=# 'for' || s:PreviousToken() == '.'") |
271 return index(split('return const let import export extends yield default delete var await void typeof throw case new of in instanceof') | 306 endif |
272 \ ,char) < (line('.') != l:n) || s:save_pos('s:previous_token') == '.' | 307 return index(split('return const let import export extends yield default delete var await void typeof throw case new in instanceof') |
273 elseif char == '>' | 308 \ ,tok) < (line('.') != a:firstline) || s:Pure('s:PreviousToken') == '.' |
274 return getline('.')[col('.')-2] == '=' || s:syn_at(line('.'),col('.')) =~? '^jsflow' | 309 elseif tok == '>' |
275 elseif char == ':' | 310 return getline('.')[col('.')-2] == '=' || s:SynAt(line('.'),col('.')) =~? 'jsflow\|^html' |
276 return !s:save_pos('s:expr_col') | 311 elseif tok == '*' |
277 elseif char == '/' | 312 return s:Pure('s:PreviousToken') == ':' |
278 return s:syn_at(line('.'),col('.')) =~? 'regex' | 313 elseif tok == ':' |
279 endif | 314 return s:Pure('eval',"s:PreviousToken() =~ '^\\K\\k*$' && !s:ExprCol()") |
280 return char !~ '[=~!<*,?^%|&([]' && | 315 elseif tok == '/' |
281 \ (char !~ '[-+]' || l:n != line('.') && getline('.')[col('.')-2] == char) | 316 return s:SynAt(line('.'),col('.')) =~? 'regex' |
317 elseif tok !~ '[=~!<,.?^%|&([]' | |
318 return tok !~ '[-+]' || line('.') != a:firstline && getline('.')[col('.')-2] == tok | |
282 endif | 319 endif |
283 endfunction | 320 endfunction |
284 | 321 |
285 function GetJavascriptIndent() | 322 function GetJavascriptIndent() |
286 let b:js_cache = get(b:,'js_cache',[0,0,0]) | 323 let b:js_cache = get(b:,'js_cache',[0,0,0]) |
287 " Get the current line. | 324 let s:synid_cache = [[],[]] |
288 call cursor(v:lnum,1) | 325 let l:line = getline(v:lnum) |
289 let l:line = getline('.') | |
290 " use synstack as it validates syn state and works in an empty line | 326 " use synstack as it validates syn state and works in an empty line |
291 let s:stack = map(synstack(v:lnum,1),"synIDattr(v:val,'name')") | 327 let s:stack = [''] + map(synstack(v:lnum,1),"synIDattr(v:val,'name')") |
292 let syns = get(s:stack,-1,'') | |
293 | 328 |
294 " start with strings,comments,etc. | 329 " start with strings,comments,etc. |
295 if syns =~? s:syng_com | 330 if s:stack[-1] =~? 'comment\|doc' |
296 if l:line =~ '^\s*\*' | 331 if l:line =~ '^\s*\*' |
297 return cindent(v:lnum) | 332 return cindent(v:lnum) |
298 elseif l:line !~ '^\s*\/[/*]' | 333 elseif l:line !~ '^\s*\/[/*]' |
299 return -1 | 334 return -1 |
300 endif | 335 endif |
301 elseif syns =~? s:syng_str | 336 elseif s:stack[-1] =~? b:syng_str |
302 if b:js_cache[0] == v:lnum - 1 && s:Balanced(v:lnum-1) | 337 if b:js_cache[0] == v:lnum - 1 && s:Balanced(v:lnum-1) |
303 let b:js_cache[0] = v:lnum | 338 let b:js_cache[0] = v:lnum |
304 endif | 339 endif |
305 return -1 | 340 return -1 |
306 endif | 341 endif |
307 let l:lnum = s:PrevCodeLine(v:lnum - 1) | 342 |
308 if !l:lnum | 343 let s:l1 = max([0,prevnonblank(v:lnum) - (s:rel ? 2000 : 1000), |
344 \ get(get(b:,'hi_indent',{}),'blocklnr')]) | |
345 call cursor(v:lnum,1) | |
346 if s:PreviousToken() is '' | |
309 return | 347 return |
310 endif | 348 endif |
349 let [l:lnum, pline] = [line('.'), getline('.')[:col('.')-1]] | |
311 | 350 |
312 let l:line = substitute(l:line,'^\s*','','') | 351 let l:line = substitute(l:line,'^\s*','','') |
352 let l:line_raw = l:line | |
313 if l:line[:1] == '/*' | 353 if l:line[:1] == '/*' |
314 let l:line = substitute(l:line,'^\%(\/\*.\{-}\*\/\s*\)*','','') | 354 let l:line = substitute(l:line,'^\%(\/\*.\{-}\*\/\s*\)*','','') |
315 endif | 355 endif |
316 if l:line =~ '^\/[/*]' | 356 if l:line =~ '^\/[/*]' |
317 let l:line = '' | 357 let l:line = '' |
318 endif | 358 endif |
319 | 359 |
320 " the containing paren, bracket, or curly. Many hacks for performance | 360 " the containing paren, bracket, or curly. Many hacks for performance |
361 call cursor(v:lnum,1) | |
321 let idx = index([']',')','}'],l:line[0]) | 362 let idx = index([']',')','}'],l:line[0]) |
322 if b:js_cache[0] >= l:lnum && b:js_cache[0] < v:lnum && | 363 if b:js_cache[0] > l:lnum && b:js_cache[0] < v:lnum || |
323 \ (b:js_cache[0] > l:lnum || s:Balanced(l:lnum)) | 364 \ b:js_cache[0] == l:lnum && s:Balanced(l:lnum) |
324 call call('cursor',b:js_cache[1:]) | 365 call call('cursor',b:js_cache[1:]) |
325 else | 366 else |
326 let [s:looksyn, s:checkIn, top] = [v:lnum - 1, 0, (!indent(l:lnum) && | 367 let [s:looksyn, s:top_col, s:check_in, s:l1] = [v:lnum - 1,0,0, |
327 \ s:syn_at(l:lnum,1) !~? s:syng_str) * l:lnum] | 368 \ max([s:l1, &smc ? search('\m^.\{'.&smc.',}','nbW',s:l1 + 1) + 1 : 0])] |
328 if idx + 1 | 369 try |
329 call s:GetPair(['\[','(','{'][idx],'])}'[idx],'bW','s:skip_func()',2000,top) | 370 if idx != -1 |
330 elseif getline(v:lnum) !~ '^\S' && syns =~? 'block' | 371 call s:GetPair(['\[','(','{'][idx],'])}'[idx],'bW','s:SkipFunc()') |
331 call s:GetPair('{','}','bW','s:skip_func()',2000,top) | 372 elseif getline(v:lnum) !~ '^\S' && s:stack[-1] =~? 'block\|^jsobject$' |
332 else | 373 call s:GetPair('{','}','bW','s:SkipFunc()') |
333 call s:alternatePair(top) | 374 else |
334 endif | 375 call s:AlternatePair() |
335 endif | 376 endif |
336 | 377 catch /^\Cout of bounds$/ |
337 let b:js_cache = [v:lnum] + (line('.') == v:lnum ? [0,0] : getpos('.')[1:2]) | 378 call cursor(v:lnum,1) |
338 let num = b:js_cache[1] | 379 endtry |
339 | 380 let b:js_cache[1:] = line('.') == v:lnum ? [0,0] : getpos('.')[1:2] |
340 let [s:W, isOp, bL, switch_offset] = [s:sw(),0,0,0] | 381 endif |
341 if !num || s:IsBlock() | 382 |
383 let [b:js_cache[0], num] = [v:lnum, b:js_cache[1]] | |
384 | |
385 let [num_ind, is_op, b_l, l:switch_offset] = [s:Nat(indent(num)),0,0,0] | |
386 if !num || s:LookingAt() == '{' && s:IsBlock() | |
342 let ilnum = line('.') | 387 let ilnum = line('.') |
343 let pline = s:save_pos('s:Trim',l:lnum) | 388 if num && s:LookingAt() == ')' && s:GetPair('(',')','bW',s:skip_expr) |
344 if num && s:looking_at() == ')' && s:GetPair('(', ')', 'bW', s:skip_expr, 100) > 0 | 389 if ilnum == num |
345 let num = ilnum == num ? line('.') : num | 390 let [num, num_ind] = [line('.'), indent('.')] |
346 if idx < 0 && s:previous_token() ==# 'switch' && s:previous_token() != '.' | 391 endif |
347 if &cino !~ ':' | 392 if idx == -1 && s:PreviousToken() ==# 'switch' && s:IsSwitch() |
348 let switch_offset = s:W | 393 let l:switch_offset = &cino !~ ':' ? s:sw() : s:ParseCino(':') |
349 else | 394 if pline[-1:] != '.' && l:line =~# '^\%(default\|case\)\>' |
350 let switch_offset = max([-indent(num),s:parse_cino(':')]) | 395 return s:Nat(num_ind + l:switch_offset) |
396 elseif &cino =~ '=' | |
397 let l:case_offset = s:ParseCino('=') | |
351 endif | 398 endif |
352 if pline[-1:] != '.' && l:line =~# '^\%(default\|case\)\>' | 399 endif |
353 return indent(num) + switch_offset | 400 endif |
401 if idx == -1 && pline[-1:] !~ '[{;]' | |
402 let sol = matchstr(l:line,s:opfirst) | |
403 if sol is '' || sol == '/' && s:SynAt(v:lnum, | |
404 \ 1 + len(getline(v:lnum)) - len(l:line)) =~? 'regex' | |
405 if s:Continues(l:lnum,pline) | |
406 let is_op = s:sw() | |
354 endif | 407 endif |
355 endif | 408 elseif num && sol =~# '^\%(in\%(stanceof\)\=\|\*\)$' |
356 endif | 409 call call('cursor',b:js_cache[1:]) |
357 if idx < 0 && pline[-1:] !~ '[{;]' | 410 if s:PreviousToken() =~ '\k' && s:Class() |
358 let isOp = (l:line =~# s:opfirst || s:continues(l:lnum,pline)) * s:W | 411 return num_ind + s:sw() |
359 let bL = s:iscontOne(l:lnum,b:js_cache[1],isOp) | 412 endif |
360 let bL -= (bL && l:line[0] == '{') * s:W | 413 let is_op = s:sw() |
361 endif | 414 else |
362 elseif idx < 0 && getline(b:js_cache[1])[b:js_cache[2]-1] == '(' && &cino =~ '(' | 415 let is_op = s:sw() |
363 let pval = s:parse_cino('(') | 416 endif |
364 return !pval ? (s:parse_cino('w') ? 0 : -(!!search('\m\S','W'.s:z,num))) + virtcol('.') : | 417 call cursor(l:lnum, len(pline)) |
365 \ max([indent('.') + pval + (s:GetPair('(',')','nbrmW',s:skip_expr,100,num) * s:W),0]) | 418 let b_l = s:Nat(s:IsContOne(b:js_cache[1],is_op) - (!is_op && l:line =~ '^{')) * s:sw() |
419 endif | |
420 elseif idx.s:LookingAt().&cino =~ '^-1(.*(' && (search('\m\S','nbW',num) || s:ParseCino('U')) | |
421 let pval = s:ParseCino('(') | |
422 if !pval | |
423 let [Wval, vcol] = [s:ParseCino('W'), virtcol('.')] | |
424 if search('\m\S','W',num) | |
425 return s:ParseCino('w') ? vcol : virtcol('.')-1 | |
426 endif | |
427 return Wval ? s:Nat(num_ind + Wval) : vcol | |
428 endif | |
429 return s:Nat(num_ind + pval + searchpair('\m(','','\m)','nbrmW',s:skip_expr,num) * s:sw()) | |
366 endif | 430 endif |
367 | 431 |
368 " main return | 432 " main return |
369 if l:line =~ '^\%([])}]\||}\)' | 433 if l:line =~ '^[])}]\|^|}' |
370 return max([indent(num),0]) | 434 if l:line_raw[0] == ')' && getline(num)[b:js_cache[2]-1] == '(' |
435 if s:ParseCino('M') | |
436 return indent(l:lnum) | |
437 elseif &cino =~# 'm' && !s:ParseCino('m') | |
438 return virtcol('.') - 1 | |
439 endif | |
440 endif | |
441 return num_ind | |
371 elseif num | 442 elseif num |
372 return indent(num) + s:W + switch_offset + bL + isOp | 443 return s:Nat(num_ind + get(l:,'case_offset',s:sw()) + l:switch_offset + b_l + is_op) |
373 endif | 444 endif |
374 return bL + isOp | 445 return b_l + is_op |
375 endfunction | 446 endfunction |
376 | 447 |
377 let &cpo = s:cpo_save | 448 let &cpo = s:cpo_save |
378 unlet s:cpo_save | 449 unlet s:cpo_save |