vim-polyglot/autoload/elixir/indent.vim

348 lines
12 KiB
VimL
Raw Permalink Normal View History

if exists('g:polyglot_disabled') && index(g:polyglot_disabled, 'elixir') != -1
finish
endif
2017-09-27 13:57:29 -04:00
if !exists("g:elixir_indent_max_lookbehind")
let g:elixir_indent_max_lookbehind = 30
endif
" Return the effective value of 'shiftwidth'
function! s:sw()
return &shiftwidth == 0 ? &tabstop : &shiftwidth
endfunction
function! elixir#indent#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 s:debug("==> Indenting line " . lnum)
call s:debug("text = '" . text . "'")
2018-02-05 22:15:01 -05:00
let [_, curs_lnum, curs_col, _] = getpos('.')
call cursor(lnum, 0)
2017-09-27 13:57:29 -04:00
let handlers = [
\'top_of_file',
\'following_trailing_binary_operator',
\'starts_with_pipe',
\'starts_with_binary_operator',
2018-10-08 13:00:59 -04:00
\'inside_block',
\'starts_with_end',
2017-09-27 13:57:29 -04:00
\'inside_generic_block',
\'follow_prev_nb'
\]
for handler in handlers
call s:debug('testing handler elixir#indent#handle_'.handler)
2018-10-08 13:00:59 -04:00
let context = {'lnum': lnum, 'text': text, 'prev_nb_lnum': prev_nb_lnum, 'prev_nb_text': prev_nb_text}
let indent = function('elixir#indent#handle_'.handler)(context)
2017-09-27 13:57:29 -04:00
if indent != -1
call s:debug('line '.lnum.': elixir#indent#handle_'.handler.' returned '.indent)
2018-02-05 22:15:01 -05:00
call cursor(curs_lnum, curs_col)
2017-09-27 13:57:29 -04:00
return indent
endif
endfor
call s:debug("defaulting")
2018-02-05 22:15:01 -05:00
call cursor(curs_lnum, curs_col)
2017-09-27 13:57:29 -04:00
return 0
endfunction
function! s:debug(str)
2017-05-17 05:07:28 -04:00
if exists("g:elixir_indent_debug") && g:elixir_indent_debug
echom a:str
endif
2016-12-20 14:57:20 -05:00
endfunction
2018-10-08 13:00:59 -04:00
function! s:starts_with(context, expr)
return s:_starts_with(a:context.text, a:expr, a:context.lnum)
endfunction
function! s:prev_starts_with(context, expr)
return s:_starts_with(a:context.prev_nb_text, a:expr, a:context.prev_nb_lnum)
endfunction
2017-05-17 05:07:28 -04:00
" Returns 0 or 1 based on whether or not the text starts with the given
" expression and is not a string or comment
2018-10-08 13:00:59 -04:00
function! s:_starts_with(text, expr, lnum)
2017-05-17 05:07:28 -04:00
let pos = match(a:text, '^\s*'.a:expr)
if pos == -1
return 0
else
" NOTE: @jbodah 2017-02-24: pos is the index of the match which is
" zero-indexed. Add one to make it the column number
2017-09-27 13:57:29 -04:00
if s:is_string_or_comment(a:lnum, pos + 1)
2017-05-17 05:07:28 -04:00
return 0
else
return 1
end
2016-12-20 14:57:20 -05:00
end
endfunction
2018-10-08 13:00:59 -04:00
function! s:prev_ends_with(context, expr)
return s:_ends_with(a:context.prev_nb_text, a:expr, a:context.prev_nb_lnum)
endfunction
2017-05-17 05:07:28 -04:00
" Returns 0 or 1 based on whether or not the text ends with the given
" expression and is not a string or comment
2018-10-08 13:00:59 -04:00
function! s:_ends_with(text, expr, lnum)
2017-05-17 05:07:28 -04:00
let pos = match(a:text, a:expr.'\s*$')
if pos == -1
return 0
else
2017-09-27 13:57:29 -04:00
if s:is_string_or_comment(a:lnum, pos)
2017-05-17 05:07:28 -04:00
return 0
else
return 1
end
2016-12-20 14:57:20 -05:00
end
endfunction
2017-05-17 05:07:28 -04:00
" Returns 0 or 1 based on whether or not the given line number and column
" number pair is a string or comment
2017-09-27 13:57:29 -04:00
function! s:is_string_or_comment(line, col)
2017-05-17 05:07:28 -04:00
return synIDattr(synID(a:line, a:col, 1), "name") =~ '\%(String\|Comment\)'
2016-12-20 14:57:20 -05:00
endfunction
2017-05-17 05:07:28 -04:00
" Skip expression for searchpair. Returns 0 or 1 based on whether the value
" under the cursor is a string or comment
function! elixir#indent#searchpair_back_skip()
" NOTE: @jbodah 2017-02-27: for some reason this function gets called with
" and index that doesn't exist in the line sometimes. Detect and account for
" that situation
let curr_col = col('.')
if getline('.')[curr_col-1] == ''
let curr_col = curr_col-1
endif
2017-09-27 13:57:29 -04:00
return s:is_string_or_comment(line('.'), curr_col)
2017-05-17 05:07:28 -04:00
endfunction
" DRY up regex for keywords that 1) makes sure we only look at complete words
" and 2) ignores atoms
2017-09-27 13:57:29 -04:00
function! s:keyword(expr)
2017-05-17 05:07:28 -04:00
return ':\@<!\<\C\%('.a:expr.'\)\>:\@!'
endfunction
" Start at the end of text and search backwards looking for a match. Also peek
" ahead if we get a match to make sure we get a complete match. This means
" that the result should be the position of the start of the right-most match
2017-09-27 13:57:29 -04:00
function! s:find_last_pos(lnum, text, match)
2017-05-17 05:07:28 -04:00
let last = len(a:text) - 1
let c = last
while c >= 0
let substr = strpart(a:text, c, last)
let peek = strpart(a:text, c - 1, last)
let ss_match = match(substr, a:match)
if ss_match != -1
let peek_match = match(peek, a:match)
if peek_match == ss_match + 1
let syng = synIDattr(synID(a:lnum, c + ss_match, 1), 'name')
if syng !~ '\%(String\|Comment\)'
return c + ss_match
end
end
end
let c -= 1
endwhile
return -1
endfunction
2018-10-08 13:00:59 -04:00
function! elixir#indent#handle_top_of_file(context)
if a:context.prev_nb_lnum == 0
2017-05-17 05:07:28 -04:00
return 0
2016-12-20 14:57:20 -05:00
else
2017-05-17 05:07:28 -04:00
return -1
2016-12-20 14:57:20 -05:00
end
endfunction
2018-10-08 13:00:59 -04:00
function! elixir#indent#handle_follow_prev_nb(context)
return s:get_base_indent(a:context.prev_nb_lnum, a:context.prev_nb_text)
2017-09-27 13:57:29 -04:00
endfunction
" Given the line at `lnum`, returns the indent of the line that acts as the 'base indent'
" for this line. In particular it traverses backwards up things like pipelines
" to find the beginning of the expression
function! s:get_base_indent(lnum, text)
let prev_nb_lnum = prevnonblank(a:lnum - 1)
let prev_nb_text = getline(prev_nb_lnum)
let binary_operator = '\%(=\|<>\|>>>\|<=\|||\|+\|\~\~\~\|-\|&&\|<<<\|/\|\^\^\^\|\*\)'
let data_structure_close = '\%(\]\|}\|)\)'
let pipe = '|>'
2018-10-08 13:00:59 -04:00
if s:_starts_with(a:text, binary_operator, a:lnum)
2017-09-27 13:57:29 -04:00
return s:get_base_indent(prev_nb_lnum, prev_nb_text)
2018-10-08 13:00:59 -04:00
elseif s:_starts_with(a:text, pipe, a:lnum)
2017-09-27 13:57:29 -04:00
return s:get_base_indent(prev_nb_lnum, prev_nb_text)
2018-10-08 13:00:59 -04:00
elseif s:_ends_with(prev_nb_text, binary_operator, prev_nb_lnum)
2017-09-27 13:57:29 -04:00
return s:get_base_indent(prev_nb_lnum, prev_nb_text)
2018-10-08 13:00:59 -04:00
elseif s:_ends_with(a:text, data_structure_close, a:lnum)
2017-09-27 13:57:29 -04:00
let data_structure_open = '\%(\[\|{\|(\)'
let close_match_idx = match(a:text, data_structure_close . '\s*$')
2018-02-05 22:15:01 -05:00
call cursor(a:lnum, close_match_idx + 1)
2017-09-27 13:57:29 -04:00
let [open_match_lnum, open_match_col] = searchpairpos(data_structure_open, '', data_structure_close, 'bnW')
let open_match_text = getline(open_match_lnum)
return s:get_base_indent(open_match_lnum, open_match_text)
else
return indent(a:lnum)
endif
endfunction
2018-10-08 13:00:59 -04:00
function! elixir#indent#handle_following_trailing_binary_operator(context)
2017-05-17 05:07:28 -04:00
let binary_operator = '\%(=\|<>\|>>>\|<=\|||\|+\|\~\~\~\|-\|&&\|<<<\|/\|\^\^\^\|\*\)'
2018-10-08 13:00:59 -04:00
if s:prev_ends_with(a:context, binary_operator)
return indent(a:context.prev_nb_lnum) + s:sw()
2017-09-27 13:57:29 -04:00
else
return -1
endif
endfunction
2018-10-08 13:00:59 -04:00
function! elixir#indent#handle_starts_with_pipe(context)
if s:starts_with(a:context, '|>')
2017-05-17 05:07:28 -04:00
let match_operator = '\%(!\|=\|<\|>\)\@<!=\%(=\|>\|\~\)\@!'
2018-10-08 13:00:59 -04:00
let pos = s:find_last_pos(a:context.prev_nb_lnum, a:context.prev_nb_text, match_operator)
2017-05-17 05:07:28 -04:00
if pos == -1
2018-10-08 13:00:59 -04:00
return indent(a:context.prev_nb_lnum)
2017-05-17 05:07:28 -04:00
else
2018-10-08 13:00:59 -04:00
let next_word_pos = match(strpart(a:context.prev_nb_text, pos+1, len(a:context.prev_nb_text)-1), '\S')
2017-05-17 05:07:28 -04:00
if next_word_pos == -1
2018-10-08 13:00:59 -04:00
return indent(a:context.prev_nb_lnum) + s:sw()
2017-05-17 05:07:28 -04:00
else
return pos + 1 + next_word_pos
end
2016-12-20 14:57:20 -05:00
end
2017-05-17 05:07:28 -04:00
else
return -1
endif
endfunction
2016-12-20 14:57:20 -05:00
2018-10-08 13:00:59 -04:00
function! elixir#indent#handle_starts_with_end(context)
if s:starts_with(a:context, s:keyword('end'))
2017-09-27 13:57:29 -04:00
let pair_lnum = searchpair(s:keyword('do\|fn'), '', s:keyword('end').'\zs', 'bnW', "line('.') == " . line('.') . " || elixir#indent#searchpair_back_skip()")
2017-05-17 05:07:28 -04:00
return indent(pair_lnum)
2016-12-20 14:57:20 -05:00
else
2017-05-17 05:07:28 -04:00
return -1
endif
2016-12-20 14:57:20 -05:00
endfunction
2018-10-08 13:00:59 -04:00
function! elixir#indent#handle_starts_with_binary_operator(context)
2017-05-17 05:07:28 -04:00
let binary_operator = '\%(=\|<>\|>>>\|<=\|||\|+\|\~\~\~\|-\|&&\|<<<\|/\|\^\^\^\|\*\)'
2018-10-08 13:00:59 -04:00
if s:starts_with(a:context, binary_operator)
2017-05-17 05:07:28 -04:00
let match_operator = '\%(!\|=\|<\|>\)\@<!=\%(=\|>\|\~\)\@!'
2018-10-08 13:00:59 -04:00
let pos = s:find_last_pos(a:context.prev_nb_lnum, a:context.prev_nb_text, match_operator)
2017-05-17 05:07:28 -04:00
if pos == -1
2018-10-08 13:00:59 -04:00
return indent(a:context.prev_nb_lnum)
2017-05-17 05:07:28 -04:00
else
2018-10-08 13:00:59 -04:00
let next_word_pos = match(strpart(a:context.prev_nb_text, pos+1, len(a:context.prev_nb_text)-1), '\S')
2017-05-17 05:07:28 -04:00
if next_word_pos == -1
2018-10-08 13:00:59 -04:00
return indent(a:context.prev_nb_lnum) + s:sw()
2017-05-17 05:07:28 -04:00
else
return pos + 1 + next_word_pos
end
end
2016-12-20 14:57:20 -05:00
else
2017-05-17 05:07:28 -04:00
return -1
endif
2016-12-20 14:57:20 -05:00
endfunction
2017-05-17 05:07:28 -04:00
" To handle nested structures properly we need to find the innermost
" nested structure. For example, we might be in a function in a map in a
" function, etc... so we need to first figure out what the innermost structure
" is then forward execution to the proper handler
2018-10-08 13:00:59 -04:00
function! elixir#indent#handle_inside_block(context)
2017-09-27 13:57:29 -04:00
let start_pattern = '\C\%(\<with\>\|\<if\>\|\<case\>\|\<cond\>\|\<try\>\|\<receive\>\|\<fn\>\|{\|\[\|(\)'
2017-05-17 05:07:28 -04:00
let end_pattern = '\C\%(\<end\>\|\]\|}\|)\)'
2018-10-08 13:00:59 -04:00
" hack - handle do: better
let block_info = searchpairpos(start_pattern, '', end_pattern, 'bnW', "line('.') == " . line('.') . " || elixir#indent#searchpair_back_skip() || getline(line('.')) =~ 'do:'", max([0, a:context.lnum - g:elixir_indent_max_lookbehind]))
let block_start_lnum = block_info[0]
let block_start_col = block_info[1]
if block_start_lnum != 0 || block_start_col != 0
let block_text = getline(block_start_lnum)
let block_start_char = block_text[block_start_col - 1]
let never_match = '\(a\)\@=b'
let config = {
\'f': {'aligned_clauses': s:keyword('end'), 'pattern_match_clauses': never_match},
\'c': {'aligned_clauses': s:keyword('end'), 'pattern_match_clauses': never_match},
2019-03-04 03:15:44 -05:00
\'t': {'aligned_clauses': s:keyword('end\|catch\|rescue\|after\|else'), 'pattern_match_clauses': s:keyword('catch\|rescue\|else')},
2018-10-08 13:00:59 -04:00
\'r': {'aligned_clauses': s:keyword('end\|after'), 'pattern_match_clauses': s:keyword('after')},
\'i': {'aligned_clauses': s:keyword('end\|else'), 'pattern_match_clauses': never_match},
\'[': {'aligned_clauses': ']', 'pattern_match_clauses': never_match},
\'{': {'aligned_clauses': '}', 'pattern_match_clauses': never_match},
\'(': {'aligned_clauses': ')', 'pattern_match_clauses': never_match}
\}
if block_start_char == 'w'
call s:debug("testing s:handle_with")
return s:handle_with(block_start_lnum, block_start_col, a:context)
2017-05-17 05:07:28 -04:00
else
2018-10-08 13:00:59 -04:00
let block_config = config[block_start_char]
if s:starts_with(a:context, block_config.aligned_clauses)
call s:debug("clause")
return indent(block_start_lnum)
else
let clause_lnum = searchpair(block_config.pattern_match_clauses, '', '*', 'bnW', "line('.') == " . line('.') . " || elixir#indent#searchpair_back_skip()", block_start_lnum)
let relative_lnum = max([clause_lnum, block_start_lnum])
call s:debug("pattern matching relative to lnum " . relative_lnum)
return s:do_handle_pattern_match_block(relative_lnum, a:context)
endif
2017-05-17 05:07:28 -04:00
end
2016-12-20 14:57:20 -05:00
else
2017-05-17 05:07:28 -04:00
return -1
2016-12-20 14:57:20 -05:00
end
endfunction
2018-10-08 13:00:59 -04:00
function! s:handle_with(start_lnum, start_col, context)
let block_info = searchpairpos('\C\%(\<with\>\|\<do\>\|\<else\>\)', '', s:keyword('end'), 'bnW', "line('.') == " . line('.') . " || elixir#indent#searchpair_back_skip()")
let block_start_lnum = block_info[0]
let block_start_col = block_info[1]
2017-09-27 13:57:29 -04:00
2018-10-08 13:00:59 -04:00
let block_start_text = getline(block_start_lnum)
let block_start_char = block_start_text[block_start_col - 1]
2016-12-20 14:57:20 -05:00
2018-10-08 13:00:59 -04:00
if s:starts_with(a:context, s:keyword('do\|else\|end'))
return indent(a:start_lnum)
elseif block_start_char == 'w' || s:starts_with(a:context, '\C\(do\|else\):')
return indent(a:start_lnum) + 5
elseif s:_starts_with(block_start_text, '\C\(do\|else\):', a:start_lnum)
return indent(block_start_lnum) + s:sw()
2017-09-27 13:57:29 -04:00
else
2018-10-08 13:00:59 -04:00
return s:do_handle_pattern_match_block(a:start_lnum, a:context)
2017-09-27 13:57:29 -04:00
end
endfunction
2018-10-08 13:00:59 -04:00
function! s:do_handle_pattern_match_block(relative_line, context)
let relative_indent = indent(a:relative_line)
" hack!
if a:context.text =~ '\(fn.*\)\@<!->'
call s:debug("current line contains ->; assuming match definition")
return relative_indent + s:sw()
elseif search('\(fn.*\)\@<!->', 'bnW', a:relative_line) != 0
call s:debug("a previous line contains ->; assuming match handler")
return relative_indent + 2 * s:sw()
2016-12-20 14:57:20 -05:00
else
2018-10-08 13:00:59 -04:00
call s:debug("couldn't find any previous ->; assuming body text")
return relative_indent + s:sw()
2016-12-20 14:57:20 -05:00
end
endfunction
2018-10-08 13:00:59 -04:00
function! elixir#indent#handle_inside_generic_block(context)
let pair_lnum = searchpair(s:keyword('do\|fn'), '', s:keyword('end'), 'bW', "line('.') == ".a:context.lnum." || s:is_string_or_comment(line('.'), col('.'))", max([0, a:context.lnum - g:elixir_indent_max_lookbehind]))
2017-05-17 05:07:28 -04:00
if pair_lnum
" TODO: @jbodah 2017-03-29: this should probably be the case in *all*
" blocks
2018-10-08 13:00:59 -04:00
if s:prev_ends_with(a:context, ',')
2017-09-27 13:57:29 -04:00
return indent(pair_lnum) + 2 * s:sw()
2017-05-17 05:07:28 -04:00
else
2017-09-27 13:57:29 -04:00
return indent(pair_lnum) + s:sw()
2017-05-17 05:07:28 -04:00
endif
2016-12-20 14:57:20 -05:00
else
2017-05-17 05:07:28 -04:00
return -1
endif
2016-12-20 14:57:20 -05:00
endfunction