Skip to content

Commit 69a53fb

Browse files
pkopriv2vim-scripts
authored andcommitted
Version b3.0
Fixed: Runaway Indentation Fixed: indentkeys correctly invokes indent Fixed: Comment lines with keywords. (Tentative fix)
1 parent 51f9a9e commit 69a53fb

File tree

2 files changed

+81
-135
lines changed

2 files changed

+81
-135
lines changed

indent/javascript.vim

Lines changed: 68 additions & 122 deletions
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,22 @@
44
" URL:
55
" Last Change: April 30, 2010
66

7+
" 0. Standard Stuff
8+
" =================
9+
710
" Only load one indent script per buffer
8-
"if exists('b:did_indent')
9-
"finish
10-
"endif
11-
"let b:did_indent = 1
11+
if exists('b:did_indent')
12+
finish
13+
endif
14+
let b:did_indent = 1
1215

1316
" Set the global log variable 1 = logging enabled, 0 = logging disabled
1417
if !exists("g:js_indent_log")
1518
let g:js_indent_log = 1
1619
endif
1720

1821
setlocal indentexpr=GetJsIndent(v:lnum)
19-
setlocal indentkeys=0{,0},0),:,!^F,o,O,e,*<Return>,=*/
20-
22+
setlocal indentkeys=0{,0},o,O,e,!<Tab>,*<Return>
2123

2224
" 1. Variables
2325
" ============
@@ -30,16 +32,9 @@ let s:js_line_comment = '\s*\(//.*\)*'
3032
let s:js_object_beg = '[{\[]\s*'
3133
let s:js_object_end = '^[^][{}]*[}\]][;,]\=\s*'
3234

