Skip to content

Commit d924101

Browse files
committed
Send cursor position to browser
From now on, we set the browser's cursor position according to where the cursor is in neovim. Though, the opposite — setting neovim's cursor to the same position as that in the browser — hasn't been implemented yet.
1 parent b4bc7e2 commit d924101

File tree

2 files changed

+15
-9
lines changed

2 files changed

+15
-9
lines changed

binary.py

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
from simple_websocket_server import WebSocket
2020
from simple_websocket_server import WebSocketServer
2121

22-
BUILD_VERSION: str = "v0.2.7"
22+
BUILD_VERSION: str = "v0.3.0"
2323

2424
WINDOWS: bool = os.name == "nt"
2525
LOCALHOST: str = "127.0.0.1" if WINDOWS else "localhost"
@@ -385,6 +385,15 @@ def _neovim_handler(self, *args):
385385
# Get the buffer contents
386386
handle = self.loop_neovim_handle
387387
buffer_contents = handle.api.buf_get_lines(self.buffer_handle, 0, -1, False)
388+
_, lnum, col, _, _ = handle.call("getcurpos")
389+
lnum -= 1 # indexing mismatch (nvim starts from 1, python starts from 0)
390+
col -= 1 # indexing mismatch (nvim starts from 1 python starts from 0)
391+
392+
# Calculate curpos
393+
curpos = 0
394+
for line in buffer_contents[:lnum]:
395+
curpos += len(line) + 1
396+
curpos += col
388397

389398
# Turn buffer_contents (a List) to a string
390399
text = "\n".join(buffer_contents)
@@ -399,13 +408,10 @@ def _neovim_handler(self, *args):
399408
self.last_set_text = None
400409

401410
# Send the text
402-
self._send_text(text)
403-
404-
def _send_text(self, text: str):
405-
# Construct and send the message
406-
message = json.dumps({"text": text, "selections": []})
407-
self.send_message(message)
408-
log(f"{self.address[1]} sent", message)
411+
message = {"text": text, "selections": [{"start": curpos, "end": curpos}]}
412+
message = json.dumps(message)
413+
self.send_message(message)
414+
log(f"{self.address[1]} sent", message)
409415

410416
def _trigger_autocmds(self, url: str):
411417
self.neovim_handle.command(f"doau nvim_ghost_user_autocommands User {url}")

binary_version

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
v0.2.7
1+
v0.3.0

0 commit comments

Comments
 (0)