mirror of
https://github.com/sheerun/vim-polyglot.git
synced 2025-11-11 13:03:50 -05:00
Update
This commit is contained in:
@@ -3,7 +3,6 @@ if !exists('g:polyglot_disabled') || index(g:polyglot_disabled, 'elixir') == -1
|
||||
let s:NO_COLON_BEFORE = ':\@<!'
|
||||
let s:NO_COLON_AFTER = ':\@!'
|
||||
let s:ENDING_SYMBOLS = '\]\|}\|)'
|
||||
let s:STARTING_SYMBOLS = '\[\|{\|('
|
||||
let s:ARROW = '->'
|
||||
let s:END_WITH_ARROW = s:ARROW.'$'
|
||||
let s:SKIP_SYNTAX = '\%(Comment\|String\)$'
|
||||
@@ -16,28 +15,31 @@ let s:MULTILINE_BLOCK = '\%(\<do\>'.s:NO_COLON_AFTER.'\|'.s:MULTILINE_FN.'\)'
|
||||
let s:BLOCK_MIDDLE = '\<\%(else\|match\|elsif\|catch\|after\|rescue\)\>'
|
||||
let s:BLOCK_END = 'end'
|
||||
let s:STARTS_WITH_PIPELINE = '^\s*|>.*$'
|
||||
let s:QUERY_FROM = '^\s*\<from\>.*\<in\>.*,'
|
||||
let s:ENDING_WITH_ASSIGNMENT = '=\s*$'
|
||||
let s:INDENT_KEYWORDS = s:NO_COLON_BEFORE.'\%('.s:MULTILINE_BLOCK.'\|'.s:BLOCK_MIDDLE.'\)'
|
||||
let s:DEINDENT_KEYWORDS = '^\s*\<\%('.s:BLOCK_END.'\|'.s:BLOCK_MIDDLE.'\)\>'
|
||||
let s:PAIR_START = '\<\%('.s:NO_COLON_BEFORE.s:BLOCK_START.'\)\>'.s:NO_COLON_AFTER
|
||||
let s:PAIR_MIDDLE = '^\s*\%('.s:BLOCK_MIDDLE.'\)\>'.s:NO_COLON_AFTER.'\zs'
|
||||
let s:PAIR_END = '\<\%('.s:NO_COLON_BEFORE.s:BLOCK_END.'\)\>\zs'
|
||||
let s:LINE_COMMENT = '^\s*#'
|
||||
let s:MATCH_OPERATOR = '[^!><=]=[^~=>]'
|
||||
|
||||
function! s:pending_parenthesis(line)
|
||||
if a:line.last.text !~ s:ARROW
|
||||
return elixir#util#count_indentable_symbol_diff(a:line.last, '(', '\%(end\s*\)\@<!)')
|
||||
if a:line.last_non_blank.text !~ s:ARROW
|
||||
return elixir#util#count_indentable_symbol_diff(a:line.last_non_blank, '(', '\%(end\s*\)\@<!)')
|
||||
end
|
||||
endfunction
|
||||
|
||||
function! s:pending_square_brackets(line)
|
||||
if a:line.last.text !~ s:ARROW
|
||||
return elixir#util#count_indentable_symbol_diff(a:line.last, '[', ']')
|
||||
if a:line.last_non_blank.text !~ s:ARROW
|
||||
return elixir#util#count_indentable_symbol_diff(a:line.last_non_blank, '[', ']')
|
||||
end
|
||||
endfunction
|
||||
|
||||
function! s:pending_brackets(line)
|
||||
if a:line.last.text !~ s:ARROW
|
||||
return elixir#util#count_indentable_symbol_diff(a:line.last, '{', '}')
|
||||
if a:line.last_non_blank.text !~ s:ARROW
|
||||
return elixir#util#count_indentable_symbol_diff(a:line.last_non_blank, '{', '}')
|
||||
end
|
||||
endfunction
|
||||
|
||||
@@ -93,13 +95,21 @@ function! elixir#indent#deindent_opened_symbols(ind, line)
|
||||
endfunction
|
||||
|
||||
function! elixir#indent#indent_after_pipeline(ind, line)
|
||||
if a:line.last.text =~ s:STARTS_WITH_PIPELINE
|
||||
if exists("b:old_ind.pipeline")
|
||||
\ && elixir#util#is_blank(a:line.last.text)
|
||||
\ && a:line.current.text !~ s:STARTS_WITH_PIPELINE
|
||||
" Reset indentation in pipelines if there is a blank line between
|
||||
" pipes
|
||||
let ind = b:old_ind.pipeline
|
||||
unlet b:old_ind.pipeline
|
||||
return ind
|
||||
elseif a:line.last_non_blank.text =~ s:STARTS_WITH_PIPELINE
|
||||
if empty(substitute(a:line.current.text, ' ', '', 'g'))
|
||||
\ || a:line.current.text =~ s:STARTS_WITH_PIPELINE
|
||||
return indent(a:line.last.num)
|
||||
elseif a:line.last.text !~ s:INDENT_KEYWORDS
|
||||
return indent(a:line.last_non_blank.num)
|
||||
elseif a:line.last_non_blank.text !~ s:INDENT_KEYWORDS
|
||||
let ind = b:old_ind.pipeline
|
||||
let b:old_ind.pipeline = 0
|
||||
unlet b:old_ind.pipeline
|
||||
return ind
|
||||
end
|
||||
end
|
||||
@@ -108,8 +118,8 @@ function! elixir#indent#indent_after_pipeline(ind, line)
|
||||
endfunction
|
||||
|
||||
function! elixir#indent#indent_assignment(ind, line)
|
||||
if a:line.last.text =~ s:ENDING_WITH_ASSIGNMENT
|
||||
let b:old_ind.pipeline = indent(a:line.last.num) " FIXME: side effect
|
||||
if a:line.last_non_blank.text =~ s:ENDING_WITH_ASSIGNMENT
|
||||
let b:old_ind.pipeline = indent(a:line.last_non_blank.num) " FIXME: side effect
|
||||
return a:ind + &sw
|
||||
else
|
||||
return a:ind
|
||||
@@ -125,7 +135,7 @@ function! elixir#indent#indent_brackets(ind, line)
|
||||
endfunction
|
||||
|
||||
function! elixir#indent#indent_case_arrow(ind, line)
|
||||
if a:line.last.text =~ s:END_WITH_ARROW && a:line.last.text !~ '\<fn\>'
|
||||
if a:line.last_non_blank.text =~ s:END_WITH_ARROW && a:line.last_non_blank.text !~ '\<fn\>'
|
||||
let b:old_ind.arrow = a:ind
|
||||
return a:ind + &sw
|
||||
else
|
||||
@@ -134,7 +144,7 @@ function! elixir#indent#indent_case_arrow(ind, line)
|
||||
endfunction
|
||||
|
||||
function! elixir#indent#indent_ending_symbols(ind, line)
|
||||
if a:line.last.text =~ '^\s*\('.s:ENDING_SYMBOLS.'\)\s*$'
|
||||
if a:line.last_non_blank.text =~ '^\s*\('.s:ENDING_SYMBOLS.'\)\s*$'
|
||||
return a:ind + &sw
|
||||
else
|
||||
return a:ind
|
||||
@@ -142,7 +152,7 @@ function! elixir#indent#indent_ending_symbols(ind, line)
|
||||
endfunction
|
||||
|
||||
function! elixir#indent#indent_keywords(ind, line)
|
||||
if a:line.last.text =~ s:INDENT_KEYWORDS
|
||||
if a:line.last_non_blank.text =~ s:INDENT_KEYWORDS && a:line.last_non_blank.text !~ s:LINE_COMMENT
|
||||
return a:ind + &sw
|
||||
else
|
||||
return a:ind
|
||||
@@ -151,10 +161,10 @@ endfunction
|
||||
|
||||
function! elixir#indent#indent_parenthesis(ind, line)
|
||||
if s:pending_parenthesis(a:line) > 0
|
||||
\ && a:line.last.text !~ s:DEF
|
||||
\ && a:line.last.text !~ s:END_WITH_ARROW
|
||||
\ && a:line.last_non_blank.text !~ s:DEF
|
||||
\ && a:line.last_non_blank.text !~ s:END_WITH_ARROW
|
||||
let b:old_ind.symbol = a:ind
|
||||
return matchend(a:line.last.text, '(')
|
||||
return matchend(a:line.last_non_blank.text, '(')
|
||||
else
|
||||
return a:ind
|
||||
end
|
||||
@@ -162,21 +172,22 @@ endfunction
|
||||
|
||||
function! elixir#indent#indent_pipeline_assignment(ind, line)
|
||||
if a:line.current.text =~ s:STARTS_WITH_PIPELINE
|
||||
\ && a:line.last.text =~ '^[^=]\+=.\+$'
|
||||
let b:old_ind.pipeline = indent(a:line.last.num)
|
||||
\ && a:line.last_non_blank.text =~ s:MATCH_OPERATOR
|
||||
let b:old_ind.pipeline = indent(a:line.last_non_blank.num)
|
||||
" if line starts with pipeline
|
||||
" and last line is an attribution
|
||||
" and last_non_blank line is an attribution
|
||||
" indents pipeline in same level as attribution
|
||||
return match(a:line.last.text, '=\s*\zs[^ ]')
|
||||
let assign_pos = match(a:line.last_non_blank.text, '=\s*\zs[^ ]')
|
||||
return (elixir#util#is_indentable_at(a:line.last_non_blank.num, assign_pos) ? assign_pos : a:ind)
|
||||
else
|
||||
return a:ind
|
||||
end
|
||||
endfunction
|
||||
|
||||
function! elixir#indent#indent_pipeline_continuation(ind, line)
|
||||
if a:line.last.text =~ s:STARTS_WITH_PIPELINE
|
||||
if a:line.last_non_blank.text =~ s:STARTS_WITH_PIPELINE
|
||||
\ && a:line.current.text =~ s:STARTS_WITH_PIPELINE
|
||||
return indent(a:line.last.num)
|
||||
return indent(a:line.last_non_blank.num)
|
||||
else
|
||||
return a:ind
|
||||
end
|
||||
@@ -184,26 +195,22 @@ endfunction
|
||||
|
||||
function! elixir#indent#indent_square_brackets(ind, line)
|
||||
if s:pending_square_brackets(a:line) > 0
|
||||
if a:line.last.text =~ '[\s*$'
|
||||
if a:line.last_non_blank.text =~ '[\s*$'
|
||||
return a:ind + &sw
|
||||
else
|
||||
" if start symbol is followed by a character, indent based on the
|
||||
" whitespace after the symbol, otherwise use the default shiftwidth
|
||||
" Avoid negative indentation index
|
||||
return matchend(a:line.last.text, '[\s*')
|
||||
return matchend(a:line.last_non_blank.text, '[\s*')
|
||||
end
|
||||
else
|
||||
return a:ind
|
||||
end
|
||||
endfunction
|
||||
|
||||
function! elixir#indent#deindent_case_arrow(ind, line)
|
||||
if get(b:old_ind, 'arrow', 0) > 0
|
||||
\ && (a:line.current.text =~ s:ARROW
|
||||
\ || a:line.current.text =~ s:BLOCK_END)
|
||||
let ind = b:old_ind.arrow
|
||||
let b:old_ind.arrow = 0
|
||||
return ind
|
||||
function! elixir#indent#indent_ecto_queries(ind, line)
|
||||
if a:line.last_non_blank.text =~ s:QUERY_FROM
|
||||
return a:ind + &sw
|
||||
else
|
||||
return a:ind
|
||||
end
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
if !exists('g:polyglot_disabled') || index(g:polyglot_disabled, 'elixir') == -1
|
||||
|
||||
let s:SKIP_SYNTAX = '\%(Comment\|String\)$'
|
||||
let s:BLOCK_SKIP = "synIDattr(synID(line('.'),col('.'),1),'name') =~? '".s:SKIP_SYNTAX."'"
|
||||
|
||||
function! elixir#util#is_indentable_at(line, col)
|
||||
if a:col == -1 " skip synID lookup for not found match
|
||||
@@ -20,31 +19,24 @@ function! elixir#util#is_indentable_at(line, col)
|
||||
\ !~ s:SKIP_SYNTAX
|
||||
endfunction
|
||||
|
||||
function! elixir#util#is_indentable_match(line, pattern)
|
||||
return elixir#util#is_indentable_at(a:line.num, match(a:line.text, a:pattern))
|
||||
endfunction
|
||||
|
||||
function! elixir#util#count_indentable_symbol_diff(line, open, close)
|
||||
if elixir#util#is_indentable_match(a:line, a:open)
|
||||
\ && elixir#util#is_indentable_match(a:line, a:close)
|
||||
return
|
||||
\ s:match_count(a:line.text, a:open)
|
||||
\ - s:match_count(a:line.text, a:close)
|
||||
else
|
||||
return 0
|
||||
end
|
||||
return
|
||||
\ s:match_count(a:line, a:open)
|
||||
\ - s:match_count(a:line, a:close)
|
||||
endfunction
|
||||
|
||||
function! s:match_count(string, pattern)
|
||||
let size = strlen(a:string)
|
||||
function! s:match_count(line, pattern)
|
||||
let size = strlen(a:line.text)
|
||||
let index = 0
|
||||
let counter = 0
|
||||
|
||||
while index < size
|
||||
let index = match(a:string, a:pattern, index)
|
||||
let index = match(a:line.text, a:pattern, index)
|
||||
if index >= 0
|
||||
let index += 1
|
||||
let counter +=1
|
||||
if elixir#util#is_indentable_at(a:line.num, index)
|
||||
let counter +=1
|
||||
end
|
||||
else
|
||||
break
|
||||
end
|
||||
@@ -53,4 +45,8 @@ function! s:match_count(string, pattern)
|
||||
return counter
|
||||
endfunction
|
||||
|
||||
function elixir#util#is_blank(string)
|
||||
return a:string =~ '^\s*$'
|
||||
endfunction
|
||||
|
||||
endif
|
||||
|
||||
Reference in New Issue
Block a user