diff --git a/rplugin/python3/ghost.py b/rplugin/python3/ghost.py index 89aa056..37c0d8e 100644 --- a/rplugin/python3/ghost.py +++ b/rplugin/python3/ghost.py @@ -206,12 +206,7 @@ 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): @@ -219,20 +214,44 @@ def ghost_notify(self, 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: @@ -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 call" + change_cmd = ("au TextChanged,TextChangedI,CursorMoved,CursorMovedI call" " GhostNotify('text_changed', %d)" % bufnr) self.nvim.command(change_cmd) logger.debug("Set up aucmd: %s", change_cmd)