vim-polyglot/indent/elixir.vim

75 lines
2.4 KiB
VimL
Raw Normal View History

if !exists('g:polyglot_disabled') || index(g:polyglot_disabled, 'elixir') == -1
2013-09-12 17:31:56 +02:00
setlocal nosmartindent
2016-12-20 20:57:20 +01:00
setlocal indentexpr=elixir#indent()
setlocal indentkeys+=0),0],0=\|>,=->
setlocal indentkeys+=0=end,0=else,0=match,0=elsif,0=catch,0=after,0=rescue
2013-09-12 17:31:56 +02:00
2016-12-20 20:57:20 +01:00
if exists("b:did_indent") || exists("*elixir#indent")
2013-09-12 17:31:56 +02:00
finish
2016-09-11 13:24:17 +02:00
end
2016-12-20 20:57:20 +01:00
let b:did_indent = 1
2013-09-12 17:31:56 +02:00
let s:cpo_save = &cpo
set cpo&vim
2016-12-20 20:57:20 +01:00
function! elixir#indent()
" initiates the `old_ind` dictionary
let b:old_ind = get(b:, 'old_ind', {})
" initialtes the `line` dictionary
let line = s:build_line(v:lnum)
2014-12-09 23:09:20 +01:00
2016-12-20 20:57:20 +01:00
if s:is_beginning_of_file(line)
" Reset `old_ind` dictionary at the beginning of the file
let b:old_ind = {}
" At the start of the file use zero indent.
return 0
elseif !s:is_indentable_line(line)
" Keep last line indentation if the current line does not have an
" indentable syntax
return indent(line.last.num)
2016-09-11 13:24:17 +02:00
else
2016-12-20 20:57:20 +01:00
" Calculates the indenation level based on the rules
" All the rules are defined in `autoload/indent.vim`
let ind = indent(line.last.num)
let ind = elixir#indent#deindent_case_arrow(ind, line)
let ind = elixir#indent#indent_parenthesis(ind, line)
let ind = elixir#indent#indent_square_brackets(ind, line)
let ind = elixir#indent#indent_brackets(ind, line)
let ind = elixir#indent#deindent_opened_symbols(ind, line)
let ind = elixir#indent#indent_pipeline_assignment(ind, line)
let ind = elixir#indent#indent_pipeline_continuation(ind, line)
let ind = elixir#indent#indent_after_pipeline(ind, line)
let ind = elixir#indent#indent_assignment(ind, line)
let ind = elixir#indent#indent_ending_symbols(ind, line)
let ind = elixir#indent#indent_keywords(ind, line)
let ind = elixir#indent#deindent_keywords(ind, line)
let ind = elixir#indent#deindent_ending_symbols(ind, line)
let ind = elixir#indent#indent_case_arrow(ind, line)
return ind
2016-09-11 13:24:17 +02:00
end
endfunction
2016-12-20 20:57:20 +01:00
function! s:is_beginning_of_file(line)
return a:line.last.num == 0
2016-09-11 13:24:17 +02:00
endfunction
2016-12-20 20:57:20 +01:00
function! s:is_indentable_line(line)
return elixir#util#is_indentable_at(a:line.current.num, 1)
2016-09-11 13:24:17 +02:00
endfunction
2014-01-09 11:59:09 +01:00
2016-12-20 20:57:20 +01:00
function! s:build_line(line)
let line = { 'current': {}, 'last': {} }
let line.current.num = a:line
let line.current.text = getline(line.current.num)
let line.last.num = prevnonblank(line.current.num - 1)
let line.last.text = getline(line.last.num)
2016-09-11 13:24:17 +02:00
2016-12-20 20:57:20 +01:00
return line
2013-09-12 17:31:56 +02:00
endfunction
let &cpo = s:cpo_save
unlet s:cpo_save
endif