Mercurial > vim
annotate runtime/indent/javascript.vim @ 10620:2198b53b9ffe v8.0.0199
patch 8.0.0199: compiler warnings for libcall
commit https://github.com/vim/vim/commit/6d721c7e10251ad8c89a461eed99d8cf2659c1df
Author: Bram Moolenaar <Bram@vim.org>
Date: Tue Jan 17 16:56:28 2017 +0100
patch 8.0.0199: compiler warnings for libcall
Problem: Warning for an unused parameter when the libcall feature is
disabled. Warning for a function type cast when compiling with
-pedantic.
Solution: Add UNUSED. Use a different type cast. (Damien Molinier)
author | Christian Brabandt <cb@256bit.org> |
---|---|
date | Tue, 17 Jan 2017 17:00:05 +0100 |
parents | 74effdaa369e |
children | 146a1e213b60 |
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 |
10548 | 5 " Last Change: December 31, 2016 |
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 | |
9860
9eaf8ef656e9
commit https://github.com/vim/vim/commit/0952131376a517fc12dc5ae908a97018b4ee23f0
Christian Brabandt <cb@256bit.org>
parents:
6823
diff
changeset
|
13 " 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
|
14 setlocal indentexpr=GetJavascriptIndent() |
10548 | 15 setlocal autoindent nolisp nosmartindent |
16 setlocal indentkeys+=0],0) | |
9860
9eaf8ef656e9
commit https://github.com/vim/vim/commit/0952131376a517fc12dc5ae908a97018b4ee23f0
Christian Brabandt <cb@256bit.org>
parents:
6823
diff
changeset
|
17 |
10548 | 18 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
|
19 |
9eaf8ef656e9
commit https://github.com/vim/vim/commit/0952131376a517fc12dc5ae908a97018b4ee23f0
Christian Brabandt <cb@256bit.org>
parents:
6823
diff
changeset
|
20 " Only define the function once. |
9eaf8ef656e9
commit https://github.com/vim/vim/commit/0952131376a517fc12dc5ae908a97018b4ee23f0
Christian Brabandt <cb@256bit.org>
parents:
6823
diff
changeset
|
21 if exists('*GetJavascriptIndent') |
9eaf8ef656e9
commit https://github.com/vim/vim/commit/0952131376a517fc12dc5ae908a97018b4ee23f0
Christian Brabandt <cb@256bit.org>
parents:
6823
diff
changeset
|
22 finish |
9eaf8ef656e9
commit https://github.com/vim/vim/commit/0952131376a517fc12dc5ae908a97018b4ee23f0
Christian Brabandt <cb@256bit.org>
parents:
6823
diff
changeset
|
23 endif |
9eaf8ef656e9
commit https://github.com/vim/vim/commit/0952131376a517fc12dc5ae908a97018b4ee23f0
Christian Brabandt <cb@256bit.org>
parents:
6823
diff
changeset
|
24 |
9eaf8ef656e9
commit https://github.com/vim/vim/commit/0952131376a517fc12dc5ae908a97018b4ee23f0
Christian Brabandt <cb@256bit.org>
parents:
6823
diff
changeset
|
25 let s:cpo_save = &cpo |
9eaf8ef656e9
commit https://github.com/vim/vim/commit/0952131376a517fc12dc5ae908a97018b4ee23f0
Christian Brabandt <cb@256bit.org>
parents:
6823
diff
changeset
|
26 set cpo&vim |
9eaf8ef656e9
commit https://github.com/vim/vim/commit/0952131376a517fc12dc5ae908a97018b4ee23f0
Christian Brabandt <cb@256bit.org>
parents:
6823
diff
changeset
|
27 |
9eaf8ef656e9
commit https://github.com/vim/vim/commit/0952131376a517fc12dc5ae908a97018b4ee23f0
Christian Brabandt <cb@256bit.org>
parents:
6823
diff
changeset
|
28 " Get shiftwidth value |
9eaf8ef656e9
commit https://github.com/vim/vim/commit/0952131376a517fc12dc5ae908a97018b4ee23f0
Christian Brabandt <cb@256bit.org>
parents:
6823
diff
changeset
|
29 if exists('*shiftwidth') |
9eaf8ef656e9
commit https://github.com/vim/vim/commit/0952131376a517fc12dc5ae908a97018b4ee23f0
Christian Brabandt <cb@256bit.org>
parents:
6823
diff
changeset
|
30 function s:sw() |
9eaf8ef656e9
commit https://github.com/vim/vim/commit/0952131376a517fc12dc5ae908a97018b4ee23f0
Christian Brabandt <cb@256bit.org>
parents:
6823
diff
changeset
|
31 return shiftwidth() |
9eaf8ef656e9
commit https://github.com/vim/vim/commit/0952131376a517fc12dc5ae908a97018b4ee23f0
Christian Brabandt <cb@256bit.org>
parents:
6823
diff
changeset
|
32 endfunction |
9eaf8ef656e9
commit https://github.com/vim/vim/commit/0952131376a517fc12dc5ae908a97018b4ee23f0
Christian Brabandt <cb@256bit.org>
parents:
6823
diff
changeset
|
33 else |
9eaf8ef656e9
commit https://github.com/vim/vim/commit/0952131376a517fc12dc5ae908a97018b4ee23f0
Christian Brabandt <cb@256bit.org>
parents:
6823
diff
changeset
|
34 function s:sw() |
9eaf8ef656e9
commit https://github.com/vim/vim/commit/0952131376a517fc12dc5ae908a97018b4ee23f0
Christian Brabandt <cb@256bit.org>
parents:
6823
diff
changeset
|
35 return &sw |
9eaf8ef656e9
commit https://github.com/vim/vim/commit/0952131376a517fc12dc5ae908a97018b4ee23f0
Christian Brabandt <cb@256bit.org>
parents:
6823
diff
changeset
|
36 endfunction |
9eaf8ef656e9
commit https://github.com/vim/vim/commit/0952131376a517fc12dc5ae908a97018b4ee23f0
Christian Brabandt <cb@256bit.org>
parents:
6823
diff
changeset
|
37 endif |
9eaf8ef656e9
commit https://github.com/vim/vim/commit/0952131376a517fc12dc5ae908a97018b4ee23f0
Christian Brabandt <cb@256bit.org>
parents:
6823
diff
changeset
|
38 |
10548 | 39 " searchpair() wrapper |
9975
03fa8a51e9dc
commit https://github.com/vim/vim/commit/e4a3bcf28d92d0bde9ca227ccb40d401038185e5
Christian Brabandt <cb@256bit.org>
parents:
9860
diff
changeset
|
40 if has('reltime') |
10548 | 41 function s:GetPair(start,end,flags,skip,time,...) |
42 return searchpair('\m'.a:start,'','\m'.a:end,a:flags,a:skip,max([prevnonblank(v:lnum) - 2000,0] + a:000),a:time) | |
9975
03fa8a51e9dc
commit https://github.com/vim/vim/commit/e4a3bcf28d92d0bde9ca227ccb40d401038185e5
Christian Brabandt <cb@256bit.org>
parents:
9860
diff
changeset
|
43 endfunction |
03fa8a51e9dc
commit https://github.com/vim/vim/commit/e4a3bcf28d92d0bde9ca227ccb40d401038185e5
Christian Brabandt <cb@256bit.org>
parents:
9860
diff
changeset
|
44 else |
10548 | 45 function s:GetPair(start,end,flags,skip,...) |
46 return searchpair('\m'.a:start,'','\m'.a:end,a:flags,a:skip,max([prevnonblank(v:lnum) - 1000,get(a:000,1)])) | |
9975
03fa8a51e9dc
commit https://github.com/vim/vim/commit/e4a3bcf28d92d0bde9ca227ccb40d401038185e5
Christian Brabandt <cb@256bit.org>
parents:
9860
diff
changeset
|
47 endfunction |
03fa8a51e9dc
commit https://github.com/vim/vim/commit/e4a3bcf28d92d0bde9ca227ccb40d401038185e5
Christian Brabandt <cb@256bit.org>
parents:
9860
diff
changeset
|
48 endif |
9860
9eaf8ef656e9
commit https://github.com/vim/vim/commit/0952131376a517fc12dc5ae908a97018b4ee23f0
Christian Brabandt <cb@256bit.org>
parents:
6823
diff
changeset
|
49 |
10548 | 50 " Regex of syntax group names that are or delimit string or are comments. |
51 let s:syng_strcom = 'string\|comment\|regex\|special\|doc\|template' | |
52 let s:syng_str = 'string\|template' | |
53 let s:syng_com = 'comment\|doc' | |
54 " Expression used to check whether we should skip a match with searchpair(). | |
55 let s:skip_expr = "synIDattr(synID(line('.'),col('.'),0),'name') =~? '".s:syng_strcom."'" | |
56 | |
57 function s:skip_func() | |
58 if !s:free || search('\m`\|\*\/','nW',s:looksyn) | |
59 let s:free = !eval(s:skip_expr) | |
60 let s:looksyn = s:free ? line('.') : s:looksyn | |
61 return !s:free | |
62 endif | |
63 let s:looksyn = line('.') | |
64 return (search('\m\/','nbW',s:looksyn) || search('\m[''"]\|\\$','nW',s:looksyn)) && eval(s:skip_expr) | |
65 endfunction | |
66 | |
67 function s:alternatePair(stop) | |
68 let pos = getpos('.')[1:2] | |
69 while search('\m[][(){}]','bW',a:stop) | |
70 if !s:skip_func() | |
71 let idx = stridx('])}',s:looking_at()) | |
72 if idx + 1 | |
73 if !s:GetPair(['\[','(','{'][idx], '])}'[idx],'bW','s:skip_func()',2000,a:stop) | |
74 break | |
75 endif | |
76 else | |
77 return | |
78 endif | |
79 endif | |
80 endwhile | |
81 call call('cursor',pos) | |
82 endfunction | |
83 | |
84 function s:save_pos(f,...) | |
85 let l:pos = getpos('.')[1:2] | |
86 let ret = call(a:f,a:000) | |
87 call call('cursor',l:pos) | |
88 return ret | |
89 endfunction | |
90 | |
91 function s:syn_at(l,c) | |
92 return synIDattr(synID(a:l,a:c,0),'name') | |
93 endfunction | |
94 | |
95 function s:looking_at() | |
96 return getline('.')[col('.')-1] | |
97 endfunction | |
98 | |
99 function s:token() | |
100 return s:looking_at() =~ '\k' ? expand('<cword>') : s:looking_at() | |
101 endfunction | |
102 | |
103 function s:b_token() | |
104 if s:looking_at() =~ '\k' | |
105 call search('\m\<','cbW') | |
106 endif | |
107 return search('\m\S','bW') | |
108 endfunction | |
109 | |
110 function s:previous_token() | |
111 let l:n = line('.') | |
112 while s:b_token() | |
113 if (s:looking_at() == '/' || line('.') != l:n && search('\m\/\/','nbW', | |
114 \ line('.'))) && s:syn_at(line('.'),col('.')) =~? s:syng_com | |
115 call search('\m\_[^/]\zs\/[/*]','bW') | |
116 else | |
117 return s:token() | |
118 endif | |
119 endwhile | |
120 return '' | |
121 endfunction | |
122 | |
123 function s:others(p) | |
124 return "((line2byte(line('.')) + col('.')) <= ".(line2byte(a:p[0]) + a:p[1]).") || ".s:skip_expr | |
125 endfunction | |
126 | |
127 function s:tern_skip(p) | |
128 return s:GetPair('{','}','nbW',s:others(a:p),200,a:p[0]) > 0 | |
129 endfunction | |
130 | |
131 function s:tern_col(p) | |
132 return s:GetPair('?',':\@<!::\@!','nbW',s:others(a:p) | |
133 \ .' || s:tern_skip('.string(a:p).')',200,a:p[0]) > 0 | |
134 endfunction | |
135 | |
136 function s:label_col() | |
137 let pos = getpos('.')[1:2] | |
138 let [s:looksyn,s:free] = pos | |
139 call s:alternatePair(0) | |
140 if s:save_pos('s:IsBlock') | |
141 let poss = getpos('.')[1:2] | |
142 return call('cursor',pos) || !s:tern_col(poss) | |
143 elseif s:looking_at() == ':' | |
144 return !s:tern_col([0,0]) | |
145 endif | |
146 endfunction | |
9860
9eaf8ef656e9
commit https://github.com/vim/vim/commit/0952131376a517fc12dc5ae908a97018b4ee23f0
Christian Brabandt <cb@256bit.org>
parents:
6823
diff
changeset
|
147 |
9eaf8ef656e9
commit https://github.com/vim/vim/commit/0952131376a517fc12dc5ae908a97018b4ee23f0
Christian Brabandt <cb@256bit.org>
parents:
6823
diff
changeset
|
148 " configurable regexes that define continuation lines, not including (, {, or [. |
10548 | 149 let s:opfirst = '^' . get(g:,'javascript_opfirst', |
150 \ '\%([<>=,?^%|*/&]\|\([-.:+]\)\1\@!\|!=\|in\%(stanceof\)\=\>\)') | |
151 let s:continuation = get(g:,'javascript_continuation', | |
152 \ '\%([<=,.~!?/*^%|&:]\|+\@<!+\|-\@<!-\|=\@<!>\|\<\%(typeof\|delete\|void\|in\|instanceof\)\)') . '$' | |
9860
9eaf8ef656e9
commit https://github.com/vim/vim/commit/0952131376a517fc12dc5ae908a97018b4ee23f0
Christian Brabandt <cb@256bit.org>
parents:
6823
diff
changeset
|
153 |
10548 | 154 function s:continues(ln,con) |
155 return !cursor(a:ln, match(' '.a:con,s:continuation)) && | |
156 \ eval((['s:syn_at(line("."),col(".")) !~? "regex"'] + | |
157 \ repeat(['s:previous_token() != "."'],5) + [1])[ | |
158 \ index(split('/ typeof in instanceof void delete'),s:token())]) | |
9975
03fa8a51e9dc
commit https://github.com/vim/vim/commit/e4a3bcf28d92d0bde9ca227ccb40d401038185e5
Christian Brabandt <cb@256bit.org>
parents:
9860
diff
changeset
|
159 endfunction |
03fa8a51e9dc
commit https://github.com/vim/vim/commit/e4a3bcf28d92d0bde9ca227ccb40d401038185e5
Christian Brabandt <cb@256bit.org>
parents:
9860
diff
changeset
|
160 |
10548 | 161 " get the line of code stripped of comments and move cursor to the last |
162 " non-comment char. | |
163 function s:Trim(ln) | |
164 let pline = substitute(getline(a:ln),'\s*$','','') | |
165 let l:max = max([match(pline,'.*[^/]\zs\/[/*]'),0]) | |
166 while l:max && s:syn_at(a:ln, strlen(pline)) =~? s:syng_com | |
167 let pline = substitute(strpart(pline, 0, l:max),'\s*$','','') | |
168 let l:max = max([match(pline,'.*[^/]\zs\/[/*]'),0]) | |
169 endwhile | |
170 return cursor(a:ln,strlen(pline)) ? pline : pline | |
9860
9eaf8ef656e9
commit https://github.com/vim/vim/commit/0952131376a517fc12dc5ae908a97018b4ee23f0
Christian Brabandt <cb@256bit.org>
parents:
6823
diff
changeset
|
171 endfunction |
9eaf8ef656e9
commit https://github.com/vim/vim/commit/0952131376a517fc12dc5ae908a97018b4ee23f0
Christian Brabandt <cb@256bit.org>
parents:
6823
diff
changeset
|
172 |
10548 | 173 " Find line above 'lnum' that isn't empty or in a comment |
9860
9eaf8ef656e9
commit https://github.com/vim/vim/commit/0952131376a517fc12dc5ae908a97018b4ee23f0
Christian Brabandt <cb@256bit.org>
parents:
6823
diff
changeset
|
174 function s:PrevCodeLine(lnum) |
10548 | 175 let l:n = prevnonblank(a:lnum) |
176 while l:n | |
177 if getline(l:n) =~ '^\s*\/[/*]' | |
178 if (stridx(getline(l:n),'`') > 0 || getline(l:n-1)[-1:] == '\') && | |
179 \ s:syn_at(l:n,1) =~? s:syng_str | |
180 return l:n | |
181 endif | |
182 let l:n = prevnonblank(l:n-1) | |
183 elseif s:syn_at(l:n,1) =~? s:syng_com | |
184 let l:n = s:save_pos('eval', | |
185 \ 'cursor('.l:n.',1) + search(''\m\/\*'',"bW")') | |
186 else | |
187 return l:n | |
9860
9eaf8ef656e9
commit https://github.com/vim/vim/commit/0952131376a517fc12dc5ae908a97018b4ee23f0
Christian Brabandt <cb@256bit.org>
parents:
6823
diff
changeset
|
188 endif |
9eaf8ef656e9
commit https://github.com/vim/vim/commit/0952131376a517fc12dc5ae908a97018b4ee23f0
Christian Brabandt <cb@256bit.org>
parents:
6823
diff
changeset
|
189 endwhile |
9eaf8ef656e9
commit https://github.com/vim/vim/commit/0952131376a517fc12dc5ae908a97018b4ee23f0
Christian Brabandt <cb@256bit.org>
parents:
6823
diff
changeset
|
190 endfunction |
1118 | 191 |
9860
9eaf8ef656e9
commit https://github.com/vim/vim/commit/0952131376a517fc12dc5ae908a97018b4ee23f0
Christian Brabandt <cb@256bit.org>
parents:
6823
diff
changeset
|
192 " 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
|
193 function s:Balanced(lnum) |
10548 | 194 let l:open = 0 |
9860
9eaf8ef656e9
commit https://github.com/vim/vim/commit/0952131376a517fc12dc5ae908a97018b4ee23f0
Christian Brabandt <cb@256bit.org>
parents:
6823
diff
changeset
|
195 let l:line = getline(a:lnum) |
9eaf8ef656e9
commit https://github.com/vim/vim/commit/0952131376a517fc12dc5ae908a97018b4ee23f0
Christian Brabandt <cb@256bit.org>
parents:
6823
diff
changeset
|
196 let pos = match(l:line, '[][(){}]', 0) |
9eaf8ef656e9
commit https://github.com/vim/vim/commit/0952131376a517fc12dc5ae908a97018b4ee23f0
Christian Brabandt <cb@256bit.org>
parents:
6823
diff
changeset
|
197 while pos != -1 |
10548 | 198 if s:syn_at(a:lnum,pos + 1) !~? s:syng_strcom |
199 let l:open += match(' ' . l:line[pos],'[[({]') | |
200 if l:open < 0 | |
201 return | |
9860
9eaf8ef656e9
commit https://github.com/vim/vim/commit/0952131376a517fc12dc5ae908a97018b4ee23f0
Christian Brabandt <cb@256bit.org>
parents:
6823
diff
changeset
|
202 endif |
9eaf8ef656e9
commit https://github.com/vim/vim/commit/0952131376a517fc12dc5ae908a97018b4ee23f0
Christian Brabandt <cb@256bit.org>
parents:
6823
diff
changeset
|
203 endif |
9eaf8ef656e9
commit https://github.com/vim/vim/commit/0952131376a517fc12dc5ae908a97018b4ee23f0
Christian Brabandt <cb@256bit.org>
parents:
6823
diff
changeset
|
204 let pos = match(l:line, '[][(){}]', pos + 1) |
9eaf8ef656e9
commit https://github.com/vim/vim/commit/0952131376a517fc12dc5ae908a97018b4ee23f0
Christian Brabandt <cb@256bit.org>
parents:
6823
diff
changeset
|
205 endwhile |
10548 | 206 return !l:open |
207 endfunction | |
208 | |
209 function s:OneScope(lnum) | |
210 let pline = s:Trim(a:lnum) | |
211 let kw = 'else do' | |
212 if pline[-1:] == ')' && s:GetPair('(', ')', 'bW', s:skip_expr, 100) > 0 | |
213 call s:previous_token() | |
214 let kw = 'for if let while with' | |
215 if index(split('await each'),s:token()) + 1 | |
216 call s:previous_token() | |
217 let kw = 'for' | |
218 endif | |
219 endif | |
220 return pline[-2:] == '=>' || index(split(kw),s:token()) + 1 && | |
221 \ s:save_pos('s:previous_token') != '.' | |
9860
9eaf8ef656e9
commit https://github.com/vim/vim/commit/0952131376a517fc12dc5ae908a97018b4ee23f0
Christian Brabandt <cb@256bit.org>
parents:
6823
diff
changeset
|
222 endfunction |
10548 | 223 |
224 " returns braceless levels started by 'i' and above lines * &sw. 'num' is the | |
225 " lineNr which encloses the entire context, 'cont' if whether line 'i' + 1 is | |
226 " a continued expression, which could have started in a braceless context | |
227 function s:iscontOne(i,num,cont) | |
228 let [l:i, l:num, bL] = [a:i, a:num + !a:num, 0] | |
229 let pind = a:num ? indent(l:num) + s:W : 0 | |
230 let ind = indent(l:i) + (a:cont ? 0 : s:W) | |
231 while l:i >= l:num && (ind > pind || l:i == l:num) | |
232 if indent(l:i) < ind && s:OneScope(l:i) | |
233 let bL += s:W | |
234 let l:i = line('.') | |
235 elseif !a:cont || bL || ind < indent(a:i) | |
236 break | |
237 endif | |
238 let ind = min([ind, indent(l:i)]) | |
239 let l:i = s:PrevCodeLine(l:i - 1) | |
240 endwhile | |
241 return bL | |
242 endfunction | |
243 | |
244 " https://github.com/sweet-js/sweet.js/wiki/design#give-lookbehind-to-the-reader | |
245 function s:IsBlock() | |
246 if s:looking_at() == '{' | |
247 let l:n = line('.') | |
248 let char = s:previous_token() | |
249 let syn = char =~ '[{>/]' ? s:syn_at(line('.'),col('.')-(char == '{')) : '' | |
250 if syn =~? 'xml\|jsx' | |
251 return char != '{' | |
252 elseif char =~ '\k' | |
253 return index(split('return const let import export yield default delete var await void typeof throw case new in instanceof') | |
254 \ ,char) < (line('.') != l:n) || s:previous_token() == '.' | |
255 elseif char == '>' | |
256 return getline('.')[col('.')-2] == '=' || syn =~? '^jsflow' | |
257 elseif char == ':' | |
258 return getline('.')[col('.')-2] != ':' && s:label_col() | |
259 endif | |
260 return syn =~? 'regex' || char !~ '[-=~!<*+,/?^%|&([]' | |
261 endif | |
262 endfunction | |
9860
9eaf8ef656e9
commit https://github.com/vim/vim/commit/0952131376a517fc12dc5ae908a97018b4ee23f0
Christian Brabandt <cb@256bit.org>
parents:
6823
diff
changeset
|
263 |
9eaf8ef656e9
commit https://github.com/vim/vim/commit/0952131376a517fc12dc5ae908a97018b4ee23f0
Christian Brabandt <cb@256bit.org>
parents:
6823
diff
changeset
|
264 function GetJavascriptIndent() |
10548 | 265 let b:js_cache = get(b:,'js_cache',[0,0,0]) |
9860
9eaf8ef656e9
commit https://github.com/vim/vim/commit/0952131376a517fc12dc5ae908a97018b4ee23f0
Christian Brabandt <cb@256bit.org>
parents:
6823
diff
changeset
|
266 " Get the current line. |
10548 | 267 call cursor(v:lnum,1) |
268 let l:line = getline('.') | |
269 let syns = s:syn_at(v:lnum, 1) | |
9860
9eaf8ef656e9
commit https://github.com/vim/vim/commit/0952131376a517fc12dc5ae908a97018b4ee23f0
Christian Brabandt <cb@256bit.org>
parents:
6823
diff
changeset
|
270 |
10548 | 271 " start with strings,comments,etc. |
272 if syns =~? s:syng_com | |
273 if l:line =~ '^\s*\*' | |
274 return cindent(v:lnum) | |
275 elseif l:line !~ '^\s*\/[/*]' | |
276 return -1 | |
277 endif | |
278 elseif syns =~? s:syng_str && l:line !~ '^[''"]' | |
279 if b:js_cache[0] == v:lnum - 1 && s:Balanced(v:lnum-1) | |
280 let b:js_cache[0] = v:lnum | |
281 endif | |
9860
9eaf8ef656e9
commit https://github.com/vim/vim/commit/0952131376a517fc12dc5ae908a97018b4ee23f0
Christian Brabandt <cb@256bit.org>
parents:
6823
diff
changeset
|
282 return -1 |
9eaf8ef656e9
commit https://github.com/vim/vim/commit/0952131376a517fc12dc5ae908a97018b4ee23f0
Christian Brabandt <cb@256bit.org>
parents:
6823
diff
changeset
|
283 endif |
9eaf8ef656e9
commit https://github.com/vim/vim/commit/0952131376a517fc12dc5ae908a97018b4ee23f0
Christian Brabandt <cb@256bit.org>
parents:
6823
diff
changeset
|
284 let l:lnum = s:PrevCodeLine(v:lnum - 1) |
10548 | 285 if !l:lnum |
286 return | |
9860
9eaf8ef656e9
commit https://github.com/vim/vim/commit/0952131376a517fc12dc5ae908a97018b4ee23f0
Christian Brabandt <cb@256bit.org>
parents:
6823
diff
changeset
|
287 endif |
9eaf8ef656e9
commit https://github.com/vim/vim/commit/0952131376a517fc12dc5ae908a97018b4ee23f0
Christian Brabandt <cb@256bit.org>
parents:
6823
diff
changeset
|
288 |
10548 | 289 let l:line = substitute(l:line,'^\s*','','') |
290 if l:line[:1] == '/*' | |
291 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
|
292 endif |
10548 | 293 if l:line =~ '^\/[/*]' |
294 let l:line = '' | |
295 endif | |
9860
9eaf8ef656e9
commit https://github.com/vim/vim/commit/0952131376a517fc12dc5ae908a97018b4ee23f0
Christian Brabandt <cb@256bit.org>
parents:
6823
diff
changeset
|
296 |
10548 | 297 " the containing paren, bracket, or curly. Many hacks for performance |
298 let idx = strlen(l:line) ? stridx('])}',l:line[0]) : -1 | |
299 if b:js_cache[0] >= l:lnum && b:js_cache[0] < v:lnum && | |
300 \ (b:js_cache[0] > l:lnum || s:Balanced(l:lnum)) | |
301 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
|
302 else |
10548 | 303 let [s:looksyn, s:free, top] = [v:lnum - 1, 1, (!indent(l:lnum) && |
304 \ s:syn_at(l:lnum,1) !~? s:syng_str) * l:lnum] | |
305 if idx + 1 | |
306 call s:GetPair(['\[','(','{'][idx], '])}'[idx],'bW','s:skip_func()',2000,top) | |
307 elseif indent(v:lnum) && syns =~? 'block' | |
308 call s:GetPair('{','}','bW','s:skip_func()',2000,top) | |
309 else | |
310 call s:alternatePair(top) | |
311 endif | |
9860
9eaf8ef656e9
commit https://github.com/vim/vim/commit/0952131376a517fc12dc5ae908a97018b4ee23f0
Christian Brabandt <cb@256bit.org>
parents:
6823
diff
changeset
|
312 endif |
9eaf8ef656e9
commit https://github.com/vim/vim/commit/0952131376a517fc12dc5ae908a97018b4ee23f0
Christian Brabandt <cb@256bit.org>
parents:
6823
diff
changeset
|
313 |
10548 | 314 if idx + 1 || l:line[:1] == '|}' |
315 if idx == 2 && search('\m\S','bW',line('.')) && s:looking_at() == ')' | |
316 call s:GetPair('(',')','bW',s:skip_expr,200) | |
317 endif | |
318 return indent('.') | |
9860
9eaf8ef656e9
commit https://github.com/vim/vim/commit/0952131376a517fc12dc5ae908a97018b4ee23f0
Christian Brabandt <cb@256bit.org>
parents:
6823
diff
changeset
|
319 endif |
9eaf8ef656e9
commit https://github.com/vim/vim/commit/0952131376a517fc12dc5ae908a97018b4ee23f0
Christian Brabandt <cb@256bit.org>
parents:
6823
diff
changeset
|
320 |
10548 | 321 let b:js_cache = [v:lnum] + (line('.') == v:lnum ? [0,0] : getpos('.')[1:2]) |
322 let num = b:js_cache[1] | |
9975
03fa8a51e9dc
commit https://github.com/vim/vim/commit/e4a3bcf28d92d0bde9ca227ccb40d401038185e5
Christian Brabandt <cb@256bit.org>
parents:
9860
diff
changeset
|
323 |
10548 | 324 let [s:W, isOp, bL, switch_offset] = [s:sw(),0,0,0] |
325 if !num || s:IsBlock() | |
326 let pline = s:save_pos('s:Trim',l:lnum) | |
327 if num && s:looking_at() == ')' && s:GetPair('(', ')', 'bW', s:skip_expr, 100) > 0 | |
328 let num = line('.') | |
329 if s:previous_token() ==# 'switch' && s:previous_token() != '.' | |
330 if &cino !~ ':' || !has('float') | |
331 let switch_offset = s:W | |
332 else | |
333 let cinc = matchlist(&cino,'.*:\(-\)\=\([0-9.]*\)\(s\)\=\C') | |
334 let switch_offset = float2nr(str2float(cinc[1].(strlen(cinc[2]) ? cinc[2] : strlen(cinc[3]))) | |
335 \ * (strlen(cinc[3]) ? s:W : 1)) | |
336 endif | |
337 if pline[-1:] != '.' && l:line =~# '^\%(default\|case\)\>' | |
338 return indent(num) + switch_offset | |
339 endif | |
340 endif | |
341 endif | |
342 if pline[-1:] !~ '[{;]' | |
343 if pline =~# ':\@<!:$' | |
344 call cursor(l:lnum,strlen(pline)) | |
345 let isOp = s:tern_col(b:js_cache[1:2]) | |
346 else | |
347 let isOp = l:line =~# s:opfirst || s:continues(l:lnum,pline) | |
348 endif | |
349 let bL = s:iscontOne(l:lnum,num,isOp) | |
350 let bL -= (bL && l:line[0] == '{') * s:W | |
351 endif | |
9860
9eaf8ef656e9
commit https://github.com/vim/vim/commit/0952131376a517fc12dc5ae908a97018b4ee23f0
Christian Brabandt <cb@256bit.org>
parents:
6823
diff
changeset
|
352 endif |
9eaf8ef656e9
commit https://github.com/vim/vim/commit/0952131376a517fc12dc5ae908a97018b4ee23f0
Christian Brabandt <cb@256bit.org>
parents:
6823
diff
changeset
|
353 |
10548 | 354 " main return |
355 if isOp | |
356 return (num ? indent(num) : -s:W) + (s:W * 2) + switch_offset + bL | |
357 elseif num | |
358 return indent(num) + s:W + switch_offset + bL | |
359 endif | |
360 return bL | |
9860
9eaf8ef656e9
commit https://github.com/vim/vim/commit/0952131376a517fc12dc5ae908a97018b4ee23f0
Christian Brabandt <cb@256bit.org>
parents:
6823
diff
changeset
|
361 endfunction |
9eaf8ef656e9
commit https://github.com/vim/vim/commit/0952131376a517fc12dc5ae908a97018b4ee23f0
Christian Brabandt <cb@256bit.org>
parents:
6823
diff
changeset
|
362 |
9eaf8ef656e9
commit https://github.com/vim/vim/commit/0952131376a517fc12dc5ae908a97018b4ee23f0
Christian Brabandt <cb@256bit.org>
parents:
6823
diff
changeset
|
363 let &cpo = s:cpo_save |
9eaf8ef656e9
commit https://github.com/vim/vim/commit/0952131376a517fc12dc5ae908a97018b4ee23f0
Christian Brabandt <cb@256bit.org>
parents:
6823
diff
changeset
|
364 unlet s:cpo_save |