From 5228a0b387743b31c62cb81f18dd71787d319f6e Mon Sep 17 00:00:00 2001 From: tannevaled Date: Wed, 18 Nov 2026 10:14:49 +0100 Subject: [PATCH 1/4] collabui: Ctrl+Z in a shared text undoes this participant's edits, never a peer's The editor's snapshot undo restores a whole buffer. In a text other people edit, that buffer predates what they typed since, and CollabText publishes whatever the buffer became, so the restore went out as the local user deleting their text (#591). The new test fails on main: A types, B types after it, A presses Ctrl+Z through the widget, and B's text is gone on every replica. TextView gains SetHistoryDelegate: while one is set the keys, Undo/Redo and CanUndo/CanRedo answer from it and the view keeps no snapshot. CollabText sets one backed by collab's own-edit undo, groups typing per word (a space, a deletion or a jump ends the group), and shows the result after each undo. The snapshot history of a view without a delegate is unchanged (control test). go.mod points collab and crdt at the heads of go-crdt/collab and go-crdt/crdt pull requests; the pins move to release tags before this merges. Co-Authored-By: Claude Sonnet 5.5 --- collabui/collabtext.go | 59 ++++++++++++++ collabui/collabundo_test.go | 131 +++++++++++++++++++++++++++++++ go.mod | 14 ++-- go.sum | 24 +++--- textview.go | 19 +++++ textview_history.go | 40 +++++++++- textview_historydelegate_test.go | 91 +++++++++++++++++++++ 7 files changed, 357 insertions(+), 21 deletions(-) create mode 100644 collabui/collabundo_test.go create mode 100644 textview_historydelegate_test.go diff --git a/collabui/collabtext.go b/collabui/collabtext.go index f86675de..f997a981 100644 --- a/collabui/collabtext.go +++ b/collabui/collabtext.go @@ -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 @@ -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), @@ -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. diff --git a/collabui/collabundo_test.go b/collabui/collabundo_test.go new file mode 100644 index 00000000..2f44c8e2 --- /dev/null +++ b/collabui/collabundo_test.go @@ -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") +} diff --git a/go.mod b/go.mod index 60885bd0..c2c9580a 100644 --- a/go.mod +++ b/go.mod @@ -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.82.2-0.20261118091250-535710576986 + github.com/go-crdt/crdt v0.58.1-0.20261118091103-8587f62d77f0 github.com/go-gfx/gfx v0.41.0 github.com/go-icons/iconoir v0.3.0 github.com/go-images/images v0.3.0 @@ -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 ) diff --git a/go.sum b/go.sum index 734570f0..10bd52b0 100644 --- a/go.sum +++ b/go.sum @@ -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.82.2-0.20261118091250-535710576986 h1:+RswSnpWm0j3OcLwpZQZuJzoMsphaEE5agOJJnbumMk= +github.com/go-crdt/collab v0.82.2-0.20261118091250-535710576986/go.mod h1:xeAUMfpJSyrHmLyE3tCK1e8l+oqm4YEBFhZmEgW7yfg= +github.com/go-crdt/crdt v0.58.1-0.20261118091103-8587f62d77f0 h1:etueh6VELrWkFR/XHG2DqzcegC/Pk4FYRkcC1u35VKo= +github.com/go-crdt/crdt v0.58.1-0.20261118091103-8587f62d77f0/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= @@ -47,8 +47,8 @@ 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= @@ -56,9 +56,9 @@ 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= diff --git a/textview.go b/textview.go index 1b62b658..b83292d5 100644 --- a/textview.go +++ b/textview.go @@ -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 @@ -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:] @@ -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] @@ -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] diff --git a/textview_history.go b/textview_history.go index 497f806f..466274c3 100644 --- a/textview_history.go +++ b/textview_history.go @@ -46,10 +46,46 @@ func (t *TextView) ClearHistory() { t.SetHistory(TextHistory{}) } // CanUndo reports whether Undo would change anything. A Disabled view cannot // undo: the history is kept, but a menu or ribbon greys its command. -func (t *TextView) CanUndo() bool { return len(t.undo) > 0 && !t.Disabled().Get() } +func (t *TextView) CanUndo() bool { + if t.hist != nil { + return t.hist.CanUndo() && !t.Disabled().Get() + } + return len(t.undo) > 0 && !t.Disabled().Get() +} // CanRedo reports whether Redo would change anything (never while Disabled). -func (t *TextView) CanRedo() bool { return len(t.redo) > 0 && !t.Disabled().Get() } +func (t *TextView) CanRedo() bool { + if t.hist != nil { + return t.hist.CanRedo() && !t.Disabled().Get() + } + return len(t.redo) > 0 && !t.Disabled().Get() +} + +// A HistoryDelegate answers undo and redo for a TextView whose text is not the +// view's alone. All four are required. +type HistoryDelegate struct { + // Undo and Redo do the work; they are called only when the matching Can + // function reports true. Whatever they change in the text, they change by + // themselves (the view does not restore a buffer). + Undo, Redo func() + CanUndo, CanRedo func() bool +} + +// SetHistoryDelegate hands undo and redo to d, or takes them back when d is nil. +// +// Ctrl+Z, Ctrl+Y, Ctrl+Shift+Z, [TextView.Undo], [TextView.Redo], CanUndo and +// CanRedo then answer from d, and the view keeps no snapshot of its own. It +// exists for text that other people edit as well. A snapshot restores a whole +// buffer, so undoing in such text restores it to a state that predates what the +// others typed since — and a binding that publishes what the buffer became +// publishes that as the local user deleting their work. A delegate undoes the +// local user's own edits, and only those. +// +// The snapshot history is kept as it was and comes back when d is nil. +func (t *TextView) SetHistoryDelegate(d *HistoryDelegate) { + t.hist = d + t.historyChanged(false) +} // ReplaceText replaces the whole buffer as one undoable edit (SetText is // not undoable: it loads a document). The caret goes to the start. diff --git a/textview_historydelegate_test.go b/textview_historydelegate_test.go new file mode 100644 index 00000000..c0789835 --- /dev/null +++ b/textview_historydelegate_test.go @@ -0,0 +1,91 @@ +// 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 toolkit + +import "testing" + +// With a history delegate set (text other people edit too, toolkit#591) the key +// path and the methods answer from it, and the view keeps no snapshot of its own. +func TestHistoryDelegateOwnsUndoAndRedo(t *testing.T) { + tv := NewTextView("") + var undone, redone int + canU, canR := true, true + var edits []bool + tv.OnHistory = func(edit bool) { edits = append(edits, edit) } + tv.SetHistoryDelegate(&HistoryDelegate{ + Undo: func() { undone++ }, + Redo: func() { redone++ }, + CanUndo: func() bool { return canU }, + CanRedo: func() bool { return canR }, + }) + + tv.OnEvent(Event{Kind: EventChar, Code: "x"}) + if got := tv.Text().Get(); got != "x" { + t.Fatalf("typing still edits the view: %q", got) + } + if u, r := tv.History().Len(); u != 0 || r != 0 { + t.Fatalf("the view kept a snapshot (%d, %d) although the delegate owns history", u, r) + } + if len(edits) == 0 || !edits[len(edits)-1] { + t.Fatalf("an edit is still announced to OnHistory: %v", edits) + } + + tv.OnEvent(Event{Kind: EventKeyDown, Code: "Ctrl+Z"}) + tv.OnEvent(Event{Kind: EventKeyDown, Code: "Ctrl+Y"}) + tv.OnEvent(Event{Kind: EventKeyDown, Code: "Ctrl+Shift+Z"}) + if undone != 1 || redone != 2 { + t.Fatalf("the keys reached the delegate %d undo, %d redo, want 1 and 2", undone, redone) + } + if got := tv.Text().Get(); got != "x" { + t.Fatalf("the view restored a buffer of its own: %q", got) + } + tv.Undo() + tv.Redo() + if undone != 2 || redone != 3 { + t.Fatalf("the methods reached the delegate %d undo, %d redo", undone, redone) + } + + canU, canR = false, false + if tv.CanUndo() || tv.CanRedo() { + t.Fatal("CanUndo/CanRedo do not answer from the delegate") + } + tv.Undo() + tv.Redo() + if undone != 2 || redone != 3 { + t.Fatal("the delegate was asked to do what it said it cannot") + } + canU, canR = true, true + tv.Disabled().Set(true) + if tv.CanUndo() || tv.CanRedo() { + t.Fatal("a disabled view can undo through its delegate") + } + tv.Disabled().Set(false) +} + +// Control: without a delegate the editor behaves as before, and taking the +// delegate back returns to the snapshot history it left untouched. +func TestHistoryDelegateRemovedRestoresSnapshots(t *testing.T) { + tv := NewTextView("") + tv.OnEvent(Event{Kind: EventChar, Code: "a"}) + tv.SetHistoryDelegate(&HistoryDelegate{ + Undo: func() { t.Fatal("delegate used") }, Redo: func() { t.Fatal("delegate used") }, + CanUndo: func() bool { return false }, CanRedo: func() bool { return false }, + }) + tv.SetHistoryDelegate(nil) + if !tv.CanUndo() { + t.Fatal("the snapshot taken before the delegate is gone") + } + tv.OnEvent(Event{Kind: EventKeyDown, Code: "Ctrl+Z"}) + if got := tv.Text().Get(); got != "" { + t.Fatalf("snapshot undo broken: %q", got) + } + if !tv.CanRedo() { + t.Fatal("no redo after a snapshot undo") + } + tv.OnEvent(Event{Kind: EventKeyDown, Code: "Ctrl+Y"}) + if got := tv.Text().Get(); got != "a" { + t.Fatalf("snapshot redo broken: %q", got) + } +} From 391aee8153f8b5dc806fd6549bf18d658b725991 Mon Sep 17 00:00:00 2001 From: tannevaled Date: Wed, 18 Nov 2026 10:29:02 +0100 Subject: [PATCH 2/4] ci: Go 1.27.4, which go-crdt/collab now requires; follow collab and crdt Co-Authored-By: Claude Sonnet 5.5 --- .github/workflows/ci.yml | 2 +- go.mod | 4 ++-- go.sum | 8 ++++---- 3 files changed, 7 insertions(+), 7 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index cbed4bdb..2a57a8a0 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -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 diff --git a/go.mod b/go.mod index c2c9580a..a8791069 100644 --- a/go.mod +++ b/go.mod @@ -3,8 +3,8 @@ module github.com/go-widgets/toolkit go 1.27.4 require ( - github.com/go-crdt/collab v0.82.2-0.20261118091250-535710576986 - github.com/go-crdt/crdt v0.58.1-0.20261118091103-8587f62d77f0 + github.com/go-crdt/collab v0.82.2-0.20261118092807-bfe63255a266 + github.com/go-crdt/crdt v0.58.1-0.20261118092720-ef8fc5065310 github.com/go-gfx/gfx v0.41.0 github.com/go-icons/iconoir v0.3.0 github.com/go-images/images v0.3.0 diff --git a/go.sum b/go.sum index 10bd52b0..59f399bf 100644 --- a/go.sum +++ b/go.sum @@ -5,10 +5,10 @@ github.com/coder/websocket v1.8.15/go.mod h1:NX3SzP+inril6yawo5CQXx8+fk145lPDC6p 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.82.2-0.20261118091250-535710576986 h1:+RswSnpWm0j3OcLwpZQZuJzoMsphaEE5agOJJnbumMk= -github.com/go-crdt/collab v0.82.2-0.20261118091250-535710576986/go.mod h1:xeAUMfpJSyrHmLyE3tCK1e8l+oqm4YEBFhZmEgW7yfg= -github.com/go-crdt/crdt v0.58.1-0.20261118091103-8587f62d77f0 h1:etueh6VELrWkFR/XHG2DqzcegC/Pk4FYRkcC1u35VKo= -github.com/go-crdt/crdt v0.58.1-0.20261118091103-8587f62d77f0/go.mod h1:kHlReXEoryOyNeoSZv7GizHHuSZ+DmVi2AuGIRb0jNc= +github.com/go-crdt/collab v0.82.2-0.20261118092807-bfe63255a266 h1:TNBq6u4pWtKXQwcO087lHkR1rv8ZDWx7qMuyGiUh7pg= +github.com/go-crdt/collab v0.82.2-0.20261118092807-bfe63255a266/go.mod h1:ah6sBwUY/LavmnycU32V0WIl4SLF5bX98RI4c9OCL9E= +github.com/go-crdt/crdt v0.58.1-0.20261118092720-ef8fc5065310 h1:Qx2mEmefnE8UGNHC4BFQdQ03rrfFljmWIKO5dZsFUR8= +github.com/go-crdt/crdt v0.58.1-0.20261118092720-ef8fc5065310/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= From cd51a9f6a7816bc1d2b39e60675bbabef331b53c Mon Sep 17 00:00:00 2001 From: tannevaled Date: Wed, 18 Nov 2026 10:39:46 +0100 Subject: [PATCH 3/4] rougelex: go mod tidy after the crdt and collab pins moved The nested module requires the root module, so a change to the root's crdt requirement leaves its go.mod untidy; its CI lane refuses that. Co-Authored-By: Claude Sonnet 5.5 --- rougelex/go.mod | 4 ++-- rougelex/go.sum | 8 ++++---- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/rougelex/go.mod b/rougelex/go.mod index 10aca2c5..e98f11c7 100644 --- a/rougelex/go.mod +++ b/rougelex/go.mod @@ -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 @@ -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.58.1-0.20261118092720-ef8fc5065310 // 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 diff --git a/rougelex/go.sum b/rougelex/go.sum index b439c56e..fdabdcb8 100644 --- a/rougelex/go.sum +++ b/rougelex/go.sum @@ -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.58.1-0.20261118092720-ef8fc5065310 h1:Qx2mEmefnE8UGNHC4BFQdQ03rrfFljmWIKO5dZsFUR8= +github.com/go-crdt/crdt v0.58.1-0.20261118092720-ef8fc5065310/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= From 4ad240a18dd1c61cebb8462ffaf6b351dbfa2c27 Mon Sep 17 00:00:00 2001 From: tannevaled Date: Wed, 18 Nov 2026 11:23:15 +0100 Subject: [PATCH 4/4] Require crdt v0.59.0 and collab v0.83.0, the releases of the undo fix crdt#182 and collab#286 are released. Replace the pseudo-versions of their branches with the tags, in the root module and in rougelex, so nothing here depends on an unreleased commit. Co-Authored-By: Claude Opus 5.5 --- go.mod | 4 ++-- go.sum | 8 ++++---- rougelex/go.mod | 2 +- rougelex/go.sum | 4 ++-- 4 files changed, 9 insertions(+), 9 deletions(-) diff --git a/go.mod b/go.mod index a8791069..d53dbe24 100644 --- a/go.mod +++ b/go.mod @@ -3,8 +3,8 @@ module github.com/go-widgets/toolkit go 1.27.4 require ( - github.com/go-crdt/collab v0.82.2-0.20261118092807-bfe63255a266 - github.com/go-crdt/crdt v0.58.1-0.20261118092720-ef8fc5065310 + 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 diff --git a/go.sum b/go.sum index 59f399bf..c15bb45d 100644 --- a/go.sum +++ b/go.sum @@ -5,10 +5,10 @@ github.com/coder/websocket v1.8.15/go.mod h1:NX3SzP+inril6yawo5CQXx8+fk145lPDC6p 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.82.2-0.20261118092807-bfe63255a266 h1:TNBq6u4pWtKXQwcO087lHkR1rv8ZDWx7qMuyGiUh7pg= -github.com/go-crdt/collab v0.82.2-0.20261118092807-bfe63255a266/go.mod h1:ah6sBwUY/LavmnycU32V0WIl4SLF5bX98RI4c9OCL9E= -github.com/go-crdt/crdt v0.58.1-0.20261118092720-ef8fc5065310 h1:Qx2mEmefnE8UGNHC4BFQdQ03rrfFljmWIKO5dZsFUR8= -github.com/go-crdt/crdt v0.58.1-0.20261118092720-ef8fc5065310/go.mod h1:kHlReXEoryOyNeoSZv7GizHHuSZ+DmVi2AuGIRb0jNc= +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= diff --git a/rougelex/go.mod b/rougelex/go.mod index e98f11c7..5e4d8729 100644 --- a/rougelex/go.mod +++ b/rougelex/go.mod @@ -9,7 +9,7 @@ require ( require ( github.com/dsnet/compress v0.0.2-0.20230904184137-39efe44ab707 // indirect - github.com/go-crdt/crdt v0.58.1-0.20261118092720-ef8fc5065310 // 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 diff --git a/rougelex/go.sum b/rougelex/go.sum index fdabdcb8..24302487 100644 --- a/rougelex/go.sum +++ b/rougelex/go.sum @@ -3,8 +3,8 @@ github.com/andybalholm/brotli v1.3.0/go.mod h1:rzTDkvFWvIrjDXZHkuS16NPggd91W3kUS 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.58.1-0.20261118092720-ef8fc5065310 h1:Qx2mEmefnE8UGNHC4BFQdQ03rrfFljmWIKO5dZsFUR8= -github.com/go-crdt/crdt v0.58.1-0.20261118092720-ef8fc5065310/go.mod h1:kHlReXEoryOyNeoSZv7GizHHuSZ+DmVi2AuGIRb0jNc= +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=