vim-polyglot/indent/elixir.vim

146 lines
4.4 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-05-02 04:42:37 -04:00
let s:no_colon_before = ':\@<!'
let s:no_colon_after = ':\@!'
let s:symbols_end = '\]\|}\|)'
let s:symbols_start = '\[\|{\|('
let s:arrow = '^.*->$'
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:starts_with_pipeline = '^\s*|>.*$'
let s:ending_with_assignment = '=\s*$'
2016-01-22 03:08:00 -05:00
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
2016-05-02 04:42:37 -04:00
let s:inside_block = 0
2014-01-24 12:06:22 -05:00
function! GetElixirIndent()
2013-09-12 11:31:56 -04:00
let lnum = prevnonblank(v:lnum - 1)
" At the start of the file use zero indent.
if lnum == 0
return 0
endif
2016-05-02 04:42:37 -04:00
let opened_symbol = 0
let current_line = getline(v:lnum)
let last_line = getline(lnum)
let ind = indent(lnum)
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
2016-05-02 04:42:37 -04:00
if last_line !~ s:arrow
let split_line = split(last_line, '\zs')
let opened_symbol += count(split_line, '(') - count(split_line, ')')
let opened_symbol += count(split_line, '[') - count(split_line, ']')
let opened_symbol += count(split_line, '{') - count(split_line, '}')
end
" if start symbol is followed by a character, indent based on the
" whitespace after the symbol, otherwise use the default shiftwidth
if last_line =~ '\('.s:symbols_start.'\).'
let opened_prefix = matchlist(last_line, '\('.s:symbols_start.'\)\s*')[0]
let ind += (opened_symbol * strlen(opened_prefix))
else
let ind += (opened_symbol * &sw)
endif
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
2016-05-02 04:42:37 -04:00
if last_line =~ s:ending_with_assignment && opened_symbol == 0
let b:old_ind = indent(lnum)
let ind += &sw
end
2014-12-09 17:09:20 -05:00
" if line starts with pipeline
2016-05-02 04:42:37 -04:00
" and last line ends with a pipeline,
2014-12-09 17:09:20 -05:00
" align them
if last_line =~ '|>.*$' &&
2016-05-02 04:42:37 -04:00
\ current_line =~ s:starts_with_pipeline
2014-12-09 17:09:20 -05:00
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
2016-05-02 04:42:37 -04:00
elseif current_line =~ s:starts_with_pipeline &&
2014-01-09 05:59:09 -05:00
\ last_line =~ '^[^=]\+=.\+$'
2016-05-02 04:42:37 -04:00
if !exists('b:old_ind') || b:old_ind == 0
let b:old_ind = indent(lnum)
end
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
2016-05-02 04:42:37 -04:00
if last_line =~ s:starts_with_pipeline &&
\ current_line !~ s:starts_with_pipeline
2014-01-09 05:59:09 -05:00
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