Skip to content

Commit

Permalink
Merge pull request #630 from pangloss/develop
Browse files Browse the repository at this point in the history
Develop -> Master
  • Loading branch information
amadeus committed Sep 14, 2016
2 parents 782cc10 + d5dbecb commit a50df5f
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 58 deletions.
2 changes: 0 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,6 @@ variables:
## Indentation Specific

* `:h cino-:`
* End-of-line continuations : `g:javascript_continuation` ( slightly complicated, look at the source code )
* Start-of-line continuations : `g:javascript_opfirst` ( same as above )

## Contributing

Expand Down
70 changes: 40 additions & 30 deletions indent/javascript.vim
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
" Language: Javascript
" Maintainer: vim-javascript community
" URL: https://github.com/pangloss/vim-javascript
" Last Change: August 20, 2016
" Last Change: September 4, 2016

" Only load this indent file when no other was loaded.
if exists('b:did_indent')
Expand Down Expand Up @@ -38,7 +38,7 @@ else
endif

let s:line_pre = '^\s*\%(\%(\%(\/\*.\{-}\)\=\*\+\/\s*\)\=\)\@>'
let s:expr_case = s:line_pre . '\%(\%(case\>.\+\)\|default\)\s*:'
let s:expr_case = s:line_pre . '\%(\%(case\>.\+\)\|default\)\s*:\C'
" Regex of syntax group names that are or delimit string or are comments.
let s:syng_strcom = '\%(s\%(tring\|pecial\)\|comment\|regex\|doc\|template\)'

Expand All @@ -47,13 +47,20 @@ let s:syng_comment = '\%(comment\|doc\)'

" Expression used to check whether we should skip a match with searchpair().
let s:skip_expr = "synIDattr(synID(line('.'),col('.'),0),'name') =~? '".s:syng_strcom."'"
function s:skip_func(lnum)
if !s:free || getline(line('.')) =~ '[''/"\\]' || search('`','nW',a:lnum) || search('\*\/','nW',a:lnum)
let s:free = !eval(s:skip_expr)
endif
let s:looksyn = s:free ? line('.') : s:looksyn
return !s:free
endfunction

if has('reltime')
function s:GetPair(start,end,flags,time)
return searchpair(a:start,'',a:end,a:flags,s:skip_expr,max([prevnonblank(v:lnum) - 2000,0]),a:time)
function s:GetPair(start,end,flags,skip,time)
return searchpair(a:start,'',a:end,a:flags,a:skip,max([prevnonblank(v:lnum) - 2000,0]),a:time)
endfunction
else
function s:GetPair(start,end,flags,n)
function s:GetPair(start,end,flags,...)
return searchpair(a:start,'',a:end,a:flags,0,max([prevnonblank(v:lnum) - 2000,0]))
endfunction
endif
Expand All @@ -62,29 +69,28 @@ let s:line_term = '\s*\%(\%(\/\%(\%(\*.\{-}\*\/\)\|\%(\*\+\)\)\)\s*\)\=$'

" configurable regexes that define continuation lines, not including (, {, or [.
if !exists('g:javascript_opfirst')
let g:javascript_opfirst = '\%([<>,:?^%|*&]\|\([-/.+]\)\1\@!\|=>\@!\|in\%(stanceof\)\=\>\)'
let g:javascript_opfirst = '\%([<>,?^%|*&]\|\/[^/*]\|\([-.:+]\)\1\@!\|=>\@!\|in\%(stanceof\)\=\>\)'
endif
if !exists('g:javascript_continuation')
let g:javascript_continuation = '\%([<=,.?/*:^%|&]\|+\@<!+\|-\@<!-\|=\@<!>\|\<in\%(stanceof\)\=\)'
let g:javascript_continuation = '\%([<=,.?/*^%|&:]\|+\@<!+\|-\@<!-\|=\@<!>\|\<in\%(stanceof\)\=\)'
endif

let g:javascript_opfirst = s:line_pre . g:javascript_opfirst
let g:javascript_continuation .= s:line_term

