Skip to content

Commit

Permalink
WIP: attach Vim
Browse files Browse the repository at this point in the history
  • Loading branch information
Shougo committed Jan 1, 2018
1 parent 6a4f622 commit 7f4bf7b
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 15 deletions.
2 changes: 2 additions & 0 deletions autoload/deoplete/init.vim
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,8 @@ function! deoplete#init#_context(event, sources) abort

return {
\ 'changedtick': b:changedtick,
\ 'serveraddr': (has('nvim') ?
\ v:servername : neovim_rpc#serveraddr()),
\ 'event': event,
\ 'input': input,
\ 'is_windows': ((has('win32') || has('win64')) ? v:true : v:false),
Expand Down
43 changes: 30 additions & 13 deletions rplugin/python3/deoplete/child.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
from collections import defaultdict
from threading import Thread
from queue import Queue
from neovim import attach

from deoplete import logger
from deoplete.exceptions import SourceInitError
Expand All @@ -21,10 +22,10 @@

class Child(logger.LoggingMixin):

def __init__(self, vim):
def __init__(self):
self.name = 'child'

self._vim = vim
self._vim = None
self._filters = {}
self._sources = {}
self._custom = []
Expand All @@ -41,29 +42,45 @@ def __init__(self, vim):
def enable_logging(self):
self.is_debug_enabled = True

def add_source(self, s):
def add_source(self, s, serveraddr):
if not self._thread:
self._thread = Thread(target=self._main_loop)
self._thread = Thread(target=self._main_loop,
args=(serveraddr))
self._thread.start()
self._queue_in.put(['add_source', [s]])
self._queue_put('add_source', [s])

def add_filter(self, f):
self._queue_in.put(['add_filter', [f]])
self._queue_put('add_filter', [f])

def set_source_attributes(self, context):
self._queue_in.put(['set_source_attributes', [context]])
self._queue_put('set_source_attributes', [context])

def set_custom(self, custom):
self._queue_in.put(['set_custom', [custom]])
self._queue_put('set_custom', [custom])

def merge_results(self, context):
self._queue_in.put(['merge_results', [context]])
self._queue_put('merge_results', [context])
if self._queue_out.empty():
return (False, [])
return self._queue_out.get()

def on_event(self, context):
self._queue_in.put(['on_event', [context]])
self._queue_put('on_event', [context])

def _queue_put(self, name, args):
self._queue_in.put([name, args])

def _attach_vim(self, serveraddr):
if len(serveraddr.split(':')) == 2:
serveraddr, port = serveraddr.split(':')
port = int(port)
self._vim = attach('tcp', address=serveraddr, port=port)
else:
self._vim = attach('socket', address=serveraddr)

def _main_loop(self, serveraddr):
self._attach_vim(serveraddr)

def _main_loop(self):
while 1:
self.debug('main_loop: begin')
[message, args] = self._queue_in.get()
Expand All @@ -78,8 +95,8 @@ def _main_loop(self):
self._set_custom(args[0])
elif message == 'on_event':
self._on_event(args[0])
# elif message == 'merge_results':
# self._merge_results(args[0])
elif message == 'merge_results':
self._merge_results(args[0])

def _add_source(self, s):
self._sources[s.name] = s
Expand Down
7 changes: 5 additions & 2 deletions rplugin/python3/deoplete/deoplete.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def __init__(self, vim):
self._child_count = 0
self._max_children = 1
for n in range(0, self._max_children):
self._children.append(Child(vim))
self._children.append(Child())

# on_init() call
context = self._vim.call('deoplete#init#_context', 'Init', [])
Expand Down Expand Up @@ -84,12 +84,14 @@ def completion_begin(self, context):
self.debug('completion_end: %s', context['input'])

def merge_results(self, context):
error(self._vim, 'merge')
is_async = False
merged_results = []
for child in self._children:
result = child.merge_results(context)
is_async = is_async or result[0]
merged_results += result[1]
error(self._vim, 'results')

if not merged_results:
return (is_async, -1, [])
Expand Down Expand Up @@ -154,7 +156,8 @@ def load_sources(self, context):
finally:
if source:
self._loaded_sources[source.name] = path
self._children[self._child_count].add_source(source)
self._children[self._child_count].add_source(
source, context['serveraddr'])
self._child_count += 1
self._child_count %= self._max_children
self.debug('Loaded Source: %s (%s)', source.name, path)
Expand Down

0 comments on commit 7f4bf7b

Please sign in to comment.