Skip to content

Commit 7a86f12

Browse files
author
Björn Linse
committed
buffer: support attachment in hosted plugins
1 parent 3777747 commit 7a86f12

File tree

2 files changed

+54
-2
lines changed

2 files changed

+54
-2
lines changed

neovim/api/buffer.py

+26-1
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,6 @@ def update_highlights(self, src_id, hls, clear_start=0, clear_end=-1,
134134
lua = self._session._get_lua_private()
135135
lua.update_highlights(self, src_id, hls, clear_start, clear_end,
136136
async_=async_)
137-
138137
@property
139138
def name(self):
140139
"""Get the buffer name."""
@@ -155,6 +154,32 @@ def number(self):
155154
"""Get the buffer number."""
156155
return self.handle
157156

157+
def attach(self, cb):
158+
sess = self._session._session
159+
if self.handle not in sess.attached_buffers:
160+
a = BufAttachState()
161+
sess.attached_buffers[self.handle] = a
162+
a.callbacks.append(cb)
163+
self.api.attach(True, {})
164+
else:
165+
a = sess.attached_buffers[self.handle]
166+
a.callbacks.append(cb)
167+
cb(self, a.changedtick, a.lines)
168+
169+
def detach():
170+
for i in range(len(a.callbacks)):
171+
if a.callbacks[i] is cb:
172+
del a.callbacks[i]
173+
return
174+
else:
175+
raise ValueError("callback already detached")
176+
177+
return detach
178+
179+
class BufAttachState(object):
180+
def __init__(self):
181+
self.callbacks = []
182+
self.lines = []
158183

159184
class Range(object):
160185
def __init__(self, buffer, start, end):

neovim/plugin/host.py

+28-1
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,17 @@ class Host(object):
3232
def __init__(self, nvim):
3333
"""Set handlers for plugin_load/plugin_unload."""
3434
self.nvim = nvim
35+
self.nvim._session.attached_buffers = {}
36+
# TODO: delet this
37+
self.nvim._session.elog = []
3538
self._specs = {}
3639
self._loaded = {}
3740
self._load_errors = {}
38-
self._notification_handlers = {}
41+
self._notification_handlers = {
42+
'nvim_buf_lines_event': self._on_buf_lines,
43+
'nvim_buf_changedtick_event': self._on_buf_changedtick,
44+
'nvim_buf_detach_event': self._on_buf_detach,
45+
}
3946
self._request_handlers = {
4047
'poll': lambda: 'ok',
4148
'specs': self._on_specs_request,
@@ -96,6 +103,7 @@ def _on_notification(self, name, args):
96103
"""Handle a msgpack-rpc notification."""
97104
if IS_PYTHON3:
98105
name = decode_if_bytes(name)
106+
self.nvim._session.elog.append([name]+args)
99107
handler = self._notification_handlers.get(name, None)
100108
if not handler:
101109
msg = self._missing_handler_error(name, 'notification')
@@ -220,3 +228,22 @@ def _configure_nvim_for(self, obj):
220228
if decode:
221229
nvim = nvim.with_decode(decode)
222230
return nvim
231+
232+
def _on_buf_lines(self, buf, changedtick, first, last, data, more):
233+
a = self.nvim._session.attached_buffers[buf.handle]
234+
a.lines[first:last] = data
235+
a.changedtick = changedtick
236+
if more: return
237+
for cb in a.callbacks:
238+
cb(buf, changedtick, a.lines)
239+
240+
def _on_buf_changedtick(self, buf, changedtick):
241+
a = self.nvim._session.attached_buffers[buf.handle]
242+
a.changedtick = changedtick
243+
for cb in a.callbacks:
244+
cb(buf, changedtick, a.lines)
245+
246+
def _on_buf_detach(self, buf, changedtick, first, last, data, more):
247+
del self.nvim._session.attached_buffers[buf.handle]
248+
for cb in a.callbacks:
249+
cb(buf, -1, None)

0 commit comments

Comments
 (0)