Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Send cursor location and text selection updates #46

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 32 additions & 13 deletions rplugin/python3/ghost.py
Original file line number Diff line number Diff line change
Expand Up @@ -206,33 +206,52 @@ def ghost_toggle_sync(self, args, range):
@neovim.command('GhostSync', range='', nargs='0', sync=True)
def ghost_sync(self, args, range):
bufnr = self.nvim.current.buffer.number
wsclient, req = buffer_handler_map[bufnr]
logger.info("sending message to client ")
text = "\n".join(self.nvim.buffers[bufnr][:])
req["text"] = text
# self.nvim.command("echo '%s'" % text)
wsclient.sendMessage(json.dumps(req))
self._send_buffer_state(bufnr)

@neovim.function("GhostNotify")
def ghost_notify(self, args):
logger.info(args)
event, bufnr = args
if bufnr not in buffer_handler_map:
return
wsclient, req = buffer_handler_map[bufnr]
logger.debug('event recd: %s, buffer: %d', event, bufnr)
if event == "text_changed" and self.syncghost:
logger.info("sending message to client ")
text = "\n".join(self.nvim.buffers[bufnr][:])
req["text"] = text
# self.nvim.command("echo '%s'" % text)
wsclient.sendMessage(json.dumps(req))
self._send_buffer_state(bufnr)
elif event == "closed":
logger.info(("Calling _handleOnWebSocketClose"
" in response to buffer"
" %d closure in nvim", bufnr))
wsclient = buffer_handler_map[bufnr][0]
self._handle_web_socket_close(wsclient)

def _send_buffer_state(self, bufnr):
if bufnr not in buffer_handler_map:
return
wsclient, req = buffer_handler_map[bufnr]
logger.info("sending message to client ")

buflines = self.nvim.buffers[bufnr]
text = "\n".join(buflines)
req["text"] = text

selections = self._get_selections(buflines)
req["selections"] = selections

# self.nvim.out_write(f"{text}\n{selections}")
wsclient.sendMessage(json.dumps(req))

def _get_selections(self, buflines):
off1 = self._loc_to_offset(".", buflines)
off2 = self._loc_to_offset("v", buflines)
start, end = min(off1, off2), max(off1, off2)
return [{"start": start, "end": end + (start != end)}]

def _loc_to_offset(self, loc_expr, buflines):
line, col = self.nvim.funcs.getpos(loc_expr)[1:3]
line_offset = sum(len(buflines[ln]) for ln in range(min(line-1, len(buflines))))
col_offset = col - 1
return line_offset + col_offset

def _handle_on_message(self, req, websocket):
try:
if websocket in buffer_handler_map:
Expand Down Expand Up @@ -262,7 +281,7 @@ def _handle_on_message(self, req, websocket):
self.nvim.command("doauto User vim-ghost#connected")
logger.debug("Raised custom au event")
self._raise_window()
change_cmd = ("au TextChanged,TextChangedI <buffer> call"
change_cmd = ("au TextChanged,TextChangedI,CursorMoved,CursorMovedI <buffer> call"
" GhostNotify('text_changed', %d)" % bufnr)
self.nvim.command(change_cmd)
logger.debug("Set up aucmd: %s", change_cmd)
Expand Down