Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ jobs:

- uses: actions/setup-go@v7
with:
go-version: "1.27.3"
go-version: "1.27.4"
check-latest: true
cache: true

Expand Down
59 changes: 59 additions & 0 deletions collabui/collabtext.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,11 @@ type CollabText struct {
// editor, so the editor's own change notifications do not echo that update
// back into the session as a fresh local edit. UI goroutine only.
applyingRemote bool
// grouping is set while an undo group is open on the text part, and lastEnd is
// the rune offset where the last local edit ended, which a keystroke has to
// start at to join it. UI goroutine only.
grouping bool
lastEnd int
// closed short-circuits a remote application still queued when Close ran. UI
// goroutine only.
closed bool
Expand Down Expand Up @@ -157,7 +162,16 @@ func NewCollabText(ed *toolkit.CodeEditor, client *collab.Client, textName strin
// for a local edit.
ed.SetText(handle.String())
c.prev = ed.Text().Get()
// Undo in a shared text is the undo of this participant's own edits. The
// editor's snapshot undo would restore a buffer that predates what peers have
// typed, and this binding would publish that as deleting it (toolkit#591).
handle.EnableUndo()
ed.SetHistoryDelegate(&toolkit.HistoryDelegate{
Undo: c.undo, Redo: c.redo,
CanUndo: handle.CanUndo, CanRedo: handle.CanRedo,
})
c.unsub = append(c.unsub,
func() { ed.SetHistoryDelegate(nil) },
ed.Text().SubscribeChanged(c.syncLocal),
ed.CursorLine().SubscribeChanged(c.publishCursor),
ed.CursorCol().SubscribeChanged(c.publishCursor),
Expand Down Expand Up @@ -199,17 +213,62 @@ func (c *CollabText) syncLocal() {
// Both are exact deltas from the same base, so a rebased offset is within
// the replica by construction and these calls cannot be out of range.
for _, op := range replay(ch, pending) {
c.group(op)
if op.Removed > 0 {
_ = c.text.Delete(op.Pos, op.Removed)
}
if op.Text != "" {
_ = c.text.Insert(op.Pos, op.Text)
}
c.lastEnd = op.Pos + utf8.RuneCountInString(op.Text)
if !typed(op) || isSpace(op.Text) {
// A word ends here: what comes next is a step of its own.
c.text.Commit()
c.grouping = false
}
}
}
c.prev = next
}

// group opens or continues the undo group an edit belongs to. Undo is per word,
// not per keystroke: single characters typed one after another, each where the
// last ended, are one step, closed by a space, a newline, a deletion, or a
// keystroke somewhere else. Only this participant's edits are in the group; what
// peers type in between is theirs and never part of it.
func (c *CollabText) group(op crdt.Change) {
if c.grouping && typed(op) && op.Pos == c.lastEnd {
return
}
c.text.Commit()
c.text.Begin()
c.grouping = true
}

// typed reports whether op is one character typed, rather than a deletion, a
// replacement or a paste.
func typed(op crdt.Change) bool {
return op.Removed == 0 && utf8.RuneCountInString(op.Text) == 1
}

func isSpace(s string) bool { return strings.TrimSpace(s) == "" }

// undo and redo are the editor's history, handed to it with
// [toolkit.TextView.SetHistoryDelegate]. They take back this participant's own
// edits in the session and then show the result; the editor never restores a
// buffer of its own, which in a shared text would read as deleting a peer's work.
func (c *CollabText) undo() {
c.grouping = false
_ = c.text.Undo()
c.applyRemote()
}

func (c *CollabText) redo() {
c.grouping = false
_ = c.text.Redo()
c.applyRemote()
}

// publishCursor publishes the local caret and selection as presence. It is the
// observer of the three caret observables; a move the binding itself caused while
// applying a remote update is skipped.
Expand Down
131 changes: 131 additions & 0 deletions collabui/collabundo_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
// Copyright (c) 2026 the go-widgets/toolkit authors. All rights reserved.
// Use of this source code is governed by a BSD-3-Clause license that can be
// found in the LICENSE file at the root of this repository.

package collabui

import (
"testing"

"github.com/go-widgets/toolkit"
)

// Ctrl+Z in a collaborative editor reverts what THIS site typed. It must never
// delete a peer's text (toolkit#591): the widget's own snapshot stack records
// whole buffers, so restoring one that predates a peer's insertion would become
// a local deletion of that insertion, published as this site's operation.

// type sends one committed character through the widget's real key path, with
// the caret at (line, col).
func (p *collabPeer) typeAt(line, col int, ch string) {
p.setCaret(line, col)
p.do(func() { p.ed.OnEvent(toolkit.Event{Kind: toolkit.EventChar, Code: ch}) })
}

// key sends one key-down through the widget's real key path.
func (p *collabPeer) key(code string) {
p.do(func() { p.ed.OnEvent(toolkit.Event{Kind: toolkit.EventKeyDown, Code: code}) })
}

func TestUndoNeverDeletesAPeersText(t *testing.T) {
url := serveCollab(t)
ada := newCollabPeer(t, url, "undo2", "ada", RGBA{R: 0xff, A: 0xff}, 1)
bo := newCollabPeer(t, url, "undo2", "bo", RGBA{B: 0xff, A: 0xff}, 2)
defer ada.close()
defer bo.close()

ada.typeAt(0, 0, "a")
bo.await(t, "a on bo", func() bool { return bo.ed.Text().Get() == "a" })
bo.typeAt(0, 1, "b")
ada.await(t, "b on ada", func() bool { return ada.ed.Text().Get() == "ab" })

ada.key("Ctrl+Z")

want := "b"
ada.await(t, "ada undo settled", func() bool { return ada.ed.Text().Get() == want })
bo.await(t, "bo sees ada's undo", func() bool { return bo.ed.Text().Get() == want })
if a, b := ada.replica(), bo.replica(); a != want || b != want {
t.Fatalf("after ada's Ctrl+Z: ada replica %q, bo replica %q, want %q (bo's text must survive)", a, b, want)
}
}

func TestUndoNeverDeletesTwoPeersText(t *testing.T) {
url := serveCollab(t)
ada := newCollabPeer(t, url, "undo3", "ada", RGBA{R: 0xff, A: 0xff}, 1)
bo := newCollabPeer(t, url, "undo3", "bo", RGBA{B: 0xff, A: 0xff}, 2)
cy := newCollabPeer(t, url, "undo3", "cy", RGBA{G: 0xff, A: 0xff}, 3)
defer ada.close()
defer bo.close()
defer cy.close()

ada.typeAt(0, 0, "a")
bo.await(t, "a on bo", func() bool { return bo.ed.Text().Get() == "a" })
bo.typeAt(0, 1, "b")
cy.await(t, "ab on cy", func() bool { return cy.ed.Text().Get() == "ab" })
cy.typeAt(0, 2, "c")
ada.await(t, "abc on ada", func() bool { return ada.ed.Text().Get() == "abc" })
bo.await(t, "abc on bo", func() bool { return bo.ed.Text().Get() == "abc" })

ada.key("Ctrl+Z")

want := "bc"
for _, p := range []*collabPeer{ada, bo, cy} {
p.await(t, "undo settled", func() bool { return p.ed.Text().Get() == want })
}
for name, p := range map[string]*collabPeer{"ada": ada, "bo": bo, "cy": cy} {
if got := p.replica(); got != want {
t.Fatalf("after ada's Ctrl+Z: %s replica %q, want %q (peers' text must survive)", name, got, want)
}
}
}

// Undo is per word, not per keystroke, and what a peer typed in the middle of the
// word is not part of it. Redo puts the word back. All of it through the keys.
func TestUndoIsPerWordAndSparesAPeerTypingInTheMiddle(t *testing.T) {
url := serveCollab(t)
ada := newCollabPeer(t, url, "undoword", "ada", RGBA{R: 0xff, A: 0xff}, 1)
bo := newCollabPeer(t, url, "undoword", "bo", RGBA{B: 0xff, A: 0xff}, 2)
defer ada.close()
defer bo.close()

for i, ch := range []string{"h", "e", "l", "l", "o", " "} {
ada.typeAt(0, i, ch)
}
bo.await(t, "hello on bo", func() bool { return bo.ed.Text().Get() == "hello " })
bo.typeAt(0, 6, "B")
ada.await(t, "B on ada", func() bool { return ada.ed.Text().Get() == "hello B" })
ada.typeAt(0, 7, "w")
ada.typeAt(0, 8, "o")
bo.await(t, "wo on bo", func() bool { return bo.ed.Text().Get() == "hello Bwo" })

both := func(what, want string) {
t.Helper()
for _, p := range []*collabPeer{ada, bo} {
p.await(t, what, func() bool { return p.ed.Text().Get() == want })
}
}
ada.key("Ctrl+Z")
both("the word wo to go", "hello B")
ada.key("Ctrl+Z")
both("hello to go, B to stay", "B")
ada.key("Ctrl+Y")
both("hello back", "hello B")
ada.key("Ctrl+Shift+Z")
both("wo back", "hello Bwo")
ada.key("Ctrl+Z")
ada.key("Ctrl+Z")
both("both words undone again", "B")
ada.key("Ctrl+Z") // nothing of ada's left: B is bo's
both("nothing of bo's taken", "B")

// A deletion is a step of its own and is put back by undo.
ada.typeAt(0, 1, "a")
ada.typeAt(0, 2, "b")
both("ab typed after B", "Bab")
ada.key("Backspace")
both("b deleted", "Ba")
ada.key("Ctrl+Z")
both("b back", "Bab")
ada.key("Ctrl+Z")
both("the word ab undone as one", "B")
}
14 changes: 7 additions & 7 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
module github.com/go-widgets/toolkit

go 1.27.2
go 1.27.4

require (
github.com/go-crdt/collab v0.77.4
github.com/go-crdt/crdt v0.57.0
github.com/go-crdt/collab v0.83.0
github.com/go-crdt/crdt v0.59.0
github.com/go-gfx/gfx v0.41.0
github.com/go-icons/iconoir v0.3.0
github.com/go-images/images v0.3.0
Expand All @@ -20,13 +20,13 @@ require (
)

require (
github.com/andybalholm/brotli v1.2.8 // indirect
github.com/andybalholm/brotli v1.3.0 // indirect
github.com/coder/websocket v1.8.15 // indirect
github.com/dsnet/compress v0.0.2-0.20230904184137-39efe44ab707 // indirect
golang.org/x/net v0.60.0 // indirect
golang.org/x/net v0.62.0 // indirect
golang.org/x/sys v0.49.0 // indirect
golang.org/x/text v0.43.0 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20260706201446-f0a921348800 // indirect
google.golang.org/grpc v1.84.0 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20260817212433-ac3dfec99bb1 // indirect
google.golang.org/grpc v1.85.0 // indirect
google.golang.org/protobuf v1.36.12 // indirect
)
24 changes: 12 additions & 12 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
github.com/andybalholm/brotli v1.2.8 h1:H9F6r7RNwzSRsx/lytpIYkbjNFMt2yNBwsJ2+0gpx78=
github.com/andybalholm/brotli v1.2.8/go.mod h1:rzTDkvFWvIrjDXZHkuS16NPggd91W3kUSvPlQ1pLaKY=
github.com/andybalholm/brotli v1.3.0 h1:IdkhjSe/+34U+fP7FSR2Q6Poj1hnHUa2GoWmzXJd6qY=
github.com/andybalholm/brotli v1.3.0/go.mod h1:rzTDkvFWvIrjDXZHkuS16NPggd91W3kUSvPlQ1pLaKY=
github.com/coder/websocket v1.8.15 h1:6B2JPeOGlpff2Uz6vOEH1Vzpi0iUz20A+lPVhPHtNUA=
github.com/coder/websocket v1.8.15/go.mod h1:NX3SzP+inril6yawo5CQXx8+fk145lPDC6pumgx0mVg=
github.com/dsnet/compress v0.0.2-0.20230904184137-39efe44ab707 h1:2tV76y6Q9BB+NEBasnqvs7e49aEBFI8ejC89PSnWH+4=
github.com/dsnet/compress v0.0.2-0.20230904184137-39efe44ab707/go.mod h1:qssHWj60/X5sZFNxpG4HBPDHVqxNm4DfnCKgrbZOT+s=
github.com/dsnet/golib v0.0.0-20171103203638-1ea166775780/go.mod h1:Lj+Z9rebOhdfkVLjJ8T6VcRQv3SXugXy999NBtR9aFY=
github.com/go-crdt/collab v0.77.4 h1:wW6opnSdSMQPw6zdZfkd857jyqJqNvWZD77KYsXgLb0=
github.com/go-crdt/collab v0.77.4/go.mod h1:LFqHBOnnr2kRFrTxCVJYWGlaF8jc7l51Z7yVrUY2RnA=
github.com/go-crdt/crdt v0.57.0 h1:sNLDTVCEnSb20gm77BUuYav8dzj5krmly/z9O7eRMCo=
github.com/go-crdt/crdt v0.57.0/go.mod h1:zdVTTDLGV5JYAoTJDqCivcAT+/B5gGebDsVd8QNZpVs=
github.com/go-crdt/collab v0.83.0 h1:U51D8EIC9EaKMJ3UHYbXwfYs3Vnq7ylwUav4pa6nfK0=
github.com/go-crdt/collab v0.83.0/go.mod h1:h3iZ9uR+tT7ghQawUM5sFtvH4Wq9qw1j7AMJBrTsM3s=
github.com/go-crdt/crdt v0.59.0 h1:2gp9+qVo6zN4Y5A6fUKkHufZjJGUJQZ2R4xecDrO0xQ=
github.com/go-crdt/crdt v0.59.0/go.mod h1:kHlReXEoryOyNeoSZv7GizHHuSZ+DmVi2AuGIRb0jNc=
github.com/go-gfx/gfx v0.41.0 h1:MVEnory0Zx8ypzuVDrTIWz4Cekv8hn5ilcYg3glJPrk=
github.com/go-gfx/gfx v0.41.0/go.mod h1:4OenoKrgdht6UXYf5SV8w2tb41hfAdy4QoxJRPsRDz4=
github.com/go-icons/iconoir v0.3.0 h1:kHG2sU5IsCvtBS1JICuXgfFo5f8UfwY1m7KYMVy97CI=
Expand Down Expand Up @@ -47,18 +47,18 @@ github.com/xyproto/randomstring v1.0.5 h1:YtlWPoRdgMu3NZtP45drfy1GKoojuR7hmRcnhZ
github.com/xyproto/randomstring v1.0.5/go.mod h1:rgmS5DeNXLivK7YprL0pY+lTuhNQW3iGxZ18UQApw/E=
golang.org/x/image v0.47.0 h1:za/8gVz++PPZqmZJBNSISICN5Rrj5zixP/8K++fgmqo=
golang.org/x/image v0.47.0/go.mod h1:IkN5o4A7vq55vXN8FWjjnhg9Rs2GOtffqsSi9mWAKYE=
golang.org/x/net v0.60.0 h1:79p50tfZlm0J9YfoDsSi639qSXNGVwEzOPLCxM2FsYU=
golang.org/x/net v0.60.0/go.mod h1:2DA/G1UfVbCpQPeWTmMPGY7Cs2PkBkwu743bVX5PIVg=
golang.org/x/net v0.62.0 h1:18ELalfM5+XNRwzM2nYqXsZQ9ByS1uhj2O/U7+5jqRY=
golang.org/x/net v0.62.0/go.mod h1:WgDCOTH8iwtB66T4/GfI94AHo/hhdb7Hah9NDseQfiI=
golang.org/x/sys v0.49.0 h1:XbzkgYJHdqh/8m2Uu0W/dQv8nktxx4BFHp1M0gROTXA=
golang.org/x/sys v0.49.0/go.mod h1:hNLxWAXmnKAxqDtdwIYC4bM9oQPEecfsnNMuSxOs3og=
golang.org/x/text v0.43.0 h1:1QivrlhwAsnMOcqgOdl9my7s9OyZl8O5/UGUTJVXoWc=
golang.org/x/text v0.43.0/go.mod h1:hbSIYA/amXcRXTFSZqh8tWHEoqRvvbKPJ2uz/KU+a+s=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
gonum.org/v1/gonum v0.17.0 h1:VbpOemQlsSMrYmn7T2OUvQ4dqxQXU+ouZFQsZOx50z4=
gonum.org/v1/gonum v0.17.0/go.mod h1:El3tOrEuMpv2UdMrbNlKEh9vd86bmQ6vqIcDwxEOc1E=
google.golang.org/genproto/googleapis/rpc v0.0.0-20260706201446-f0a921348800 h1:qEHAMpSaUhtD0p3NbEEI83HwNGFxEwaSJ1G9PLnCBZE=
google.golang.org/genproto/googleapis/rpc v0.0.0-20260706201446-f0a921348800/go.mod h1:4Hqkh8ycfw05ld/3BWL7rJOSfebL2Q+DVDeRgYgxUU8=
google.golang.org/grpc v1.84.0 h1:soMyaPJ8pAak5PIQ0DGBUir0XRo2fRoMqhNWMLlLxO0=
google.golang.org/grpc v1.84.0/go.mod h1:ljCht0DrxQrXBDRTZp52Qxh3Ffk8CdYm2sj4O2QN2C0=
google.golang.org/genproto/googleapis/rpc v0.0.0-20260817212433-ac3dfec99bb1 h1:utmqiUzZAEINbLmRyAt1QjVkMyLQ+lvIwCpLaB1rZfw=
google.golang.org/genproto/googleapis/rpc v0.0.0-20260817212433-ac3dfec99bb1/go.mod h1:4Hqkh8ycfw05ld/3BWL7rJOSfebL2Q+DVDeRgYgxUU8=
google.golang.org/grpc v1.85.0 h1:BPBCfuVRQ1qvBhKDOq9uvQllMvO59rKsYhXpp7Dd8vw=
google.golang.org/grpc v1.85.0/go.mod h1:0rQrOD58yjal/0EaVbGBANindixsCZNQPDV3VKtaKuQ=
google.golang.org/protobuf v1.36.12 h1:pJOKDDOyeXErUroCihFAd5LQuwXBSpVnKGrj5o/fwxc=
google.golang.org/protobuf v1.36.12/go.mod h1:HTf+CrKn2C3g5S8VImy6tdcUvCska2kB7j23XfzDpco=
4 changes: 2 additions & 2 deletions rougelex/go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module github.com/go-widgets/toolkit/rougelex

go 1.27.2
go 1.27.4

require (
github.com/go-rouge/rouge v0.7.1
Expand All @@ -9,7 +9,7 @@ require (

require (
github.com/dsnet/compress v0.0.2-0.20230904184137-39efe44ab707 // indirect
github.com/go-crdt/crdt v0.57.0 // indirect
github.com/go-crdt/crdt v0.59.0 // indirect
github.com/go-gfx/gfx v0.41.0 // indirect
github.com/go-images/images v0.3.0 // indirect
github.com/go-opentype/fonts v0.14.0 // indirect
Expand Down
8 changes: 4 additions & 4 deletions rougelex/go.sum
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
github.com/andybalholm/brotli v1.2.8 h1:H9F6r7RNwzSRsx/lytpIYkbjNFMt2yNBwsJ2+0gpx78=
github.com/andybalholm/brotli v1.2.8/go.mod h1:rzTDkvFWvIrjDXZHkuS16NPggd91W3kUSvPlQ1pLaKY=
github.com/andybalholm/brotli v1.3.0 h1:IdkhjSe/+34U+fP7FSR2Q6Poj1hnHUa2GoWmzXJd6qY=
github.com/andybalholm/brotli v1.3.0/go.mod h1:rzTDkvFWvIrjDXZHkuS16NPggd91W3kUSvPlQ1pLaKY=
github.com/dsnet/compress v0.0.2-0.20230904184137-39efe44ab707 h1:2tV76y6Q9BB+NEBasnqvs7e49aEBFI8ejC89PSnWH+4=
github.com/dsnet/compress v0.0.2-0.20230904184137-39efe44ab707/go.mod h1:qssHWj60/X5sZFNxpG4HBPDHVqxNm4DfnCKgrbZOT+s=
github.com/dsnet/golib v0.0.0-20171103203638-1ea166775780/go.mod h1:Lj+Z9rebOhdfkVLjJ8T6VcRQv3SXugXy999NBtR9aFY=
github.com/go-crdt/crdt v0.57.0 h1:sNLDTVCEnSb20gm77BUuYav8dzj5krmly/z9O7eRMCo=
github.com/go-crdt/crdt v0.57.0/go.mod h1:zdVTTDLGV5JYAoTJDqCivcAT+/B5gGebDsVd8QNZpVs=
github.com/go-crdt/crdt v0.59.0 h1:2gp9+qVo6zN4Y5A6fUKkHufZjJGUJQZ2R4xecDrO0xQ=
github.com/go-crdt/crdt v0.59.0/go.mod h1:kHlReXEoryOyNeoSZv7GizHHuSZ+DmVi2AuGIRb0jNc=
github.com/go-gfx/gfx v0.41.0 h1:MVEnory0Zx8ypzuVDrTIWz4Cekv8hn5ilcYg3glJPrk=
github.com/go-gfx/gfx v0.41.0/go.mod h1:4OenoKrgdht6UXYf5SV8w2tb41hfAdy4QoxJRPsRDz4=
github.com/go-icons/iconoir v0.3.0 h1:kHG2sU5IsCvtBS1JICuXgfFo5f8UfwY1m7KYMVy97CI=
Expand Down
19 changes: 19 additions & 0 deletions textview.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,9 @@ type TextView struct {
// coalescing of consecutive keystrokes -- simplest correct
// behaviour, matching the sibling's documented choice).
undo, redo []tvSnapshot
// hist, when set, owns undo and redo instead of the snapshot stacks. See
// [TextView.SetHistoryDelegate].
hist *HistoryDelegate

// selAnchorLine/selAnchorCol remember where a mouse selection began: an
// EventClick records the caret it placed as the anchor, and each
Expand Down Expand Up @@ -370,6 +373,12 @@ func (t *TextView) restore(s tvSnapshot) {
// no-op edits -- e.g. Backspace at the buffer start, an empty
// EventChar -- don't waste a stack slot).
func (t *TextView) pushUndo() {
if t.hist != nil {
// The delegate records the edit itself; a snapshot of the buffer is exactly
// what it must not be given back.
t.historyChanged(true)
return
}
t.undo = append(t.undo, t.snapshot())
if len(t.undo) > maxUndo {
t.undo = t.undo[len(t.undo)-maxUndo:]
Expand All @@ -385,6 +394,11 @@ func (t *TextView) Undo() {
if !t.CanUndo() {
return
}
if t.hist != nil {
t.hist.Undo()
t.historyChanged(false)
return
}
t.redo = append(t.redo, t.snapshot())
last := t.undo[len(t.undo)-1]
t.undo = t.undo[:len(t.undo)-1]
Expand All @@ -400,6 +414,11 @@ func (t *TextView) Redo() {
if !t.CanRedo() {
return
}
if t.hist != nil {
t.hist.Redo()
t.historyChanged(false)
return
}
t.undo = append(t.undo, t.snapshot())
last := t.redo[len(t.redo)-1]
t.redo = t.redo[:len(t.redo)-1]
Expand Down
Loading
Loading