Skip to content

Transform cursor positions through text history#6

Open
bxff wants to merge 2 commits intomasterfrom
feat/version-aware-cursors
Open

Transform cursor positions through text history#6
bxff wants to merge 2 commits intomasterfrom
feat/version-aware-cursors

Conversation

@bxff
Copy link
Copy Markdown
Member

@bxff bxff commented Mar 27, 2026

Cursor PUTs don't carry any version context. If the server has applied edits the client hasn't seen yet, the position lands on the wrong character and the cursor visibly jumps until the client catches up.

This adds an optional Version header to cursor PUTs. When the server sees it, it grabs the patches between that version and current via xfSince, then transforms the cursor positions before storing. Same thing simpleton already does to reconcile text edits from clients at different points in history.

No Version header means no transform, so existing clients keep working.

Changes

client/simpleton-sync.js: expose a version getter on the returned object so cursor code can read it.

client/cursor-sync.js: accept an optional get_version callback. When provided, its return value is sent as a Version header on cursor PUTs. cursor_highlights gets an attach(simpleton_client) method to wire the two together.

server.js: in the cursor PUT handler, if there's a Version header, transform the cursor data through intervening patches before storing. The transform lives in a closure at the call site so it can reach get_xf_patches and OpLog_remote_to_local.

client/editor.html, client/markdown-editor.html: call cursors.attach(simpleton) to actually enable the feature in the bundled editors.

README.md: updated the cursor example and API docs to include attach().

test/cursor-tests.js: three tests covering stale version rebase, missing version (stored as-is), and current version (no-op).

Usage

var sc = simpleton_client(url, { ... })
var cursors = cursor_highlights(textarea, url)
cursors.attach(sc)

@toomim
Copy link
Copy Markdown
Member

toomim commented Mar 28, 2026

Can you explain a situation in which this happens? Is this a common issue, or a rare case? Is this where you first load a document that has people actively editing it, and in that case, the cursor might move a bit before you are fully caught-up?

When I thought through the cursor protocol, I was originally going to maintain versions on cursors, but then it seemed like we don't need them. I'm wondering if there's a common case where visible cursor jumping is happening that I'm not aware of.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants