Skip to content

Commit 8eb7313

Browse files
pkopriv2vim-scripts
authored andcommitted
Version b2.1
Fixed: Object endings regex. Now doesn't include preceding {, or [; Fixed: Multi-Line invocation (Caveat: Must end in a ; or ,)
1 parent 1ad8bf5 commit 8eb7313

File tree

2 files changed

+102
-48
lines changed

2 files changed

+102
-48
lines changed

indent/javascript.vim

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@
44
" URL:
55
" Last Change: April 30, 2010
66

7-
if exists('b:did_indent')
8-
finish
9-
endif
10-
let b:did_indent = 1
7+
"if exists('b:did_indent')
8+
"finish
9+
"endif
10+
"let b:did_indent = 1
1111

1212
setlocal indentexpr=GetJsIndent(v:lnum)
1313
setlocal indentkeys=0{,0},0),:,!^F,o,O,e,*<Return>,=*/
@@ -22,7 +22,7 @@ let s:js_line_comment = '\s*\(//.*\)*'
2222

2323
" Simple Objects
2424
let s:js_object_beg = '[{\[]\s*'
25-
let s:js_object_end = '^[^}\]][}\]][;,]\=\s*'
25+
let s:js_object_end = '^[^][{}]*[}\]][;,]\=\s*'
2626

2727
" Immediately Executed Anonymous Function
2828
let s:js_s_anon_beg = '(\s*function\s*(.*)\s*'
@@ -45,6 +45,10 @@ let s:js_multi_beg = '([^()]*\s*'
4545
let s:js_s_multi_end = '^[^()]*)\s*'
4646
let s:js_m_multi_end = s:js_s_multi_end . '\s*{\s*'
4747

48+
" Multi line invocation
49+
let s:js_multi_invok_beg = s:js_multi_beg
50+
let s:js_multi_invok_end = s:js_s_multi_end . '[;,]\{1}\s*'
51+
4852
" Special switch control
4953
let s:js_s_switch_beg = 'switch\s*(.*)\s*' "Actually not allowed.
5054
let s:js_m_switch_beg = s:js_s_switch_beg . '\s*{\s*'
@@ -54,6 +58,8 @@ let s:js_switch_mid = '\(case.*\|default\)\s*:\s*'
5458
" Single line comment (// xxx)
5559
let s:syn_comment = 'Comment'
5660

61+
echo "} // 0" =~ s:js_object_end . s:js_line_comment . '$'
62+
5763

5864
" 2. Aux. Functions
5965
" =================
@@ -173,8 +179,8 @@ function! GetJsIndent(lnum)
173179

174180

175181

176-
" Handle: Mutli-Line Invocation/Declaration
177-
" ===========================================
182+
" Handle: Mutli-Line Block Invocation/Function Declaration
183+
" ========================================================
178184
if pline =~ s:js_multi_beg . s:js_line_comment . '$'
179185
echo "Pline matched multi invoke/declare"
180186
return ind + &sw
@@ -208,9 +214,16 @@ function! GetJsIndent(lnum)
208214
return ind - &sw
209215
endif
210216

217+
" Handle: Multi-Line Invocation
218+
" =============================
219+
if line =~ s:js_multi_invok_end . s:js_line_comment . '$'
220+
echo "Pline matched multi invocation end"
221+
return ind - &sw
222+
endif
223+
211224

212225
" Handle: Switch Control Blocks
213-
" ===============================
226+
" =============================
214227
if pline =~ s:js_m_switch_beg . s:js_line_comment . '$'
215228
echo "PLine matched switch cntrl beginning"
216229
return ind
@@ -309,10 +322,10 @@ function! GetJsIndent(lnum)
309322
endif
310323
endif
311324

312-
if line =~ s:js_m_cntrl_end . s:js_line_comment . '$'
313-
echo "Line matched multi line control end"
314-
return ind - &sw
315-
endif
325+
"if line =~ s:js_m_cntrl_end . s:js_line_comment . '$'
326+
"echo "Line matched multi line control end"
327+
"return ind - &sw
328+
"endif
316329

317330
" Handle: Basic Objects
318331
" =====================

test.js

Lines changed: 77 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
// Sample Javascript test code
33
//
44
//
5-
/
65

76
// = Case: Arithmetic (1)
87
var x = 1 + 1; // 0
@@ -172,7 +171,6 @@ for (var i = 0; i < blah.length; i++) { // 0
172171
// = Case: for (2)
173172
for (var i = 0; i < blah.length; i++) // 0
174173
blah[i]; // 1
175-
176174
x; // 0
177175

178176
// = Case: switch
@@ -191,43 +189,43 @@ default: // 0
191189
}
192190

193191
// = Case: try (1)
194-
try {
195-
x;
196-
}
192+
try { // 0
193+
x; // 1
194+
} // 0
197195

198-
// = case: try (2)
199-
try
200-
x;
201-
y;
196+
// = Case: try (2)
197+
try // 0
198+
x; // 1
199+
y; // 0
202200

203201
// = Case: try (3)
204-
try
205-
{
206-
x;
207-
}
202+
try // 0
203+
{ // 0
204+
x; // 1
205+
} // 0
208206

209207
// = Case: try catch (1)
210-
try {
211-
x;
212-
} catch(e) {
213-
y;
214-
}
208+
try { // 0
209+
x; // 1
210+
} catch(e) { // 0
211+
y; // 1
212+
} // 0
215213

216-
// case: try catch (2)
217-
try
218-
{
219-
x;
220-
}
221-
catch(e)
222-
{
223-
y;
224-
}
214+
// Case: try catch (2)
215+
try // 0
216+
{ // 0
217+
x; // 1
218+
} // 0
219+
catch(e) // 0
220+
{ // 0
221+
y; // 1
222+
} // 0
225223

226224
// Case: try catch (3)
227-
try
228-
x;
229-
catch(e)
230-
y;
225+
try // 0
226+
x; // 1
227+
catch(e) // 0
228+
y; // 1
231229

232230

233231
// Case: try catch finally (1)
@@ -289,8 +287,51 @@ finally // 1
289287
} // 0
290288
)(x); // 0
291289

292-
// = Case: Anonymous Function (2)
293-
(function(x) { // 0
294-
x; // 1
295-
y; // 1
296-
})(x); // 0
290+
291+
// = Case: COMPLEX
292+
(function(window, undefined) { // 0
293+
// = Class: Test // 1
294+
// // 1
295+
// = Description: This is a // 1
296+
// test class. // 1
297+
// // 1
298+
var Test = new Class({ // 1
299+
initialize: function() { // 2
300+
this.test = test; // 3
301+
}, // 2
302+
303+
// = Method: test // 2
304+
// // 2
305+
// = Description: // 2
306+
// // 2
307+
test: function(blah) { // 2
308+
if(blah) { // 3
309+
return "blah"; // 4
310+
} else if(blah === undefined) // 3
311+
return "blahblah"; // 4
312+
else { // 3
313+
// another comment. // 4
314+
return "blahblahblah"; // 4
315+
} // 3
316+
317+
var x = { // 3
318+
y: function() { // 4
319+
for (var i = 0; i < blah.length; i++) { // 5
320+
blah[i]; // 6
321+
}; // 5
322+
} // 4
323+
}; // 3
324+
325+
return new function() { // 3
326+
}; // 3
327+
}, // 2
328+
329+
// = Method: blah // 2
330+
// // 2
331+
// = Description: description // 2
332+
// // 2
333+
blah: function(haha) { // 2
334+
return this.test; // 3
335+
} // 2
336+
}); // 1
337+
})(this); // 0

0 commit comments

Comments
 (0)