summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorw0rp <devw0rp@gmail.com>2018-07-16 17:57:07 +0100
committerw0rp <devw0rp@gmail.com>2018-07-16 17:57:07 +0100
commit37df1f8ceb97f19f4576c00c8146aad3a5a8a648 (patch)
treeb87047d5132f27ea15136ec6dec2f9e9751e0120
parentaa54c10bae015bddff02eecd88866c0cad007664 (diff)
downloadale-37df1f8ceb97f19f4576c00c8146aad3a5a8a648.zip
#1700 - Stop handling completion results if you leave insert mode
-rw-r--r--autoload/ale/completion.vim9
-rw-r--r--test/completion/test_completion_events.vader42
-rw-r--r--test/completion/test_lsp_completion_messages.vader10
3 files changed, 59 insertions, 2 deletions
diff --git a/autoload/ale/completion.vim b/autoload/ale/completion.vim
index 694cb655..0f9ae4e0 100644
--- a/autoload/ale/completion.vim
+++ b/autoload/ale/completion.vim
@@ -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
diff --git a/test/completion/test_completion_events.vader b/test/completion/test_completion_events.vader
index 96182013..504d79c2 100644
--- a/test/completion/test_completion_events.vader
+++ b/test/completion/test_completion_events.vader
@@ -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')
diff --git a/test/completion/test_lsp_completion_messages.vader b/test/completion/test_lsp_completion_messages.vader
index e5aac176..00a174dc 100644
--- a/test/completion/test_lsp_completion_messages.vader
+++ b/test/completion/test_lsp_completion_messages.vader
@@ -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()