-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathjavascript.vim
More file actions
337 lines (276 loc) · 9.02 KB
/
Copy pathjavascript.vim
File metadata and controls
337 lines (276 loc) · 9.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
" Vim indent file
" Language: JavaScript
" Author: Preston Koprivica (pkopriv2@gmail.com)
" URL:
" Last Change: April 30, 2010
if exists('b:did_indent')
finish
endif
let b:did_indent = 1
setlocal indentexpr=GetJsIndent(v:lnum)
setlocal indentkeys=0{,0},0),:,!^F,o,O,e,*<Return>,=*/
" 1. Variables
" ============
" Inline comments (for anchoring other statements)
let s:js_line_comment = '\s*\(//.*\)*'
" Simple Objects
let s:js_object_beg = '[{\[]\s*'
let s:js_object_end = '^[^}\]][}\]][;,]\=\s*'
" Immediately Executed Anonymous Function
let s:js_s_anon_beg = '(\s*function\s*(.*)\s*'
let s:js_s_anon_end = ')(.*)[;,]\s*'
let s:js_m_anon_beg = s:js_s_anon_beg . '\s*{\s*'
let s:js_m_anon_end = '\s*}\s*' . s:js_s_anon_end
" Simple control blocks (those not beginngin with "{")
let s:js_s_cntrl_beg = '\(\(\(if\|for\|with\)\s*(.*)\)\|try\)\s*'
let s:js_s_cntrl_mid = '\(\(\(else\s*if\|catch\)\s*(.*)\)\|\(finally\|else\)\)\s*'
" Multi line control blocks (those beginning with "{")
let s:js_m_cntrl_beg = s:js_s_cntrl_beg . '\s*{\s*'
let s:js_m_cntrl_mid = '}\=\s*' . s:js_s_cntrl_mid . '\s*{\s*'
let s:js_m_cntrl_end = '}\s*'
" Multi line declarations & invocations
let s:js_multi_beg = '([^()]*\s*'
let s:js_s_multi_end = '^[^()]*)\s*'
let s:js_m_multi_end = s:js_s_multi_end . '\s*{\s*'
" Special switch control
let s:js_s_switch_beg = 'switch\s*(.*)\s*' "Actually not allowed.
let s:js_m_switch_beg = s:js_s_switch_beg . '\s*{\s*'
let s:js_switch_mid = '\(case.*\|default\)\s*:\s*'
" Single line comment (// xxx)
let s:syn_comment = 'Comment'
" 2. Aux. Functions
" =================
" = Method: GetNonCommentLine
"
" Grabs the nearest non-commented line
function! s:GetNonCommentLine(lnum)
let lnum = prevnonblank(a:lnum)
while lnum > 0
if s:IsComment(lnum)
let lnum = prevnonblank(lnum - 1)
else
return lnum
endif
endwhile
return lnum
endfunction
" = Method: IsInComment
"
" Determines whether the specified position is contained in a comment. "Note:
" This depends on a
function! s:IsInComment(lnum, cnum)
return synIDattr(synID(a:lnum, a:cnum, 1), 'name') =~ s:syn_comment
endfunction
" = Method: IsComment
"
" Determines whether a line is a comment or not.
function! s:IsComment(lnum)
let line = getline(a:lnum)
return s:IsInComment(a:lnum, 1) && s:IsInComment(a:lnum, strlen(line)) "Doesn't absolutely work. Only Probably!
endfunction
" 3. Indenter
" ===========
function! GetJsIndent(lnum)
" Grab the first non-comment line prior to this line
let pnum = s:GetNonCommentLine(a:lnum-1)
" First line, start at indent = 0
if pnum == 0
echo "No, noncomment lines prior to: " . a:lnum
return 0
endif
" Grab the second non-comment line prior to this line
let ppnum = s:GetNonCommentLine(pnum-1)
echo "Line: " . a:lnum
echo "PLine: " . pnum
echo "PPLine: " . ppnum
" Grab the lines themselves.
let line = getline(a:lnum)
let pline = getline(pnum)
let ppline = getline(ppnum)
" Determine the current level of indentation
let ind = indent(pnum)
" Handle: Immediately executed anonymous functions
" ================================================'
if pline =~ s:js_s_anon_beg . s:js_line_comment . '$'
echo "PLine matched anonymous function without ending {"
if line =~ s:js_object_beg . s:js_line_comment . '$'
echo "Line matched object beginning"
return ind
else
echo "Line didn't match object beginning. NOT SURE WHAT TO DO!"
return ind
endif
endif
if pline =~ s:js_m_anon_beg . s:js_line_comment . '$'
echo "Pline matched anonymous function with ending {"
if line =~ s:js_m_cntrl_end . s:js_line_comment . '$' || line =~ s:js_m_anon_end . s:js_line_comment . '$'
echo "Line matched } or anonymous function end"
return ind
else
echo "Line didn't match } or anymous function end"
return ind + &sw
endif
endif
if line =~ '^' . s:js_s_anon_end . s:js_line_comment . '$'
echo "Line matched anonymous ending with )(*)"
if pline =~ s:js_object_end . s:js_line_comment . '$'
echo "PLine matched object end"
return ind
else
echo "Line didn't match object end. NOT SURE WHAT TO DO!"
return ind
endif
endif
if line =~ s:js_m_anon_end . s:js_line_comment . '$'
echo "Line matched anonymous ending with })(*)"
if pline =~ s:js_object_beg . s:js_line_comment . '$'
echo "PLine matched object beginning"
return ind
else
echo "PLine didnt' match object beginning"
return ind - &sw
endif
endif
" Handle: Mutli-Line Invocation/Declaration
" ===========================================
if pline =~ s:js_multi_beg . s:js_line_comment . '$'
echo "Pline matched multi invoke/declare"
return ind + &sw
endif
if pline =~ s:js_s_multi_end . s:js_line_comment . '$'
echo "Pline matched multi end without inline {"
if line =~ s:js_object_beg . s:js_line_comment . '$'
echo "Line matched object beg"
return ind - &sw
else
echo "line didn't match object beginning"
return ind
endif
endif
if pline =~ s:js_m_multi_end . s:js_line_comment . '$'
echo "Pline matched multi end with inline {"
if line =~ s:js_object_end . s:js_line_comment . '$'
echo "Line matched object end"
return ind - &sw
else
echo "Line didn't matched object end"
return ind
endif
endif
if ppline =~ s:js_s_multi_end . s:js_line_comment . '$' &&
\ pline !~ s:js_object_beg . s:js_line_comment . '$'
echo "PPLine matched multi invoke/declaration end without inline {"
return ind - &sw
endif
" Handle: Switch Control Blocks
" ===============================
if pline =~ s:js_m_switch_beg . s:js_line_comment . '$'
echo "PLine matched switch cntrl beginning"
return ind
endif
if pline =~ s:js_switch_mid . s:js_line_comment . '$'
echo "PLine matched switch cntrl mid"
if line =~ s:js_switch_mid . s:js_line_comment . '$'
echo "Line matched a cntrl mid"
return ind
else
echo "Line didnt matcha cntrl mid"
return ind + &sw
endif
endif
if line =~ s:js_switch_mid " Doesn't need end anchor
echo "Line matched cntrl mid"
return ind - &sw
endif
" Handle: Single Line Control Blocks
" ==========================
if pline =~ s:js_s_cntrl_beg . s:js_line_comment . '$'
echo "Pline matched single line control beg"
if line =~ s:js_s_cntrl_mid. s:js_line_comment . '$' || line =~ s:js_object_beg. s:js_line_comment . '$'
echo "Line matched single line control mid"
return ind
else
echo "Line didn't match single line control mid"
return ind + &sw
endif
endif
if pline =~ s:js_s_cntrl_mid . s:js_line_comment . '$'
echo "Pline matched single line control mid"
if line =~ s:js_s_cntrl_mid . s:js_line_comment . '$' || line =~ s:js_object_beg . s:js_line_comment . '$'
echo "Line matched single line control mid"
return ind
else
echo "Line didn't match single line control mid"
return ind + &sw
endif
endif
if line =~ s:js_s_cntrl_mid . s:js_line_comment . '$'
echo "Line matched single line control mid"
if pline =~ s:js_m_cntrl_end . s:js_line_comment . '$'
echo "PLine matched multi line control end"
return ind
else
echo "Pline didn't match object end"
return ind - &sw
endif
endif
if ( ppline =~ s:js_s_cntrl_beg . s:js_line_comment . '$' || ppline =~ s:js_s_cntrl_mid . s:js_line_comment . '$' ) &&
\ pline !~ s:js_object_beg . s:js_line_comment . '$'
echo "PPLine matched single line control beg or mid"
return ind - &sw
endif
" Handle: Multi Line Cntrl Blocks
" ===============================
if pline =~ s:js_m_cntrl_beg . s:js_line_comment . '$'
echo "Pline matched multi line control beg"
if line =~ s:js_m_cntrl_mid . s:js_line_comment . '$' || line =~ s:js_m_cntrl_end . s:js_line_comment . '$'
echo "Line matched multi line control mid or end"
return ind
else
echo "Line didn't match multi line control mid or end"
return ind + &sw
endif
endif
if pline =~ s:js_m_cntrl_mid . s:js_line_comment . '$'
echo "Pline matched multi line control mid"
if line =~ s:js_m_cntrl_mid . s:js_line_comment . '$' || line =~ s:js_m_cntrl_end . s:js_line_comment . '$'
echo "Line matched multi line control mid or end"
return ind
else
echo "Line didn't match multi line control mid or end"
return ind + &sw
endif
endif
if line =~ s:js_m_cntrl_mid . s:js_line_comment . '$'
echo "Line matched multi line control mid"
if pline =~ s:js_m_cntrl_end . s:js_line_comment . '$'
echo "PLine matched multi line control end"
return ind
else
echo "PLine didn't match multi line control end"
return ind - &sw
endif
endif
if line =~ s:js_m_cntrl_end . s:js_line_comment . '$'
echo "Line matched multi line control end"
return ind - &sw
endif
" Handle: Basic Objects
" =====================
if pline =~ s:js_object_beg . s:js_line_comment . '$'
echo "PLine matched object beginning"
if line =~ s:js_object_end . s:js_line_comment . '$'
echo "Line matched object end"
return ind
else
echo "Line didn't match object end"
return ind + &sw
endif
endif
if line =~ s:js_object_end . s:js_line_comment . '$'
echo "Line matched object end"
return ind - &sw
endif
echo "Line didn't match anything. Retaining indent"
return ind
endfunction