33-
" Immediately Executed Anonymous Function
34-
let s:js_s_anon_beg = '(\s*function\s*(.*)\s*'
35-
let s:js_s_anon_end = ')(.*)[;,]\s*'
36-
37-
let s:js_m_anon_beg = s:js_s_anon_beg . '\s*{\s*'
38-
let s:js_m_anon_end = '\s*}\s*' . s:js_s_anon_end
39-
4035
" Simple control blocks (those not beginngin with "{")
41-
let s:js_s_cntrl_beg = '\(\(\(if\|for\|with\|while\)\s*(.*)\)\|\(try\|do\)\)\s*'
42-
let s:js_s_cntrl_mid = '\(\(\(else\s*if\|catch\)\s*(.*)\)\|\(finally\|else\)\)\s*'
36+
let s:js_s_cntrl_beg = '^\s*\(\(\(if\|for\|with\|while\)\s*(.*)\)\|\(try\|do\)\)\s*'
37+
let s:js_s_cntrl_mid = '^\s*\(\(\(else\s*if\|catch\)\s*(.*)\)\|\(finally\|else\)\)\s*'
4338

4439
" Multi line control blocks (those beginning with "{")
4540
let s:js_m_cntrl_beg = s:js_s_cntrl_beg . '\s*{\s*'
@@ -58,12 +53,10 @@ let s:js_multi_invok_end = s:js_s_multi_end . '[;,]\{1}\s*'
5853
" Special switch control
5954
let s:js_s_switch_beg = 'switch\s*(.*)\s*' "Actually not allowed.
6055
let s:js_m_switch_beg = s:js_s_switch_beg . '\s*{\s*'
61-
6256
let s:js_switch_mid = '^.*\(case.*\|default\)\s*:\s*'
6357

6458
" Single line comment (// xxx)
65-
let s:syn_comment = 'Comment'
66-
59+
let s:syn_comment = '\(Comment\|String\)'
6760

6861
" 2. Aux. Functions
6962
" =================
@@ -93,7 +86,7 @@ endfunction
9386
" Determines whether the specified position is contained in a comment. "Note:
9487
" This depends on a
9588
function! s:IsInComment(lnum, cnum)
96-
return synIDattr(synID(a:lnum, a:cnum, 1), 'name') =~ s:syn_comment
89+
return synIDattr(synID(a:lnum, a:cnum, 1), 'name') =~? s:syn_comment
9790
endfunction
9891

9992

@@ -106,7 +99,7 @@ function! s:IsComment(lnum)
10699

107100
return s:IsInComment(a:lnum, 1) && s:IsInComment(a:lnum, strlen(line)) "Doesn't absolutely work. Only Probably!
108101
endfunction
109-
102+
110103

111104

112105
" = Method: Log
@@ -147,54 +140,6 @@ function! GetJsIndent(lnum)
147140
" Determine the current level of indentation
148141
let ind = indent(pnum)
149142

150-
" Handle: Immediately executed anonymous functions
151-
" ================================================'
152-
if pline =~ s:js_s_anon_beg . s:js_line_comment . '$'
153-
call s:Log("PLine matched anonymous function without ending {")
154-
if line =~ s:js_object_beg . s:js_line_comment . '$'
155-
call s:Log("Line matched object beginning")
156-
return ind
157-
else
158-
call s:Log("Line didn't match object beginning. NOT SURE WHAT TO DO!")
159-
return ind
160-
endif
161-
endif
162-
163-
if pline =~ s:js_m_anon_beg . s:js_line_comment . '$'
164-
call s:Log("Pline matched anonymous function with ending {")
165-
if line =~ s:js_m_cntrl_end . s:js_line_comment . '$' || line =~ s:js_m_anon_end . s:js_line_comment . '$'
166-
call s:Log("Line matched } or anonymous function end")
167-
return ind
168-
else
169-
call s:Log("Line didn't match } or anymous function end")
170-
return ind + &sw
171-
endif
172-
endif
173-
174-
if line =~ '^' . s:js_s_anon_end . s:js_line_comment . '$'
175-
call s:Log("Line matched anonymous ending with )(*)")
176-
if pline =~ s:js_object_end . s:js_line_comment . '$'
177-
call s:Log("PLine matched object end")
178-
return ind
179-
else
180-
call s:Log("Line didn't match object end. NOT SURE WHAT TO DO!")
181-
return ind
182-
endif
183-
endif
184-
185-
if line =~ s:js_m_anon_end . s:js_line_comment . '$'
186-
call s:Log("Line matched anonymous ending with })(*)")
187-
if pline =~ s:js_object_beg . s:js_line_comment . '$'
188-
call s:Log("PLine matched object beginning")
189-
return ind
190-
else
191-
call s:Log("PLine didnt' match object beginning")
192-
return ind - &sw
193-
endif
194-
endif
195-
196-
197-
198143
" Handle: Mutli-Line Block Invocation/Function Declaration
199144
" ========================================================
200145
if pline =~ s:js_multi_beg . s:js_line_comment . '$'
@@ -264,7 +209,7 @@ function! GetJsIndent(lnum)
264209
call s:Log("Line matched a cntrl mid")
265210
return ind
266211
else
267-
call s:Log("Line didnt matcha cntrl mid")
212+
call s:Log("Line didnt match a cntrl mid")
268213
return ind + &sw
269214
endif
270215
endif
@@ -316,68 +261,69 @@ function! GetJsIndent(lnum)
316261
endif
317262

318263

319-
" Handle: Multi Line Cntrl Blocks
320-
" ===============================
321-
if pline =~ s:js_m_cntrl_beg . s:js_line_comment . '$'
322-
call s:Log("Pline matched multi line control beg")
323-
if line =~ s:js_m_cntrl_mid . s:js_line_comment . '$' || line =~ s:js_m_cntrl_end . s:js_line_comment . '$'
324-
call s:Log("Line matched multi line control mid or end")
325-
return ind
326-
else
327-
call s:Log("Line didn't match multi line control mid or end")
328-
return ind + &sw
329-
endif
330-
endif
264+
" Handle: {}
265+
" ==========
266+
if line =~ '^[^{]*}' && !s:IsComment(a:lnum) && line !~ '"[^}]*}[^}]*"'
267+
call s:Log("Line matched closing bracket")
331268

332-
if pline =~ s:js_m_cntrl_mid . s:js_line_comment . '$'
333-
call s:Log("Pline matched multi line control mid")
334-
if line =~ s:js_m_cntrl_mid . s:js_line_comment . '$' || line =~ s:js_m_cntrl_end . s:js_line_comment . '$'
335-
call s:Log("Line matched multi line control mid or end")
336-
return ind
337-
else
338-
call s:Log("Line didn't match multi line control mid or end")
339-
return ind + &sw
340-
endif
341-
endif
269+
" Save the cursor position.
270+
let curpos = getpos(".")
342271

343-
if line =~ s:js_m_cntrl_mid . s:js_line_comment . '$'
344-
call s:Log("Line matched multi line control mid")
345-
if pline =~ s:js_m_cntrl_end . s:js_line_comment . '$'
346-
call s:Log("PLine matched multi line control end")
347-
return ind
348-
else
349-
call s:Log("PLine didn't match multi line control end")
350-
return ind - &sw
272+
" Set the cursor position to the beginning of the line (default
273+
" behavior when using ==)
274+
call setpos(".", [0, a:lnum, 1, 0])
275+
276+
" Search for the opening tag
277+
let mnum = searchpair('{', '', '}', 'bW',
278+
\ 'synIDattr(synID(line("."), col("."), 0), "name") =~? s:syn_comment' )
279+
280+
"Restore the cursor position
281+
call setpos(".", curpos)
282+
283+
let mind = indent(mnum)
284+
let mline = getline(mnum)
285+
286+
call s:Log("Matched found at: " . mnum)
287+
288+
if mline =~ s:js_m_multi_end " Fixes multi line invocation
289+
call s:Log("MLine matched multi line invocation")
290+
return mind - &sw
291+
else
292+
return mind
351293
endif
352294
endif
353295

354-
if line =~ s:js_m_cntrl_end . s:js_line_comment . '$'
355-
call s:Log("Line matched multi line control end")
356-
if pline =~ s:js_object_beg . s:js_line_comment . '$'
357-
call s:Log("Pline matched object beginning")
358-
return ind
359-
else
360-
call s:Log("Pline didn't match object beginning")
361-
return ind - &sw
362-
endif
296+
if pline =~ '{[^}]*$' && pline !~ '"[^{]*{[^{]*"'
297+
call s:Log("Pline matched opening {")
298+
return ind + &sw
363299
endif
364300

365-
" Handle: Basic Objects
366-
" =====================
367-
if pline =~ s:js_object_beg . s:js_line_comment . '$'
368-
call s:Log("PLine matched object beginning")
369-
if line =~ s:js_object_end . s:js_line_comment . '$'
370-
call s:Log("Line matched object end")
371-
return ind
372-
else
373-
call s:Log("Line didn't match object end")
374-
return ind + &sw
375-
endif
301+
" Handle: []
302+
" ==========
303+
if line =~ '^[^\[]*\]' && !s:IsComment(a:lnum) && line !~ '"[^\]]*\][^\]]*"'
304+
call s:Log("Line matched closing ]")
305+
306+
" Save the cursor position.
307+
let curpos = getpos(".")
308+
309+
" Set the cursor position to the beginning of the line (default
310+
" behavior when using ==)
311+
call setpos(".", [0, a:lnum, 1, 0])
312+
313+
" Search for the opening tag
314+
let mnum = searchpair('\[', '', '\]', 'bW',
315+
\ 'synIDattr(synID(line("."), col("."), 0), "name") =~? s:syn_comment' )
316+
317+
"Restore the cursor position
318+
call setpos(".", curpos)
319+
320+
call s:Log("Matched found at: " . mnum)
321+
return indent(mnum)
376322
endif
377323

378-
if line =~ s:js_object_end . s:js_line_comment . '$'
379-
call s:Log("Line matched object end")
380-
return ind - &sw
324+
if pline =~ '\[[^\]]*$' && pline !~ '"[^\[]*\[[^\[]*"'
325+
call s:Log("Pline matched opening [")
326+
return ind + &sw
381327
endif
382328

383329
call s:Log("Line didn't match anything. Retaining indent")

test.js

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -59,16 +59,16 @@ var x = { // 0
5959

6060
// Case: Function(4)
6161
function $blah( // 0
62-
x, // 1
63-
y, // 1
64-
z ) { // 1
62+
x, // 1
63+
y, // 1
64+
z ) { // 1
6565
} // 0
6666

6767
// Case: Function (5)
6868
function $blah( // 0
69-
x, // 1
70-
y, // 1
71-
z ) { // 1
69+
x, // 1
70+
y, // 1
71+
z ) { // 1
7272
x; // 1
7373
} // 0
7474

@@ -92,15 +92,15 @@ if(x) // 0
9292

9393
// = Case: if (4)
9494
if( x == y && // 0
95-
y == z || // 1
96-
z == w) { // 1
95+
y == z || // 1
96+
z == w) { // 1
9797
x; // 1
9898
} // 0
9999

100100
// = Case: if (4a)
101101
if( x == y && // 0
102-
y == z || // 1
103-
z == w) // 1
102+
y == z || // 1
103+
z == w) // 1
104104
{ // 0
105105
x; // 1
106106
} // 0
@@ -398,9 +398,9 @@ $(document).bind('click', function() { // 0
398398
} // 3
399399

400400
switch(true) { // 3
401-
case 'case1': break; // 3
402-
case 'case2': break; // 3
403-
default: // 3
401+
case 'case1': break; // 3
402+
case 'case2': break; // 3
403+
default: // 3
404404
} // 3
405405

406406
try { // 3

0 commit comments

Comments
 (0)