2015-07-18 17:05:45 -04:00
|
|
|
if !exists('g:polyglot_disabled') || index(g:polyglot_disabled, 'javascript') == -1
|
|
|
|
|
Add support for basic languages
coffee, cucumbeer, eruby, haml, haskell, javascript,
json, less, nginx, ocaml, ruby, sass, scss, slim,
stylus, textile, tmux
2013-09-12 10:17:03 -04:00
|
|
|
" Vim indent file
|
|
|
|
" Language: Javascript
|
2016-12-20 14:57:20 -05:00
|
|
|
" Maintainer: Chris Paul ( https://github.com/bounceme )
|
2016-09-11 07:24:17 -04:00
|
|
|
" URL: https://github.com/pangloss/vim-javascript
|
2017-05-17 05:07:28 -04:00
|
|
|
" Last Change: May 16, 2017
|
Add support for basic languages
coffee, cucumbeer, eruby, haml, haskell, javascript,
json, less, nginx, ocaml, ruby, sass, scss, slim,
stylus, textile, tmux
2013-09-12 10:17:03 -04:00
|
|
|
|
|
|
|
" Only load this indent file when no other was loaded.
|
2016-09-11 07:24:17 -04:00
|
|
|
if exists('b:did_indent')
|
Add support for basic languages
coffee, cucumbeer, eruby, haml, haskell, javascript,
json, less, nginx, ocaml, ruby, sass, scss, slim,
stylus, textile, tmux
2013-09-12 10:17:03 -04:00
|
|
|
finish
|
|
|
|
endif
|
|
|
|
let b:did_indent = 1
|
|
|
|
|
2017-05-17 05:07:28 -04:00
|
|
|
" indent correctly if inside <script>
|
|
|
|
" vim/vim@690afe1 for the switch from cindent
|
|
|
|
let b:html_indent_script1 = 'inc'
|
|
|
|
|
Add support for basic languages
coffee, cucumbeer, eruby, haml, haskell, javascript,
json, less, nginx, ocaml, ruby, sass, scss, slim,
stylus, textile, tmux
2013-09-12 10:17:03 -04:00
|
|
|
" Now, set up our indentation expression and keys that trigger it.
|
|
|
|
setlocal indentexpr=GetJavascriptIndent()
|
2016-12-20 14:57:20 -05:00
|
|
|
setlocal autoindent nolisp nosmartindent
|
|
|
|
setlocal indentkeys+=0],0)
|
2017-03-23 06:28:19 -04:00
|
|
|
" Testable with something like:
|
|
|
|
" vim -eNs "+filetype plugin indent on" "+syntax on" "+set ft=javascript" \
|
|
|
|
" "+norm! gg=G" '+%print' '+:q!' testfile.js \
|
|
|
|
" | diff -uBZ testfile.js -
|
2016-07-30 07:18:40 -04:00
|
|
|
|
2016-12-20 14:57:20 -05:00
|
|
|
let b:undo_indent = 'setlocal indentexpr< smartindent< autoindent< indentkeys<'
|
Add support for basic languages
coffee, cucumbeer, eruby, haml, haskell, javascript,
json, less, nginx, ocaml, ruby, sass, scss, slim,
stylus, textile, tmux
2013-09-12 10:17:03 -04:00
|
|
|
|
2017-05-17 05:07:28 -04:00
|
|
|
" Regex of syntax group names that are or delimit string or are comments.
|
|
|
|
let b:syng_strcom = get(b:,'syng_strcom','string\|comment\|regex\|special\|doc\|template\%(braces\)\@!')
|
|
|
|
let b:syng_str = get(b:,'syng_str','string\|template\|special')
|
|
|
|
" template strings may want to be excluded when editing graphql:
|
|
|
|
" au! Filetype javascript let b:syng_str = '^\%(.*template\)\@!.*string\|special'
|
|
|
|
" au! Filetype javascript let b:syng_strcom = '^\%(.*template\)\@!.*string\|comment\|regex\|special\|doc'
|
|
|
|
|
Add support for basic languages
coffee, cucumbeer, eruby, haml, haskell, javascript,
json, less, nginx, ocaml, ruby, sass, scss, slim,
stylus, textile, tmux
2013-09-12 10:17:03 -04:00
|
|
|
" Only define the function once.
|
2016-09-11 07:24:17 -04:00
|
|
|
if exists('*GetJavascriptIndent')
|
Add support for basic languages
coffee, cucumbeer, eruby, haml, haskell, javascript,
json, less, nginx, ocaml, ruby, sass, scss, slim,
stylus, textile, tmux
2013-09-12 10:17:03 -04:00
|
|
|
finish
|
|
|
|
endif
|
|
|
|
|
|
|
|
let s:cpo_save = &cpo
|
|
|
|
set cpo&vim
|
|
|
|
|
2016-05-03 06:56:11 -04:00
|
|
|
" Get shiftwidth value
|
|
|
|
if exists('*shiftwidth')
|
2016-09-11 07:24:17 -04:00
|
|
|
function s:sw()
|
2016-05-03 06:56:11 -04:00
|
|
|
return shiftwidth()
|
2016-09-11 07:24:17 -04:00
|
|
|
endfunction
|
2016-05-03 06:56:11 -04:00
|
|
|
else
|
2016-09-11 07:24:17 -04:00
|
|
|
function s:sw()
|
2017-05-17 05:07:28 -04:00
|
|
|
return &l:shiftwidth ? &l:shiftwidth : &l:tabstop
|
2016-09-11 07:24:17 -04:00
|
|
|
endfunction
|
2016-05-03 06:56:11 -04:00
|
|
|
endif
|
|
|
|
|
2017-03-23 06:28:19 -04:00
|
|
|
" Performance for forwards search(): start search at pos rather than masking
|
|
|
|
" matches before pos.
|
|
|
|
let s:z = has('patch-7.4.984') ? 'z' : ''
|
|
|
|
|
2017-05-17 05:07:28 -04:00
|
|
|
let s:syng_com = 'comment\|doc'
|
|
|
|
" Expression used to check whether we should skip a match with searchpair().
|
|
|
|
let s:skip_expr = "s:syn_at(line('.'),col('.')) =~? b:syng_strcom"
|
|
|
|
|
2016-12-20 14:57:20 -05:00
|
|
|
" searchpair() wrapper
|
2016-09-11 07:24:17 -04:00
|
|
|
if has('reltime')
|
2016-12-20 14:57:20 -05:00
|
|
|
function s:GetPair(start,end,flags,skip,time,...)
|
2017-02-02 15:16:29 -05:00
|
|
|
return searchpair('\m'.a:start,'','\m'.a:end,a:flags,a:skip,max([prevnonblank(v:lnum) - 2000,0] + a:000),a:time)
|
2016-09-11 07:24:17 -04:00
|
|
|
endfunction
|
|
|
|
else
|
2016-12-20 14:57:20 -05:00
|
|
|
function s:GetPair(start,end,flags,skip,...)
|
2017-02-02 15:16:29 -05:00
|
|
|
return searchpair('\m'.a:start,'','\m'.a:end,a:flags,a:skip,max([prevnonblank(v:lnum) - 1000,get(a:000,1)]))
|
2016-09-11 07:24:17 -04:00
|
|
|
endfunction
|
|
|
|
endif
|
2016-06-17 14:47:16 -04:00
|
|
|
|
2017-05-17 05:07:28 -04:00
|
|
|
function s:syn_at(l,c)
|
|
|
|
let pos = join([a:l,a:c],',')
|
|
|
|
if has_key(s:synId_cache,pos)
|
|
|
|
return s:synId_cache[pos]
|
|
|
|
endif
|
|
|
|
let s:synId_cache[pos] = synIDattr(synID(a:l,a:c,0),'name')
|
|
|
|
return s:synId_cache[pos]
|
|
|
|
endfunction
|
2016-12-20 14:57:20 -05:00
|
|
|
|
2017-05-17 05:07:28 -04:00
|
|
|
function s:parse_cino(f)
|
|
|
|
let [cin, divider, n] = [strridx(&cino,a:f), 0, '']
|
|
|
|
if cin == -1
|
|
|
|
return
|
|
|
|
endif
|
|
|
|
let [sign, cstr] = &cino[cin+1] ==# '-' ? [-1, &cino[cin+2:]] : [1, &cino[cin+1:]]
|
|
|
|
for c in split(cstr,'\zs')
|
|
|
|
if c ==# '.' && !divider
|
|
|
|
let divider = 1
|
|
|
|
elseif c ==# 's'
|
|
|
|
if n is ''
|
|
|
|
let n = s:W
|
|
|
|
else
|
|
|
|
let n = str2nr(n) * s:W
|
|
|
|
endif
|
|
|
|
break
|
|
|
|
elseif c =~ '\d'
|
|
|
|
let [n, divider] .= [c, 0]
|
|
|
|
else
|
|
|
|
break
|
|
|
|
endif
|
|
|
|
endfor
|
|
|
|
return sign * str2nr(n) / max([str2nr(divider),1])
|
2017-03-23 06:28:19 -04:00
|
|
|
endfunction
|
|
|
|
|
2017-05-17 05:07:28 -04:00
|
|
|
" Optimized {skip} expr, used only once per GetJavascriptIndent() call
|
2016-12-20 14:57:20 -05:00
|
|
|
function s:skip_func()
|
2017-05-17 05:07:28 -04:00
|
|
|
if s:topCol == 1 || line('.') < s:scriptTag
|
|
|
|
return {} " E728, used as limit condition for loops and searchpair()
|
|
|
|
endif
|
|
|
|
let s:topCol = col('.')
|
|
|
|
if getline('.') =~ '\%<'.s:topCol.'c\/.\{-}\/\|\%>'.s:topCol.'c[''"]\|\\$'
|
|
|
|
if eval(s:skip_expr)
|
|
|
|
let s:topCol = 0
|
|
|
|
endif
|
|
|
|
return !s:topCol
|
2017-03-23 06:28:19 -04:00
|
|
|
elseif s:checkIn || search('\m`\|\${\|\*\/','nW'.s:z,s:looksyn)
|
|
|
|
let s:checkIn = eval(s:skip_expr)
|
2017-05-17 05:07:28 -04:00
|
|
|
if s:checkIn
|
|
|
|
let s:topCol = 0
|
|
|
|
endif
|
2016-12-20 14:57:20 -05:00
|
|
|
endif
|
|
|
|
let s:looksyn = line('.')
|
2017-03-23 06:28:19 -04:00
|
|
|
return s:checkIn
|
2016-12-20 14:57:20 -05:00
|
|
|
endfunction
|
Add support for basic languages
coffee, cucumbeer, eruby, haml, haskell, javascript,
json, less, nginx, ocaml, ruby, sass, scss, slim,
stylus, textile, tmux
2013-09-12 10:17:03 -04:00
|
|
|
|
2017-05-17 05:07:28 -04:00
|
|
|
function s:alternatePair()
|
|
|
|
let [l:pos, pat, l:for] = [getpos('.'), '[][(){};]', 3]
|
|
|
|
while search('\m'.pat,'bW')
|
2017-03-23 06:28:19 -04:00
|
|
|
if s:skip_func() | continue | endif
|
|
|
|
let idx = stridx('])};',s:looking_at())
|
2017-05-17 05:07:28 -04:00
|
|
|
if idx is 3
|
|
|
|
if l:for is 1
|
|
|
|
return s:GetPair('{','}','bW','s:skip_func()',2000) > 0 || setpos('.',l:pos)
|
|
|
|
endif
|
|
|
|
let [pat, l:for] = ['[{}();]', l:for - 1]
|
|
|
|
elseif idx + 1
|
|
|
|
if s:GetPair(['\[','(','{'][idx], '])}'[idx],'bW','s:skip_func()',2000) < 1
|
2017-03-23 06:28:19 -04:00
|
|
|
break
|
2016-12-20 14:57:20 -05:00
|
|
|
endif
|
2017-03-23 06:28:19 -04:00
|
|
|
else
|
|
|
|
return
|
2016-12-20 14:57:20 -05:00
|
|
|
endif
|
|
|
|
endwhile
|
2017-05-17 05:07:28 -04:00
|
|
|
call setpos('.',l:pos)
|
2016-12-20 14:57:20 -05:00
|
|
|
endfunction
|
2016-07-30 07:18:40 -04:00
|
|
|
|
2016-12-20 14:57:20 -05:00
|
|
|
function s:looking_at()
|
|
|
|
return getline('.')[col('.')-1]
|
2016-05-30 19:53:12 -04:00
|
|
|
endfunction
|
Add support for basic languages
coffee, cucumbeer, eruby, haml, haskell, javascript,
json, less, nginx, ocaml, ruby, sass, scss, slim,
stylus, textile, tmux
2013-09-12 10:17:03 -04:00
|
|
|
|
2016-12-20 14:57:20 -05:00
|
|
|
function s:token()
|
|
|
|
return s:looking_at() =~ '\k' ? expand('<cword>') : s:looking_at()
|
Add support for basic languages
coffee, cucumbeer, eruby, haml, haskell, javascript,
json, less, nginx, ocaml, ruby, sass, scss, slim,
stylus, textile, tmux
2013-09-12 10:17:03 -04:00
|
|
|
endfunction
|
|
|
|
|
2017-02-02 15:16:29 -05:00
|
|
|
function s:previous_token()
|
2017-05-17 05:07:28 -04:00
|
|
|
let l:pos = getpos('.')
|
2017-03-23 06:28:19 -04:00
|
|
|
if search('\m\k\{1,}\|\S','ebW')
|
2017-05-17 05:07:28 -04:00
|
|
|
if (strpart(getline('.'),col('.')-2,2) == '*/' || line('.') != l:pos[1] &&
|
|
|
|
\ getline('.')[:col('.')-1] =~ '\/\/') && s:syn_at(line('.'),col('.')) =~? s:syng_com
|
2017-03-23 06:28:19 -04:00
|
|
|
while search('\m\S\ze\_s*\/[/*]','bW')
|
|
|
|
if s:syn_at(line('.'),col('.')) !~? s:syng_com
|
2017-02-02 15:16:29 -05:00
|
|
|
return s:token()
|
|
|
|
endif
|
|
|
|
endwhile
|
|
|
|
else
|
|
|
|
return s:token()
|
|
|
|
endif
|
2017-05-17 05:07:28 -04:00
|
|
|
call setpos('.',l:pos)
|
2017-02-02 15:16:29 -05:00
|
|
|
endif
|
|
|
|
return ''
|
2016-12-20 14:57:20 -05:00
|
|
|
endfunction
|
|
|
|
|
2017-05-17 05:07:28 -04:00
|
|
|
for s:__ in ['__previous_token','__IsBlock']
|
|
|
|
function s:{s:__}(...)
|
|
|
|
let l:pos = getpos('.')
|
|
|
|
try
|
|
|
|
return call('s:'.matchstr(expand('<sfile>'),'.*__\zs\w\+'),a:000)
|
|
|
|
catch
|
|
|
|
finally
|
|
|
|
call setpos('.',l:pos)
|
|
|
|
endtry
|
|
|
|
endfunction
|
|
|
|
endfor
|
|
|
|
|
2017-03-23 06:28:19 -04:00
|
|
|
function s:expr_col()
|
|
|
|
if getline('.')[col('.')-2] == ':'
|
|
|
|
return 1
|
2017-02-02 15:16:29 -05:00
|
|
|
endif
|
2017-05-17 05:07:28 -04:00
|
|
|
let [bal, l:pos] = [0, getpos('.')]
|
|
|
|
while bal < 1 && search('\m[{}?:;]','bW',s:scriptTag)
|
|
|
|
if eval(s:skip_expr)
|
|
|
|
continue
|
|
|
|
elseif s:looking_at() == ':'
|
|
|
|
let bal -= strpart(getline('.'),col('.')-2,3) !~ '::'
|
|
|
|
elseif s:looking_at() == '?'
|
|
|
|
let bal += 1
|
|
|
|
elseif s:looking_at() == '{' && getpos('.')[1:2] != b:js_cache[1:] && !s:IsBlock()
|
|
|
|
let bal = 1
|
|
|
|
elseif s:looking_at() != '}' || s:GetPair('{','}','bW',s:skip_expr,200) < 1
|
|
|
|
break
|
|
|
|
endif
|
2017-03-23 06:28:19 -04:00
|
|
|
endwhile
|
2017-05-17 05:07:28 -04:00
|
|
|
call setpos('.',l:pos)
|
|
|
|
return max([bal,0])
|
2016-12-20 14:57:20 -05:00
|
|
|
endfunction
|
|
|
|
|
|
|
|
" configurable regexes that define continuation lines, not including (, {, or [.
|
|
|
|
let s:opfirst = '^' . get(g:,'javascript_opfirst',
|
2017-03-23 06:28:19 -04:00
|
|
|
\ '\C\%([<>=,?^%|*/&]\|\([-.:+]\)\1\@!\|!=\|in\%(stanceof\)\=\>\)')
|
2016-12-20 14:57:20 -05:00
|
|
|
let s:continuation = get(g:,'javascript_continuation',
|
2017-05-17 05:07:28 -04:00
|
|
|
\ '\C\%([<=,.~!?/*^%|&:]\|+\@<!+\|-\@<!-\|=\@<!>\|\<\%(typeof\|new\|delete\|void\|in\|instanceof\|await\)\)') . '$'
|
2016-12-20 14:57:20 -05:00
|
|
|
|
|
|
|
function s:continues(ln,con)
|
2017-05-17 05:07:28 -04:00
|
|
|
let token = matchstr(a:con[-15:],s:continuation)
|
|
|
|
if strlen(token)
|
|
|
|
call cursor(a:ln,strlen(a:con))
|
|
|
|
if token =~ '[/>]'
|
|
|
|
return s:syn_at(a:ln,col('.')) !~? (token == '>' ? 'jsflow\|^html' : 'regex')
|
|
|
|
elseif token =~ '\l'
|
2017-03-23 06:28:19 -04:00
|
|
|
return s:previous_token() != '.'
|
2017-05-17 05:07:28 -04:00
|
|
|
elseif token == ':'
|
2017-03-23 06:28:19 -04:00
|
|
|
return s:expr_col()
|
|
|
|
endif
|
|
|
|
return 1
|
|
|
|
endif
|
2016-12-20 14:57:20 -05:00
|
|
|
endfunction
|
2016-09-11 07:24:17 -04:00
|
|
|
|
2017-02-02 15:16:29 -05:00
|
|
|
function s:Trim(ln)
|
2017-03-23 06:28:19 -04:00
|
|
|
let pline = substitute(getline(a:ln),'\s*$','','')
|
|
|
|
let l:max = max([strridx(pline,'//'), strridx(pline,'/*')])
|
|
|
|
while l:max != -1 && s:syn_at(a:ln, strlen(pline)) =~? s:syng_com
|
|
|
|
let pline = pline[: l:max]
|
|
|
|
let l:max = max([strridx(pline,'//'), strridx(pline,'/*')])
|
|
|
|
let pline = substitute(pline[:-2],'\s*$','','')
|
|
|
|
endwhile
|
2017-05-17 05:07:28 -04:00
|
|
|
return pline
|
2016-12-20 14:57:20 -05:00
|
|
|
endfunction
|
|
|
|
|
|
|
|
" Find line above 'lnum' that isn't empty or in a comment
|
2016-07-30 07:18:40 -04:00
|
|
|
function s:PrevCodeLine(lnum)
|
2017-05-17 05:07:28 -04:00
|
|
|
let l:n = prevnonblank(a:lnum)
|
2017-02-02 15:16:29 -05:00
|
|
|
while l:n
|
2017-03-23 06:28:19 -04:00
|
|
|
if getline(l:n) =~ '^\s*\/[/*]'
|
2017-05-17 05:07:28 -04:00
|
|
|
if (stridx(getline(l:n),'`') > 0 || getline(l:n-1)[-1:] == '\') &&
|
|
|
|
\ s:syn_at(l:n,1) =~? b:syng_str
|
|
|
|
break
|
|
|
|
endif
|
2017-02-02 15:16:29 -05:00
|
|
|
let l:n = prevnonblank(l:n-1)
|
2017-03-23 06:28:19 -04:00
|
|
|
elseif stridx(getline(l:n), '*/') + 1 && s:syn_at(l:n,1) =~? s:syng_com
|
2017-05-17 05:07:28 -04:00
|
|
|
let l:pos = getpos('.')
|
2017-03-23 06:28:19 -04:00
|
|
|
call cursor(l:n,1)
|
2017-05-17 05:07:28 -04:00
|
|
|
let l:n = search('\m\S\_s*\/\*','nbW')
|
|
|
|
call setpos('.',l:pos)
|
2017-02-02 15:16:29 -05:00
|
|
|
else
|
2017-03-23 06:28:19 -04:00
|
|
|
break
|
2017-02-02 15:16:29 -05:00
|
|
|
endif
|
Add support for basic languages
coffee, cucumbeer, eruby, haml, haskell, javascript,
json, less, nginx, ocaml, ruby, sass, scss, slim,
stylus, textile, tmux
2013-09-12 10:17:03 -04:00
|
|
|
endwhile
|
2017-03-23 06:28:19 -04:00
|
|
|
return l:n
|
Add support for basic languages
coffee, cucumbeer, eruby, haml, haskell, javascript,
json, less, nginx, ocaml, ruby, sass, scss, slim,
stylus, textile, tmux
2013-09-12 10:17:03 -04:00
|
|
|
endfunction
|
|
|
|
|
2016-09-11 07:24:17 -04:00
|
|
|
" Check if line 'lnum' has a balanced amount of parentheses.
|
|
|
|
function s:Balanced(lnum)
|
2016-12-20 14:57:20 -05:00
|
|
|
let l:open = 0
|
2016-09-11 07:24:17 -04:00
|
|
|
let l:line = getline(a:lnum)
|
|
|
|
let pos = match(l:line, '[][(){}]', 0)
|
Add support for basic languages
coffee, cucumbeer, eruby, haml, haskell, javascript,
json, less, nginx, ocaml, ruby, sass, scss, slim,
stylus, textile, tmux
2013-09-12 10:17:03 -04:00
|
|
|
while pos != -1
|
2017-05-17 05:07:28 -04:00
|
|
|
if s:syn_at(a:lnum,pos + 1) !~? b:syng_strcom
|
2016-12-20 14:57:20 -05:00
|
|
|
let l:open += match(' ' . l:line[pos],'[[({]')
|
|
|
|
if l:open < 0
|
|
|
|
return
|
Add support for basic languages
coffee, cucumbeer, eruby, haml, haskell, javascript,
json, less, nginx, ocaml, ruby, sass, scss, slim,
stylus, textile, tmux
2013-09-12 10:17:03 -04:00
|
|
|
endif
|
|
|
|
endif
|
2017-03-23 06:28:19 -04:00
|
|
|
let pos = match(l:line, (l:open ?
|
2017-03-24 11:08:42 -04:00
|
|
|
\ '['.matchstr(['][','()','{}'],l:line[pos]).']' :
|
2017-03-23 06:28:19 -04:00
|
|
|
\ '[][(){}]'), pos + 1)
|
Add support for basic languages
coffee, cucumbeer, eruby, haml, haskell, javascript,
json, less, nginx, ocaml, ruby, sass, scss, slim,
stylus, textile, tmux
2013-09-12 10:17:03 -04:00
|
|
|
endwhile
|
2016-12-20 14:57:20 -05:00
|
|
|
return !l:open
|
Add support for basic languages
coffee, cucumbeer, eruby, haml, haskell, javascript,
json, less, nginx, ocaml, ruby, sass, scss, slim,
stylus, textile, tmux
2013-09-12 10:17:03 -04:00
|
|
|
endfunction
|
|
|
|
|
2016-12-20 14:57:20 -05:00
|
|
|
function s:OneScope(lnum)
|
2017-02-02 15:16:29 -05:00
|
|
|
let pline = s:Trim(a:lnum)
|
2017-05-17 05:07:28 -04:00
|
|
|
call cursor(a:lnum,strlen(pline))
|
2017-02-02 15:16:29 -05:00
|
|
|
let kw = 'else do'
|
2016-12-20 14:57:20 -05:00
|
|
|
if pline[-1:] == ')' && s:GetPair('(', ')', 'bW', s:skip_expr, 100) > 0
|
2017-03-23 06:28:19 -04:00
|
|
|
if s:previous_token() =~# '^\%(await\|each\)$'
|
2017-02-02 15:16:29 -05:00
|
|
|
call s:previous_token()
|
|
|
|
let kw = 'for'
|
2017-03-23 06:28:19 -04:00
|
|
|
else
|
|
|
|
let kw = 'for if let while with'
|
2016-12-20 14:57:20 -05:00
|
|
|
endif
|
|
|
|
endif
|
2017-02-02 15:16:29 -05:00
|
|
|
return pline[-2:] == '=>' || index(split(kw),s:token()) + 1 &&
|
2017-05-17 05:07:28 -04:00
|
|
|
\ s:__previous_token() != '.' && !s:doWhile()
|
|
|
|
endfunction
|
|
|
|
|
|
|
|
function s:doWhile()
|
|
|
|
if expand('<cword>') ==# 'while'
|
|
|
|
let [bal, l:pos] = [0, getpos('.')]
|
|
|
|
call search('\m\<','cbW')
|
|
|
|
while bal < 1 && search('\m\C[{}]\|\<\%(do\|while\)\>','bW')
|
|
|
|
if eval(s:skip_expr)
|
|
|
|
continue
|
|
|
|
elseif s:looking_at() ==# 'd'
|
|
|
|
let bal += s:__IsBlock(1)
|
|
|
|
elseif s:looking_at() ==# 'w'
|
|
|
|
let bal -= s:__previous_token() != '.'
|
|
|
|
elseif s:looking_at() != '}' || s:GetPair('{','}','bW',s:skip_expr,200) < 1
|
|
|
|
break
|
|
|
|
endif
|
|
|
|
endwhile
|
|
|
|
call setpos('.',l:pos)
|
|
|
|
return max([bal,0])
|
|
|
|
endif
|
2016-12-20 14:57:20 -05:00
|
|
|
endfunction
|
|
|
|
|
|
|
|
" returns braceless levels started by 'i' and above lines * &sw. 'num' is the
|
|
|
|
" lineNr which encloses the entire context, 'cont' if whether line 'i' + 1 is
|
|
|
|
" a continued expression, which could have started in a braceless context
|
|
|
|
function s:iscontOne(i,num,cont)
|
|
|
|
let [l:i, l:num, bL] = [a:i, a:num + !a:num, 0]
|
|
|
|
let pind = a:num ? indent(l:num) + s:W : 0
|
|
|
|
let ind = indent(l:i) + (a:cont ? 0 : s:W)
|
|
|
|
while l:i >= l:num && (ind > pind || l:i == l:num)
|
|
|
|
if indent(l:i) < ind && s:OneScope(l:i)
|
|
|
|
let bL += s:W
|
|
|
|
let l:i = line('.')
|
|
|
|
elseif !a:cont || bL || ind < indent(a:i)
|
|
|
|
break
|
|
|
|
endif
|
|
|
|
let ind = min([ind, indent(l:i)])
|
|
|
|
let l:i = s:PrevCodeLine(l:i - 1)
|
|
|
|
endwhile
|
|
|
|
return bL
|
|
|
|
endfunction
|
|
|
|
|
|
|
|
" https://github.com/sweet-js/sweet.js/wiki/design#give-lookbehind-to-the-reader
|
2017-05-17 05:07:28 -04:00
|
|
|
function s:IsBlock(...)
|
|
|
|
if a:0 || s:looking_at() == '{'
|
2017-02-02 15:16:29 -05:00
|
|
|
let l:n = line('.')
|
|
|
|
let char = s:previous_token()
|
2017-03-23 06:28:19 -04:00
|
|
|
if match(s:stack,'\cxml\|jsx') + 1 && s:syn_at(line('.'),col('.')-1) =~? 'xml\|jsx'
|
2017-02-02 15:16:29 -05:00
|
|
|
return char != '{'
|
|
|
|
elseif char =~ '\k'
|
2017-03-23 06:28:19 -04:00
|
|
|
if char ==# 'type'
|
2017-05-17 05:07:28 -04:00
|
|
|
return s:__previous_token() !~# '^\%(im\|ex\)port$'
|
2017-03-23 06:28:19 -04:00
|
|
|
endif
|
|
|
|
return index(split('return const let import export extends yield default delete var await void typeof throw case new of in instanceof')
|
2017-05-17 05:07:28 -04:00
|
|
|
\ ,char) < (line('.') != l:n) || s:__previous_token() == '.'
|
2017-02-02 15:16:29 -05:00
|
|
|
elseif char == '>'
|
2017-05-17 05:07:28 -04:00
|
|
|
return getline('.')[col('.')-2] == '=' || s:syn_at(line('.'),col('.')) =~? 'jsflow\|^html'
|
|
|
|
elseif char == '*'
|
|
|
|
return s:__previous_token() == ':'
|
2017-02-02 15:16:29 -05:00
|
|
|
elseif char == ':'
|
2017-05-17 05:07:28 -04:00
|
|
|
return !s:expr_col()
|
2017-03-23 06:28:19 -04:00
|
|
|
elseif char == '/'
|
|
|
|
return s:syn_at(line('.'),col('.')) =~? 'regex'
|
2017-02-02 15:16:29 -05:00
|
|
|
endif
|
2017-05-17 05:07:28 -04:00
|
|
|
return char !~ '[=~!<,.?^%|&([]' &&
|
2017-02-02 15:16:29 -05:00
|
|
|
\ (char !~ '[-+]' || l:n != line('.') && getline('.')[col('.')-2] == char)
|
2016-05-30 19:53:12 -04:00
|
|
|
endif
|
2016-12-20 14:57:20 -05:00
|
|
|
endfunction
|
|
|
|
|
2017-05-17 05:07:28 -04:00
|
|
|
|
2016-12-20 14:57:20 -05:00
|
|
|
function GetJavascriptIndent()
|
|
|
|
let b:js_cache = get(b:,'js_cache',[0,0,0])
|
2017-05-17 05:07:28 -04:00
|
|
|
let s:synId_cache = {}
|
2016-09-11 07:24:17 -04:00
|
|
|
" Get the current line.
|
2017-02-02 15:16:29 -05:00
|
|
|
call cursor(v:lnum,1)
|
|
|
|
let l:line = getline('.')
|
2017-03-23 06:28:19 -04:00
|
|
|
" use synstack as it validates syn state and works in an empty line
|
|
|
|
let s:stack = map(synstack(v:lnum,1),"synIDattr(v:val,'name')")
|
|
|
|
let syns = get(s:stack,-1,'')
|
2016-05-30 19:53:12 -04:00
|
|
|
|
2016-12-20 14:57:20 -05:00
|
|
|
" start with strings,comments,etc.
|
|
|
|
if syns =~? s:syng_com
|
|
|
|
if l:line =~ '^\s*\*'
|
|
|
|
return cindent(v:lnum)
|
|
|
|
elseif l:line !~ '^\s*\/[/*]'
|
|
|
|
return -1
|
|
|
|
endif
|
2017-05-17 05:07:28 -04:00
|
|
|
elseif syns =~? b:syng_str
|
2016-12-20 14:57:20 -05:00
|
|
|
if b:js_cache[0] == v:lnum - 1 && s:Balanced(v:lnum-1)
|
|
|
|
let b:js_cache[0] = v:lnum
|
|
|
|
endif
|
2016-07-30 07:18:40 -04:00
|
|
|
return -1
|
2016-05-30 19:53:12 -04:00
|
|
|
endif
|
2016-09-11 07:24:17 -04:00
|
|
|
let l:lnum = s:PrevCodeLine(v:lnum - 1)
|
2016-12-20 14:57:20 -05:00
|
|
|
if !l:lnum
|
|
|
|
return
|
2016-09-11 07:24:17 -04:00
|
|
|
endif
|
Add support for basic languages
coffee, cucumbeer, eruby, haml, haskell, javascript,
json, less, nginx, ocaml, ruby, sass, scss, slim,
stylus, textile, tmux
2013-09-12 10:17:03 -04:00
|
|
|
|
2016-12-20 14:57:20 -05:00
|
|
|
let l:line = substitute(l:line,'^\s*','','')
|
|
|
|
if l:line[:1] == '/*'
|
|
|
|
let l:line = substitute(l:line,'^\%(\/\*.\{-}\*\/\s*\)*','','')
|
|
|
|
endif
|
|
|
|
if l:line =~ '^\/[/*]'
|
|
|
|
let l:line = ''
|
Add support for basic languages
coffee, cucumbeer, eruby, haml, haskell, javascript,
json, less, nginx, ocaml, ruby, sass, scss, slim,
stylus, textile, tmux
2013-09-12 10:17:03 -04:00
|
|
|
endif
|
|
|
|
|
2016-12-20 14:57:20 -05:00
|
|
|
" the containing paren, bracket, or curly. Many hacks for performance
|
2017-05-17 05:07:28 -04:00
|
|
|
let [ s:scriptTag, idx ] = [ get(get(b:,'hi_indent',{}),'blocklnr'),
|
|
|
|
\ index([']',')','}'],l:line[0]) ]
|
2016-12-20 14:57:20 -05:00
|
|
|
if b:js_cache[0] >= l:lnum && b:js_cache[0] < v:lnum &&
|
|
|
|
\ (b:js_cache[0] > l:lnum || s:Balanced(l:lnum))
|
2017-05-17 05:07:28 -04:00
|
|
|
call call('cursor',b:js_cache[2] ? b:js_cache[1:] : [0,0])
|
2016-07-30 07:18:40 -04:00
|
|
|
else
|
2017-05-17 05:07:28 -04:00
|
|
|
let [s:looksyn, s:checkIn, s:topCol] = [v:lnum - 1, 0, 0]
|
2016-12-20 14:57:20 -05:00
|
|
|
if idx + 1
|
2017-05-17 05:07:28 -04:00
|
|
|
call s:GetPair(['\[','(','{'][idx],'])}'[idx],'bW','s:skip_func()',2000)
|
2017-03-23 06:28:19 -04:00
|
|
|
elseif getline(v:lnum) !~ '^\S' && syns =~? 'block'
|
2017-05-17 05:07:28 -04:00
|
|
|
call s:GetPair('{','}','bW','s:skip_func()',2000)
|
2016-12-20 14:57:20 -05:00
|
|
|
else
|
2017-05-17 05:07:28 -04:00
|
|
|
call s:alternatePair()
|
2016-12-20 14:57:20 -05:00
|
|
|
endif
|
2016-09-11 07:24:17 -04:00
|
|
|
endif
|
2016-05-03 06:56:11 -04:00
|
|
|
|
2017-05-17 05:07:28 -04:00
|
|
|
let b:js_cache = [v:lnum] + (line('.') == v:lnum ? [s:scriptTag,0] : getpos('.')[1:2])
|
2016-12-20 14:57:20 -05:00
|
|
|
let num = b:js_cache[1]
|
|
|
|
|
|
|
|
let [s:W, isOp, bL, switch_offset] = [s:sw(),0,0,0]
|
2017-05-17 05:07:28 -04:00
|
|
|
if !b:js_cache[2] || s:IsBlock()
|
2017-02-02 15:16:29 -05:00
|
|
|
let ilnum = line('.')
|
2017-05-17 05:07:28 -04:00
|
|
|
let pline = s:Trim(l:lnum)
|
|
|
|
if b:js_cache[2] && s:looking_at() == ')' && s:GetPair('(',')','bW',s:skip_expr,100) > 0
|
2017-02-02 15:16:29 -05:00
|
|
|
let num = ilnum == num ? line('.') : num
|
|
|
|
if idx < 0 && s:previous_token() ==# 'switch' && s:previous_token() != '.'
|
2017-05-17 05:07:28 -04:00
|
|
|
let switch_offset = &cino !~ ':' ? s:W : max([-indent(num),s:parse_cino(':')])
|
2017-02-02 15:16:29 -05:00
|
|
|
if pline[-1:] != '.' && l:line =~# '^\%(default\|case\)\>'
|
2016-12-20 14:57:20 -05:00
|
|
|
return indent(num) + switch_offset
|
|
|
|
endif
|
|
|
|
endif
|
|
|
|
endif
|
2017-03-23 06:28:19 -04:00
|
|
|
if idx < 0 && pline[-1:] !~ '[{;]'
|
|
|
|
let isOp = (l:line =~# s:opfirst || s:continues(l:lnum,pline)) * s:W
|
2017-02-02 15:16:29 -05:00
|
|
|
let bL = s:iscontOne(l:lnum,b:js_cache[1],isOp)
|
2016-12-20 14:57:20 -05:00
|
|
|
let bL -= (bL && l:line[0] == '{') * s:W
|
|
|
|
endif
|
2017-03-23 06:28:19 -04:00
|
|
|
elseif idx < 0 && getline(b:js_cache[1])[b:js_cache[2]-1] == '(' && &cino =~ '('
|
|
|
|
let pval = s:parse_cino('(')
|
2017-05-17 05:07:28 -04:00
|
|
|
return !pval || !search('\m\S','nbW',num) && !s:parse_cino('U') ?
|
|
|
|
\ (s:parse_cino('w') ? 0 : -!!search('\m\S','W'.s:z,num)) + virtcol('.') :
|
|
|
|
\ max([indent('.') + pval + s:GetPair('(',')','nbrmW',s:skip_expr,100,num) * s:W,0])
|
2016-09-11 07:24:17 -04:00
|
|
|
endif
|
Add support for basic languages
coffee, cucumbeer, eruby, haml, haskell, javascript,
json, less, nginx, ocaml, ruby, sass, scss, slim,
stylus, textile, tmux
2013-09-12 10:17:03 -04:00
|
|
|
|
2016-12-20 14:57:20 -05:00
|
|
|
" main return
|
2017-05-17 05:07:28 -04:00
|
|
|
if l:line =~ '^[])}]\|^|}'
|
2017-03-23 06:28:19 -04:00
|
|
|
return max([indent(num),0])
|
2016-12-20 14:57:20 -05:00
|
|
|
elseif num
|
2017-02-02 15:16:29 -05:00
|
|
|
return indent(num) + s:W + switch_offset + bL + isOp
|
2016-12-20 14:57:20 -05:00
|
|
|
endif
|
2017-02-02 15:16:29 -05:00
|
|
|
return bL + isOp
|
Add support for basic languages
coffee, cucumbeer, eruby, haml, haskell, javascript,
json, less, nginx, ocaml, ruby, sass, scss, slim,
stylus, textile, tmux
2013-09-12 10:17:03 -04:00
|
|
|
endfunction
|
|
|
|
|
|
|
|
let &cpo = s:cpo_save
|
|
|
|
unlet s:cpo_save
|
2015-07-18 17:05:45 -04:00
|
|
|
|
|
|
|
endif
|