Skip to content

Commit 2df952b

Browse files
Refactor async_clj_omni shared utils into pythonx
1 parent 5564935 commit 2df952b

File tree

6 files changed

+68
-37
lines changed

6 files changed

+68
-37
lines changed
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
class Error(Exception):
2+
"""Base class for exceptions in this module."""
3+
pass
4+
5+
def gather_conn_info(vim):
6+
try:
7+
client = vim.eval("fireplace#client()")
8+
except Exception:
9+
raise Error
10+
11+
if client:
12+
connection = client.get("connection", {})
13+
transport = connection.get("transport")
14+
15+
if not transport:
16+
raise Error
17+
18+
ns = ""
19+
try:
20+
ns = vim.eval("fireplace#ns()")
21+
except Exception:
22+
pass
23+
24+
return [client, connection, transport, ns]

pythonx/cm_sources/foo.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# -*- coding: utf-8 -*-
2+
3+
from cm import register_source
4+
register_source(name='fireplace',
5+
abbreviation='🔥',
6+
scopes=['clojure'],
7+
word_pattern=r'[\w!$%&*+/:<=>?@\^_~\-\.#]+',
8+
priority=9)
9+
# TODO early_cache = 0 OR figure out cm_refresh_patterns
10+
11+
import re
12+
13+
class Source:
14+
def __init__(self,nvim):
15+
self._nvim = nvim
16+
17+
def cm_refresh(self,info,ctx):
18+
matches = ['foo_bar','foo_baz', 'req$\'uire']
19+
self._nvim.call('cm#complete', info['name'], ctx, ctx['startcol'], matches, async=True)

rplugin/python3/deoplete/sources/acid.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import os
55
basedir = os.path.dirname(os.path.realpath(__file__))
66
sys.path.append(os.path.join(basedir, "../../acid"))
7-
sys.path.append(os.path.join(basedir, "../../async_clj_omni"))
7+
sys.path.append(os.path.join(basedir, "../../../../pythonx/async_clj_omni"))
88

99
try:
1010
from acid.nvim import localhost, path_to_ns

rplugin/python3/deoplete/sources/async_clj.py

Lines changed: 24 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,10 @@
33
import os
44
basedir = os.path.dirname(os.path.realpath(__file__))
55
sys.path.append(os.path.join(basedir, "vim_nrepl_python_client/"))
6-
sys.path.append(os.path.join(basedir, "../../async_clj_omni"))
6+
sys.path.append(os.path.join(basedir, "../../../../pythonx/async_clj_omni"))
77

88
from async_clj_omni.cider import cider_gather # NOQA
9+
from async_clj_omni import fireplace
910
from .base import Base # NOQA
1011
import nrepl # NOQA
1112

@@ -35,46 +36,33 @@ def __init__(self, vim):
3536

3637
def gather_candidates(self, context):
3738
self.debug("Gathering candidates")
38-
client = False
39-
try:
40-
client = self.vim.eval("fireplace#client()")
41-
except Exception:
42-
pass
43-
44-
if client:
45-
connection = client.get("connection", {})
46-
transport = connection.get("transport")
47-
if not transport:
48-
return []
4939

50-
ns = ""
51-
try:
52-
ns = self.vim.eval("fireplace#ns()")
53-
except Exception:
54-
pass
55-
56-
host = transport.get("host")
57-
port = transport.get("port")
40+
try:
41+
client, connection, transport, ns = fireplace.gather_conn_info(self.vim)
42+
except fireplace.Error:
43+
self.exception("Unable to get connection info")
44+
return []
5845

59-
conn_string = "nrepl://{}:{}".format(host, port)
46+
host = transport.get("host")
47+
port = transport.get("port")
6048

61-
if conn_string not in self.__conns:
62-
conn = nrepl.connect(conn_string)
49+
conn_string = "nrepl://{}:{}".format(host, port)
6350

64-
def global_watch(cmsg, cwc, ckey):
65-
self.debug("Received message for {}".format(conn_string))
66-
self.debug(cmsg)
51+
if conn_string not in self.__conns:
52+
conn = nrepl.connect(conn_string)
6753

68-
wc = nrepl.WatchableConnection(conn)
69-
self.__conns[conn_string] = wc
70-
wc.watch("global_watch", {}, global_watch)
54+
def global_watch(cmsg, cwc, ckey):
55+
self.debug("Received message for {}".format(conn_string))
56+
self.debug(cmsg)
7157

72-
wc = self.__conns.get(conn_string)
73-
self.debug(self.__conns)
58+
wc = nrepl.WatchableConnection(conn)
59+
self.__conns[conn_string] = wc
60+
wc.watch("global_watch", {}, global_watch)
7461

75-
return cider_gather(Fireplace_nrepl(wc),
76-
context["complete_str"],
77-
connection.get("session"),
78-
ns)
62+
wc = self.__conns.get(conn_string)
63+
self.debug(self.__conns)
7964

80-
return []
65+
return cider_gather(Fireplace_nrepl(wc),
66+
context["complete_str"],
67+
connection.get("session"),
68+
ns)

0 commit comments

Comments
 (0)