Skip to content

Commit 8e710ba

Browse files
bouncemeamadeus
authored andcommitted
changes according to #466 (#463)
support less common object defs . comment detection compatibility. only use a c style when not in a block
1 parent ceb3341 commit 8e710ba

File tree

1 file changed

+35
-43
lines changed

1 file changed

+35
-43
lines changed

indent/javascript.vim

Lines changed: 35 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ setlocal nosmartindent
1616
" Now, set up our indentation expression and keys that trigger it.
1717
setlocal indentexpr=GetJavascriptIndent()
1818
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
2020
setlocal cinoptions+=j1,J1
2121

2222
" Only define the function once.
@@ -45,16 +45,13 @@ let s:line_pre = '^\s*\%(\/\*.*\*\/\s*\)*'
4545
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'
4646
let s:expr_case = s:line_pre . '\%(case\s\+[^\:]*\|default\)\s*:\s*\C'
4747
" 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'
4949

5050
" Regex of syntax group names that are strings.
5151
let s:syng_string = 'regex\c'
5252

5353
" 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'
5855

5956
" Expression used to check whether we should skip a match with searchpair().
6057
let s:skip_expr = "synIDattr(synID(line('.'),col('.'),1),'name') =~ '".s:syng_strcom."'"
@@ -114,28 +111,23 @@ function s:IsInString(lnum, col)
114111
endfunction
115112

116113
" 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
124116
endfunction
125117

126118
" Find line above 'lnum' that isn't empty, in a comment, or in a string.
127119
function s:PrevNonBlankNonString(lnum)
128120
let lnum = prevnonblank(a:lnum)
129121
while lnum > 0
130122
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)
133125
call cursor(lnum, com)
134-
let parlnum = search('\/\*', 'nbW')
126+
let parlnum = search('\%(\/\/.*\)\@<!\/\*', 'nbW')
135127
if parlnum > 0
136128
let lnum = parlnum
137129
end
138-
elseif line !~ '^' . s:line_term
130+
elseif line !~ '^' . s:line_term && !s:IsInStringOrComment(lnum,1)
139131
break
140132
endif
141133
let lnum = prevnonblank(lnum - 1)
@@ -153,18 +145,17 @@ function s:GetMSL(lnum, in_one_line_scope)
153145
" Otherwise, terminate search as we have found our MSL already.
154146
let line = getline(lnum)
155147
let line2 = getline(msl)
156-
let col2 = matchend(line2, ')')
157148
if ((s:Match(lnum,s:continuation_regex) || s:Match(lnum, s:comma_last)) &&
158149
\ !s:Match(lnum, s:expr_case)) || s:IsInString(lnum, strlen(line))
159150
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
168159

169160
else
170161

@@ -331,19 +322,22 @@ function GetJavascriptIndent()
331322
let line = getline(v:lnum)
332323
" previous nonblank line number
333324
let prevline = prevnonblank(v:lnum - 1)
325+
" previous line of code
326+
let lnum = s:PrevNonBlankNonString(v:lnum - 1)
334327

335328
" to not change multiline string values
336329
if line !~ '^[''"`]' && synIDattr(synID(v:lnum, 1, 1), 'name') =~? 'string\|template'
337330
return -1
338331
endif
339332

340333
" 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)
342335
return cindent(v:lnum)
343336
endif
344337

345338
" 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)
347341
return cindent(v:lnum)
348342
endif
349343

@@ -358,16 +352,13 @@ function GetJavascriptIndent()
358352
let col = matchend(line, s:line_pre . '[]})]')
359353
if col > 0 && !s:IsInStringOrComment(v:lnum, col)
360354
call cursor(v:lnum, col)
361-
362-
363355
let parlnum = s:lookForParens('(\|{\|\[', ')\|}\|\]', 'nbW', 0)
364356
if parlnum > 0
365357
let ind = s:InMultiVarStatement(parlnum, 0, 0) ? indent(parlnum) : indent(s:GetMSL(parlnum, 0))
366358
endif
367359
return ind
368360
endif
369361

370-
let lnum = s:PrevNonBlankNonString(v:lnum - 1)
371362

372363
" If line starts with an operator...
373364
if (line =~ s:operator_first)
@@ -376,16 +367,17 @@ function GetJavascriptIndent()
376367
return indent(lnum)
377368
end
378369
let counts = s:LineHasOpeningBrackets(lnum)
379-
if counts[0] == '2' || counts[1] == '2' || counts[2] == '2'
370+
if counts =~ '2'
380371
call cursor(lnum, 1)
381372
" Search for the opening tag
382373
let parlnum = s:lookForParens('(\|{\|\[', ')\|}\|\]', 'nbW', 0)
383374
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)
385378
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
389381
return indent(lnum) + s:sw()
390382
end
391383

@@ -400,7 +392,7 @@ function GetJavascriptIndent()
400392
return indent(mnum) - s:sw()
401393
end
402394
elseif s:Match(lnum, s:operator_first)
403-
if counts[0] != '1' && counts[1] != '1' && counts[2] != '1'
395+
if counts !~ '1'
404396
return indent(lnum) - s:sw()
405397
end
406398
end
@@ -412,7 +404,8 @@ function GetJavascriptIndent()
412404
" If the line is empty and the previous nonblank line was a multi-line
413405
" comment, use that comment's indent. Deduct one char to account for the
414406
" space in ' */'.
415-
if line =~ '^\s*$' && s:IsInMultilineComment(prevline, 1)
407+
if line =~ '^\s*$' && getline(prevline) !~ '\%(\%(^\s*\/\/\|\/\*\).*\)\@<!\*\/' &&
408+
\ s:IsInComment(prevline, 1)
416409
return indent(prevline) - 1
417410
endif
418411

@@ -441,15 +434,14 @@ function GetJavascriptIndent()
441434
" add indent depending on the bracket type.
442435
if s:Match(lnum, '[[({})\]]')
443436
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'
446438
call cursor(lnum, 1)
447439
" Search for the opening tag
448440
let parlnum = s:lookForParens('(\|{\|\[', ')\|}\|\]', 'nbW', 0)
449-
if parlnum > 0
441+
if parlnum > 0 && !s:InMultiVarStatement(parlnum,0,0)
450442
return indent(s:GetMSL(parlnum, 0))
451443
end
452-
elseif counts[1] == '1' || counts[2] == '1' || counts[0] == '1' || s:Onescope(lnum)
444+
elseif counts =~ '1' || s:Onescope(lnum)
453445
return ind + s:sw()
454446
else
455447
call cursor(v:lnum, vcol)
@@ -502,7 +494,7 @@ function! Fixedgq(lnum, count)
502494
endif
503495

504496
" 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)
506498
return 1
507499
endif
508500

0 commit comments

Comments
 (0)