diff options
Diffstat (limited to 'indent')
-rw-r--r-- | indent/elixir.vim | 37 | ||||
-rw-r--r-- | indent/eruby.vim | 2 | ||||
-rw-r--r-- | indent/graphql.vim | 81 | ||||
-rw-r--r-- | indent/haskell.vim | 19 | ||||
-rw-r--r-- | indent/html.vim | 44 | ||||
-rw-r--r-- | indent/javascript.vim | 517 | ||||
-rw-r--r-- | indent/kotlin.vim | 18 | ||||
-rw-r--r-- | indent/lua.vim | 16 | ||||
-rw-r--r-- | indent/mako.vim | 5 | ||||
-rw-r--r-- | indent/plantuml.vim | 2 | ||||
-rw-r--r-- | indent/purescript.vim | 203 | ||||
-rw-r--r-- | indent/rust.vim | 15 | ||||
-rw-r--r-- | indent/swift.vim | 6 | ||||
-rw-r--r-- | indent/terraform.vim | 4 | ||||
-rw-r--r-- | indent/vue.vim | 6 |
15 files changed, 606 insertions, 369 deletions
diff --git a/indent/elixir.vim b/indent/elixir.vim index 0ddaec44..5882565f 100644 --- a/indent/elixir.vim +++ b/indent/elixir.vim @@ -7,42 +7,13 @@ let b:did_indent = 1 setlocal indentexpr=elixir#indent(v:lnum) -setlocal indentkeys+=0=end,0=catch,0=rescue,0=after,0=else,=->,0},0],0),0=\|>,0=<> +setlocal indentkeys+==after,=catch,=do,=else,=end,=rescue, +setlocal indentkeys+=*<Return>,=->,=\|>,=<>,0},0],0) + " TODO: @jbodah 2017-02-27: all operators should cause reindent when typed function! elixir#indent(lnum) - let lnum = a:lnum - let text = getline(lnum) - let prev_nb_lnum = prevnonblank(lnum-1) - let prev_nb_text = getline(prev_nb_lnum) - - call elixir#indent#debug("==> Indenting line " . lnum) - call elixir#indent#debug("text = '" . text . "'") - - let handlers = [ - \'top_of_file', - \'starts_with_end', - \'starts_with_mid_or_end_block_keyword', - \'following_trailing_do', - \'following_trailing_binary_operator', - \'starts_with_pipe', - \'starts_with_close_bracket', - \'starts_with_binary_operator', - \'inside_nested_construct', - \'starts_with_comment', - \'inside_generic_block' - \] - for handler in handlers - call elixir#indent#debug('testing handler elixir#indent#handle_'.handler) - let indent = function('elixir#indent#handle_'.handler)(lnum, text, prev_nb_lnum, prev_nb_text) - if indent != -1 - call elixir#indent#debug('line '.lnum.': elixir#indent#handle_'.handler.' returned '.indent) - return indent - endif - endfor - - call elixir#indent#debug("defaulting") - return 0 + return elixir#indent#indent(a:lnum) endfunction endif diff --git a/indent/eruby.vim b/indent/eruby.vim index 5b21ab91..6fd76600 100644 --- a/indent/eruby.vim +++ b/indent/eruby.vim @@ -14,7 +14,7 @@ runtime! indent/ruby.vim unlet! b:did_indent setlocal indentexpr= -if exists("b:eruby_subtype") +if exists("b:eruby_subtype") && b:eruby_subtype != '' && b:eruby_subtype !=# 'eruby' exe "runtime! indent/".b:eruby_subtype.".vim" else runtime! indent/html.vim diff --git a/indent/graphql.vim b/indent/graphql.vim new file mode 100644 index 00000000..ed9cfaa2 --- /dev/null +++ b/indent/graphql.vim @@ -0,0 +1,81 @@ +if !exists('g:polyglot_disabled') || index(g:polyglot_disabled, 'graphql') == -1 + +" Vim indent file +" Language: GraphQL +" Maintainer: Jon Parise <jon@indelible.org> + +if exists('b:did_indent') + finish +endif +let b:did_indent = 1 + +setlocal autoindent +setlocal nocindent +setlocal nolisp +setlocal nosmartindent + +setlocal indentexpr=GetGraphQLIndent() +setlocal indentkeys=0{,0},0),0[,0],0#,!^F,o,O,e + +" If our indentation function already exists, we have nothing more to do. +if exists('*GetGraphQLIndent') + finish +endif + +let s:cpo_save = &cpoptions +set cpoptions&vim + +" Check if the character at lnum:col is inside a string. +function s:InString(lnum, col) + return synIDattr(synID(a:lnum, a:col, 1), 'name') is# 'graphqlString' +endfunction + +function GetGraphQLIndent() + " If this is the first non-blank line, we have nothing more to do because + " all of our indentation rules are based on matching against earlier lines. + let l:prevlnum = prevnonblank(v:lnum - 1) + if l:prevlnum == 0 + return 0 + endif + + let l:line = getline(v:lnum) + + " If this line contains just a closing bracket, find its matching opening + " bracket and indent the closing backet to match. + let l:col = matchend(l:line, '^\s*[]})]') + if l:col > 0 && !s:InString(v:lnum, l:col) + let l:bracket = l:line[l:col - 1] + call cursor(v:lnum, l:col) + + if l:bracket is# '}' + let l:matched = searchpair('{', '', '}', 'bW') + elseif l:bracket is# ']' + let l:matched = searchpair('\[', '', '\]', 'bW') + elseif l:bracket is# ')' + let l:matched = searchpair('(', '', ')', 'bW') + else + let l:matched = -1 + endif + + return l:matched > 0 ? indent(l:matched) : virtcol('.') - 1 + endif + + " If we're inside of a multiline string, continue with the same indentation. + if s:InString(v:lnum, matchend(l:line, '^\s*') + 1) + return indent(v:lnum) + endif + + " If the previous line contained an opening bracket, and we are still in it, + " add indent depending on the bracket type. + if getline(l:prevlnum) =~# '[[{(]\s*$' + return indent(l:prevlnum) + shiftwidth() + endif + + " Default to the existing indentation level. + return indent(l:prevlnum) +endfunction + +let &cpoptions = s:cpo_save +unlet s:cpo_save + +endif diff --git a/indent/haskell.vim b/indent/haskell.vim index e63515ce..5c33e550 100644 --- a/indent/haskell.vim +++ b/indent/haskell.vim @@ -36,9 +36,18 @@ endif if !exists('g:haskell_indent_let') " let x = 0 in " >>>>x + " + " let x = 0 + " y = 1 let g:haskell_indent_let = 4 endif +if !exists('g:haskell_indent_let_no_in') + " let x = 0 + " x + let g:haskell_indent_let_no_in = 4 +endif + if !exists('g:haskell_indent_where') " where f :: Int -> Int " >>>>>>f x = x @@ -210,6 +219,9 @@ function! GetHaskellIndent() " " let x = 1 " >>>>y = 2 + " + " let x = 1 + " y 2 if l:prevline =~ '\C\<let\>\s\+.\+$' if l:line =~ '\C^\s*\<let\>' let l:s = match(l:prevline, '\C\<let\>') @@ -221,11 +233,16 @@ function! GetHaskellIndent() if s:isSYN('haskellLet', v:lnum - 1, l:s + 1) return l:s + g:haskell_indent_in endif - else + elseif l:line =~ '\s=\s' let l:s = match(l:prevline, '\C\<let\>') if s:isSYN('haskellLet', v:lnum - 1, l:s + 1) return l:s + g:haskell_indent_let endif + else + let l:s = match(l:prevline, '\C\<let\>') + if s:isSYN('haskellLet', v:lnum - 1, l:s + 1) + return l:s + g:haskell_indent_let_no_in + endif endif endif diff --git a/indent/html.vim b/indent/html.vim index 0cbc3b17..1e5691f7 100644 --- a/indent/html.vim +++ b/indent/html.vim @@ -260,20 +260,28 @@ let s:html_indent_tags = '[a-z_][a-z0-9_.-]*' let s:cpo_save = &cpo set cpo-=C -" [-- count indent-increasing tags of line a:lnum --] -fun! <SID>HtmlIndentOpen(lnum, pattern) - let s = substitute('x'.getline(a:lnum), - \ '.\{-}\(\(<\)\('.a:pattern.'\)\>\)', "\1", 'g') +func! <SID>HtmlIndentPatternCount(content, pattern) + let s = substitute('x'.a:content, a:pattern, "\1", 'g') let s = substitute(s, "[^\1].*$", '', '') return strlen(s) endfun +" [-- count indent-increasing tags of line a:lnum --] +fun! <SID>HtmlIndentOpen(lnum, pattern) + return <SID>HtmlIndentPatternCount(getline(a:lnum), + \ '.\{-}\(\(<\)\('.a:pattern.'\)\>\)') +endfun + " [-- count indent-decreasing tags of line a:lnum --] fun! <SID>HtmlIndentClose(lnum, pattern) - let s = substitute('x'.getline(a:lnum), - \ '.\{-}\(\(<\)/\('.a:pattern.'\)\>>\)', "\1", 'g') - let s = substitute(s, "[^\1].*$", '', '') - return strlen(s) + return <SID>HtmlIndentPatternCount(getline(a:lnum), + \ '.\{-}\(\(<\)/\('.a:pattern.'\)\>>\)') +endfun + +" [-- count self close tags of line a:lnum --] +fun! <SID>HtmlIndentSelfClose(lnum, pattern) + return <SID>HtmlIndentPatternCount(getline(a:lnum), + \ '.\{-}\(\(<\('.a:pattern.'\).*\)\@<!\/>\)') endfun " [-- count indent-increasing '{' of (java|css) line a:lnum --] @@ -292,8 +300,9 @@ fun! <SID>HtmlIndentSum(lnum, style) if a:style == match(getline(a:lnum), '^\s*</\<\('.s:html_indent_tags.'\)\>') let open = <SID>HtmlIndentOpen(a:lnum, s:html_indent_tags) - <SID>HtmlIndentOpen(a:lnum, s:html_noindent_tags) let close = <SID>HtmlIndentClose(a:lnum, s:html_indent_tags) - <SID>HtmlIndentClose(a:lnum, s:html_noindent_tags) - if 0 != open || 0 != close - return open - close + let self_close = <SID>HtmlIndentSelfClose(a:lnum, s:html_noindent_tags) + if 0 != open || 0 != close || 0 != self_close + return open - close - self_close endif endif endif @@ -310,6 +319,13 @@ fun! <SID>HtmlIndentSum(lnum, style) endfun fun! HtmlIndentGet(lnum) + " Get shiftwidth value. + if exists('*shiftwidth') + let sw = shiftwidth() + else + let sw = &sw + endif + " Find a non-empty line above the current line. let lnum = prevnonblank(a:lnum - 1) @@ -396,7 +412,7 @@ fun! HtmlIndentGet(lnum) endif if 0 == match(getline(a:lnum), '^\s*</') - return indent(preline) - (1*&sw) + return indent(preline) - (1*sw) else return indent(preline) endif @@ -417,7 +433,7 @@ fun! HtmlIndentGet(lnum) " let tags_exp = '<\(' . join(tags, '\|') . '\)>' " let close_tags_exp = '</\(' . join(tags, '\|') . '\)>' " if getline(a:lnum) =~ tags_exp - " let block_start = search('^'.repeat(' ', lind + (&sw * ind - 1)).'\S' , 'bnW') + " let block_start = search('^'.repeat(' ', lind + (sw * ind - 1)).'\S' , 'bnW') " let prev_tag = search(tags_exp, 'bW', block_start) " let prev_closetag = search(close_tags_exp, 'W', a:lnum) " if prev_tag && !prev_closetag @@ -426,7 +442,7 @@ fun! HtmlIndentGet(lnum) " endif " if getline(a:lnum) =~ '</\w\+>' - " let block_start = search('^'.repeat(' ', lind + (&sw * ind - 1)).'\S' , 'bnW') + " let block_start = search('^'.repeat(' ', lind + (sw * ind - 1)).'\S' , 'bnW') " let prev_tag = search(tags_exp, 'bW', block_start) " let prev_closetag = search(close_tags_exp, 'W', a:lnum) " if prev_tag && !prev_closetag @@ -439,7 +455,7 @@ fun! HtmlIndentGet(lnum) setlocal noic endif - return lind + (&sw * ind) + return lind + (sw * ind) endfun let &cpo = s:cpo_save diff --git a/indent/javascript.vim b/indent/javascript.vim index 8f922eca..100efbc5 100644 --- a/indent/javascript.vim +++ b/indent/javascript.vim @@ -4,7 +4,7 @@ if !exists('g:polyglot_disabled') || index(g:polyglot_disabled, 'javascript') == " Language: Javascript " Maintainer: Chris Paul ( https://github.com/bounceme ) " URL: https://github.com/pangloss/vim-javascript -" Last Change: May 16, 2017 +" Last Change: September 18, 2017 " Only load this indent file when no other was loaded. if exists('b:did_indent') @@ -57,343 +57,295 @@ endif " matches before pos. let s:z = has('patch-7.4.984') ? 'z' : '' -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" +let s:skip_expr = "s:SynAt(line('.'),col('.')) =~? b:syng_strcom" +let s:in_comm = s:skip_expr[:-14] . "'comment\\|doc'" +let s:rel = has('reltime') " searchpair() wrapper -if has('reltime') - 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) +if s:rel + function s:GetPair(start,end,flags,skip) + return searchpair('\m'.a:start,'','\m'.a:end,a:flags,a:skip,s:l1,a:skip ==# 's:SkipFunc()' ? 2000 : 200) endfunction else - 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)])) + function s:GetPair(start,end,flags,skip) + return searchpair('\m'.a:start,'','\m'.a:end,a:flags,a:skip,s:l1) endfunction endif -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] +function s:SynAt(l,c) + let byte = line2byte(a:l) + a:c - 1 + let pos = index(s:synid_cache[0], byte) + if pos == -1 + let s:synid_cache[:] += [[byte], [synIDattr(synID(a:l, a:c, 0), 'name')]] endif - let s:synId_cache[pos] = synIDattr(synID(a:l,a:c,0),'name') - return s:synId_cache[pos] + return s:synid_cache[1][pos] endfunction -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:]] +function s:ParseCino(f) + let [divider, n, cstr] = [0] + matchlist(&cino, + \ '\%(.*,\)\=\%(\%d'.char2nr(a:f).'\(-\)\=\([.s0-9]*\)\)\=')[1:2] for c in split(cstr,'\zs') - if c ==# '.' && !divider + if c == '.' && !divider let divider = 1 elseif c ==# 's' - if n is '' - let n = s:W - else - let n = str2nr(n) * s:W + if n !~ '\d' + return n . s:sw() + 0 endif + let n = str2nr(n) * s:sw() break - elseif c =~ '\d' - let [n, divider] .= [c, 0] else - break + let [n, divider] .= [c, 0] endif endfor - return sign * str2nr(n) / max([str2nr(divider),1]) + return str2nr(n) / max([str2nr(divider),1]) endfunction -" Optimized {skip} expr, used only once per GetJavascriptIndent() call -function s:skip_func() - if s:topCol == 1 || line('.') < s:scriptTag - return {} " E728, used as limit condition for loops and searchpair() +" Optimized {skip} expr, only callable from the search loop which +" GetJavascriptIndent does to find the containing [[{(] (side-effects) +function s:SkipFunc() + if s:top_col == 1 + throw 'out of bounds' endif - let s:topCol = col('.') - if getline('.') =~ '\%<'.s:topCol.'c\/.\{-}\/\|\%>'.s:topCol.'c[''"]\|\\$' + let s:top_col = 0 + if s:check_in if eval(s:skip_expr) - let s:topCol = 0 + return 1 endif - return !s:topCol - elseif s:checkIn || search('\m`\|\${\|\*\/','nW'.s:z,s:looksyn) - let s:checkIn = eval(s:skip_expr) - if s:checkIn - let s:topCol = 0 + let s:check_in = 0 + elseif getline('.') =~ '\%<'.col('.').'c\/.\{-}\/\|\%>'.col('.').'c[''"]\|\\$' + if eval(s:skip_expr) + return 1 endif + elseif search('\m`\|\${\|\*\/','nW'.s:z,s:looksyn) && eval(s:skip_expr) + let s:check_in = 1 + return 1 endif - let s:looksyn = line('.') - return s:checkIn + let [s:looksyn, s:top_col] = getpos('.')[1:2] endfunction -function s:alternatePair() - let [l:pos, pat, l:for] = [getpos('.'), '[][(){};]', 3] - while search('\m'.pat,'bW') - if s:skip_func() | continue | endif - let idx = stridx('])};',s:looking_at()) - 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 +function s:AlternatePair() + let [pat, l:for] = ['[][(){};]', 2] + while s:SearchLoop(pat,'bW','s:SkipFunc()') + if s:LookingAt() == ';' + if !l:for + if s:GetPair('{','}','bW','s:SkipFunc()') + return + endif break + else + let [pat, l:for] = ['[{}();]', l:for - 1] endif else - return + let idx = stridx('])}',s:LookingAt()) + if idx == -1 + return + elseif !s:GetPair(['\[','(','{'][idx],'])}'[idx],'bW','s:SkipFunc()') + break + endif endif endwhile - call setpos('.',l:pos) + throw 'out of bounds' endfunction -function s:looking_at() +function s:Nat(int) + return a:int * (a:int > 0) +endfunction + +function s:LookingAt() return getline('.')[col('.')-1] endfunction -function s:token() - return s:looking_at() =~ '\k' ? expand('<cword>') : s:looking_at() +function s:Token() + return s:LookingAt() =~ '\k' ? expand('<cword>') : s:LookingAt() endfunction -function s:previous_token() - let l:pos = getpos('.') +function s:PreviousToken() + let l:col = col('.') if search('\m\k\{1,}\|\S','ebW') - if (strpart(getline('.'),col('.')-2,2) == '*/' || line('.') != l:pos[1] && - \ getline('.')[:col('.')-1] =~ '\/\/') && 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 + if search('\m\*\%#\/\|\/\/\%<'.a:firstline.'l','nbW',line('.')) && eval(s:in_comm) + if s:SearchLoop('\S\ze\_s*\/[/*]','bW',s:in_comm) + return s:Token() + endif + call cursor(a:firstline, l:col) else - return s:token() + return s:Token() endif - call setpos('.',l:pos) endif return '' endfunction -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 +function s:Pure(f,...) + return eval("[call(a:f,a:000),cursor(a:firstline,".col('.').")][0]") +endfunction -function s:expr_col() - if getline('.')[col('.')-2] == ':' - return 1 - endif - 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() == '?' +function s:SearchLoop(pat,flags,expr) + return s:GetPair(a:pat,'\_$.',a:flags,a:expr) +endfunction + +function s:ExprCol() + let bal = 0 + while s:SearchLoop('[{}?]\|\_[^:]\zs::\@!','bW',s:skip_expr) + if s:LookingAt() == ':' + let bal -= 1 + elseif s:LookingAt() == '?' 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 + if bal == 1 + break + endif + elseif s:LookingAt() == '{' + let bal = !s:IsBlock() + break + elseif !s:GetPair('{','}','bW',s:skip_expr) break endif endwhile - call setpos('.',l:pos) - return max([bal,0]) + return s:Nat(bal) endfunction " configurable regexes that define continuation lines, not including (, {, or [. let s:opfirst = '^' . get(g:,'javascript_opfirst', - \ '\C\%([<>=,?^%|*/&]\|\([-.:+]\)\1\@!\|!=\|in\%(stanceof\)\=\>\)') + \ '\C\%([<>=,.?^%|/&]\|\([-:+]\)\1\@!\|\*\+\|!=\|in\%(stanceof\)\=\>\)') let s:continuation = get(g:,'javascript_continuation', \ '\C\%([<=,.~!?/*^%|&:]\|+\@<!+\|-\@<!-\|=\@<!>\|\<\%(typeof\|new\|delete\|void\|in\|instanceof\|await\)\)') . '$' -function s:continues(ln,con) - 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' - return s:previous_token() != '.' - elseif token == ':' - return s:expr_col() - endif - return 1 +function s:Continues(ln,con) + let tok = matchstr(a:con[-15:],s:continuation) + if tok =~ '[a-z:]' + call cursor(a:ln, len(a:con)) + return tok == ':' ? s:ExprCol() : s:PreviousToken() != '.' + elseif tok !~ '[/>]' + return tok isnot '' endif -endfunction - -function s:Trim(ln) - 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 - return pline -endfunction - -" Find line above 'lnum' that isn't empty or in a comment -function s:PrevCodeLine(lnum) - let l:n = prevnonblank(a:lnum) - while l:n - if getline(l:n) =~ '^\s*\/[/*]' - if (stridx(getline(l:n),'`') > 0 || getline(l:n-1)[-1:] == '\') && - \ s:syn_at(l:n,1) =~? b:syng_str - break - endif - let l:n = prevnonblank(l:n-1) - elseif stridx(getline(l:n), '*/') + 1 && s:syn_at(l:n,1) =~? s:syng_com - let l:pos = getpos('.') - call cursor(l:n,1) - let l:n = search('\m\S\_s*\/\*','nbW') - call setpos('.',l:pos) - else - break - endif - endwhile - return l:n + return s:SynAt(a:ln, len(a:con)) !~? (tok == '>' ? 'jsflow\|^html' : 'regex') endfunction " Check if line 'lnum' has a balanced amount of parentheses. function s:Balanced(lnum) - let l:open = 0 - let l:line = getline(a:lnum) - let pos = match(l:line, '[][(){}]', 0) + let [l:open, l:line] = [0, getline(a:lnum)] + let pos = match(l:line, '[][(){}]') while pos != -1 - if s:syn_at(a:lnum,pos + 1) !~? b:syng_strcom + if s:SynAt(a:lnum,pos + 1) !~? b:syng_strcom let l:open += match(' ' . l:line[pos],'[[({]') if l:open < 0 return endif endif - let pos = match(l:line, (l:open ? - \ '['.matchstr(['][','()','{}'],l:line[pos]).']' : - \ '[][(){}]'), pos + 1) + let pos = match(l:line, !l:open ? '[][(){}]' : '()' =~ l:line[pos] ? + \ '[()]' : '{}' =~ l:line[pos] ? '[{}]' : '[][]', pos + 1) endwhile return !l:open endfunction -function s:OneScope(lnum) - let pline = s:Trim(a:lnum) - call cursor(a:lnum,strlen(pline)) - let kw = 'else do' - if pline[-1:] == ')' && s:GetPair('(', ')', 'bW', s:skip_expr, 100) > 0 - if s:previous_token() =~# '^\%(await\|each\)$' - call s:previous_token() - let kw = 'for' - else - let kw = 'for if let while with' - endif +function s:OneScope() + if s:LookingAt() == ')' && s:GetPair('(', ')', 'bW', s:skip_expr) + let tok = s:PreviousToken() + return (count(split('for if let while with'),tok) || + \ tok =~# '^await$\|^each$' && s:PreviousToken() ==# 'for') && + \ s:Pure('s:PreviousToken') != '.' && !(tok == 'while' && s:DoWhile()) + elseif s:Token() =~# '^else$\|^do$' + return s:Pure('s:PreviousToken') != '.' endif - return pline[-2:] == '=>' || index(split(kw),s:token()) + 1 && - \ s:__previous_token() != '.' && !s:doWhile() + return strpart(getline('.'),col('.')-2,2) == '=>' 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]) +function s:DoWhile() + let cpos = searchpos('\m\<','cbW') + if s:SearchLoop('\C[{}]\|\<\%(do\|while\)\>','bW',s:skip_expr) + if s:{s:LookingAt() == '}' && s:GetPair('{','}','bW',s:skip_expr) ? + \ 'Previous' : ''}Token() ==# 'do' && s:IsBlock() + return 1 + endif + call call('cursor',cpos) endif 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) +" returns total offset from braceless contexts. 'num' is the lineNr which +" encloses the entire context, 'cont' if whether a:firstline is a continued +" expression, which could have started in a braceless context +function s:IsContOne(num,cont) + let [l:num, b_l] = [a:num + !a:num, 0] + let pind = a:num ? indent(a:num) + s:sw() : 0 + let ind = indent('.') + !a:cont + while line('.') > l:num && ind > pind || line('.') == l:num + if indent('.') < ind && s:OneScope() + let b_l += 1 + elseif !a:cont || b_l || ind < indent(a:firstline) + break + else + call cursor(0,1) + endif + let ind = min([ind, indent('.')]) + if s:PreviousToken() is '' break endif - let ind = min([ind, indent(l:i)]) - let l:i = s:PrevCodeLine(l:i - 1) endwhile - return bL + return b_l +endfunction + +function s:IsSwitch() + call call('cursor',b:js_cache[1:]) + return search('\m\C\%#.\_s*\%(\%(\/\/.*\_$\|\/\*\_.\{-}\*\/\)\@>\_s*\)*\%(case\|default\)\>','nWc'.s:z) endfunction " https://github.com/sweet-js/sweet.js/wiki/design#give-lookbehind-to-the-reader -function s:IsBlock(...) - if a:0 || s:looking_at() == '{' - let l:n = line('.') - let char = s:previous_token() - if match(s:stack,'\cxml\|jsx') + 1 && s:syn_at(line('.'),col('.')-1) =~? 'xml\|jsx' - return char != '{' - elseif char =~ '\k' - 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:__previous_token() == '.' - elseif char == '>' - return getline('.')[col('.')-2] == '=' || s:syn_at(line('.'),col('.')) =~? 'jsflow\|^html' - elseif char == '*' - return s:__previous_token() == ':' - elseif char == ':' - return !s:expr_col() - elseif char == '/' - return s:syn_at(line('.'),col('.')) =~? 'regex' +function s:IsBlock() + let tok = s:PreviousToken() + if join(s:stack) =~? 'xml\|jsx' && s:SynAt(line('.'),col('.')-1) =~? 'xml\|jsx' + return tok != '{' + elseif tok =~ '\k' + if tok ==# 'type' + return s:Pure('eval',"s:PreviousToken() !~# '^\\%(im\\|ex\\)port$' || s:PreviousToken() == '.'") + elseif tok ==# 'of' + return s:Pure('eval',"!s:GetPair('[[({]','[])}]','bW',s:skip_expr) || s:LookingAt() != '(' ||" + \ ."s:{s:PreviousToken() ==# 'await' ? 'Previous' : ''}Token() !=# 'for' || s:PreviousToken() == '.'") endif - return char !~ '[=~!<,.?^%|&([]' && - \ (char !~ '[-+]' || l:n != line('.') && getline('.')[col('.')-2] == char) + return index(split('return const let import export extends yield default delete var await void typeof throw case new in instanceof') + \ ,tok) < (line('.') != a:firstline) || s:Pure('s:PreviousToken') == '.' + elseif tok == '>' + return getline('.')[col('.')-2] == '=' || s:SynAt(line('.'),col('.')) =~? 'jsflow\|^html' + elseif tok == '*' + return s:Pure('s:PreviousToken') == ':' + elseif tok == ':' + return s:Pure('eval',"s:PreviousToken() =~ '^\\K\\k*$' && !s:ExprCol()") + elseif tok == '/' + return s:SynAt(line('.'),col('.')) =~? 'regex' + elseif tok !~ '[=~!<,.?^%|&([]' + return tok !~ '[-+]' || line('.') != a:firstline && getline('.')[col('.')-2] == tok endif endfunction - function GetJavascriptIndent() let b:js_cache = get(b:,'js_cache',[0,0,0]) - let s:synId_cache = {} - " Get the current line. - call cursor(v:lnum,1) - let l:line = getline('.') + let s:synid_cache = [[],[]] + let l:line = getline(v:lnum) " 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,'') + let s:stack = [''] + map(synstack(v:lnum,1),"synIDattr(v:val,'name')") " start with strings,comments,etc. - if syns =~? s:syng_com + if s:stack[-1] =~? 'comment\|doc' if l:line =~ '^\s*\*' return cindent(v:lnum) elseif l:line !~ '^\s*\/[/*]' return -1 endif - elseif syns =~? b:syng_str + elseif s:stack[-1] =~? b:syng_str if b:js_cache[0] == v:lnum - 1 && s:Balanced(v:lnum-1) let b:js_cache[0] = v:lnum endif return -1 endif - let l:lnum = s:PrevCodeLine(v:lnum - 1) - if !l:lnum + + let s:l1 = max([0,prevnonblank(v:lnum) - (s:rel ? 2000 : 1000), + \ get(get(b:,'hi_indent',{}),'blocklnr')]) + call cursor(v:lnum,1) + if s:PreviousToken() is '' return endif + let [l:lnum, pline] = [line('.'), getline('.')[:col('.')-1]] let l:line = substitute(l:line,'^\s*','','') + let l:line_raw = l:line if l:line[:1] == '/*' let l:line = substitute(l:line,'^\%(\/\*.\{-}\*\/\s*\)*','','') endif @@ -402,57 +354,94 @@ function GetJavascriptIndent() endif " the containing paren, bracket, or curly. Many hacks for performance - let [ s:scriptTag, idx ] = [ get(get(b:,'hi_indent',{}),'blocklnr'), - \ index([']',')','}'],l:line[0]) ] - 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[2] ? b:js_cache[1:] : [0,0]) + call cursor(v:lnum,1) + let idx = index([']',')','}'],l:line[0]) + 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:]) else - let [s:looksyn, s:checkIn, s:topCol] = [v:lnum - 1, 0, 0] - if idx + 1 - call s:GetPair(['\[','(','{'][idx],'])}'[idx],'bW','s:skip_func()',2000) - elseif getline(v:lnum) !~ '^\S' && syns =~? 'block' - call s:GetPair('{','}','bW','s:skip_func()',2000) - else - call s:alternatePair() - endif + let [s:looksyn, s:top_col, s:check_in, s:l1] = [v:lnum - 1,0,0, + \ max([s:l1, &smc ? search('\m^.\{'.&smc.',}','nbW',s:l1 + 1) + 1 : 0])] + try + if idx != -1 + call s:GetPair(['\[','(','{'][idx],'])}'[idx],'bW','s:SkipFunc()') + elseif getline(v:lnum) !~ '^\S' && s:stack[-1] =~? 'block\|^jsobject$' + call s:GetPair('{','}','bW','s:SkipFunc()') + else + call s:AlternatePair() + endif + catch /^\Cout of bounds$/ + call cursor(v:lnum,1) + endtry + let b:js_cache[1:] = line('.') == v:lnum ? [0,0] : getpos('.')[1:2] endif - let b:js_cache = [v:lnum] + (line('.') == v:lnum ? [s:scriptTag,0] : getpos('.')[1:2]) - let num = b:js_cache[1] + let [b:js_cache[0], num] = [v:lnum, b:js_cache[1]] - let [s:W, isOp, bL, switch_offset] = [s:sw(),0,0,0] - if !b:js_cache[2] || s:IsBlock() + let [num_ind, is_op, b_l, l:switch_offset] = [s:Nat(indent(num)),0,0,0] + if !num || s:LookingAt() == '{' && s:IsBlock() let ilnum = line('.') - let pline = s:Trim(l:lnum) - if b:js_cache[2] && s:looking_at() == ')' && s:GetPair('(',')','bW',s:skip_expr,100) > 0 - let num = ilnum == num ? line('.') : num - if idx < 0 && s:previous_token() ==# 'switch' && s:previous_token() != '.' - let switch_offset = &cino !~ ':' ? s:W : max([-indent(num),s:parse_cino(':')]) + if num && s:LookingAt() == ')' && s:GetPair('(',')','bW',s:skip_expr) + if ilnum == num + let [num, num_ind] = [line('.'), indent('.')] + endif + if idx == -1 && s:PreviousToken() ==# 'switch' && s:IsSwitch() + let l:switch_offset = &cino !~ ':' ? s:sw() : s:ParseCino(':') if pline[-1:] != '.' && l:line =~# '^\%(default\|case\)\>' - return indent(num) + switch_offset + return s:Nat(num_ind + l:switch_offset) + elseif &cino =~ '=' + let l:case_offset = s:ParseCino('=') endif endif endif - 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) - let bL -= (bL && l:line[0] == '{') * s:W + if idx == -1 && pline[-1:] !~ '[{;]' + let sol = matchstr(l:line,s:opfirst) + if sol is '' || sol == '/' && s:SynAt(v:lnum, + \ 1 + len(getline(v:lnum)) - len(l:line)) =~? 'regex' + if s:Continues(l:lnum,pline) + let is_op = s:sw() + endif + elseif num && sol =~# '^\%(in\%(stanceof\)\=\|\*\)$' + call cursor(l:lnum, len(pline)) + if s:LookingAt() == '}' && s:GetPair('{','}','bW',s:skip_expr) && + \ s:PreviousToken() == ')' && s:GetPair('(',')','bW',s:skip_expr) && + \ (s:PreviousToken() == ']' || s:Token() =~ '\k' && + \ s:{s:PreviousToken() == '*' ? 'Previous' : ''}Token() !=# 'function') + return num_ind + s:sw() + endif + let is_op = s:sw() + else + let is_op = s:sw() + endif + call cursor(l:lnum, len(pline)) + let b_l = s:Nat(s:IsContOne(b:js_cache[1],is_op) - (!is_op && l:line =~ '^{')) * s:sw() endif - elseif idx < 0 && getline(b:js_cache[1])[b:js_cache[2]-1] == '(' && &cino =~ '(' - let pval = s:parse_cino('(') - 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]) + elseif idx.s:LookingAt().&cino =~ '^-1(.*(' && (search('\m\S','nbW',num) || s:ParseCino('U')) + let pval = s:ParseCino('(') + if !pval + let [Wval, vcol] = [s:ParseCino('W'), virtcol('.')] + if search('\m\S','W',num) + return s:ParseCino('w') ? vcol : virtcol('.')-1 + endif + return Wval ? s:Nat(num_ind + Wval) : vcol + endif + return s:Nat(num_ind + pval + searchpair('\m(','','\m)','nbrmW',s:skip_expr,num) * s:sw()) endif " main return if l:line =~ '^[])}]\|^|}' - return max([indent(num),0]) + if l:line_raw[0] == ')' && getline(num)[b:js_cache[2]-1] == '(' + if s:ParseCino('M') + return indent(l:lnum) + elseif &cino =~# 'm' && !s:ParseCino('m') + return virtcol('.') - 1 + endif + endif + return num_ind elseif num - return indent(num) + s:W + switch_offset + bL + isOp + return s:Nat(num_ind + get(l:,'case_offset',s:sw()) + l:switch_offset + b_l + is_op) endif - return bL + isOp + return b_l + is_op endfunction let &cpo = s:cpo_save diff --git a/indent/kotlin.vim b/indent/kotlin.vim index 20bc1ee9..aacf3edb 100644 --- a/indent/kotlin.vim +++ b/indent/kotlin.vim @@ -3,13 +3,14 @@ if !exists('g:polyglot_disabled') || index(g:polyglot_disabled, 'kotlin') == -1 " Vim indent file " Language: Kotlin " Maintainer: Alexander Udalov -" Latest Revision: 27 June 2015 +" Latest Revision: 15 July 2017 if exists("b:did_indent") finish endif let b:did_indent = 1 +setlocal cinoptions& cinoptions+=j1,L0 setlocal indentexpr=GetKotlinIndent() setlocal indentkeys=0},0),!^F,o,O,e,<CR> setlocal autoindent " TODO ? @@ -25,6 +26,21 @@ function! GetKotlinIndent() let prev_indent = indent(prev_num) let cur = getline(v:lnum) + if cur =~ '^\s*\*' + return cindent(v:lnum) + endif + + if prev =~ '^\s*\*/' + let st = prev + while st > 1 + if getline(st) =~ '^\s*/\*' + break + endif + let st = st - 1 + endwhile + return indent(st) + endif + let prev_open_paren = prev =~ '^.*(\s*$' let cur_close_paren = cur =~ '^\s*).*$' diff --git a/indent/lua.vim b/indent/lua.vim index 901ac854..3c33c032 100644 --- a/indent/lua.vim +++ b/indent/lua.vim @@ -24,21 +24,21 @@ endif " Variables -----------------------------------------------{{{1 -let s:open_patt = '\%(\<\%(function\|if\|repeat\|do\)\>\|(\|{\)' -let s:middle_patt = '\<\%(else\|elseif\)\>' -let s:close_patt = '\%(\<\%(end\|until\)\>\|)\|}\)' +let s:open_patt = '\C\%(\<\%(function\|if\|repeat\|do\)\>\|(\|{\)' +let s:middle_patt = '\C\<\%(else\|elseif\)\>' +let s:close_patt = '\C\%(\<\%(end\|until\)\>\|)\|}\)' let s:anon_func_start = '\S\+\s*[({].*\<function\s*(.*)\s*$' let s:anon_func_end = '\<end\%(\s*[)}]\)\+' " Expression used to check whether we should skip a match with searchpair(). -let s:skip_expr = "synIDattr(synID(line('.'),col('.'),1),'name') =~ 'luaComment\\|luaString'" +let s:skip_expr = "synIDattr(synID(line('.'),col('.'),1),'name') =~# 'luaComment\\|luaString'" " Auxiliary Functions -------------------------------------{{{1 function s:IsInCommentOrString(lnum, col) - return synIDattr(synID(a:lnum, a:col, 1), 'name') =~ 'luaCommentLong\|luaStringLong' - \ && !(getline(a:lnum) =~ '^\s*\%(--\)\?\[=*\[') " opening tag is not considered 'in' + return synIDattr(synID(a:lnum, a:col, 1), 'name') =~# 'luaCommentLong\|luaStringLong' + \ && !(getline(a:lnum) =~# '^\s*\%(--\)\?\[=*\[') " opening tag is not considered 'in' endfunction " Find line above 'lnum' that isn't blank, in a comment or string. @@ -85,7 +85,7 @@ function GetLuaIndent() endif " special case: call(with, {anon = function() -- should indent only once - if num_pairs > 1 && contents_prev =~ s:anon_func_start + if num_pairs > 1 && contents_prev =~# s:anon_func_start let i = 1 endif @@ -98,7 +98,7 @@ function GetLuaIndent() endif " special case: end}) -- end of call with anon func should unindent once - if num_pairs > 1 && contents_cur =~ s:anon_func_end + if num_pairs > 1 && contents_cur =~# s:anon_func_end let i = -1 endif diff --git a/indent/mako.vim b/indent/mako.vim index bd6120a5..e3971197 100644 --- a/indent/mako.vim +++ b/indent/mako.vim @@ -44,8 +44,6 @@ if !exists('g:polyglot_disabled') || index(g:polyglot_disabled, 'mako') == -1 " 0.1 - 06 June 2009 " - Initial public release of mako indent file -let sw=2 " default shiftwidth of 2 spaces - if exists("b:did_indent") finish endif @@ -55,6 +53,9 @@ setlocal nosmartindent setlocal noautoindent setlocal nocindent setlocal nolisp +setlocal expandtab +setlocal softtabstop=2 +setlocal shiftwidth=2 setlocal indentexpr=GetMakoIndent() setlocal indentkeys+=*<Return>,<>>,<bs>,end,: diff --git a/indent/plantuml.vim b/indent/plantuml.vim index a5e6fcdf..1cfc91b7 100644 --- a/indent/plantuml.vim +++ b/indent/plantuml.vim @@ -19,7 +19,7 @@ let s:incIndent = \ '^\s*[hr]\?note\>\%(\%("[^"]*" \<as\>\)\@![^:]\)*$\|' . \ '^\s*title\s*$\|' . \ '^\s*skinparam\>.*{\s*$\|' . - \ '^\s*\%(state\|class\|partition\|rectangle\|enum\|interface\|namespace\)\>.*{' + \ '^\s*\%(state\|class\|partition\|rectangle\|enum\|interface\|namespace\|object\)\>.*{' let s:decIndent = '^\s*\%(end\|else\|}\)' diff --git a/indent/purescript.vim b/indent/purescript.vim index c456c608..c22da095 100644 --- a/indent/purescript.vim +++ b/indent/purescript.vim @@ -37,9 +37,16 @@ if !exists('g:purescript_indent_let') let g:purescript_indent_let = 4 endif +if !exists('g:purescript_indent_in') + " let x = 0 + " >in + let g:purescript_indent_in = 1 +endif + if !exists('g:purescript_indent_where') - " where f :: Int -> Int - " >>>>>>f x = x + " where + " >>f :: Int -> Int + " >>f x = x let g:purescript_indent_where = 6 endif @@ -49,16 +56,29 @@ if !exists('g:purescript_indent_do') let g:purescript_indent_do = 3 endif +if !exists('g:purescript_indent_dot') + " f + " :: forall a + " >. String + " -> String + let g:purescript_indent_dot = 1 +endif + setlocal indentexpr=GetPurescriptIndent() -setlocal indentkeys=!^F,o,O,},=where,=in +setlocal indentkeys=!^F,o,O,},=where,=in,=::,=->,=→,==>,=⇒ + +function! s:GetSynStack(lnum, col) + return map(synstack(a:lnum, a:col), { key, val -> synIDattr(val, "name") }) +endfunction function! GetPurescriptIndent() + let ppline = getline(v:lnum - 2) let prevline = getline(v:lnum - 1) let line = getline(v:lnum) if line =~ '^\s*\<where\>' - let s = match(prevline, '\S') - return s + 2 + let s = indent(v:lnum - 1) + return max([s, &l:shiftwidth]) endif if line =~ '^\s*\<in\>' @@ -67,72 +87,191 @@ function! GetPurescriptIndent() while s <= 0 && n > 0 let n = n - 1 - let s = match(getline(n),'\<let\>') + let s = match(getline(n), '\<let\>') + if s >= 0 && index(s:GetSynStack(v:lnum - 1, s), 'purescriptString') != -1 + let s = -1 + endif endwhile - return s + 1 + return s + g:purescript_indent_in + endif + + let s = match(prevline, '^\s*\zs\(--\|import\)') + if s >= 0 + " comments + " imports + return s + endif + + if prevline =~ '^\S.*::' && line !~ '^\s*\(\.\|->\|→\|=>\|⇒\)' && !~ '^instance' + " f :: String + " -> String + return 0 + endif + + let s = match(prevline, '[[:alnum:][:blank:]]\@<=|[[:alnum:][:blank:]$]') + if s >= 0 && prevline !~ '^class\>' && index(s:GetSynStack(v:lnum - 1, s), "purescriptFunctionDecl") == -1 + " ident pattern guards but not if we are in a type declaration + " what we detect using syntax groups + if prevline =~ '|\s*otherwise\>' + return indent(search('^\s*\k', 'bnW')) + " somehow this pattern does not work :/ + " return indent(search('^\(\s*|\)\@!', 'bnW')) + else + return s + endif + endif + + let s = match(line, '\%(\\.\{-}\)\@<=->') + if s >= 0 + " inline lambda + return indent(v:lnum) + endif + + " indent rules for -> (lambdas and case expressions) + let s = match(line, '->') + let p = match(prevline, '\\') + " protect that we are not in a type signature + " and not in a case expression + if s >= 0 && index(s:GetSynStack(s == 0 ? v:lnum - 1 : v:lnum, max([1, s])), "purescriptFunctionDecl") == -1 + \ && p >= 0 && index(s:GetSynStack(v:lnum - 1, p), "purescriptString") == -1 + return p + endif + + if prevline =~ '^\S' + " start typing signature, function body, data & newtype on next line + return &l:shiftwidth endif - if prevline =~ '[!#$%&*+./<>?@\\^|~-]\s*$' + if ppline =~ '^\S' && prevline =~ '^\s*$' + return 0 + endif + + if line =~ '^\s*\%(::\|∷\)' + return match(prevline, '\S') + &l:shiftwidth + endif + + if prevline =~ '^\s*\(::\|∷\)\s*forall' + return match(prevline, '\S') + g:purescript_indent_dot + endif + + let s = match(prevline, '^\s*\zs\%(::\|∷\|=>\|⇒\|->\|→\)') + let r = match(prevline, '^\s*\zs\.') + if s >= 0 || r >= 0 + if s >= 0 + if line !~ '^\s*\%(::\|∷\|=>\|⇒\|->\|→\)' && line !~ '^\s*$' + return s - 2 + else + return s + endif + elseif r >= 0 + if line !~ '^\s\%(::\|∷\|=>\|⇒\|->\|→\)' + return r - g:purescript_indent_dot + else + return r + endif + endif + endif + + if prevline =~ '[!#$%&*+./<>?@\\^~-]\s*$' let s = match(prevline, '=') if s > 0 - return s + 2 + return s + &l:shiftwidth endif - let s = match(prevline, ':') + let s = match(prevline, '\<:\>') if s > 0 - return s + 3 + return s + &l:shiftwidth else - return match(prevline, '\S') + return match(prevline, '\S') + &l:shiftwidth endif endif if prevline =~ '[{([][^})\]]\+$' + echom "return 1" return match(prevline, '[{([]') endif - if prevline =~ '\<let\>\s\+.\+\(\<in\>\)\?\s*$' + let s = match(prevline, '\<let\>\s\+\zs\S') + if s >= 0 && index(s:GetSynStack(v:lnum - 1, s), 'purescriptString') == -1 + return s + endif + + let s = match(prevline, '\<let\>\s*$') + if s >= 0 && index(s:GetSynStack(v:lnum - 1, s), 'purescriptString') == -1 + return s + g:purescript_indent_let + endif + + let s = match(prevline, '\<let\>\s\+.\+\(\<in\>\)\?\s*$') + if s >= 0 && index(s:GetSynStack(v:lnum - 1, s), 'purescriptString') == -1 return match(prevline, '\<let\>') + g:purescript_indent_let endif - if prevline !~ '\<else\>' - let s = match(prevline, '\<if\>.*\&.*\zs\<then\>') - if s > 0 - return s + let s = searchpairpos('\%(--.\{-}\)\@<!\<if\>', '\<then\>', '\<else\>.*\zs$', 'bnrc')[0] + if s > 0 + " this rule ensures that using `=` in visual mode will correctly indent + " `if then else`, but it does not handle lines after `then` and `else` + if line =~ '\<\%(then\|else\)\>' + return match(getline(s), '\<if\>') + &l:shiftwidth endif + endif - let s = match(prevline, '\<if\>') - if s > 0 - return s + g:purescript_indent_if - endif + let p = match(prevline, '\<if\>\%(.\{-}\<then\>.\{-}\<else\>\)\@!') + if p > 0 + return p + &l:shiftwidth + endif + + let s = match(prevline, '=\s*$') + if s >= 0 && index(s:GetSynStack(v:lnum - 1, s), 'purescriptString') == -1 + return match(prevline, '\S') + &l:shiftwidth + endif + + let s = match(prevline, '[{([]\s*$') + if s >= 0 && index(s:GetSynStack(v:lnum - 1, s), 'purescriptString') == -1 + return match(prevline, '\S') + (line !~ '^\s*[})]]' ? 0 : &l:shiftwidth) + endif + + if prevline =~ '^class' + return &l:shiftwidth + endif + + let s = match(prevline, '\<where\>\s*$') + if s >= 0 && index(s:GetSynStack(v:lnum - 1, s), 'purescriptString') == -1 + return match(prevline, '\S') + g:purescript_indent_where endif - if prevline =~ '\(\<where\>\|\<do\>\|=\|[{([]\)\s*$' - return match(prevline, '\S') + &shiftwidth + let s = match(prevline, '\<where\>\s\+\zs\S\+.*$') + if s >= 0 && index(s:GetSynStack(v:lnum - 1, s), 'purescriptString') == -1 + return s endif - if prevline =~ '\<where\>\s\+\S\+.*$' - return match(prevline, '\<where\>') + g:purescript_indent_where + let s = match(prevline, '\<do\>\s*$') + if s >= 0 && index(s:GetSynStack(v:lnum - 1, s), 'purescriptString') == -1 + return match(prevline, '\S') + g:purescript_indent_do endif - if prevline =~ '\<do\>\s\+\S\+.*$' - return match(prevline, '\<do\>') + g:purescript_indent_do + let s = match(prevline, '\<do\>\s\+\zs\S\+.*$') + if s >= 0 && index(s:GetSynStack(v:lnum - 1, s), 'purescriptString') == -1 + return s endif - if prevline =~ '^\s*\<data\>\s\+[^=]\+\s\+=\s\+\S\+.*$' + let s = match(prevline, '^\s*\<data\>\s\+[^=]\+\s\+=\s\+\S\+.*$') + if s >= 0 && index(s:GetSynStack(v:lnum - 1, s), 'purescriptString') == -1 return match(prevline, '=') endif - if prevline =~ '\<case\>\s\+.\+\<of\>\s*$' + let s = match(prevline, '\<case\>\s\+.\+\<of\>\s*$') + if s >= 0 && index(s:GetSynStack(v:lnum - 1, s), 'purescriptString') == -1 return match(prevline, '\<case\>') + g:purescript_indent_case endif if prevline =~ '^\s*\<\data\>\s\+\S\+\s*$' - return match(prevline, '\<data\>') + &shiftwidth + return match(prevline, '\<data\>') + &l:shiftwidth endif - if (line =~ '^\s*}\s*' && prevline !~ '^\s*;') - return match(prevline, '\S') - &shiftwidth + let s = match(prevline, '^\s*[}\]]') + if s >= 0 && index(s:GetSynStack(v:lnum - 1, s), 'purescriptString') == -1 + return match(prevline, '\S') - &l:shiftwidth endif return match(prevline, '\S') diff --git a/indent/rust.vim b/indent/rust.vim index fec789de..042e2ab5 100644 --- a/indent/rust.vim +++ b/indent/rust.vim @@ -3,11 +3,12 @@ if !exists('g:polyglot_disabled') || index(g:polyglot_disabled, 'rust') == -1 " Vim indent file " Language: Rust " Author: Chris Morgan <me@chrismorgan.info> -" Last Change: 2016 Jul 15 +" Last Change: 2017 Mar 21 +" For bugs, patches and license go to https://github.com/rust-lang/rust.vim " Only load this indent file when no other was loaded. if exists("b:did_indent") - finish + finish endif let b:did_indent = 1 @@ -15,7 +16,7 @@ setlocal cindent setlocal cinoptions=L0,(0,Ws,J1,j1 setlocal cinkeys=0{,0},!^F,o,O,0[,0] " Don't think cinwords will actually do anything at all... never mind -setlocal cinwords=for,if,else,while,loop,impl,mod,unsafe,trait,struct,enum,fn,extern +setlocal cinwords=for,if,else,while,loop,impl,mod,unsafe,trait,struct,enum,fn,extern,macro " Some preliminary settings setlocal nolisp " Make sure lisp indenting doesn't supersede us @@ -27,9 +28,12 @@ setlocal indentexpr=GetRustIndent(v:lnum) " Only define the function once. if exists("*GetRustIndent") - finish + finish endif +let s:save_cpo = &cpo +set cpo&vim + " Come here when loading the script the first time. function! s:get_line_trimmed(lnum) @@ -207,4 +211,7 @@ function GetRustIndent(lnum) return cindent(a:lnum) endfunction +let &cpo = s:save_cpo +unlet s:save_cpo + endif diff --git a/indent/swift.vim b/indent/swift.vim index 773e6451..92df0fc8 100644 --- a/indent/swift.vim +++ b/indent/swift.vim @@ -51,7 +51,7 @@ endfunction function! s:IsCommentLine(lnum) return synIDattr(synID(a:lnum, - \ match(getline(a:lnum), "\S") + 1, 0), "name") + \ match(getline(a:lnum), "\\S") + 1, 0), "name") \ ==# "swiftComment" endfunction @@ -227,8 +227,8 @@ function! SwiftIndent(...) if numOpenParens > 0 let savePosition = getcurpos() " Must be at EOL because open paren has to be above (left of) the cursor - call cursor(previousNum, col("$")) - let previousParen = searchpair("(", "", ")", "bWn", "s:IsExcludedFromIndent()") + call cursor(previousNum, [previousNum, col("$")]) + let previousParen = searchpair("(", "", ")", "cbWn", "s:IsExcludedFromIndent()") call setpos(".", savePosition) return indent(previousParen) + shiftwidth() endif diff --git a/indent/terraform.vim b/indent/terraform.vim index 30c78229..5a29dfb4 100644 --- a/indent/terraform.vim +++ b/indent/terraform.vim @@ -30,7 +30,7 @@ function! TerraformIndent(lnum) let thisindent = previndent " block open? - if prevline =~ '[\[{]\s*$' + if prevline =~ '[\[{\(]\s*$' let thisindent += &sw endif @@ -38,7 +38,7 @@ function! TerraformIndent(lnum) let thisline = substitute(getline(a:lnum), '//.*$', '', '') " block close? - if thisline =~ '^\s*[\]}]' + if thisline =~ '^\s*[\)\]}]' let thisindent -= &sw endif diff --git a/indent/vue.vim b/indent/vue.vim index 038996ba..226c6522 100644 --- a/indent/vue.vim +++ b/indent/vue.vim @@ -25,10 +25,10 @@ let s:languages = [ \ { 'name': 'javascript', 'pairs': ['<script', '</script>'] }, \ ] -for language in s:languages +for s:language in s:languages " Set 'indentexpr' if the user has an indent file installed for the language - if strlen(globpath(&rtp, 'indent/'. language.name .'.vim')) - let language.indentexpr = s:get_indentexpr(language.name) + if strlen(globpath(&rtp, 'indent/'. s:language.name .'.vim')) + let s:language.indentexpr = s:get_indentexpr(s:language.name) endif endfor |