function s:OneScope(lnum,text,add)
function s:OneScope(lnum,text)
return a:text =~# '\%(\<else\|\<do\|=>\)' . s:line_term ? 'no b' :
\ ((a:add && a:text =~ s:line_pre . '$' && search('\%' . s:PrevCodeLine(a:lnum - 1) . 'l.)' . s:line_term)) ||
\ cursor(a:lnum, match(a:text, ')' . s:line_term)) > -1) &&
\ s:GetPair('(', ')', 'cbW', 100) > 0 && search('\C\l\+\_s*\%#','bW') &&
\ (a:add || ((expand('<cword>') !=# 'while' || !s:GetPair('\C\<do\>', '\C\<while\>','nbW',100)) &&
\ expand('cword') !=# 'each' || search('\C\<for\_s\+\%#','nbW'))) ? expand('<cword>') : ''
\ cursor(a:lnum, match(' ' . a:text, ')' . s:line_term)) > -1 &&
\ s:GetPair('(', ')', 'bW', s:skip_expr, 100) > 0 && search('\C\l\+\_s*\%#','bW') &&
\ (expand('<cword>') !=# 'while' || s:GetPair('\C\<do\>', '\C\<while\>','nbW',s:skip_expr,100) <= 0) &&
\ (expand('<cword>') !=# 'each' || search('\C\<for\_s\+\%#','nbW')) ? expand('<cword>') : ''
endfunction

" https://github.com/sweet-js/sweet.js/wiki/design#give-lookbehind-to-the-reader
function s:IsBlock()
return getline(line('.'))[col('.')-1] == '{' && !search(
\ '\C\%(\<return\s*\|\%([-=~!<*+,.?^%|&\[(]\|=\@<!>\|\*\@<!\/\|\<\%(var\|const\|let\|yield\|delete\|void\|t\%(ypeof\|hrow\)\|new\|\<in\%(stanceof\)\=\)\)\_s*\)\%#','bnW') &&
\ (!search(':\_s*\%#','bW') || (!s:GetPair('[({[]','[])}]','bW',200) || s:IsBlock()))
\ '\C\%(\<return\s*\|\%([-=~!<*+,.?^%|&\[(]\|=\@<!>\|\*\@<!\/\|\<\%(var\|const\|let\|import\|export\%(\_s\+default\)\=\|yield\|delete\|void\|t\%(ypeof\|hrow\)\|new\|in\%(stanceof\)\=\)\)\_s*\)\%#','bnW') &&
\ (search(s:expr_case . '\_s*\%#','nbW') || !search('[{:]\_s*\%#','bW') || s:IsBlock())
endfunction

" Auxiliary Functions {{{2
Expand All @@ -109,9 +115,12 @@ function s:Balanced(lnum)
if synIDattr(synID(a:lnum,pos + 1,0),'name') !~? s:syng_strcom
let idx = stridx('(){}[]', l:line[pos])
if idx % 2 == 0
let open_{idx} = open_{idx} + 1
let open_{idx} += 1
else
let open_{idx - 1} = open_{idx - 1} - 1
let open_{idx - 1} -= 1
if open_{idx - 1} < 0
return 0
endif
endif
endif
let pos = match(l:line, '[][(){}]', pos + 1)
Expand All @@ -129,7 +138,7 @@ function GetJavascriptIndent()
let syns = synIDattr(synID(v:lnum, 1, 0), 'name')

" start with strings,comments,etc.{{{2
if (l:line !~ '^[''"`]' && syns =~? '\%(string\|template\)') ||
if (l:line !~ '^[''"]' && syns =~? '\%(string\|template\)') ||
\ (l:line !~ '^\s*[/*]' && syns =~? s:syng_comment)
return -1
endif
Expand All @@ -152,36 +161,37 @@ function GetJavascriptIndent()

" the containing paren, bracket, curly. Memoize, last lineNr either has the
" same scope or starts a new one, unless if it closed a scope.
let [s:looksyn,s:free] = [v:lnum - 1,1]
call cursor(v:lnum,1)
if b:js_cache[0] >= l:lnum && b:js_cache[0] < v:lnum && b:js_cache[0] &&
if b:js_cache[0] < v:lnum && b:js_cache[0] >= l:lnum &&
\ (b:js_cache[0] > l:lnum || s:Balanced(l:lnum) > 0)
let num = b:js_cache[1]
elseif syns != '' && l:line[0] =~ '\s'
let pattern = syns =~? 'block' ? ['{','}'] : syns =~? 'jsparen' ? ['(',')'] :
\ syns =~? 'jsbracket'? ['\[','\]'] : ['[({[]','[])}]']
let num = s:GetPair(pattern[0],pattern[1],'bW',2000)
let num = s:GetPair(pattern[0],pattern[1],'bW','s:skip_func(s:looksyn)',2000)
else
let num = s:GetPair('[({[]','[])}]','bW',2000)
let num = s:GetPair('[({[]','[])}]','bW','s:skip_func(s:looksyn)',2000)
endif

let b:js_cache = [v:lnum,num,line('.') == v:lnum ? b:js_cache[2] : col('.')]

if l:line =~ s:line_pre . '[])}]'
return indent(num)
endif

let pline = substitute(substitute(getline(l:lnum),s:expr_case,'\=repeat(" ",strlen(submatch(0)))',''), '\%(:\@<!\/\/.*\)$', '','')
call cursor(b:js_cache[1],b:js_cache[2])

let swcase = getline(l:lnum) =~# s:expr_case
let pline = swcase ? getline(l:lnum) : substitute(getline(l:lnum), '\%(:\@<!\/\/.*\)$', '','')
let inb = num == 0 || num < l:lnum && ((l:line !~ s:line_pre . ',' && pline !~ ',' . s:line_term) || s:IsBlock())
let switch_offset = num == 0 || s:OneScope(num, strpart(getline(num),0,b:js_cache[2] - 1),1) !=# 'switch' ? 0 :
\ &cino !~ ':' || !has('float') ? s:sw() :
let switch_offset = num <= 0 || !(search(')\_s*\%#','bW') &&
\ s:GetPair('(', ')', 'bW', s:skip_expr, 100) > 0 && search('\C\<switch\_s*\%#','bW')) ? 0 :
\ &cino !~ ':' || !has('float') ? s:sw() :
\ float2nr(str2float(matchstr(&cino,'.*:\zs[-0-9.]*')) * (&cino =~# '.*:[^,]*s' ? s:sw() : 1))

" most significant, find the indent amount
if inb && !swcase && ((l:line =~# g:javascript_opfirst || pline =~# g:javascript_continuation) ||
\ num < l:lnum && s:OneScope(l:lnum,pline,0) =~# '\<\%(for\|each\|if\|let\|no\sb\|w\%(hile\|ith\)\)\>' &&
\ l:line !~ s:line_pre . '{')
let isOp = l:line =~# g:javascript_opfirst || pline =~# g:javascript_continuation
if isOp && (num <= 0 || cursor(b:js_cache[1],b:js_cache[2]) || s:IsBlock()) ||
\ s:OneScope(l:lnum,pline) =~# '\<\%(for\|each\|if\|let\|no\sb\|w\%(hile\|ith\)\)\>' &&
\ l:line !~ s:line_pre . '{'
return (num > 0 ? indent(num) : -s:sw()) + (s:sw() * 2) + switch_offset
elseif num > 0
return indent(num) + s:sw() + switch_offset
Expand Down
Loading

0 comments on commit a50df5f

Please sign in to comment.