1
|
|
|
"""Bridge for connecting a UI instance to nvim.""" |
2
|
|
|
import sys |
3
|
|
|
import os |
4
|
|
|
from threading import Semaphore, Thread |
5
|
|
|
from traceback import format_exc |
6
|
|
|
from inspect import signature |
7
|
|
|
|
8
|
|
|
|
9
|
|
|
class UIBridge(object): |
10
|
|
|
|
11
|
|
|
"""UIBridge class. Connects a Nvim instance to a UI class.""" |
12
|
|
|
|
13
|
|
|
def connect(self, nvim, ui, profile=None, notify=False): |
14
|
|
|
"""Connect nvim and the ui. |
15
|
|
|
|
16
|
|
|
This will start loops for handling the UI and nvim events while |
17
|
|
|
also synchronizing both. |
18
|
|
|
""" |
19
|
|
|
self._notify = notify |
|
|
|
|
20
|
|
|
self._error = None |
|
|
|
|
21
|
|
|
self._nvim = nvim |
|
|
|
|
22
|
|
|
self._ui = ui |
|
|
|
|
23
|
|
|
self._profile = profile |
|
|
|
|
24
|
|
|
self._sem = Semaphore(0) |
|
|
|
|
25
|
|
|
self.debug_events = len(os.environ.get("NVIM_PYTHON_UI_DEBUG", "")) > 0 |
|
|
|
|
26
|
|
|
t = Thread(target=self._nvim_event_loop) |
27
|
|
|
t.daemon = True |
28
|
|
|
t.start() |
29
|
|
|
self._ui_event_loop() |
30
|
|
|
if self._error: |
31
|
|
|
print(self._error) |
32
|
|
|
if self._profile: |
33
|
|
|
print(self._profile) |
34
|
|
|
|
35
|
|
|
def exit(self): |
36
|
|
|
"""Disconnect by exiting nvim.""" |
37
|
|
|
self.detach() |
38
|
|
|
self._call(self._nvim.quit) |
39
|
|
|
|
40
|
|
|
def input(self, input_str): |
41
|
|
|
"""Send input to nvim.""" |
42
|
|
|
self._call(self._nvim.input, input_str) |
43
|
|
|
|
44
|
|
|
def resize(self, columns, rows): |
45
|
|
|
"""Send a resize request to nvim.""" |
46
|
|
|
self._call(self._nvim.ui_try_resize, columns, rows) |
47
|
|
|
|
48
|
|
|
def attach(self, columns, rows, **options): |
49
|
|
|
"""Attach the UI to nvim.""" |
50
|
|
|
self._call(self._nvim.api.ui_attach, columns, rows, options) |
51
|
|
|
|
52
|
|
|
def detach(self): |
53
|
|
|
"""Detach the UI from nvim.""" |
54
|
|
|
self._call(self._nvim.ui_detach) |
55
|
|
|
|
56
|
|
|
def _call(self, fn, *args): |
57
|
|
|
self._nvim.async_call(fn, *args) |
58
|
|
|
|
59
|
|
|
def _ui_event_loop(self): |
60
|
|
|
self._sem.acquire() |
61
|
|
|
if self._profile: |
62
|
|
|
import StringIO |
|
|
|
|
63
|
|
|
import cProfile |
64
|
|
|
import pstats |
65
|
|
|
pr = cProfile.Profile() |
66
|
|
|
pr.enable() |
67
|
|
|
self._ui.start(self) |
68
|
|
|
if self._profile: |
69
|
|
|
pr.disable() |
70
|
|
|
s = StringIO.StringIO() |
71
|
|
|
ps = pstats.Stats(pr, stream=s) |
72
|
|
|
ps.strip_dirs().sort_stats(self._profile).print_stats(30) |
73
|
|
|
self._profile = s.getvalue() |
|
|
|
|
74
|
|
|
|
75
|
|
|
def _nvim_event_loop(self): |
76
|
|
|
def on_setup(): |
77
|
|
|
self._sem.release() |
78
|
|
|
|
79
|
|
|
def on_request(method, args): |
|
|
|
|
80
|
|
|
raise Exception('Not implemented') |
81
|
|
|
|
82
|
|
|
def on_notification(method, updates): |
83
|
|
|
def apply_updates(): |
84
|
|
|
if self._notify: |
85
|
|
|
sys.stdout.write('attached\n') |
86
|
|
|
sys.stdout.flush() |
87
|
|
|
self._notify = False |
|
|
|
|
88
|
|
|
try: |
89
|
|
|
for update in updates: |
90
|
|
|
# import sys |
91
|
|
|
# l = [','.join([str(a) for a in args]) |
92
|
|
|
# for args in update[1:]] |
93
|
|
|
# print >> sys.stderr, update[0], ' '.join(l) |
94
|
|
|
try: |
95
|
|
|
handler = getattr(self._ui, '_nvim_' + update[0]) |
96
|
|
|
nparam = len(signature(handler).parameters) |
97
|
|
|
|
98
|
|
|
except AttributeError: |
99
|
|
|
if self.debug_events: |
100
|
|
|
print(repr(update), file=sys.stderr) |
101
|
|
|
else: |
102
|
|
|
if self.debug_events and len(update[1]) > nparam: |
103
|
|
|
print(repr(update), file=sys.stderr) |
104
|
|
|
for args in update[1:]: |
105
|
|
|
handler(*args[:nparam]) |
106
|
|
|
except Exception: |
|
|
|
|
107
|
|
|
self._error = format_exc() |
|
|
|
|
108
|
|
|
self._call(self._nvim.quit) |
109
|
|
|
if method == 'redraw': |
110
|
|
|
self._ui.schedule_screen_update(apply_updates) |
111
|
|
|
|
112
|
|
|
self._nvim.run_loop(on_request, on_notification, on_setup) |
113
|
|
|
self._ui.quit() |
114
|
|
|
|
It is generally a good practice to initialize all attributes to default values in the
__init__
method: