Mercurial > vim
annotate 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 |
rev | line source |
---|---|
1118 | 1 " Vim indent file |
9860
9eaf8ef656e9
commit https://github.com/vim/vim/commit/0952131376a517fc12dc5ae908a97018b4ee23f0
Christian Brabandt <cb@256bit.org>
parents:
6823
diff
changeset
|
2 " Language: Javascript |
9975
03fa8a51e9dc
commit https://github.com/vim/vim/commit/e4a3bcf28d92d0bde9ca227ccb40d401038185e5
Christian Brabandt <cb@256bit.org>
parents:
9860
diff
changeset
|
3 " Maintainer: Chris Paul ( https://github.com/bounceme ) |
9860
9eaf8ef656e9
commit https://github.com/vim/vim/commit/0952131376a517fc12dc5ae908a97018b4ee23f0
Christian Brabandt <cb@256bit.org>
parents:
6823
diff
changeset
|
4 " URL: https://github.com/pangloss/vim-javascript |
12499 | 5 " Last Change: September 18, 2017 |
1118 | 6 |
7 " Only load this indent file when no other was loaded. | |
9860
9eaf8ef656e9
commit https://github.com/vim/vim/commit/0952131376a517fc12dc5ae908a97018b4ee23f0
Christian Brabandt <cb@256bit.org>
parents:
6823
diff
changeset
|
8 if exists('b:did_indent') |
9eaf8ef656e9
commit https://github.com/vim/vim/commit/0952131376a517fc12dc5ae908a97018b4ee23f0
Christian Brabandt <cb@256bit.org>
parents:
6823
diff
changeset
|
9 finish |
1118 | 10 endif |
11 let b:did_indent = 1 | |
12 | |
12499 | 13 " indent correctly if inside <script> |
14 " vim/vim@690afe1 for the switch from cindent | |
15 let b:html_indent_script1 = 'inc' | |
16 | |
9860
9eaf8ef656e9
commit https://github.com/vim/vim/commit/0952131376a517fc12dc5ae908a97018b4ee23f0
Christian Brabandt <cb@256bit.org>
parents:
6823
diff
changeset
|
17 " Now, set up our indentation expression and keys that trigger it. |
9eaf8ef656e9
commit https://github.com/vim/vim/commit/0952131376a517fc12dc5ae908a97018b4ee23f0
Christian Brabandt <cb@256bit.org>
parents:
6823
diff
changeset
|
18 setlocal indentexpr=GetJavascriptIndent() |
10548 | 19 setlocal autoindent nolisp nosmartindent |
20 setlocal indentkeys+=0],0) | |
11229
146a1e213b60
Update runtime files. Add Rust support.
Christian Brabandt <cb@256bit.org>
parents:
10548
diff
changeset
|
21 " Testable with something like: |
146a1e213b60
Update runtime files. Add Rust support.
Christian Brabandt <cb@256bit.org>
parents:
10548
diff
changeset
|
22 " vim -eNs "+filetype plugin indent on" "+syntax on" "+set ft=javascript" \ |
146a1e213b60
Update runtime files. Add Rust support.
Christian Brabandt <cb@256bit.org>
parents:
10548
diff
changeset
|
23 " "+norm! gg=G" '+%print' '+:q!' testfile.js \ |
146a1e213b60
Update runtime files. Add Rust support.
Christian Brabandt <cb@256bit.org>
parents:
10548
diff
changeset
|
24 " | diff -uBZ testfile.js - |
9860
9eaf8ef656e9
commit https://github.com/vim/vim/commit/0952131376a517fc12dc5ae908a97018b4ee23f0
Christian Brabandt <cb@256bit.org>
parents:
6823
diff
changeset
|
25 |
10548 | 26 let b:undo_indent = 'setlocal indentexpr< smartindent< autoindent< indentkeys<' |
9860
9eaf8ef656e9
commit https://github.com/vim/vim/commit/0952131376a517fc12dc5ae908a97018b4ee23f0
Christian Brabandt <cb@256bit.org>
parents:
6823
diff
changeset
|
27 |
12499 | 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 | |
9860
9eaf8ef656e9
commit https://github.com/vim/vim/commit/0952131376a517fc12dc5ae908a97018b4ee23f0
Christian Brabandt <cb@256bit.org>
parents:
6823
diff
changeset
|
35 " Only define the function once. |
9eaf8ef656e9
commit https://github.com/vim/vim/commit/0952131376a517fc12dc5ae908a97018b4ee23f0
Christian Brabandt <cb@256bit.org>
parents:
6823
diff
changeset
|
36 if exists('*GetJavascriptIndent') |
9eaf8ef656e9
commit https://github.com/vim/vim/commit/0952131376a517fc12dc5ae908a97018b4ee23f0
Christian Brabandt <cb@256bit.org>
parents:
6823
diff
changeset
|
37 finish |
9eaf8ef656e9
commit https://github.com/vim/vim/commit/0952131376a517fc12dc5ae908a97018b4ee23f0
Christian Brabandt <cb@256bit.org>
parents:
6823
diff
changeset
|
38 endif |
9eaf8ef656e9
commit https://github.com/vim/vim/commit/0952131376a517fc12dc5ae908a97018b4ee23f0
Christian Brabandt <cb@256bit.org>
parents:
6823
diff
changeset
|
39 |
9eaf8ef656e9
commit https://github.com/vim/vim/commit/0952131376a517fc12dc5ae908a97018b4ee23f0
Christian Brabandt <cb@256bit.org>
parents:
6823
diff
changeset
|
40 let s:cpo_save = &cpo |
9eaf8ef656e9
commit https://github.com/vim/vim/commit/0952131376a517fc12dc5ae908a97018b4ee23f0
Christian Brabandt <cb@256bit.org>
parents:
6823
diff
changeset
|
41 set cpo&vim |
9eaf8ef656e9
commit https://github.com/vim/vim/commit/0952131376a517fc12dc5ae908a97018b4ee23f0
Christian Brabandt <cb@256bit.org>
parents:
6823
diff
changeset
|
42 |
9eaf8ef656e9
commit https://github.com/vim/vim/commit/0952131376a517fc12dc5ae908a97018b4ee23f0
Christian Brabandt <cb@256bit.org>
parents:
6823
diff
changeset
|
43 " Get shiftwidth value |
9eaf8ef656e9
commit https://github.com/vim/vim/commit/0952131376a517fc12dc5ae908a97018b4ee23f0
Christian Brabandt <cb@256bit.org>
parents:
6823
diff
changeset
|
44 if exists('*shiftwidth') |
9eaf8ef656e9
commit https://github.com/vim/vim/commit/0952131376a517fc12dc5ae908a97018b4ee23f0
Christian Brabandt <cb@256bit.org>
parents:
6823
diff
changeset
|
45 function s:sw() |
9eaf8ef656e9
commit https://github.com/vim/vim/commit/0952131376a517fc12dc5ae908a97018b4ee23f0
Christian Brabandt <cb@256bit.org>
parents:
6823
diff
changeset
|
46 return shiftwidth() |
9eaf8ef656e9
commit https://github.com/vim/vim/commit/0952131376a517fc12dc5ae908a97018b4ee23f0
Christian Brabandt <cb@256bit.org>
parents:
6823
diff
changeset
|
47 endfunction |
9eaf8ef656e9
commit https://github.com/vim/vim/commit/0952131376a517fc12dc5ae908a97018b4ee23f0
Christian Brabandt <cb@256bit.org>
parents:
6823
diff
changeset
|
48 else |
9eaf8ef656e9
commit https://github.com/vim/vim/commit/0952131376a517fc12dc5ae908a97018b4ee23f0
Christian Brabandt <cb@256bit.org>
parents:
6823
diff
changeset
|
49 function s:sw() |
12499 | 50 return &l:shiftwidth ? &l:shiftwidth : &l:tabstop |
9860
9eaf8ef656e9
commit https://github.com/vim/vim/commit/0952131376a517fc12dc5ae908a97018b4ee23f0
Christian Brabandt <cb@256bit.org>
parents:
6823
diff
changeset
|
51 endfunction |
9eaf8ef656e9
commit https://github.com/vim/vim/commit/0952131376a517fc12dc5ae908a97018b4ee23f0
Christian Brabandt <cb@256bit.org>
parents:
6823
diff
changeset
|
52 endif |
9eaf8ef656e9
commit https://github.com/vim/vim/commit/0952131376a517fc12dc5ae908a97018b4ee23f0
Christian Brabandt <cb@256bit.org>
parents:
6823
diff
changeset
|
53 |
11229
146a1e213b60
Update runtime files. Add Rust support.
Christian Brabandt <cb@256bit.org>
parents:
10548
diff
changeset
|
54 " Performance for forwards search(): start search at pos rather than masking |
146a1e213b60
Update runtime files. Add Rust support.
Christian Brabandt <cb@256bit.org>
parents:
10548
diff
changeset
|
55 " matches before pos. |
146a1e213b60
Update runtime files. Add Rust support.
Christian Brabandt <cb@256bit.org>
parents:
10548
diff
changeset
|
56 let s:z = has('patch-7.4.984') ? 'z' : '' |
146a1e213b60
Update runtime files. Add Rust support.
Christian Brabandt <cb@256bit.org>
parents:
10548
diff
changeset
|
57 |
12499 | 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') | |
10548 | 63 " searchpair() wrapper |
12499 | 64 if s:rel |
65 function s:GetPair(start,end,flags,skip) | |
66 return searchpair('\m'.a:start,'','\m'.a:end,a:flags,a:skip,s:l1,a:skip ==# 's:SkipFunc()' ? 2000 : 200) | |
9975
03fa8a51e9dc
commit https://github.com/vim/vim/commit/e4a3bcf28d92d0bde9ca227ccb40d401038185e5
Christian Brabandt <cb@256bit.org>
parents:
9860
diff
changeset
|
67 endfunction |
03fa8a51e9dc
commit https://github.com/vim/vim/commit/e4a3bcf28d92d0bde9ca227ccb40d401038185e5
Christian Brabandt <cb@256bit.org>
parents:
9860
diff
changeset
|
68 else |
12499 | 69 function s:GetPair(start,end,flags,skip) |
70 return searchpair('\m'.a:start,'','\m'.a:end,a:flags,a:skip,s:l1) | |
9975
03fa8a51e9dc
commit https://github.com/vim/vim/commit/e4a3bcf28d92d0bde9ca227ccb40d401038185e5
Christian Brabandt <cb@256bit.org>
parents:
9860
diff
changeset
|
71 endfunction |
03fa8a51e9dc
commit https://github.com/vim/vim/commit/e4a3bcf28d92d0bde9ca227ccb40d401038185e5
Christian Brabandt <cb@256bit.org>
parents:
9860
diff
changeset
|
72 endif |
9860
9eaf8ef656e9
commit https://github.com/vim/vim/commit/0952131376a517fc12dc5ae908a97018b4ee23f0
Christian Brabandt <cb@256bit.org>
parents:
6823
diff
changeset
|
73 |
12499 | 74 function s:SynAt(l,c) |
75 let byte = line2byte(a:l) + a:c - 1 | |
76 let pos = index(s:synid_cache[0], byte) | |
77 if pos == -1 | |
78 let s:synid_cache[:] += [[byte], [synIDattr(synID(a:l, a:c, 0), 'name')]] | |
79 endif | |
80 return s:synid_cache[1][pos] | |
11229
146a1e213b60
Update runtime files. Add Rust support.
Christian Brabandt <cb@256bit.org>
parents:
10548
diff
changeset
|
81 endfunction |
146a1e213b60
Update runtime files. Add Rust support.
Christian Brabandt <cb@256bit.org>
parents:
10548
diff
changeset
|
82 |
12499 | 83 function s:ParseCino(f) |
84 let [divider, n, cstr] = [0] + matchlist(&cino, | |
85 \ '\%(.*,\)\=\%(\%d'.char2nr(a:f).'\(-\)\=\([.s0-9]*\)\)\=')[1:2] | |
86 for c in split(cstr,'\zs') | |
87 if c == '.' && !divider | |
88 let divider = 1 | |
89 elseif c ==# 's' | |
90 if n !~ '\d' | |
91 return n . s:sw() + 0 | |
92 endif | |
93 let n = str2nr(n) * s:sw() | |
94 break | |
95 else | |
96 let [n, divider] .= [c, 0] | |
97 endif | |
98 endfor | |
99 return str2nr(n) / max([str2nr(divider),1]) | |
10548 | 100 endfunction |
101 | |
12499 | 102 " Optimized {skip} expr, only callable from the search loop which |
103 " GetJavascriptIndent does to find the containing [[{(] (side-effects) | |
104 function s:SkipFunc() | |
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 | |
134 break | |
135 else | |
136 let [pat, l:for] = ['[{}();]', l:for - 1] | |
137 endif | |
138 else | |
139 let idx = stridx('])}',s:LookingAt()) | |
140 if idx == -1 | |
141 return | |
142 elseif !s:GetPair(['\[','(','{'][idx],'])}'[idx],'bW','s:SkipFunc()') | |
11229
146a1e213b60
Update runtime files. Add Rust support.
Christian Brabandt <cb@256bit.org>
parents:
10548
diff
changeset
|
143 break |
10548 | 144 endif |
145 endif | |
146 endwhile | |
12499 | 147 throw 'out of bounds' |
10548 | 148 endfunction |
149 | |
12499 | 150 function s:Nat(int) |
151 return a:int * (a:int > 0) | |
10548 | 152 endfunction |
153 | |
12499 | 154 function s:LookingAt() |
10548 | 155 return getline('.')[col('.')-1] |
156 endfunction | |
157 | |
12499 | 158 function s:Token() |
159 return s:LookingAt() =~ '\k' ? expand('<cword>') : s:LookingAt() | |
10548 | 160 endfunction |
161 | |
12499 | 162 function s:PreviousToken() |
163 let l:col = col('.') | |
164 if search('\m\k\{1,}\|\S','ebW') | |
165 if search('\m\*\%#\/\|\/\/\%<'.a:firstline.'l','nbW',line('.')) && eval(s:in_comm) | |
166 if s:SearchLoop('\S\ze\_s*\/[/*]','bW',s:in_comm) | |
167 return s:Token() | |
168 endif | |
169 call cursor(a:firstline, l:col) | |
10548 | 170 else |
12499 | 171 return s:Token() |
10548 | 172 endif |
11229
146a1e213b60
Update runtime files. Add Rust support.
Christian Brabandt <cb@256bit.org>
parents:
10548
diff
changeset
|
173 endif |
10548 | 174 return '' |
175 endfunction | |
176 | |
12499 | 177 function s:Pure(f,...) |
178 return eval("[call(a:f,a:000),cursor(a:firstline,".col('.').")][0]") | |
179 endfunction | |
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() | |
11229
146a1e213b60
Update runtime files. Add Rust support.
Christian Brabandt <cb@256bit.org>
parents:
10548
diff
changeset
|
186 let bal = 0 |
12499 | 187 while s:SearchLoop('[{}?]\|\_[^:]\zs::\@!','bW',s:skip_expr) |
188 if s:LookingAt() == ':' | |
189 let bal -= 1 | |
190 elseif s:LookingAt() == '?' | |
191 let bal += 1 | |
192 if bal == 1 | |
193 break | |
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 | |
11229
146a1e213b60
Update runtime files. Add Rust support.
Christian Brabandt <cb@256bit.org>
parents:
10548
diff
changeset
|
201 endwhile |
12499 | 202 return s:Nat(bal) |
10548 | 203 endfunction |
9860
9eaf8ef656e9
commit https://github.com/vim/vim/commit/0952131376a517fc12dc5ae908a97018b4ee23f0
Christian Brabandt <cb@256bit.org>
parents:
6823
diff
changeset
|
204 |
9eaf8ef656e9
commit https://github.com/vim/vim/commit/0952131376a517fc12dc5ae908a97018b4ee23f0
Christian Brabandt <cb@256bit.org>
parents:
6823
diff
changeset
|
205 " configurable regexes that define continuation lines, not including (, {, or [. |
10548 | 206 let s:opfirst = '^' . get(g:,'javascript_opfirst', |
12499 | 207 \ '\C\%([<>=,.?^%|/&]\|\([-:+]\)\1\@!\|\*\+\|!=\|in\%(stanceof\)\=\>\)') |
10548 | 208 let s:continuation = get(g:,'javascript_continuation', |
12499 | 209 \ '\C\%([<=,.~!?/*^%|&:]\|+\@<!+\|-\@<!-\|=\@<!>\|\<\%(typeof\|new\|delete\|void\|in\|instanceof\|await\)\)') . '$' |
9975
03fa8a51e9dc
commit https://github.com/vim/vim/commit/e4a3bcf28d92d0bde9ca227ccb40d401038185e5
Christian Brabandt <cb@256bit.org>
parents:
9860
diff
changeset
|
210 |
12499 | 211 function s:Continues(ln,con) |
212 let tok = matchstr(a:con[-15:],s:continuation) | |
213 if tok =~ '[a-z:]' | |
214 call cursor(a:ln, len(a:con)) | |
215 return tok == ':' ? s:ExprCol() : s:PreviousToken() != '.' | |
216 elseif tok !~ '[/>]' | |
217 return tok isnot '' | |
218 endif | |
219 return s:SynAt(a:ln, len(a:con)) !~? (tok == '>' ? 'jsflow\|^html' : 'regex') | |
9860
9eaf8ef656e9
commit https://github.com/vim/vim/commit/0952131376a517fc12dc5ae908a97018b4ee23f0
Christian Brabandt <cb@256bit.org>
parents:
6823
diff
changeset
|
220 endfunction |
1118 | 221 |
9860
9eaf8ef656e9
commit https://github.com/vim/vim/commit/0952131376a517fc12dc5ae908a97018b4ee23f0
Christian Brabandt <cb@256bit.org>
parents:
6823
diff
changeset
|
222 " Check if line 'lnum' has a balanced amount of parentheses. |
9eaf8ef656e9
commit https://github.com/vim/vim/commit/0952131376a517fc12dc5ae908a97018b4ee23f0
Christian Brabandt <cb@256bit.org>
parents:
6823
diff
changeset
|
223 function s:Balanced(lnum) |
12499 | 224 let [l:open, l:line] = [0, getline(a:lnum)] |
225 let pos = match(l:line, '[][(){}]') | |
9860
9eaf8ef656e9
commit https://github.com/vim/vim/commit/0952131376a517fc12dc5ae908a97018b4ee23f0
Christian Brabandt <cb@256bit.org>
parents:
6823
diff
changeset
|
226 while pos != -1 |
12499 | 227 if s:SynAt(a:lnum,pos + 1) !~? b:syng_strcom |
10548 | 228 let l:open += match(' ' . l:line[pos],'[[({]') |
229 if l:open < 0 | |
230 return | |
9860
9eaf8ef656e9
commit https://github.com/vim/vim/commit/0952131376a517fc12dc5ae908a97018b4ee23f0
Christian Brabandt <cb@256bit.org>
parents:
6823
diff
changeset
|
231 endif |
9eaf8ef656e9
commit https://github.com/vim/vim/commit/0952131376a517fc12dc5ae908a97018b4ee23f0
Christian Brabandt <cb@256bit.org>
parents:
6823
diff
changeset
|
232 endif |
12499 | 233 let pos = match(l:line, !l:open ? '[][(){}]' : '()' =~ l:line[pos] ? |
234 \ '[()]' : '{}' =~ l:line[pos] ? '[{}]' : '[][]', pos + 1) | |
9860
9eaf8ef656e9
commit https://github.com/vim/vim/commit/0952131376a517fc12dc5ae908a97018b4ee23f0
Christian Brabandt <cb@256bit.org>
parents:
6823
diff
changeset
|
235 endwhile |
10548 | 236 return !l:open |
237 endfunction | |
238 | |
12499 | 239 function s:OneScope() |
240 if s:LookingAt() == ')' && s:GetPair('(', ')', 'bW', s:skip_expr) | |
241 let tok = s:PreviousToken() | |
242 return (count(split('for if let while with'),tok) || | |
243 \ tok =~# '^await$\|^each$' && s:PreviousToken() ==# 'for') && | |
244 \ s:Pure('s:PreviousToken') != '.' && !(tok == 'while' && s:DoWhile()) | |
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 | |
10548 | 257 endif |
12499 | 258 call call('cursor',cpos) |
10548 | 259 endif |
9860
9eaf8ef656e9
commit https://github.com/vim/vim/commit/0952131376a517fc12dc5ae908a97018b4ee23f0
Christian Brabandt <cb@256bit.org>
parents:
6823
diff
changeset
|
260 endfunction |
10548 | 261 |
12499 | 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 | |
274 else | |
275 call cursor(0,1) | |
276 endif | |
277 let ind = min([ind, indent('.')]) | |
278 if s:PreviousToken() is '' | |
10548 | 279 break |
280 endif | |
281 endwhile | |
12499 | 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()) | |
10548 | 293 endfunction |
294 | |
295 " https://github.com/sweet-js/sweet.js/wiki/design#give-lookbehind-to-the-reader | |
296 function s:IsBlock() | |
12499 | 297 let tok = s:PreviousToken() |
298 if join(s:stack) =~? 'xml\|jsx' && s:SynAt(line('.'),col('.')-1) =~? 'xml\|jsx' | |
299 return tok != '{' | |
300 elseif tok =~ '\k' | |
301 if tok ==# 'type' | |
302 return s:Pure('eval',"s:PreviousToken() !~# '^\\%(im\\|ex\\)port$' || s:PreviousToken() == '.'") | |
303 elseif tok ==# 'of' | |
304 return s:Pure('eval',"!s:GetPair('[[({]','[])}]','bW',s:skip_expr) || s:LookingAt() != '(' ||" | |
305 \ ."s:{s:PreviousToken() ==# 'await' ? 'Previous' : ''}Token() !=# 'for' || s:PreviousToken() == '.'") | |
10548 | 306 endif |
12499 | 307 return index(split('return const let import export extends yield default delete var await void typeof throw case new in instanceof') |
308 \ ,tok) < (line('.') != a:firstline) || s:Pure('s:PreviousToken') == '.' | |
309 elseif tok == '>' | |
310 return getline('.')[col('.')-2] == '=' || s:SynAt(line('.'),col('.')) =~? 'jsflow\|^html' | |
311 elseif tok == '*' | |
312 return s:Pure('s:PreviousToken') == ':' | |
313 elseif tok == ':' | |
314 return s:Pure('eval',"s:PreviousToken() =~ '^\\K\\k*$' && !s:ExprCol()") | |
315 elseif tok == '/' | |
316 return s:SynAt(line('.'),col('.')) =~? 'regex' | |
317 elseif tok !~ '[=~!<,.?^%|&([]' | |
318 return tok !~ '[-+]' || line('.') != a:firstline && getline('.')[col('.')-2] == tok | |
10548 | 319 endif |
320 endfunction | |
9860
9eaf8ef656e9
commit https://github.com/vim/vim/commit/0952131376a517fc12dc5ae908a97018b4ee23f0
Christian Brabandt <cb@256bit.org>
parents:
6823
diff
changeset
|
321 |
9eaf8ef656e9
commit https://github.com/vim/vim/commit/0952131376a517fc12dc5ae908a97018b4ee23f0
Christian Brabandt <cb@256bit.org>
parents:
6823
diff
changeset
|
322 function GetJavascriptIndent() |
10548 | 323 let b:js_cache = get(b:,'js_cache',[0,0,0]) |
12499 | 324 let s:synid_cache = [[],[]] |
325 let l:line = getline(v:lnum) | |
11229
146a1e213b60
Update runtime files. Add Rust support.
Christian Brabandt <cb@256bit.org>
parents:
10548
diff
changeset
|
326 " use synstack as it validates syn state and works in an empty line |
12499 | 327 let s:stack = [''] + map(synstack(v:lnum,1),"synIDattr(v:val,'name')") |
9860
9eaf8ef656e9
commit https://github.com/vim/vim/commit/0952131376a517fc12dc5ae908a97018b4ee23f0
Christian Brabandt <cb@256bit.org>
parents:
6823
diff
changeset
|
328 |
10548 | 329 " start with strings,comments,etc. |
12499 | 330 if s:stack[-1] =~? 'comment\|doc' |
10548 | 331 if l:line =~ '^\s*\*' |
332 return cindent(v:lnum) | |
333 elseif l:line !~ '^\s*\/[/*]' | |
334 return -1 | |
335 endif | |
12499 | 336 elseif s:stack[-1] =~? b:syng_str |
10548 | 337 if b:js_cache[0] == v:lnum - 1 && s:Balanced(v:lnum-1) |
338 let b:js_cache[0] = v:lnum | |
339 endif | |
9860
9eaf8ef656e9
commit https://github.com/vim/vim/commit/0952131376a517fc12dc5ae908a97018b4ee23f0
Christian Brabandt <cb@256bit.org>
parents:
6823
diff
changeset
|
340 return -1 |
9eaf8ef656e9
commit https://github.com/vim/vim/commit/0952131376a517fc12dc5ae908a97018b4ee23f0
Christian Brabandt <cb@256bit.org>
parents:
6823
diff
changeset
|
341 endif |
12499 | 342 |
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 '' | |
10548 | 347 return |
9860
9eaf8ef656e9
commit https://github.com/vim/vim/commit/0952131376a517fc12dc5ae908a97018b4ee23f0
Christian Brabandt <cb@256bit.org>
parents:
6823
diff
changeset
|
348 endif |
12499 | 349 let [l:lnum, pline] = [line('.'), getline('.')[:col('.')-1]] |
9860
9eaf8ef656e9
commit https://github.com/vim/vim/commit/0952131376a517fc12dc5ae908a97018b4ee23f0
Christian Brabandt <cb@256bit.org>
parents:
6823
diff
changeset
|
350 |
10548 | 351 let l:line = substitute(l:line,'^\s*','','') |
12499 | 352 let l:line_raw = l:line |
10548 | 353 if l:line[:1] == '/*' |
354 let l:line = substitute(l:line,'^\%(\/\*.\{-}\*\/\s*\)*','','') | |
9860
9eaf8ef656e9
commit https://github.com/vim/vim/commit/0952131376a517fc12dc5ae908a97018b4ee23f0
Christian Brabandt <cb@256bit.org>
parents:
6823
diff
changeset
|
355 endif |
10548 | 356 if l:line =~ '^\/[/*]' |
357 let l:line = '' | |
358 endif | |
9860
9eaf8ef656e9
commit https://github.com/vim/vim/commit/0952131376a517fc12dc5ae908a97018b4ee23f0
Christian Brabandt <cb@256bit.org>
parents:
6823
diff
changeset
|
359 |
10548 | 360 " the containing paren, bracket, or curly. Many hacks for performance |
12499 | 361 call cursor(v:lnum,1) |
11229
146a1e213b60
Update runtime files. Add Rust support.
Christian Brabandt <cb@256bit.org>
parents:
10548
diff
changeset
|
362 let idx = index([']',')','}'],l:line[0]) |
12499 | 363 if b:js_cache[0] > l:lnum && b:js_cache[0] < v:lnum || |
364 \ b:js_cache[0] == l:lnum && s:Balanced(l:lnum) | |
10548 | 365 call call('cursor',b:js_cache[1:]) |
9860
9eaf8ef656e9
commit https://github.com/vim/vim/commit/0952131376a517fc12dc5ae908a97018b4ee23f0
Christian Brabandt <cb@256bit.org>
parents:
6823
diff
changeset
|
366 else |
12499 | 367 let [s:looksyn, s:top_col, s:check_in, s:l1] = [v:lnum - 1,0,0, |
368 \ max([s:l1, &smc ? search('\m^.\{'.&smc.',}','nbW',s:l1 + 1) + 1 : 0])] | |
369 try | |
370 if idx != -1 | |
371 call s:GetPair(['\[','(','{'][idx],'])}'[idx],'bW','s:SkipFunc()') | |
372 elseif getline(v:lnum) !~ '^\S' && s:stack[-1] =~? 'block\|^jsobject$' | |
373 call s:GetPair('{','}','bW','s:SkipFunc()') | |
374 else | |
375 call s:AlternatePair() | |
376 endif | |
377 catch /^\Cout of bounds$/ | |
378 call cursor(v:lnum,1) | |
379 endtry | |
380 let b:js_cache[1:] = line('.') == v:lnum ? [0,0] : getpos('.')[1:2] | |
9860
9eaf8ef656e9
commit https://github.com/vim/vim/commit/0952131376a517fc12dc5ae908a97018b4ee23f0
Christian Brabandt <cb@256bit.org>
parents:
6823
diff
changeset
|
381 endif |
9eaf8ef656e9
commit https://github.com/vim/vim/commit/0952131376a517fc12dc5ae908a97018b4ee23f0
Christian Brabandt <cb@256bit.org>
parents:
6823
diff
changeset
|
382 |
12499 | 383 let [b:js_cache[0], num] = [v:lnum, b:js_cache[1]] |
9975
03fa8a51e9dc
commit https://github.com/vim/vim/commit/e4a3bcf28d92d0bde9ca227ccb40d401038185e5
Christian Brabandt <cb@256bit.org>
parents:
9860
diff
changeset
|
384 |
12499 | 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() | |
11229
146a1e213b60
Update runtime files. Add Rust support.
Christian Brabandt <cb@256bit.org>
parents:
10548
diff
changeset
|
387 let ilnum = line('.') |
12499 | 388 if num && s:LookingAt() == ')' && s:GetPair('(',')','bW',s:skip_expr) |
389 if ilnum == num | |
390 let [num, num_ind] = [line('.'), indent('.')] | |
391 endif | |
392 if idx == -1 && s:PreviousToken() ==# 'switch' && s:IsSwitch() | |
393 let l:switch_offset = &cino !~ ':' ? s:sw() : s:ParseCino(':') | |
10548 | 394 if pline[-1:] != '.' && l:line =~# '^\%(default\|case\)\>' |
12499 | 395 return s:Nat(num_ind + l:switch_offset) |
396 elseif &cino =~ '=' | |
397 let l:case_offset = s:ParseCino('=') | |
10548 | 398 endif |
399 endif | |
400 endif | |
12499 | 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() | |
407 endif | |
408 elseif num && sol =~# '^\%(in\%(stanceof\)\=\|\*\)$' | |
409 call call('cursor',b:js_cache[1:]) | |
410 if s:PreviousToken() =~ '\k' && s:Class() | |
411 return num_ind + s:sw() | |
412 endif | |
413 let is_op = s:sw() | |
414 else | |
415 let is_op = s:sw() | |
416 endif | |
417 call cursor(l:lnum, len(pline)) | |
418 let b_l = s:Nat(s:IsContOne(b:js_cache[1],is_op) - (!is_op && l:line =~ '^{')) * s:sw() | |
10548 | 419 endif |
12499 | 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()) | |
9860
9eaf8ef656e9
commit https://github.com/vim/vim/commit/0952131376a517fc12dc5ae908a97018b4ee23f0
Christian Brabandt <cb@256bit.org>
parents:
6823
diff
changeset
|
430 endif |
9eaf8ef656e9
commit https://github.com/vim/vim/commit/0952131376a517fc12dc5ae908a97018b4ee23f0
Christian Brabandt <cb@256bit.org>
parents:
6823
diff
changeset
|
431 |
10548 | 432 " main return |
12499 | 433 if l:line =~ '^[])}]\|^|}' |
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 | |
10548 | 442 elseif num |
12499 | 443 return s:Nat(num_ind + get(l:,'case_offset',s:sw()) + l:switch_offset + b_l + is_op) |
10548 | 444 endif |
12499 | 445 return b_l + is_op |
9860
9eaf8ef656e9
commit https://github.com/vim/vim/commit/0952131376a517fc12dc5ae908a97018b4ee23f0
Christian Brabandt <cb@256bit.org>
parents:
6823
diff
changeset
|
446 endfunction |
9eaf8ef656e9
commit https://github.com/vim/vim/commit/0952131376a517fc12dc5ae908a97018b4ee23f0
Christian Brabandt <cb@256bit.org>
parents:
6823
diff
changeset
|
447 |
9eaf8ef656e9
commit https://github.com/vim/vim/commit/0952131376a517fc12dc5ae908a97018b4ee23f0
Christian Brabandt <cb@256bit.org>
parents:
6823
diff
changeset
|
448 let &cpo = s:cpo_save |
9eaf8ef656e9
commit https://github.com/vim/vim/commit/0952131376a517fc12dc5ae908a97018b4ee23f0
Christian Brabandt <cb@256bit.org>
parents:
6823
diff
changeset
|
449 unlet s:cpo_save |