Skip to content

Commit

Permalink
Try to make indentation a bit faster
Browse files Browse the repository at this point in the history
- remove metadata variable
- Remove sleep from CI (make tests faster in CI)
  • Loading branch information
kassio committed Sep 7, 2016
1 parent dba0c5f commit 0ef5fe3
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 60 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Expand Up @@ -5,4 +5,4 @@ before_install: sudo apt-get install vim-gtk
before_script:
- "export DISPLAY=:99.0"
- "sh -e /etc/init.d/xvfb start"
script: "CI=true bin/rspec --color -f d"
script: "bin/rspec --color -f d"
110 changes: 52 additions & 58 deletions indent/elixir.vim
Expand Up @@ -35,60 +35,38 @@ let s:pair_start = '\<\%('.s:no_colon_before.s:block_start.'\)\>'.s:no_colon_aft
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'

function! s:metadata()
let metadata = {}
let metadata.current_line_ref = v:lnum
let metadata.last_line_ref = prevnonblank(metadata.current_line_ref - 1)
let metadata.current_line = getline(metadata.current_line_ref)
let metadata.last_line = getline(metadata.last_line_ref)
let metadata.pending_parenthesis = 0
let metadata.opened_symbol = 0

if metadata.last_line !~ s:arrow
let splitted_line = split(metadata.last_line, '\zs')
let metadata.pending_parenthesis =
\ + count(splitted_line, '(') - count(splitted_line, ')')
let metadata.opened_symbol =
\ + metadata.pending_parenthesis
\ + count(splitted_line, '[') - count(splitted_line, ']')
\ + count(splitted_line, '{') - count(splitted_line, '}')
end

return metadata
endfunction

function! s:is_indentable_syntax()
" 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'.
call synID(s:metadata().current_line_ref, 1, 1)
call synID(s:current_line_ref, 1, 1)
" This forces vim to sync the syntax.
syntax sync fromstart

return synIDattr(synID(s:metadata().current_line_ref, 1, 1), "name")
return synIDattr(synID(s:current_line_ref, 1, 1), "name")
\ !~ s:skip_syntax
endfunction

function! s:indent_opened_symbol(ind)
if s:metadata().opened_symbol > 0
if s:metadata().pending_parenthesis > 0
\ && s:metadata().last_line !~ '^\s*def'
\ && s:metadata().last_line !~ s:arrow
if s:opened_symbol > 0
if s:pending_parenthesis > 0
\ && s:last_line !~ '^\s*def'
\ && s:last_line !~ s:arrow
let b:old_ind = a:ind
return matchend(s:metadata().last_line, '(')
return matchend(s:last_line, '(')
" 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
elseif s:metadata().last_line =~ '\('.s:symbols_start.'\).'
elseif s:last_line =~ '\('.s:symbols_start.'\).'
let regex = '\('.s:symbols_start.'\)\s*'
let opened_prefix = matchlist(s:metadata().last_line, regex)[0]
return a:ind + (s:metadata().opened_symbol * strlen(opened_prefix))
let opened_prefix = matchlist(s:last_line, regex)[0]
return a:ind + (s:opened_symbol * strlen(opened_prefix))
else
return a:ind + (s:metadata().opened_symbol * &sw)
return a:ind + (s:opened_symbol * &sw)
end
elseif s:metadata().opened_symbol < 0
let ind = get(b:, 'old_ind', a:ind + (s:metadata().opened_symbol * &sw))
elseif s:opened_symbol < 0
let ind = get(b:, 'old_ind', a:ind + (s:opened_symbol * &sw))
let ind = float2nr(ceil(floor(ind)/&sw)*&sw)
return ind <= 0 ? 0 : ind
else
Expand All @@ -97,54 +75,53 @@ function! s:indent_opened_symbol(ind)
endfunction

function! s:indent_last_line_end_symbol_or_indent_keyword(ind)
if s:metadata().last_line =~ '^\s*\('.s:symbols_end.'\)'
\ || s:metadata().last_line =~ s:indent_keywords
if s:last_line =~ '^\s*\('.s:symbols_end.'\)'
\ || s:last_line =~ s:indent_keywords
return a:ind + &sw
else
return a:ind
end
endfunction

function! s:indent_symbols_ending(ind)
if s:metadata().current_line =~ '^\s*\('.s:symbols_end.'\)'
if s:current_line =~ '^\s*\('.s:symbols_end.'\)'
return a:ind - &sw
else
return a:ind
end
endfunction

