Skip to content

Commit c5fba22

Browse files
committed
wrapping comments improved (#440)
* wrapping comments improved this improves most regexes and the prevnonblanknonstring function
1 parent c180b0c commit c5fba22

File tree

1 file changed

+19
-17
lines changed

1 file changed

+19
-17
lines changed

indent/javascript.vim

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -41,16 +41,17 @@ endif
4141
" 1. Variables {{{1
4242
" ============
4343

44-
let s:js_keywords = '^\s*\%(break\|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'
45-
let s:expr_case = '^\s*\%(case\s\+[^\:]*\|default\)\s*:\s*\C'
44+
let s:line_pre = '^\s*\%(\/\*.*\*\/\s*\)*'
45+
let s:js_keywords = s:line_pre . '\%(break\|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+
let s:expr_case = s:line_pre . '\%(case\s\+[^\:]*\|default\)\s*:\s*\C'
4647
" Regex of syntax group names that are or delimit string or are comments.
4748
let s:syng_strcom = '\%(string\|regex\|comment\|template\)\c'
4849

4950
" Regex of syntax group names that are strings.
5051
let s:syng_string = 'regex\c'
5152

5253
" Regex of syntax group names that are strings or documentation.
53-
let s:syng_multiline = 'comment\c'
54+
let s:syng_multiline = '\%(comment\|doc\)\c'
5455

5556
" Regex of syntax group names that are line comment.
5657
let s:syng_linecom = 'linecomment\c'
@@ -61,7 +62,7 @@ let s:skip_expr = "synIDattr(synID(line('.'),col('.'),1),'name') =~ '".s:syng_st
6162
let s:line_term = '\s*\%(\%(\/\/.*\)\=\|\%(\/\*.*\*\/\s*\)*\)$'
6263

6364
" Regex that defines continuation lines, not including (, {, or [.
64-
let s:continuation_regex = '\%([\\*/.?:]\|+\@<!+\|-\@<!-\|=\|||\|&&\)' . s:line_term
65+
let s:continuation_regex = '\%([*.?:]\|+\@<!+\|-\@<!-\|\*\@<!\/\|=\|||\|&&\)' . s:line_term
6566

6667
let s:one_line_scope_regex = '\%(\<else\>\|=>\)\C' . s:line_term
6768

@@ -83,11 +84,11 @@ endfunction
8384
" Regex that defines blocks.
8485
let s:block_regex = '[{([]' . s:line_term
8586

86-
let s:operator_first = '^\s*\%([*.:?]\|\([-/+]\)\1\@!\|||\|&&\)'
87+
let s:operator_first = s:line_pre . '\%([.:?]\|\([-/+*]\)\%(\1\|\*\|\/\)\@!\|||\|&&\)'
8788

88-
let s:var_stmt = '^\s*\%(const\|let\|var\)\s\+\C'
89+
let s:var_stmt = s:line_pre . '\%(const\|let\|var\)\s\+\C'
8990

90-
let s:comma_first = '^\s*,'
91+
let s:comma_first = s:line_pre . ','
9192
let s:comma_last = ',' . s:line_term
9293

9394
" 2. Auxiliary Functions {{{1
@@ -121,15 +122,15 @@ function s:PrevNonBlankNonString(lnum)
121122
" Go in and out of blocks comments as necessary.
122123
" If the line isn't empty (with opt. comment) or in a string, end search.
123124
let line = getline(lnum)
124-
if s:IsInMultilineComment(lnum, matchend(line, '/\*') - 1)
125+
if s:IsInMultilineComment(lnum, matchend(line, '^\s*/\*') - 1) && line !~ s:line_pre . '$'
125126
if in_block
126127
let in_block = 0
127128
else
128129
break
129130
endif
130-
elseif !in_block && s:IsInMultilineComment(lnum, matchend(line, '\*/') - 1)
131+
elseif !in_block && s:IsInMultilineComment(lnum, match(line, '\*/\s*$') + 1) && line !~ s:line_pre . '$'
131132
let in_block = 1
132-
elseif !in_block && line !~ '^\s*\%(//\).*$' && !(s:IsInStringOrComment(lnum, 1) && s:IsInStringOrComment(lnum, strlen(line)))
133+
elseif !in_block && line !~ s:line_pre . '\%(//\).*$' && !(s:IsInStringOrComment(lnum, 1) && s:IsInStringOrComment(lnum, strlen(line)))
133134
break
134135
endif
135136
let lnum = prevnonblank(lnum - 1)
@@ -191,7 +192,7 @@ function s:InMultiVarStatement(lnum, cont, prev)
191192
" let type = synIDattr(synID(lnum, indent(lnum) + 1, 0), 'name')
192193

193194
" loop through previous expressions to find a var statement
194-
while lnum > 0 && (s:Match(lnum, s:comma_last) || (a:cont && getline(lnum) =~ '^\s*}') || (a:prev && s:Match(a:prev, s:comma_last)))
195+
while lnum > 0 && (s:Match(lnum, s:comma_last) || (a:cont && getline(lnum) =~ s:line_pre . '}') || (a:prev && s:Match(a:prev, s:comma_last)))
195196
" if the line is a js keyword
196197
if a:cont
197198
call cursor(lnum,1)
@@ -341,17 +342,18 @@ function GetJavascriptIndent()
341342
let prevline = prevnonblank(v:lnum - 1)
342343

343344
" to not change multiline string values
344-
if synIDattr(synID(v:lnum, 1, 1), 'name') =~? 'string\|template' && line !~ '^\s*[''"`]'
345+
if synIDattr(synID(v:lnum, 1, 1), 'name') =~? 'string\|template' && line !~ s:line_pre . '[''"`]'
345346
return indent(v:lnum)
346347
endif
347348

348349
" If we are in a multi-line comment, cindent does the right thing.
349-
if s:IsInMultilineComment(v:lnum, 1) && !s:IsLineComment(v:lnum, 1)
350+
if s:IsInMultilineComment(v:lnum, 1) && !s:IsLineComment(v:lnum, 1) &&
351+
\ s:IsInMultilineComment(v:lnum, match(line, '\s*$'))
350352
return cindent(v:lnum)
351353
endif
352354

353355
" single opening bracket will assume you want a c style of indenting
354-
if s:Match(v:lnum, '^\s*{' . s:line_term)
356+
if s:Match(v:lnum, s:line_pre . '{' . s:line_term)
355357
return cindent(v:lnum)
356358
endif
357359

@@ -370,7 +372,7 @@ function GetJavascriptIndent()
370372
" If we got a closing bracket on an empty line, find its match and indent
371373
" according to it. For parentheses we indent to its column - 1, for the
372374
" others we indent to the containing line's MSL's level. Return -1 if fail.
373-
let col = matchend(line, '^\s*[],})]')
375+
let col = matchend(line, s:line_pre . '[],})]')
374376
if col > 0 && !s:IsInStringOrComment(v:lnum, col)
375377
call cursor(v:lnum, col)
376378

@@ -489,13 +491,13 @@ function GetJavascriptIndent()
489491
if searchpair('(', '', ')', 'bW', s:skip_expr) > 0
490492
return indent(s:GetMSL(line('.'), 0))
491493
end
492-
elseif counts[1] == '2' && !s:Match(lnum, '^\s*}')
494+
elseif counts[1] == '2' && !s:Match(lnum, s:line_pre . '}')
493495
call cursor(lnum, 1)
494496
" Search for the opening tag
495497
if searchpair('{', '', '}', 'bW', s:skip_expr) > 0
496498
return indent(s:GetMSL(line('.'), 0))
497499
end
498-
elseif counts[2] == '2' && !s:Match(lnum, '^\s*]')
500+
elseif counts[2] == '2' && !s:Match(lnum, s:line_pre . ']')
499501
call cursor(lnum, 1)
500502
" Search for the opening tag
501503
if searchpair('\[', '', '\]', 'bW', s:skip_expr) > 0

0 commit comments

Comments
 (0)