Skip to content
gpdoc
Playground

Collaboration caret

Exchange presence updates and track remote carets and selections.

CollaborationCaret lets a peer share who they are and where they are editing. It uses the Yjs awareness protocol to send transient presence updates. Your view can use these updates to draw a named caret or a selected range.

Start with the project from the Collaboration example. Add the caret crate to that project.

Terminal window
cargo add gpdoc-plugin-collaboration-caret

Start with Alice’s session and its seed update from the Collaboration example. Replace the line that creates Bob’s session with the following setup. The painter and the receive handler share Bob’s awareness state.

use gpdoc_plugin_collaboration_caret::{CollaborationCaret, collaboration_carets, paint_remote_caret};
use std::{cell::{OnceCell, RefCell}, rc::Rc};
let awareness = Rc::new(OnceCell::<RefCell<CollaborationCaret>>::new());
let remote_carets = Rc::clone(&awareness);
let peers = collaboration_carets(move |session| {
remote_carets.get().map_or_else(Vec::new, |state| state.borrow().peers(session))
});
let bob_config = EditorConfig::builder()
.plugin(paragraph())
.plugin(peers.with_view(paint_remote_caret))
.build()
.expect("configure editor");
let bob = Collaboration::from_update(&bob_config, "default", &seed)?;
let remote = awareness.get_or_init(|| RefCell::new(CollaborationCaret::new(&bob)));

The snippets below inspect Bob’s session before binding it. To display the caret, open the view with bob_config and pass bob to view.bind_collaboration(bob, send_update, window, cx)?. After binding, use the session from view.collaboration() for lookup and notify the view after each awareness update. See Connect a GPUI view.

peers.with_view(...) Receives each part and its measured CaretLayout
├── CaretPart::Selection Highlight rectangles behind the selected text
├── CaretPart::Cursor Insertion line at the remote selection head
└── CaretPart::Label Name above the cursor

Replace paint_remote_caret in the example’s with_view call with paint_peer. Forward the selection and cursor to the default painter to display a peer’s editing position without a name label.

use gpdoc_plugin_collaboration_caret::{CaretLayout, CaretPart};
use gpui::{App, Window};
fn paint_peer(part: CaretPart, caret: &CaretLayout, window: &mut Window, cx: &mut App) {
match part {
CaretPart::Selection | CaretPart::Cursor => paint_remote_caret(part, caret, window, cx),
CaretPart::Label => {}
}
}

Use Alice’s session and the shared receiver from the example above.

use gpdoc_plugin_collaboration_caret::User;

Create one awareness instance for each session and keep it while that peer is connected. Each instance uses its session’s document client ID. User is a JSON object (serde_json::Map<String, serde_json::Value>). Supply the fields your view will display.

let mut local = CollaborationCaret::new(&alice);
let user = User::from_iter([("name".into(), "Alice".into())]);

Place Alice’s caret at the start of the paragraph. With focused set to true, publish encodes her user data and current relative selection if available. Send the V1 awareness bytes to Bob and apply them with receive. Your app can send these updates whenever focus or selection changes.

let selection = Selection::caret(DocPos(1));
alice.select(selection)?;
let update = local.publish(&alice, user.clone(), true)?;
remote.borrow_mut().receive(&update)?;

Call peers as the view paints so a remote selection resolves against the current text. The result is a Vec<RemoteCaret> sorted by client, a u64 client ID that distinguishes users even if they share a name. Use user to draw a label and selection to draw a caret or range. A hidden or unresolved selection yields None.

let peers = remote.borrow().peers(&bob);
assert_eq!(peers.len(), 1);
assert_eq!(peers[0].user["name"], "Alice");
assert_eq!(peers[0].selection, Some(selection));

On focus loss, publish the same user with focused set to false. Bob can still show Alice in a list of connected users.

let hidden = local.publish(&alice, user, false)?;
remote.borrow_mut().receive(&hidden)?;
assert_eq!(remote.borrow().peers(&bob)[0].selection, None);

Before closing the connection, send the update returned by leave. Once Bob receives it, Alice disappears from his peer list. Keep these presence messages transient and route saved document edits through Collaboration::apply_update.

remote.borrow_mut().receive(&local.leave()?)?;
assert!(remote.borrow().peers(&bob).is_empty());

collaboration-caret/src/lib.rs