vim-polyglot/indent/elixir.vim

115 lines
3.2 KiB
VimL
Raw Normal View History

2013-09-12 11:31:56 -04:00
" Vim indent file
" Language: Elixir
" Maintainer: Carlos Galdino <carloshsgaldino@gmail.com>
" Last Change: 2013 Apr 24
if exists("b:did_indent")
finish
endif
let b:did_indent = 1
setlocal nosmartindent
2014-01-24 12:06:22 -05:00
setlocal indentexpr=GetElixirIndent()
2014-11-10 20:37:21 -05:00
setlocal indentkeys+=0=end,0=else,0=match,0=elsif,0=catch,0=after,0=rescue
2013-09-12 11:31:56 -04:00
if exists("*GetElixirIndent")
finish
endif
let s:cpo_save = &cpo
set cpo&vim
let s:skip_syntax = '\%(Comment\|String\)$'
let s:block_skip = "synIDattr(synID(line('.'),col('.'),1),'name') =~? '" . s:skip_syntax . "'"
let s:block_start = 'do\|fn'
let s:block_middle = 'else\|match\|elsif\|catch\|after\|rescue'
let s:block_end = 'end'
2014-01-09 05:59:09 -05:00
let s:arrow = '^.*->$'
2013-11-02 18:27:57 -04:00
let s:pipeline = '^\s*|>.*$'
2013-09-12 11:31:56 -04:00
2014-01-09 05:59:09 -05:00
let s:indent_keywords = '\<\%(' . s:block_start . '\|' . s:block_middle . '\)$' . '\|' . s:arrow
let s:deindent_keywords = '^\s*\<\%(' . s:block_end . '\|' . s:block_middle . '\)\>' . '\|' . s:arrow
2013-09-12 11:31:56 -04:00
2014-01-24 12:06:22 -05:00
function! GetElixirIndent()
2013-09-12 11:31:56 -04:00
let lnum = prevnonblank(v:lnum - 1)
let ind = indent(lnum)
" At the start of the file use zero indent.
if lnum == 0
return 0
endif
2014-01-24 12:06:22 -05:00
" TODO: Remove these 2 lines
" I don't know why, but for the test on spec/indent/lists_spec.rb:24.
" Vim is making some mess on parsing the syntax of 'end', it is being
" recognized as 'elixirString' when should be recognized as 'elixirBlock'.
" This forces vim to sync the syntax.
call synID(v:lnum, 1, 1)
syntax sync fromstart
2013-11-02 18:27:57 -04:00
if synIDattr(synID(v:lnum, 1, 1), "name") !~ s:skip_syntax
2014-01-09 05:59:09 -05:00
let current_line = getline(v:lnum)
let last_line = getline(lnum)
let splited_line = split(last_line, '\zs')
let opened_symbol = 0
2013-09-18 16:51:23 -04:00
let opened_symbol += count(splited_line, '[') - count(splited_line, ']')
let opened_symbol += count(splited_line, '{') - count(splited_line, '}')
let ind += opened_symbol * &sw
2014-03-16 09:48:30 -04:00
if current_line =~ '^\s*\(\]\|}\)'
let ind -= &sw
endif
2014-01-09 05:59:09 -05:00
if last_line =~ s:indent_keywords
2013-09-12 11:31:56 -04:00
let ind += &sw
endif
2014-12-09 17:09:20 -05:00
" if line starts with pipeline
" and last line contains pipeline(s)
" align them
if last_line =~ '|>.*$' &&
\ current_line =~ s:pipeline
let ind = float2nr(match(last_line, '|>') / &sw) * &sw
2013-11-02 18:27:57 -04:00
" if line starts with pipeline
2014-01-09 05:59:09 -05:00
" and last line is an attribution
" indents pipeline in same level as attribution
2014-12-09 17:09:20 -05:00
elseif current_line =~ s:pipeline &&
2014-01-09 05:59:09 -05:00
\ last_line =~ '^[^=]\+=.\+$'
let b:old_ind = ind
2014-01-24 12:06:22 -05:00
let ind = float2nr(matchend(last_line, '=\s*[^ ]') / &sw) * &sw
2013-11-02 18:27:57 -04:00
endif
" if last line starts with pipeline
" and current line doesn't start with pipeline
2014-01-09 05:59:09 -05:00
" returns the indentation before the pipeline
if last_line =~ s:pipeline &&
\ current_line !~ s:pipeline
let ind = b:old_ind
2013-11-02 18:27:57 -04:00
endif
2014-01-09 05:59:09 -05:00
if current_line =~ s:deindent_keywords
2013-09-12 11:31:56 -04:00
let bslnum = searchpair( '\<\%(' . s:block_start . '\):\@!\>',
\ '\<\%(' . s:block_middle . '\):\@!\>\zs',
\ '\<:\@<!' . s:block_end . '\>\zs',
\ 'nbW',
\ s:block_skip )
2014-01-24 12:06:22 -05:00
2013-09-12 11:31:56 -04:00
let ind = indent(bslnum)
endif
2014-01-09 05:59:09 -05:00
" indent case statements '->'
if current_line =~ s:arrow
let ind += &sw
endif
2013-09-12 11:31:56 -04:00
endif
return ind
endfunction
let &cpo = s:cpo_save
unlet s:cpo_save