| 
									
										
										
										
											2007-05-05 17:10:09 +00:00
										 |  |  | " Vim indent file | 
					
						
							| 
									
										
										
										
											2016-08-12 22:54:35 +02:00
										 |  |  | " Language: Javascript | 
					
						
							| 
									
										
										
										
											2016-08-26 19:52:37 +02:00
										 |  |  | " Maintainer: Chris Paul ( https://github.com/bounceme ) | 
					
						
							| 
									
										
										
										
											2016-08-12 22:54:35 +02:00
										 |  |  | " URL: https://github.com/pangloss/vim-javascript | 
					
						
							| 
									
										
										
										
											2017-03-21 19:18:29 +01:00
										 |  |  | " Last Change: March 21, 2017 | 
					
						
							| 
									
										
										
										
											2007-05-05 17:10:09 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | " Only load this indent file when no other was loaded. | 
					
						
							| 
									
										
										
										
											2016-08-12 22:54:35 +02:00
										 |  |  | if exists('b:did_indent') | 
					
						
							|  |  |  |   finish | 
					
						
							| 
									
										
										
										
											2007-05-05 17:10:09 +00:00
										 |  |  | endif | 
					
						
							|  |  |  | let b:did_indent = 1 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-08-12 22:54:35 +02:00
										 |  |  | " Now, set up our indentation expression and keys that trigger it. | 
					
						
							|  |  |  | setlocal indentexpr=GetJavascriptIndent() | 
					
						
							| 
									
										
										
										
											2017-01-10 13:31:15 +01:00
										 |  |  | setlocal autoindent nolisp nosmartindent | 
					
						
							|  |  |  | setlocal indentkeys+=0],0) | 
					
						
							| 
									
										
										
										
											2017-03-21 19:18:29 +01: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 - | 
					
						
							| 
									
										
										
										
											2007-05-05 17:10:09 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-10 13:31:15 +01:00
										 |  |  | let b:undo_indent = 'setlocal indentexpr< smartindent< autoindent< indentkeys<' | 
					
						
							| 
									
										
										
										
											2016-08-12 22:54:35 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | " Only define the function once. | 
					
						
							|  |  |  | if exists('*GetJavascriptIndent') | 
					
						
							|  |  |  |   finish | 
					
						
							|  |  |  | endif | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | let s:cpo_save = &cpo | 
					
						
							|  |  |  | set cpo&vim | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | " Get shiftwidth value | 
					
						
							|  |  |  | if exists('*shiftwidth') | 
					
						
							|  |  |  |   function s:sw() | 
					
						
							|  |  |  |     return shiftwidth() | 
					
						
							|  |  |  |   endfunction | 
					
						
							|  |  |  | else | 
					
						
							|  |  |  |   function s:sw() | 
					
						
							| 
									
										
										
										
											2017-03-21 19:18:29 +01:00
										 |  |  |     return &l:shiftwidth == 0 ? &l:tabstop : &l:shiftwidth | 
					
						
							| 
									
										
										
										
											2016-08-12 22:54:35 +02:00
										 |  |  |   endfunction | 
					
						
							|  |  |  | endif | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-21 19:18:29 +01: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-01-10 13:31:15 +01:00
										 |  |  | " searchpair() wrapper | 
					
						
							| 
									
										
										
										
											2016-08-26 19:52:37 +02:00
										 |  |  | if has('reltime') | 
					
						
							| 
									
										
										
										
											2017-01-10 13:31:15 +01:00
										 |  |  |   function s:GetPair(start,end,flags,skip,time,...) | 
					
						
							|  |  |  |     return searchpair('\m'.a:start,'','\m'.a:end,a:flags,a:skip,max([prevnonblank(v:lnum) - 2000,0] + a:000),a:time) | 
					
						
							| 
									
										
										
										
											2016-08-26 19:52:37 +02:00
										 |  |  |   endfunction | 
					
						
							|  |  |  | else | 
					
						
							| 
									
										
										
										
											2017-01-10 13:31:15 +01:00
										 |  |  |   function s:GetPair(start,end,flags,skip,...) | 
					
						
							|  |  |  |     return searchpair('\m'.a:start,'','\m'.a:end,a:flags,a:skip,max([prevnonblank(v:lnum) - 1000,get(a:000,1)])) | 
					
						
							| 
									
										
										
										
											2016-08-26 19:52:37 +02:00
										 |  |  |   endfunction | 
					
						
							|  |  |  | endif | 
					
						
							| 
									
										
										
										
											2016-08-12 22:54:35 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-10 13:31:15 +01:00
										 |  |  | " Regex of syntax group names that are or delimit string or are comments. | 
					
						
							| 
									
										
										
										
											2017-03-21 19:18:29 +01:00
										 |  |  | let s:syng_strcom = 'string\|comment\|regex\|special\|doc\|template\%(braces\)\@!' | 
					
						
							|  |  |  | let s:syng_str = 'string\|template\|special' | 
					
						
							| 
									
										
										
										
											2017-01-10 13:31:15 +01:00
										 |  |  | let s:syng_com = '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."'" | 
					
						
							| 
									
										
										
										
											2016-08-12 22:54:35 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-21 19:18:29 +01:00
										 |  |  | function s:parse_cino(f) abort | 
					
						
							|  |  |  |   return float2nr(eval(substitute(substitute(join(split( | 
					
						
							|  |  |  |         \ matchstr(&cino,'.*'.a:f.'\zs[^,]*'), 's',1), '*'.s:W) | 
					
						
							|  |  |  |         \ , '^-\=\zs\*','',''), '^-\=\zs\.','0.',''))) | 
					
						
							|  |  |  | endfunction | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-10 13:31:15 +01:00
										 |  |  | function s:skip_func() | 
					
						
							| 
									
										
										
										
											2017-03-21 19:18:29 +01:00
										 |  |  |   if getline('.') =~ '\%<'.col('.').'c\/.\{-}\/\|\%>'.col('.').'c[''"]\|\\$' | 
					
						
							|  |  |  |     return eval(s:skip_expr) | 
					
						
							|  |  |  |   elseif s:checkIn || search('\m`\|\${\|\*\/','nW'.s:z,s:looksyn) | 
					
						
							|  |  |  |     let s:checkIn = eval(s:skip_expr) | 
					
						
							| 
									
										
										
										
											2017-01-10 13:31:15 +01:00
										 |  |  |   endif | 
					
						
							|  |  |  |   let s:looksyn = line('.') | 
					
						
							| 
									
										
										
										
											2017-03-21 19:18:29 +01:00
										 |  |  |   return s:checkIn | 
					
						
							| 
									
										
										
										
											2017-01-10 13:31:15 +01:00
										 |  |  | endfunction | 
					
						
							| 
									
										
										
										
											2016-08-26 19:52:37 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-10 13:31:15 +01:00
										 |  |  | function s:alternatePair(stop) | 
					
						
							|  |  |  |   let pos = getpos('.')[1:2] | 
					
						
							| 
									
										
										
										
											2017-03-21 19:18:29 +01:00
										 |  |  |   let pat = '[][(){};]' | 
					
						
							|  |  |  |   while search('\m'.pat,'bW',a:stop) | 
					
						
							|  |  |  |     if s:skip_func() | continue | endif | 
					
						
							|  |  |  |     let idx = stridx('])};',s:looking_at()) | 
					
						
							|  |  |  |     if idx is 3 | let pat = '[{}()]' | continue | endif | 
					
						
							|  |  |  |     if idx + 1 | 
					
						
							|  |  |  |       if s:GetPair(['\[','(','{'][idx], '])}'[idx],'bW','s:skip_func()',2000,a:stop) <= 0 | 
					
						
							|  |  |  |         break | 
					
						
							| 
									
										
										
										
											2017-01-10 13:31:15 +01:00
										 |  |  |       endif | 
					
						
							| 
									
										
										
										
											2017-03-21 19:18:29 +01:00
										 |  |  |     else | 
					
						
							|  |  |  |       return | 
					
						
							| 
									
										
										
										
											2017-01-10 13:31:15 +01:00
										 |  |  |     endif | 
					
						
							|  |  |  |   endwhile | 
					
						
							|  |  |  |   call call('cursor',pos) | 
					
						
							|  |  |  | endfunction | 
					
						
							| 
									
										
										
										
											2016-08-12 22:54:35 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-10 13:31:15 +01:00
										 |  |  | function s:save_pos(f,...) | 
					
						
							|  |  |  |   let l:pos = getpos('.')[1:2] | 
					
						
							|  |  |  |   let ret = call(a:f,a:000) | 
					
						
							|  |  |  |   call call('cursor',l:pos) | 
					
						
							|  |  |  |   return ret | 
					
						
							| 
									
										
										
										
											2016-08-12 22:54:35 +02:00
										 |  |  | endfunction | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-10 13:31:15 +01:00
										 |  |  | function s:syn_at(l,c) | 
					
						
							|  |  |  |   return synIDattr(synID(a:l,a:c,0),'name') | 
					
						
							|  |  |  | endfunction | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | function s:looking_at() | 
					
						
							|  |  |  |   return getline('.')[col('.')-1] | 
					
						
							|  |  |  | endfunction | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | function s:token() | 
					
						
							|  |  |  |   return s:looking_at() =~ '\k' ? expand('<cword>') : s:looking_at() | 
					
						
							|  |  |  | endfunction | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | function s:previous_token() | 
					
						
							| 
									
										
										
										
											2017-03-21 19:18:29 +01:00
										 |  |  |   let l:pos = getpos('.')[1:2] | 
					
						
							|  |  |  |   if search('\m\k\{1,}\zs\k\|\S','bW') | 
					
						
							|  |  |  |     if (getline('.')[col('.')-2:col('.')-1] == '*/' || line('.') != l:pos[0] && | 
					
						
							|  |  |  |           \ getline('.') =~ '\%<'.col('.').'c\/\/') && s:syn_at(line('.'),col('.')) =~? s:syng_com | 
					
						
							|  |  |  |       while search('\m\S\ze\_s*\/[/*]','bW') | 
					
						
							|  |  |  |         if s:syn_at(line('.'),col('.')) !~? s:syng_com | 
					
						
							|  |  |  |           return s:token() | 
					
						
							|  |  |  |         endif | 
					
						
							|  |  |  |       endwhile | 
					
						
							| 
									
										
										
										
											2017-01-10 13:31:15 +01:00
										 |  |  |     else | 
					
						
							|  |  |  |       return s:token() | 
					
						
							|  |  |  |     endif | 
					
						
							| 
									
										
										
										
											2017-03-21 19:18:29 +01:00
										 |  |  |   endif | 
					
						
							|  |  |  |   call call('cursor',l:pos) | 
					
						
							| 
									
										
										
										
											2017-01-10 13:31:15 +01:00
										 |  |  |   return '' | 
					
						
							|  |  |  | endfunction | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-21 19:18:29 +01:00
										 |  |  | function s:expr_col() | 
					
						
							|  |  |  |   if getline('.')[col('.')-2] == ':' | 
					
						
							|  |  |  |     return 1 | 
					
						
							| 
									
										
										
										
											2017-01-10 13:31:15 +01:00
										 |  |  |   endif | 
					
						
							| 
									
										
										
										
											2017-03-21 19:18:29 +01:00
										 |  |  |   let bal = 0 | 
					
						
							|  |  |  |   while search('\m[{}?:;]','bW') | 
					
						
							|  |  |  |     if eval(s:skip_expr) | continue | endif | 
					
						
							|  |  |  |     " switch (looking_at()) | 
					
						
							|  |  |  |     exe {   '}': "if s:GetPair('{','}','bW',s:skip_expr,200) <= 0 | return | endif", | 
					
						
							|  |  |  |           \ ';': "return", | 
					
						
							|  |  |  |           \ '{': "return getpos('.')[1:2] != b:js_cache[1:] && !s:IsBlock()", | 
					
						
							|  |  |  |           \ ':': "let bal -= getline('.')[max([col('.')-2,0]):col('.')] !~ '::'", | 
					
						
							|  |  |  |           \ '?': "let bal += 1 | if bal > 0 | return 1 | endif" }[s:looking_at()] | 
					
						
							|  |  |  |   endwhile | 
					
						
							| 
									
										
										
										
											2017-01-10 13:31:15 +01:00
										 |  |  | endfunction | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | " configurable regexes that define continuation lines, not including (, {, or [. | 
					
						
							|  |  |  | let s:opfirst = '^' . get(g:,'javascript_opfirst', | 
					
						
							| 
									
										
										
										
											2017-03-21 19:18:29 +01:00
										 |  |  |       \ '\C\%([<>=,?^%|*/&]\|\([-.:+]\)\1\@!\|!=\|in\%(stanceof\)\=\>\)') | 
					
						
							| 
									
										
										
										
											2017-01-10 13:31:15 +01:00
										 |  |  | let s:continuation = get(g:,'javascript_continuation', | 
					
						
							| 
									
										
										
										
											2017-03-21 19:18:29 +01:00
										 |  |  |       \ '\C\%([-+<>=,.~!?/*^%|&:]\|\<\%(typeof\|new\|delete\|void\|in\|instanceof\|await\)\)') . '$' | 
					
						
							| 
									
										
										
										
											2017-01-10 13:31:15 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | function s:continues(ln,con) | 
					
						
							| 
									
										
										
										
											2017-03-21 19:18:29 +01:00
										 |  |  |   if !cursor(a:ln, match(' '.a:con,s:continuation)) | 
					
						
							|  |  |  |     let teol = s:looking_at() | 
					
						
							|  |  |  |     if teol == '/' | 
					
						
							|  |  |  |       return s:syn_at(line('.'),col('.')) !~? 'regex' | 
					
						
							|  |  |  |     elseif teol =~ '[-+>]' | 
					
						
							|  |  |  |       return getline('.')[col('.')-2] != tr(teol,'>','=') | 
					
						
							|  |  |  |     elseif teol =~ '\l' | 
					
						
							|  |  |  |       return s:previous_token() != '.' | 
					
						
							|  |  |  |     elseif teol == ':' | 
					
						
							|  |  |  |       return s:expr_col() | 
					
						
							|  |  |  |     endif | 
					
						
							|  |  |  |     return 1 | 
					
						
							|  |  |  |   endif | 
					
						
							| 
									
										
										
										
											2016-08-12 22:54:35 +02:00
										 |  |  | endfunction | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-10 13:31:15 +01:00
										 |  |  | " get the line of code stripped of comments and move cursor to the last | 
					
						
							|  |  |  | " non-comment char. | 
					
						
							|  |  |  | function s:Trim(ln) | 
					
						
							|  |  |  |   let pline = substitute(getline(a:ln),'\s*$','','') | 
					
						
							| 
									
										
										
										
											2017-03-21 19:18:29 +01:00
										 |  |  |   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*$','','') | 
					
						
							| 
									
										
										
										
											2017-01-10 13:31:15 +01:00
										 |  |  |   endwhile | 
					
						
							| 
									
										
										
										
											2017-03-21 19:18:29 +01:00
										 |  |  |   return pline is '' || cursor(a:ln,strlen(pline)) ? pline : pline | 
					
						
							| 
									
										
										
										
											2017-01-10 13:31:15 +01:00
										 |  |  | endfunction | 
					
						
							| 
									
										
										
										
											2016-08-26 19:52:37 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-10 13:31:15 +01:00
										 |  |  | " Find line above 'lnum' that isn't empty or in a comment | 
					
						
							| 
									
										
										
										
											2016-08-12 22:54:35 +02:00
										 |  |  | function s:PrevCodeLine(lnum) | 
					
						
							| 
									
										
										
										
											2017-03-21 19:18:29 +01:00
										 |  |  |   let [l:pos, l:n] = [getpos('.')[1:2], prevnonblank(a:lnum)] | 
					
						
							| 
									
										
										
										
											2017-01-10 13:31:15 +01:00
										 |  |  |   while l:n | 
					
						
							|  |  |  |     if getline(l:n) =~ '^\s*\/[/*]' | 
					
						
							|  |  |  |       let l:n = prevnonblank(l:n-1) | 
					
						
							| 
									
										
										
										
											2017-03-21 19:18:29 +01:00
										 |  |  |     elseif stridx(getline(l:n), '*/') + 1 && s:syn_at(l:n,1) =~? s:syng_com | 
					
						
							|  |  |  |       call cursor(l:n,1) | 
					
						
							|  |  |  |       keepjumps norm! [* | 
					
						
							|  |  |  |       let l:n = search('\m\S','nbW') | 
					
						
							| 
									
										
										
										
											2017-01-10 13:31:15 +01:00
										 |  |  |     else | 
					
						
							| 
									
										
										
										
											2017-03-21 19:18:29 +01:00
										 |  |  |       break | 
					
						
							| 
									
										
										
										
											2016-08-12 22:54:35 +02:00
										 |  |  |     endif | 
					
						
							|  |  |  |   endwhile | 
					
						
							| 
									
										
										
										
											2017-03-21 19:18:29 +01:00
										 |  |  |   call call('cursor',l:pos) | 
					
						
							|  |  |  |   return l:n | 
					
						
							| 
									
										
										
										
											2016-08-12 22:54:35 +02:00
										 |  |  | endfunction | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | " Check if line 'lnum' has a balanced amount of parentheses. | 
					
						
							|  |  |  | function s:Balanced(lnum) | 
					
						
							| 
									
										
										
										
											2017-01-10 13:31:15 +01:00
										 |  |  |   let l:open = 0 | 
					
						
							| 
									
										
										
										
											2016-08-12 22:54:35 +02:00
										 |  |  |   let l:line = getline(a:lnum) | 
					
						
							|  |  |  |   let pos = match(l:line, '[][(){}]', 0) | 
					
						
							|  |  |  |   while pos != -1 | 
					
						
							| 
									
										
										
										
											2017-01-10 13:31:15 +01:00
										 |  |  |     if s:syn_at(a:lnum,pos + 1) !~? s:syng_strcom | 
					
						
							|  |  |  |       let l:open += match(' ' . l:line[pos],'[[({]') | 
					
						
							|  |  |  |       if l:open < 0 | 
					
						
							|  |  |  |         return | 
					
						
							| 
									
										
										
										
											2016-08-12 22:54:35 +02:00
										 |  |  |       endif | 
					
						
							|  |  |  |     endif | 
					
						
							| 
									
										
										
										
											2017-03-21 19:18:29 +01:00
										 |  |  |     let pos = match(l:line, (l:open ? | 
					
						
							|  |  |  |           \ '['.escape(tr(l:line[pos],'({[]})',')}][{(').l:line[pos],']').']' : | 
					
						
							|  |  |  |           \ '[][(){}]'), pos + 1) | 
					
						
							| 
									
										
										
										
											2016-08-12 22:54:35 +02:00
										 |  |  |   endwhile | 
					
						
							| 
									
										
										
										
											2017-01-10 13:31:15 +01:00
										 |  |  |   return !l:open | 
					
						
							| 
									
										
										
										
											2016-08-12 22:54:35 +02:00
										 |  |  | endfunction | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-10 13:31:15 +01:00
										 |  |  | function s:OneScope(lnum) | 
					
						
							|  |  |  |   let pline = s:Trim(a:lnum) | 
					
						
							|  |  |  |   let kw = 'else do' | 
					
						
							|  |  |  |   if pline[-1:] == ')' && s:GetPair('(', ')', 'bW', s:skip_expr, 100) > 0 | 
					
						
							| 
									
										
										
										
											2017-03-21 19:18:29 +01:00
										 |  |  |     if s:previous_token() =~# '^\%(await\|each\)$' | 
					
						
							| 
									
										
										
										
											2017-01-10 13:31:15 +01:00
										 |  |  |       call s:previous_token() | 
					
						
							|  |  |  |       let kw = 'for' | 
					
						
							| 
									
										
										
										
											2017-03-21 19:18:29 +01:00
										 |  |  |     else | 
					
						
							|  |  |  |       let kw = 'for if let while with' | 
					
						
							| 
									
										
										
										
											2017-01-10 13:31:15 +01:00
										 |  |  |     endif | 
					
						
							|  |  |  |   endif | 
					
						
							|  |  |  |   return pline[-2:] == '=>' || index(split(kw),s:token()) + 1 && | 
					
						
							|  |  |  |         \ s:save_pos('s:previous_token') != '.' | 
					
						
							|  |  |  | 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 | 
					
						
							|  |  |  | function s:IsBlock() | 
					
						
							|  |  |  |   if s:looking_at() == '{' | 
					
						
							|  |  |  |     let l:n = line('.') | 
					
						
							|  |  |  |     let char = s:previous_token() | 
					
						
							| 
									
										
										
										
											2017-03-21 19:18:29 +01:00
										 |  |  |     if match(s:stack,'\cxml\|jsx') + 1 && s:syn_at(line('.'),col('.')-1) =~? 'xml\|jsx' | 
					
						
							| 
									
										
										
										
											2017-01-10 13:31:15 +01:00
										 |  |  |       return char != '{' | 
					
						
							|  |  |  |     elseif char =~ '\k' | 
					
						
							| 
									
										
										
										
											2017-03-21 19:18:29 +01:00
										 |  |  |       if char ==# 'type' | 
					
						
							|  |  |  |         return s:previous_token() !~# '^\%(im\|ex\)port$' | 
					
						
							|  |  |  |       endif | 
					
						
							|  |  |  |       return index(split('return const let import export extends yield default delete var await void typeof throw case new of in instanceof') | 
					
						
							|  |  |  |             \ ,char) < (line('.') != l:n) || s:save_pos('s:previous_token') == '.' | 
					
						
							| 
									
										
										
										
											2017-01-10 13:31:15 +01:00
										 |  |  |     elseif char == '>' | 
					
						
							| 
									
										
										
										
											2017-03-21 19:18:29 +01:00
										 |  |  |       return getline('.')[col('.')-2] == '=' || s:syn_at(line('.'),col('.')) =~? '^jsflow' | 
					
						
							| 
									
										
										
										
											2017-01-10 13:31:15 +01:00
										 |  |  |     elseif char == ':' | 
					
						
							| 
									
										
										
										
											2017-03-21 19:18:29 +01:00
										 |  |  |       return !s:save_pos('s:expr_col') | 
					
						
							|  |  |  |     elseif char == '/' | 
					
						
							|  |  |  |       return s:syn_at(line('.'),col('.')) =~? 'regex' | 
					
						
							| 
									
										
										
										
											2017-01-10 13:31:15 +01:00
										 |  |  |     endif | 
					
						
							| 
									
										
										
										
											2017-03-21 19:18:29 +01:00
										 |  |  |     return char !~ '[=~!<*,?^%|&([]' && | 
					
						
							|  |  |  |           \ (char !~ '[-+]' || l:n != line('.') && getline('.')[col('.')-2] == char) | 
					
						
							| 
									
										
										
										
											2016-08-12 22:54:35 +02:00
										 |  |  |   endif | 
					
						
							| 
									
										
										
										
											2017-01-10 13:31:15 +01:00
										 |  |  | endfunction | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | function GetJavascriptIndent() | 
					
						
							|  |  |  |   let b:js_cache = get(b:,'js_cache',[0,0,0]) | 
					
						
							| 
									
										
										
										
											2016-08-12 22:54:35 +02:00
										 |  |  |   " Get the current line. | 
					
						
							| 
									
										
										
										
											2017-01-10 13:31:15 +01:00
										 |  |  |   call cursor(v:lnum,1) | 
					
						
							|  |  |  |   let l:line = getline('.') | 
					
						
							| 
									
										
										
										
											2017-03-21 19:18:29 +01: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-08-12 22:54:35 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-10 13:31:15 +01: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-03-21 19:18:29 +01:00
										 |  |  |   elseif syns =~? s:syng_str | 
					
						
							| 
									
										
										
										
											2017-01-10 13:31:15 +01:00
										 |  |  |     if b:js_cache[0] == v:lnum - 1 && s:Balanced(v:lnum-1) | 
					
						
							|  |  |  |       let b:js_cache[0] = v:lnum | 
					
						
							|  |  |  |     endif | 
					
						
							| 
									
										
										
										
											2016-08-12 22:54:35 +02:00
										 |  |  |     return -1 | 
					
						
							|  |  |  |   endif | 
					
						
							|  |  |  |   let l:lnum = s:PrevCodeLine(v:lnum - 1) | 
					
						
							| 
									
										
										
										
											2017-01-10 13:31:15 +01:00
										 |  |  |   if !l:lnum | 
					
						
							|  |  |  |     return | 
					
						
							| 
									
										
										
										
											2016-08-12 22:54:35 +02:00
										 |  |  |   endif | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-10 13:31:15 +01: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 = '' | 
					
						
							| 
									
										
										
										
											2016-08-12 22:54:35 +02:00
										 |  |  |   endif | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-10 13:31:15 +01:00
										 |  |  |   " the containing paren, bracket, or curly. Many hacks for performance | 
					
						
							| 
									
										
										
										
											2017-03-21 19:18:29 +01:00
										 |  |  |   let idx = index([']',')','}'],l:line[0]) | 
					
						
							| 
									
										
										
										
											2017-01-10 13:31:15 +01:00
										 |  |  |   if b:js_cache[0] >= l:lnum && b:js_cache[0] < v:lnum && | 
					
						
							|  |  |  |         \ (b:js_cache[0] > l:lnum || s:Balanced(l:lnum)) | 
					
						
							|  |  |  |     call call('cursor',b:js_cache[1:]) | 
					
						
							| 
									
										
										
										
											2016-08-12 22:54:35 +02:00
										 |  |  |   else | 
					
						
							| 
									
										
										
										
											2017-03-21 19:18:29 +01:00
										 |  |  |     let [s:looksyn, s:checkIn, top] = [v:lnum - 1, 0, (!indent(l:lnum) && | 
					
						
							| 
									
										
										
										
											2017-01-10 13:31:15 +01:00
										 |  |  |           \ s:syn_at(l:lnum,1) !~? s:syng_str) * l:lnum] | 
					
						
							|  |  |  |     if idx + 1 | 
					
						
							| 
									
										
										
										
											2017-03-21 19:18:29 +01:00
										 |  |  |       call s:GetPair(['\[','(','{'][idx],'])}'[idx],'bW','s:skip_func()',2000,top) | 
					
						
							|  |  |  |     elseif getline(v:lnum) !~ '^\S' && syns =~? 'block' | 
					
						
							| 
									
										
										
										
											2017-01-10 13:31:15 +01:00
										 |  |  |       call s:GetPair('{','}','bW','s:skip_func()',2000,top) | 
					
						
							|  |  |  |     else | 
					
						
							|  |  |  |       call s:alternatePair(top) | 
					
						
							|  |  |  |     endif | 
					
						
							| 
									
										
										
										
											2016-08-12 22:54:35 +02:00
										 |  |  |   endif | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-10 13:31:15 +01:00
										 |  |  |   let b:js_cache = [v:lnum] + (line('.') == v:lnum ? [0,0] : getpos('.')[1:2]) | 
					
						
							|  |  |  |   let num = b:js_cache[1] | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   let [s:W, isOp, bL, switch_offset] = [s:sw(),0,0,0] | 
					
						
							|  |  |  |   if !num || s:IsBlock() | 
					
						
							| 
									
										
										
										
											2017-03-21 19:18:29 +01:00
										 |  |  |     let ilnum = line('.') | 
					
						
							| 
									
										
										
										
											2017-01-10 13:31:15 +01:00
										 |  |  |     let pline = s:save_pos('s:Trim',l:lnum) | 
					
						
							|  |  |  |     if num && s:looking_at() == ')' && s:GetPair('(', ')', 'bW', s:skip_expr, 100) > 0 | 
					
						
							| 
									
										
										
										
											2017-03-21 19:18:29 +01:00
										 |  |  |       let num = ilnum == num ? line('.') : num | 
					
						
							|  |  |  |       if idx < 0 && s:previous_token() ==# 'switch' && s:previous_token() != '.' | 
					
						
							|  |  |  |         if &cino !~ ':' | 
					
						
							| 
									
										
										
										
											2017-01-10 13:31:15 +01:00
										 |  |  |           let switch_offset = s:W | 
					
						
							|  |  |  |         else | 
					
						
							| 
									
										
										
										
											2017-03-21 19:18:29 +01:00
										 |  |  |           let switch_offset = max([-indent(num),s:parse_cino(':')]) | 
					
						
							| 
									
										
										
										
											2017-01-10 13:31:15 +01:00
										 |  |  |         endif | 
					
						
							|  |  |  |         if pline[-1:] != '.' && l:line =~# '^\%(default\|case\)\>' | 
					
						
							|  |  |  |           return indent(num) + switch_offset | 
					
						
							|  |  |  |         endif | 
					
						
							|  |  |  |       endif | 
					
						
							|  |  |  |     endif | 
					
						
							| 
									
										
										
										
											2017-03-21 19:18:29 +01:00
										 |  |  |     if idx < 0 && pline[-1:] !~ '[{;]' | 
					
						
							|  |  |  |       let isOp = (l:line =~# s:opfirst || s:continues(l:lnum,pline)) * s:W | 
					
						
							|  |  |  |       let bL = s:iscontOne(l:lnum,b:js_cache[1],isOp) | 
					
						
							| 
									
										
										
										
											2017-01-10 13:31:15 +01:00
										 |  |  |       let bL -= (bL && l:line[0] == '{') * s:W | 
					
						
							|  |  |  |     endif | 
					
						
							| 
									
										
										
										
											2017-03-21 19:18:29 +01:00
										 |  |  |   elseif idx < 0 && getline(b:js_cache[1])[b:js_cache[2]-1] == '(' && &cino =~ '(' | 
					
						
							|  |  |  |     let pval = s:parse_cino('(') | 
					
						
							|  |  |  |     return !pval ? (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-08-12 22:54:35 +02:00
										 |  |  |   endif | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-10 13:31:15 +01:00
										 |  |  |   " main return | 
					
						
							| 
									
										
										
										
											2017-03-21 19:18:29 +01:00
										 |  |  |   if l:line =~ '^\%([])}]\||}\)' | 
					
						
							|  |  |  |     return max([indent(num),0]) | 
					
						
							| 
									
										
										
										
											2017-01-10 13:31:15 +01:00
										 |  |  |   elseif num | 
					
						
							| 
									
										
										
										
											2017-03-21 19:18:29 +01:00
										 |  |  |     return indent(num) + s:W + switch_offset + bL + isOp | 
					
						
							| 
									
										
										
										
											2017-01-10 13:31:15 +01:00
										 |  |  |   endif | 
					
						
							| 
									
										
										
										
											2017-03-21 19:18:29 +01:00
										 |  |  |   return bL + isOp | 
					
						
							| 
									
										
										
										
											2016-08-12 22:54:35 +02:00
										 |  |  | endfunction | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | let &cpo = s:cpo_save | 
					
						
							|  |  |  | unlet s:cpo_save |