Send local cursor position to peers

This commit is contained in:
Jared Miller 2026-01-27 20:44:36 -05:00
parent 80da4f9f5b
commit 5275c99976
Signed by: shmup
GPG key ID: 22B5C6D66A38B06C
2 changed files with 23 additions and 0 deletions

View file

@ -160,6 +160,19 @@ for await (const chunk of Bun.stdin.stream()) {
case "content":
setContent(msg.text);
break;
case "cursor":
if (ws && msg.line !== undefined && msg.col !== undefined) {
ws.send(
JSON.stringify({
type: "awareness",
data: {
clientId: Math.floor(Math.random() * 1000000),
cursor: { line: msg.line, col: msg.col },
},
}),
);
}
break;
case "disconnect":
ws?.close();
send({ type: "disconnected" });

View file

@ -74,6 +74,15 @@ def SendBuffer()
Send({type: 'content', text: content})
enddef
def SendCursor()
if !connected
return
endif
const pos = getpos('.')
# pos is [bufnum, line, col, off] - line/col are 1-indexed
Send({type: 'cursor', line: pos[1] - 1, col: pos[2] - 1})
enddef
def ShowPeerCursor(data: dict<any>)
const client_id = string(data.clientId)
@ -131,6 +140,7 @@ export def Connect(room_name: string)
augroup CollabVim
autocmd!
autocmd TextChanged,TextChangedI * call SendBuffer()
autocmd CursorMoved,CursorMovedI <buffer> call SendCursor()
augroup END
enddef