Skip to content

Commit

Permalink
Add TypeScript autoimport support for deoplete (#2779)
Browse files Browse the repository at this point in the history
* Add autoimport support for deoplete

* Fix test_deoplete_source.py

* Use callback instead of is_async for deoplete

Shuogo, the author of Deoplete, does not recommend using the `is_async`
option:

> I think is_async is not recommended. It is not so useful and broken.
> You should use callback system instead.

Link: Shougo/deoplete.nvim#1006 (comment)

Incidentally, the same thread mentiones an issue started by w0rp:
Shougo/deoplete.nvim#976

The deoplete docs also say is_async is deprecated:

> is_async        (Bool)
>     If the gather is asynchronous, the source must set
>     it to "True". A typical strategy for an asynchronous
>     gather_candidates method to use this flag is to
>     set is_async flag to True while results are being
>     produced in the background (optionally, returning them
>     as they become ready). Once background processing
>     has completed, is_async flag should be set to False
>     indicating that this is the last portion of the
>     candidates.
>
>     Note: The feature is deprecated and not recommended.
>     You should use callback system by
>     |deoplete#auto_complete()| instead.

Link: https://github.com/Shougo/deoplete.nvim/blob/master/doc/deoplete.txt

Co-authored-by: w0rp <w0rp@users.noreply.github.com>
  • Loading branch information
jeremija and w0rp committed Jan 1, 2020
1 parent 874c98b commit 0cb432c
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 71 deletions.
5 changes: 3 additions & 2 deletions autoload/ale/completion.vim
Expand Up @@ -388,7 +388,6 @@ function! s:CompletionStillValid(request_id) abort
\&& b:ale_completion_info.line == l:line
\&& (
\ b:ale_completion_info.column == l:column
\ || b:ale_completion_info.source is# 'deoplete'
\ || b:ale_completion_info.source is# 'ale-omnifunc'
\ || b:ale_completion_info.source is# 'ale-callback'
\)
Expand Down Expand Up @@ -805,7 +804,9 @@ endfunction
function! ale#completion#HandleUserData(completed_item) abort
let l:source = get(get(b:, 'ale_completion_info', {}), 'source', '')

if l:source isnot# 'ale-automatic' && l:source isnot# 'ale-manual' && l:source isnot# 'ale-callback'
if l:source isnot# 'ale-automatic'
\&& l:source isnot# 'ale-manual'
\&& l:source isnot# 'ale-callback'
return
endif

Expand Down
22 changes: 9 additions & 13 deletions rplugin/python3/deoplete/sources/ale.py
Expand Up @@ -24,6 +24,7 @@ def __init__(self, vim):
self.rank = 1000
self.is_bytepos = True
self.min_pattern_length = 1
self.is_volatile = True
# Do not forget to update s:trigger_character_map in completion.vim in
# updating entries in this map.
self.input_patterns = {
Expand All @@ -44,21 +45,16 @@ def gather_candidates(self, context):
if not self.vim.call('ale#completion#CanProvideCompletions'):
return None

if context.get('is_refresh'):
context['is_async'] = False
event = context.get('event')

if context['is_async']:
# Result is the same as for omnifunc, or None.
if event == 'Async':
result = self.vim.call('ale#completion#GetCompletionResult')
return result or []

if result is not None:
context['is_async'] = False

return result
else:
context['is_async'] = True

# Request some completion results.
self.vim.call('ale#completion#GetCompletions', 'deoplete')
if context.get('is_refresh'):
self.vim.command(
"call ale#completion#GetCompletions('ale-callback', " + \
"{'callback': {completions -> deoplete#auto_complete() }})"
)

return []
6 changes: 3 additions & 3 deletions test/completion/test_completion_events.vader
Expand Up @@ -233,7 +233,7 @@ Execute(ale#completion#Show() should make the correct feedkeys() call for manual
AssertEqual [["\<Plug>(ale_show_completion_menu)"]], g:feedkeys_calls

Execute(ale#completion#Show() should not call feedkeys() for other sources):
let b:ale_completion_info = {'source': 'deoplete'}
let b:ale_completion_info = {'source': 'other-source'}
call ale#completion#Show([{'word': 'x', 'kind': 'v', 'icase': 1}])

sleep 1ms
Expand Down Expand Up @@ -334,7 +334,7 @@ Execute(b:ale_completion_info should be set up correctly when requesting complet
Execute(b:ale_completion_info should be set up correctly for other sources):
let b:ale_completion_result = []
call setpos('.', [bufnr(''), 3, 14, 0])
call ale#completion#GetCompletions('deoplete')
call ale#completion#GetCompletions('ale-callback')

AssertEqual
\ {
Expand All @@ -344,7 +344,7 @@ Execute(b:ale_completion_info should be set up correctly for other sources):
\ 'line_length': 14,
\ 'line': 3,
\ 'prefix': 'ab',
\ 'source': 'deoplete',
\ 'source': 'ale-callback',
\ },
\ b:ale_completion_info
Assert !exists('b:ale_completion_result')
Expand Down
72 changes: 19 additions & 53 deletions test/python/test_deoplete_source.py
Expand Up @@ -8,24 +8,31 @@


class VimMock(object):
def __init__(self, call_list, call_results):
def __init__(self, call_list, call_results, commands):
self.__call_list = call_list
self.__call_results = call_results

self.__commands = commands

def call(self, function, *args):
self.__call_list.append((function, args))

return self.__call_results.get(function, 0)

def command(self, command):
self.__commands.append(command)


class DeopleteSourceTest(unittest.TestCase):
def setUp(self):
super(DeopleteSourceTest, self).setUp()

self.call_list = []
self.call_results = {'ale#completion#CanProvideCompletions': 1}
self.commands = []
self.source = ale_module.Source('vim')
self.source.vim = VimMock(self.call_list, self.call_results)
self.source.vim = VimMock(
self.call_list, self.call_results, self.commands)

def test_attributes(self):
"""
Expand All @@ -48,6 +55,7 @@ def test_attributes(self):
'cpp': r'(\.|::|->)\w*$',
},
'is_bytepos': True,
'is_volatile': True,
'mark': '[L]',
'min_pattern_length': 1,
'name': 'ale',
Expand All @@ -64,70 +72,28 @@ def test_complete_position(self):
])

def test_request_completion_results(self):
context = {'is_async': False}
context = {'event': 'TextChangedI', 'is_refresh': True}

self.assertEqual(self.source.gather_candidates(context), [])
self.assertEqual(context, {'is_async': True})
self.assertEqual(self.call_list, [
('ale#completion#CanProvideCompletions', ()),
('ale#completion#GetCompletions', ('deoplete',)),
])
self.assertEqual(self.commands, [
"call ale#completion#GetCompletions('ale-callback', " + \
"{'callback': {completions -> deoplete#auto_complete() }})"
])

def test_request_completion_results_from_buffer_without_providers(self):
self.call_results['ale#completion#CanProvideCompletions'] = 0
context = {'is_async': False}
context = {'event': 'TextChangedI', 'is_refresh': True}

self.assertIsNone(self.source.gather_candidates(context), [])
self.assertEqual(context, {'is_async': False})
self.assertEqual(self.call_list, [
('ale#completion#CanProvideCompletions', ()),
])

def test_refresh_completion_results(self):
context = {'is_async': False}

self.assertEqual(self.source.gather_candidates(context), [])
self.assertEqual(context, {'is_async': True})
self.assertEqual(self.call_list, [
('ale#completion#CanProvideCompletions', ()),
('ale#completion#GetCompletions', ('deoplete',)),
])

context = {'is_async': True, 'is_refresh': True}

self.assertEqual(self.source.gather_candidates(context), [])
self.assertEqual(context, {'is_async': True, 'is_refresh': True})
self.assertEqual(self.call_list, [
('ale#completion#CanProvideCompletions', ()),
('ale#completion#GetCompletions', ('deoplete',)),
('ale#completion#CanProvideCompletions', ()),
('ale#completion#GetCompletions', ('deoplete',)),
])

def test_poll_no_result(self):
context = {'is_async': True}
self.call_results['ale#completion#GetCompletionResult'] = None

self.assertEqual(self.source.gather_candidates(context), [])
self.assertEqual(context, {'is_async': True})
self.assertEqual(self.call_list, [
('ale#completion#CanProvideCompletions', ()),
('ale#completion#GetCompletionResult', ()),
])

def test_poll_empty_result_ready(self):
context = {'is_async': True}
self.call_results['ale#completion#GetCompletionResult'] = []

self.assertEqual(self.source.gather_candidates(context), [])
self.assertEqual(context, {'is_async': False})
self.assertEqual(self.call_list, [
('ale#completion#CanProvideCompletions', ()),
('ale#completion#GetCompletionResult', ()),
])

def test_poll_non_empty_result_ready(self):
context = {'is_async': True}
def test_async_event(self):
context = {'event': 'Async', 'is_refresh': True}
self.call_results['ale#completion#GetCompletionResult'] = [
{
'word': 'foobar',
Expand All @@ -147,7 +113,7 @@ def test_poll_non_empty_result_ready(self):
'info': '',
},
])
self.assertEqual(context, {'is_async': False})

self.assertEqual(self.call_list, [
('ale#completion#CanProvideCompletions', ()),
('ale#completion#GetCompletionResult', ()),
Expand Down

0 comments on commit 0cb432c

Please sign in to comment.