#1700 - Stop handling completion results if you leave insert mode

This commit is contained in:
w0rp 2018-07-16 17:57:07 +01:00
parent aa54c10bae
commit 37df1f8ceb
No known key found for this signature in database
GPG Key ID: 0FC1ECAA8C81CD83
3 changed files with 59 additions and 2 deletions

View File

@ -170,6 +170,10 @@ function! ale#completion#OmniFunc(findstart, base) abort
endfunction
function! ale#completion#Show(response, completion_parser) abort
if ale#util#Mode() isnot# 'i'
return
endif
" Remember the old omnifunc value, if there is one.
" If we don't store an old one, we'll just never reset the option.
" This will stop some random exceptions from appearing.
@ -189,7 +193,8 @@ endfunction
function! s:CompletionStillValid(request_id) abort
let [l:line, l:column] = getcurpos()[1:2]
return has_key(b:, 'ale_completion_info')
return ale#util#Mode() is# 'i'
\&& has_key(b:, 'ale_completion_info')
\&& b:ale_completion_info.request_id == a:request_id
\&& b:ale_completion_info.line == l:line
\&& b:ale_completion_info.column == l:column
@ -477,7 +482,7 @@ function! s:TimerHandler(...) abort
" When running the timer callback, we have to be sure that the cursor
" hasn't moved from where it was when we requested completions by typing.
if s:timer_pos == [l:line, l:column]
if s:timer_pos == [l:line, l:column] && ale#util#Mode() is# 'i'
call ale#completion#GetCompletions()
endif
endfunction

View File

@ -8,6 +8,7 @@ Before:
let g:ale_completion_enabled = 1
let g:get_completions_called = 0
let g:feedkeys_calls = []
let g:fake_mode = 'i'
let &l:completeopt = 'menu,menuone,preview,noselect,noinsert'
@ -17,6 +18,11 @@ Before:
call add(g:feedkeys_calls, [a:string, a:mode])
endfunction
" Pretend we're in insert mode for most tests.
function! ale#util#Mode(...) abort
return g:fake_mode
endfunction
function! CheckCompletionCalled(expect_success) abort
let g:get_completions_called = 0
@ -35,6 +41,7 @@ Before:
After:
Restore
unlet! g:fake_mode
unlet! g:get_completions_called
unlet! b:ale_old_omnifunc
unlet! b:ale_old_completopt
@ -49,6 +56,12 @@ After:
" This stops the tests from failing randomly.
call ale#completion#StopTimer()
" Reset the function. The runtime command below should fix this, but doesn't
" seem to fix it.
function! ale#util#Mode(...) abort
return call('mode', a:000)
endfunction
runtime autoload/ale/completion.vim
runtime autoload/ale/util.vim
@ -78,6 +91,22 @@ Execute(ale#completion#GetCompletions should not be called when the cursor posit
Assert !g:get_completions_called
Execute(ale#completion#GetCompletions should not be called if you switch to normal mode):
let &l:completeopt = 'menu,preview'
let g:fake_mode = 'n'
" We just want to check if the function is called.
function! ale#completion#GetCompletions()
let g:get_completions_called = 1
endfunction
let g:ale_completion_delay = 0
call ale#completion#Queue()
sleep 1m
Assert !g:get_completions_called
Execute(Completion should not be done shortly after the CompleteDone function):
call CheckCompletionCalled(1)
call ale#completion#Done()
@ -128,6 +157,19 @@ Execute(ale#completion#Show() should make the correct feedkeys() call):
AssertEqual [["\<C-x>\<C-o>", 'n']], g:feedkeys_calls
Execute(ale#completion#Show() shouldn't do anything if you switch back to normal mode):
let &l:completeopt = 'menu,preview'
let g:fake_mode = 'n'
call ale#completion#Show('Response', 'Parser')
AssertEqual 'menu,preview', &l:completeopt
Assert !exists('b:ale_old_omnifunc')
Assert !exists('b:ale_old_completopt')
Assert !exists('b:ale_completion_response')
Assert !exists('b:ale_completion_parser')
AssertEqual [], g:feedkeys_calls
Execute(ale#completion#Show() should set up the response and parser):
call ale#completion#Show('Response', 'Parser')

View File

@ -30,6 +30,11 @@ Before:
\}
endfunction
" Pretend we're in insert mode for most tests.
function! ale#util#Mode(...) abort
return 'i'
endfunction
" Replace the Send function for LSP, so we can monitor calls to it.
function! ale#lsp#Send(conn_id, message, ...) abort
call add(g:message_list, a:message)
@ -49,6 +54,11 @@ After:
unlet! b:ale_linters
unlet! b:ale_tsserver_completion_names
" Reset the function.
function! ale#util#Mode(...) abort
return call('mode', a:000)
endfunction
call ale#lsp#RemoveConnectionWithID(347)
call ale#test#RestoreDirectory()
call ale#linter#Reset()