@@ -16,7 +16,7 @@ setlocal nosmartindent
16
16
" Now, set up our indentation expression and keys that trigger it.
17
17
setlocal indentexpr = GetJavascriptIndent ()
18
18
setlocal formatexpr = Fixedgq (v: lnum ,v: count )
19
- setlocal indentkeys = 0 {,0 },0 ),0 ],0 \, :,! ^F,o ,O,e
19
+ setlocal indentkeys = 0 {,0 },0 ),0 ],0 \, * <Return> , :,! ^F,o ,O,e
20
20
setlocal cinoptions += j1,J1
21
21
22
22
" Only define the function once.
@@ -45,16 +45,13 @@ let s:line_pre = '^\s*\%(\/\*.*\*\/\s*\)*'
45
45
let s: js_keywords = s: line_pre . ' \%(break\|import\|export\|catch\|const\|continue\|debugger\|delete\|do\|else\|finally\|for\|function\|if\|in\|instanceof\|let\|new\|return\|switch\|this\|throw\|try\|typeof\|var\|void\|while\|with\)\>\C'
46
46
let s: expr_case = s: line_pre . ' \%(case\s\+[^\:]*\|default\)\s*:\s*\C'
47
47
" Regex of syntax group names that are or delimit string or are comments.
48
- let s: syng_strcom = ' \%(string\|regex\|comment\|template\)\c'
48
+ let s: syng_strcom = ' \%(string\|regex\|special\| comment\|template\)\c'
49
49
50
50
" Regex of syntax group names that are strings.
51
51
let s: syng_string = ' regex\c'
52
52
53
53
" Regex of syntax group names that are strings or documentation.
54
- let s: syng_multiline = ' \%(comment\|doc\)\c'
55
-
56
- " Regex of syntax group names that are line comment.
57
- let s: syng_linecom = ' linecomment\c'
54
+ let s: syng_comment = ' \%(comment\|doc\)\c'
58
55
59
56
" Expression used to check whether we should skip a match with searchpair().
60
57
let s: skip_expr = " synIDattr(synID(line('.'),col('.'),1),'name') =~ '" .s: syng_strcom ." '"
@@ -114,28 +111,23 @@ function s:IsInString(lnum, col)
114
111
endfunction
115
112
116
113
" Check if the character at lnum:col is inside a multi-line comment.
117
- function s: IsInMultilineComment (lnum, col )
118
- return ! s: IsLineComment (a: lnum , a: col ) && synIDattr (synID (a: lnum , a: col , 1 ), ' name' ) = ~ s: syng_multiline
119
- endfunction
120
-
121
- " Check if the character at lnum:col is a line comment.
122
- function s: IsLineComment (lnum, col )
123
- return synIDattr (synID (a: lnum , a: col , 1 ), ' name' ) = ~ s: syng_linecom
114
+ function s: IsInComment (lnum, col )
115
+ return synIDattr (synID (a: lnum , a: col , 1 ), ' name' ) = ~ s: syng_comment
124
116
endfunction
125
117
126
118
" Find line above 'lnum' that isn't empty, in a comment, or in a string.
127
119
function s: PrevNonBlankNonString (lnum)
128
120
let lnum = prevnonblank (a: lnum )
129
121
while lnum > 0
130
122
let line = getline (lnum)
131
- let com = match (line , ' \*\/' ) + 1
132
- if s: IsInMultilineComment (lnum, com )
123
+ let com = match (line , ' \%(\/\*.*\)\@<!\ *\/' ) + 1
124
+ if s: IsInComment (lnum, com )
133
125
call cursor (lnum, com )
134
- let parlnum = search (' \/\*' , ' nbW' )
126
+ let parlnum = search (' \%(\/\/.*\)\@<!\ /\*' , ' nbW' )
135
127
if parlnum > 0
136
128
let lnum = parlnum
137
129
end
138
- elseif line !~ ' ^' . s: line_term
130
+ elseif line !~ ' ^' . s: line_term && ! s: IsInStringOrComment (lnum, 1 )
139
131
break
140
132
endif
141
133
let lnum = prevnonblank (lnum - 1 )
@@ -153,18 +145,17 @@ function s:GetMSL(lnum, in_one_line_scope)
153
145
" Otherwise, terminate search as we have found our MSL already.
154
146
let line = getline (lnum)
155
147
let line2 = getline (msl)
156
- let col2 = matchend (line2, ' )' )
157
148
if ((s: Match (lnum,s: continuation_regex ) || s: Match (lnum, s: comma_last )) &&
158
149
\ ! s: Match (lnum, s: expr_case )) || s: IsInString (lnum, strlen (line ))
159
150
let msl = lnum
160
-
161
- " if there are more closing brackets, continue from the line which has the matching opening bracket
162
- elseif col2 > 0 && ! s: IsInStringOrComment (msl, col2) && s: LineHasOpeningBrackets (msl)[ 0 ] == ' 2 ' && ! a: in_one_line_scope
163
- call cursor (msl, 1 )
164
- if s: lookForParens ( ' ( ' , ' ) ' , ' bW ' , 0 ) > 0
165
- let lnum = line ( ' . ' )
166
- let msl = lnum
167
- endif
151
+ if s: Match (lnum, s: line_pre . ' []})] ' ) && ! a: in_one_line_scope
152
+ call cursor (lnum, 1 )
153
+ let parlnum = s: lookForParens ( ' (\|{\|\[ ' , ' )\|}\|\] ' , ' nbW ' , 0 )
154
+ if parlnum > 0
155
+ let lnum = parlnum
156
+ continue
157
+ end
158
+ end
168
159
169
160
else
170
161
@@ -331,19 +322,22 @@ function GetJavascriptIndent()
331
322
let line = getline (v: lnum )
332
323
" previous nonblank line number
333
324
let prevline = prevnonblank (v: lnum - 1 )
325
+ " previous line of code
326
+ let lnum = s: PrevNonBlankNonString (v: lnum - 1 )
334
327
335
328
" to not change multiline string values
336
329
if line !~ ' ^['' "`]' && synIDattr (synID (v: lnum , 1 , 1 ), ' name' ) = ~? ' string\|template'
337
330
return -1
338
331
endif
339
332
340
333
" If we are in a multi-line comment, cindent does the right thing.
341
- if s: IsInMultilineComment ( v: lnum , 1 ) && line !~ ' ^\/\*'
334
+ if line !~ ' ^\%(\ /\*\|\s*\/\/\) ' && s: IsInComment ( v: lnum , 1 )
342
335
return cindent (v: lnum )
343
336
endif
344
337
345
338
" single opening bracket will assume you want a c style of indenting
346
- if s: Match (v: lnum , s: line_pre . ' {' . s: line_term )
339
+ if s: Match (v: lnum , s: line_pre . ' {' . s: line_term ) && ! s: Match (lnum,s: block_regex ) &&
340
+ \ ! s: Match (lnum,s: comma_last )
347
341
return cindent (v: lnum )
348
342
endif
349
343
@@ -358,16 +352,13 @@ function GetJavascriptIndent()
358
352
let col = matchend (line , s: line_pre . ' []})]' )
359
353
if col > 0 && ! s: IsInStringOrComment (v: lnum , col )
360
354
call cursor (v: lnum , col )
361
-
362
-
363
355
let parlnum = s: lookForParens (' (\|{\|\[' , ' )\|}\|\]' , ' nbW' , 0 )
364
356
if parlnum > 0
365
357
let ind = s: InMultiVarStatement (parlnum, 0 , 0 ) ? indent (parlnum) : indent (s: GetMSL (parlnum, 0 ))
366
358
endif
367
359
return ind
368
360
endif
369
361
370
- let lnum = s: PrevNonBlankNonString (v: lnum - 1 )
371
362
372
363
" If line starts with an operator...
373
364
if (line = ~ s: operator_first )
@@ -376,16 +367,17 @@ function GetJavascriptIndent()
376
367
return indent (lnum)
377
368
end
378
369
let counts = s: LineHasOpeningBrackets (lnum)
379
- if counts[ 0 ] == ' 2 ' || counts[ 1 ] == ' 2 ' || counts[ 2 ] == ' 2'
370
+ if counts = ~ ' 2'
380
371
call cursor (lnum, 1 )
381
372
" Search for the opening tag
382
373
let parlnum = s: lookForParens (' (\|{\|\[' , ' )\|}\|\]' , ' nbW' , 0 )
383
374
if parlnum > 0
384
- return ! s: Match (parlnum, s: operator_first ) ? indent (lnum) + s: sw () : indent (parlnum)
375
+ return ! s: Match (parlnum, s: operator_first ) &&
376
+ \ synIDattr (synID (v: lnum , 1 , 1 ), ' name' ) !~? ' jsbracket\|jsparen\|jsobject' ?
377
+ \ indent (lnum) + s: sw () : indent (parlnum)
385
378
end
386
- elseif line !~ s: line_pre . ' ,\s*\%(\%(\(['' "]\).*\1\)\|\%(\h\w*\)\)\s*:.*' . s: line_term &&
387
- \ synIDattr (synID (v: lnum , 1 , 1 ), ' name' ) !~? ' jsbracket\|jsparen'
388
- " otherwise, indent 1 level
379
+ elseif synIDattr (synID (v: lnum , 1 , 1 ), ' name' ) !~? ' jsbracket\|jsparen\|jsobject'
380
+ " otherwise, if not in an key/val;array item;param, indent 1 level
389
381
return indent (lnum) + s: sw ()
390
382
end
391
383
@@ -400,7 +392,7 @@ function GetJavascriptIndent()
400
392
return indent (mnum) - s: sw ()
401
393
end
402
394
elseif s: Match (lnum, s: operator_first )
403
- if counts[ 0 ] != ' 1 ' && counts[ 1 ] != ' 1 ' && counts[ 2 ] != ' 1'
395
+ if counts !~ ' 1'
404
396
return indent (lnum) - s: sw ()
405
397
end
406
398
end
@@ -412,7 +404,8 @@ function GetJavascriptIndent()
412
404
" If the line is empty and the previous nonblank line was a multi-line
413
405
" comment, use that comment's indent. Deduct one char to account for the
414
406
" space in ' */'.
415
- if line = ~ ' ^\s*$' && s: IsInMultilineComment (prevline, 1 )
407
+ if line = ~ ' ^\s*$' && getline (prevline) !~ ' \%(\%(^\s*\/\/\|\/\*\).*\)\@<!\*\/' &&
408
+ \ s: IsInComment (prevline, 1 )
416
409
return indent (prevline) - 1
417
410
endif
418
411
@@ -441,15 +434,14 @@ function GetJavascriptIndent()
441
434
" add indent depending on the bracket type.
442
435
if s: Match (lnum, ' [[({})\]]' )
443
436
let counts = s: LineHasOpeningBrackets (lnum)
444
- if counts[0 ] == ' 2' || (counts[1 ] == ' 2' && ! s: Match (lnum, s: line_pre . ' }' )) ||
445
- \ (counts[2 ] == ' 2' && ! s: Match (lnum, s: line_pre . ' ]' ))
437
+ if counts = ~ ' 2'
446
438
call cursor (lnum, 1 )
447
439
" Search for the opening tag
448
440
let parlnum = s: lookForParens (' (\|{\|\[' , ' )\|}\|\]' , ' nbW' , 0 )
449
- if parlnum > 0
441
+ if parlnum > 0 && ! s: InMultiVarStatement (parlnum, 0 , 0 )
450
442
return indent (s: GetMSL (parlnum, 0 ))
451
443
end
452
- elseif counts[ 1 ] == ' 1 ' || counts[ 2 ] == ' 1 ' || counts[ 0 ] == ' 1' || s: Onescope (lnum)
444
+ elseif counts = ~ ' 1' || s: Onescope (lnum)
453
445
return ind + s: sw ()
454
446
else
455
447
call cursor (v: lnum , vcol)
@@ -502,7 +494,7 @@ function! Fixedgq(lnum, count)
502
494
endif
503
495
504
496
" This gq is only meant to do code with strings, not comments
505
- if s: IsLineComment ( a: lnum , l: first_char ) || s: IsInMultilineComment (a: lnum , l: first_char )
497
+ if s: IsInComment (a: lnum , l: first_char )
506
498
return 1
507
499
endif
508
500
0 commit comments