vim-polyglot/indent/elixir.vim

123 lines
3.5 KiB
VimL
Raw Normal View History

if !exists('g:polyglot_disabled') || index(g:polyglot_disabled, 'elixir') == -1
2013-09-12 11:31:56 -04:00
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()
2016-01-22 03:08:00 -05:00
setlocal indentkeys+=0),0],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
2016-01-22 03:08:00 -05:00
let s:no_colon_before = ':\@<!'
let s:no_colon_after = ':\@!'
let s:symbols_end = '\]\|}'
let s:arrow = '^.*->$'
let s:pipeline = '^\s*|>.*$'
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'
let s:indent_keywords = '\<'.s:no_colon_before.'\%('.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
2016-01-22 03:08:00 -05:00
let s:pair_start = '\<\%('.s:no_colon_before.s:block_start.'\)\>'.s:no_colon_after
let s:pair_middle = '\<\%('.s:block_middle.'\)\>'.s:no_colon_after.'\zs'
let s:pair_end = '\<\%('.s:no_colon_before.s:block_end.'\)\>\zs'
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, '}')
2016-01-22 03:08:00 -05:00
let ind += (opened_symbol * &sw)
2013-09-18 16:51:23 -04:00
2016-01-22 03:08:00 -05:00
if last_line =~ '^\s*\('.s:symbols_end.'\)' || last_line =~ s:indent_keywords
2015-05-11 09:05:13 -04:00
let ind += &sw
endif
2016-01-22 03:08:00 -05:00
if current_line =~ '^\s*\('.s:symbols_end.'\)'
2014-03-16 09:48:30 -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
2016-01-22 03:08:00 -05:00
let bslnum = searchpair(
\ s:pair_start,
\ s:pair_middle,
\ s:pair_end,
2013-09-12 11:31:56 -04:00
\ 'nbW',
2016-01-22 03:08:00 -05:00
\ 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
endif