Skip to content

wrapping comments improved #440

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
May 24, 2016
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 19 additions & 17 deletions indent/javascript.vim
Original file line number Diff line number Diff line change
Expand Up @@ -41,16 +41,17 @@ endif
" 1. Variables {{{1
" ============

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

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

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

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

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

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

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

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

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

let s:comma_first = '^\s*,'
let s:comma_first = s:line_pre . ','
let s:comma_last = ',' . s:line_term

" 2. Auxiliary Functions {{{1
Expand Down Expand Up @@ -121,15 +122,15 @@ function s:PrevNonBlankNonString(lnum)
" Go in and out of blocks comments as necessary.
" If the line isn't empty (with opt. comment) or in a string, end search.
let line = getline(lnum)
if s:IsInMultilineComment(lnum, matchend(line, '/\*') - 1)
if s:IsInMultilineComment(lnum, matchend(line, '^\s*/\*') - 1) && line !~ s:line_pre . '$'
if in_block
let in_block = 0
else
break
endif
elseif !in_block && s:IsInMultilineComment(lnum, matchend(line, '\*/') - 1)
elseif !in_block && s:IsInMultilineComment(lnum, match(line, '\*/\s*$') + 1) && line !~ s:line_pre . '$'
let in_block = 1
elseif !in_block && line !~ '^\s*\%(//\).*$' && !(s:IsInStringOrComment(lnum, 1) && s:IsInStringOrComment(lnum, strlen(line)))
elseif !in_block && line !~ s:line_pre . '\%(//\).*$' && !(s:IsInStringOrComment(lnum, 1) && s:IsInStringOrComment(lnum, strlen(line)))
break
endif
let lnum = prevnonblank(lnum - 1)
Expand Down Expand Up @@ -191,7 +192,7 @@ function s:InMultiVarStatement(lnum, cont, prev)
" let type = synIDattr(synID(lnum, indent(lnum) + 1, 0), 'name')

" loop through previous expressions to find a var statement
while lnum > 0 && (s:Match(lnum, s:comma_last) || (a:cont && getline(lnum) =~ '^\s*}') || (a:prev && s:Match(a:prev, s:comma_last)))
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)))
" if the line is a js keyword
if a:cont
call cursor(lnum,1)
Expand Down Expand Up @@ -341,17 +342,18 @@ function GetJavascriptIndent()
let prevline = prevnonblank(v:lnum - 1)

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

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

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

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

Expand Down Expand Up @@ -489,13 +491,13 @@ function GetJavascriptIndent()
if searchpair('(', '', ')', 'bW', s:skip_expr) > 0
return indent(s:GetMSL(line('.'), 0))
end
elseif counts[1] == '2' && !s:Match(lnum, '^\s*}')
elseif counts[1] == '2' && !s:Match(lnum, s:line_pre . '}')
call cursor(lnum, 1)
" Search for the opening tag
if searchpair('{', '', '}', 'bW', s:skip_expr) > 0
return indent(s:GetMSL(line('.'), 0))
end
elseif counts[2] == '2' && !s:Match(lnum, '^\s*]')
elseif counts[2] == '2' && !s:Match(lnum, s:line_pre . ']')
call cursor(lnum, 1)
" Search for the opening tag
if searchpair('\[', '', '\]', 'bW', s:skip_expr) > 0
Expand Down