function! s:indent_assignment(ind)
if s:metadata().last_line =~ s:ending_with_assignment
\ && s:metadata().opened_symbol == 0
let b:old_ind = indent(s:metadata().last_line_ref) " FIXME: side effect
if s:last_line =~ s:ending_with_assignment
let b:old_ind = indent(s:last_line_ref) " FIXME: side effect
return a:ind + &sw
else
return a:ind
end
endfunction

function! s:indent_pipeline(ind)
if s:metadata().last_line =~ s:starts_with_pipeline
\ && s:metadata().current_line =~ s:starts_with_pipeline
indent(s:metadata().last_line_ref)
elseif s:metadata().current_line =~ s:starts_with_pipeline
\ && s:metadata().last_line =~ '^[^=]\+=.\+$'
let b:old_ind = indent(s:metadata().last_line_ref)
if s:last_line =~ s:starts_with_pipeline
\ && s:current_line =~ s:starts_with_pipeline
indent(s:last_line_ref)
elseif s:current_line =~ s:starts_with_pipeline
\ && s:last_line =~ '^[^=]\+=.\+$'
let b:old_ind = indent(s:last_line_ref)
" if line starts with pipeline
" and last line is an attribution
" indents pipeline in same level as attribution
return match(s:metadata().last_line, '=\s*\zs[^ ]')
return match(s:last_line, '=\s*\zs[^ ]')
else
return a:ind
end
endfunction

function! s:indent_after_pipeline(ind)
if s:metadata().last_line =~ s:starts_with_pipeline
if empty(substitute(s:metadata().current_line, ' ', '', 'g'))
\ || s:metadata().current_line =~ s:starts_with_pipeline
return indent(s:metadata().last_line_ref)
elseif s:metadata().last_line !~ s:indent_keywords
if s:last_line =~ s:starts_with_pipeline
if empty(substitute(s:current_line, ' ', '', 'g'))
\ || s:current_line =~ s:starts_with_pipeline
return indent(s:last_line_ref)
elseif s:last_line !~ s:indent_keywords
return b:old_ind
else
return a:ind
Expand All @@ -155,7 +132,7 @@ function! s:indent_after_pipeline(ind)
endfunction

function! s:deindent_keyword(ind)
if s:metadata().current_line =~ s:deindent_keywords
if s:current_line =~ s:deindent_keywords
let bslnum = searchpair(
\ s:pair_start,
\ s:pair_middle,
Expand All @@ -171,7 +148,7 @@ function! s:deindent_keyword(ind)
endfunction

function! s:indent_arrow(ind)
if s:metadata().current_line =~ s:arrow
if s:current_line =~ s:arrow
" indent case statements '->'
return a:ind + &sw
else
Expand All @@ -180,14 +157,31 @@ function! s:indent_arrow(ind)
endfunction

function! GetElixirIndent()
if s:metadata().last_line_ref == 0
let s:current_line_ref = v:lnum
let s:last_line_ref = prevnonblank(s:current_line_ref - 1)
let s:current_line = getline(s:current_line_ref)
let s:last_line = getline(s:last_line_ref)
let s:pending_parenthesis = 0
let s:opened_symbol = 0

if s:last_line !~ s:arrow
let splitted_line = split(s:last_line, '\zs')
let s:pending_parenthesis =
\ + count(splitted_line, '(') - count(splitted_line, ')')
let s:opened_symbol =
\ + s:pending_parenthesis
\ + count(splitted_line, '[') - count(splitted_line, ']')
\ + count(splitted_line, '{') - count(splitted_line, '}')
end

if s:last_line_ref == 0
" At the start of the file use zero indent.
return 0
elseif !s:is_indentable_syntax()
" Current syntax is not indentable, keep last line indentation
return indent(s:metadata().last_line_ref)
return indent(s:last_line_ref)
else
let ind = indent(s:metadata().last_line_ref)
let ind = indent(s:last_line_ref)
let ind = s:indent_opened_symbol(ind)
let ind = s:indent_symbols_ending(ind)
let ind = s:indent_pipeline(ind)
Expand Down
1 change: 0 additions & 1 deletion spec/spec_helper.rb
Expand Up @@ -14,7 +14,6 @@ def reindent(code)
@vim.normal 'ggVG999<<'
# force vim to indent the file
@vim.normal 'gg=G'
sleep 0.1 if ENV['CI']
end
end

Expand Down

0 comments on commit 0ef5fe3

Please sign in to comment.