Skip to content

Commit d8ce4c8

Browse files
Refactor acid completion into generic lib
This makes porting to NCM easier
1 parent 157b7f8 commit d8ce4c8

File tree

2 files changed

+86
-71
lines changed

2 files changed

+86
-71
lines changed

pythonx/async_clj_omni/acid.py

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
import threading
2+
from async_clj_omni.cider import cider_gather # NOQA
3+
try:
4+
from acid.nvim import localhost, path_to_ns
5+
from acid.session import SessionHandler, send
6+
loaded = True
7+
except:
8+
loaded = False
9+
10+
11+
class Acid_nrepl:
12+
def __init__(self, wc):
13+
self.wc = wc
14+
15+
def send(self, msg):
16+
self.wc.send(msg)
17+
18+
def watch(self, name, q, callback):
19+
self.wc.watch(name, q, callback)
20+
21+
def unwatch(self, name):
22+
self.wc.unwatch(name)
23+
24+
25+
class AcidManager:
26+
def __init__(self, logger, vim):
27+
self._vim = vim
28+
self._logger = logger
29+
self.__conns = {}
30+
31+
def on_init(self):
32+
if loaded:
33+
self.acid_sessions = SessionHandler()
34+
else:
35+
self._logger.debug('Acid.nvim not found. Please install it.')
36+
self.sessions = {}
37+
38+
def get_wc(self, url):
39+
return self.acid_sessions.get_or_create(url)
40+
41+
def get_session(self, url, wc):
42+
if url in self.sessions:
43+
return self.sessions[url]
44+
45+
session_event = threading.Event()
46+
47+
def clone_handler(msg, wc, key):
48+
wc.unwatch(key)
49+
self.sessions[url] = msg['new-session']
50+
session_event.set()
51+
52+
wc.watch('dyn-session', {'new-session': None}, clone_handler)
53+
wc.send({'op': 'clone'})
54+
session_event.wait(0.5)
55+
56+
return self.sessions[url]
57+
58+
def gather_candidates(self, keyword):
59+
if not loaded:
60+
return []
61+
62+
address = localhost(self._vim)
63+
if address is None:
64+
return []
65+
url = "nrepl://{}:{}".format(*address)
66+
wc = self.get_wc(url)
67+
session = self.get_session(url, wc)
68+
ns = path_to_ns(self._vim)
69+
70+
def global_watch(cmsg, cwc, ckey):
71+
self._logger.debug("Received message for {}".format(url))
72+
self._logger.debug(cmsg)
73+
74+
wc.watch('global_watch', {}, global_watch)
75+
76+
return cider_gather(self._logger,
77+
Acid_nrepl(wc),
78+
keyword,
79+
session,
80+
ns)
Lines changed: 6 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -1,92 +1,27 @@
1-
import threading
1+
import deoplete.logger
22
# Adds a git submodule to the import path
33
import sys
44
import os
55
basedir = os.path.dirname(os.path.realpath(__file__))
66
sys.path.append(os.path.join(basedir, "../../acid"))
77
sys.path.append(os.path.join(basedir, "../../../../pythonx/"))
88

9-
try:
10-
from acid.nvim import localhost, path_to_ns
11-
from acid.session import SessionHandler, send
12-
loaded = True
13-
except:
14-
loaded = False
15-
16-
from async_clj_omni.cider import cider_gather # NOQA
9+
from async_clj_omni.acid import Acid_nrepl, AcidManager
1710
from .base import Base # NOQA
1811

1912

20-
class Acid_nrepl:
21-
def __init__(self, wc):
22-
self.wc = wc
23-
24-
def send(self, msg):
25-
self.wc.send(msg)
26-
27-
def watch(self, name, q, callback):
28-
self.wc.watch(name, q, callback)
29-
30-
def unwatch(self, name):
31-
self.wc.unwatch(name)
32-
33-
3413
class Source(Base):
3514
def __init__(self, vim):
3615
Base.__init__(self, vim)
3716
self.name = "acid"
3817
self.mark = "[acid]"
3918
self.filetypes = ['clojure']
4019
self.rank = 200
41-
self.__conns = {}
20+
self._vim = vim
21+
self._AcidManager = AcidManager(deoplete.logger.getLogger('acid_cider_completion_manager'), vim)
4222

4323
def on_init(self, context):
44-
if loaded:
45-
self.acid_sessions = SessionHandler()
46-
else:
47-
self.debug('echomsg "Acid.nvim not found. Please install it."')
48-
self.sessions = {}
49-
50-
def get_wc(self, url):
51-
return self.acid_sessions.get_or_create(url)
52-
53-
def get_session(self, url, wc):
54-
if url in self.sessions:
55-
return self.sessions[url]
56-
57-
session_event = threading.Event()
58-
59-
def clone_handler(msg, wc, key):
60-
wc.unwatch(key)
61-
self.sessions[url] = msg['new-session']
62-
session_event.set()
63-
64-
wc.watch('dyn-session', {'new-session': None}, clone_handler)
65-
wc.send({'op': 'clone'})
66-
session_event.wait(0.5)
67-
68-
return self.sessions[url]
24+
self._AcidManager.on_init()
6925

7026
def gather_candidates(self, context):
71-
if not loaded:
72-
return []
73-
74-
address = localhost(self.vim)
75-
if address is None:
76-
return []
77-
url = "nrepl://{}:{}".format(*address)
78-
wc = self.get_wc(url)
79-
session = self.get_session(url, wc)
80-
ns = path_to_ns(self.vim)
81-
82-
def global_watch(cmsg, cwc, ckey):
83-
self.debug("Received message for {}".format(url))
84-
self.debug(cmsg)
85-
86-
wc.watch('global_watch', {}, global_watch)
87-
88-
return cider_gather(self,
89-
Acid_nrepl(wc),
90-
context["complete_str"],
91-
session,
92-
ns)
27+
return self._AcidManager.gather_candidates(context["complete_str"])

0 commit comments

Comments
 (0)