IME: guard distant commits; arm the resync on the first anomaly
The boundary trace (PADIME at EditorStateChanged) showed why a desynchronized IME never heals itself: gioui's EditorReplace advances its stored selection state when the commit arrives, so the post-commit selection push is always deduplicated away and updateSelection is never sent after a commit. A desynced IME is therefore healed only by a snippet restart — and the resync was armed only after THREE consecutive anomalies, while the phone incident's first anomaly (a 5-rune autocorrect 19,000 runes from the caret, width > 2) sailed through the old <=2-rune guard and clobbered distant text before any resync could fire. Now: - the guard also snaps commits that are far from the caret AND outside the live selection (a legitimate commit is always local: at the caret, inside the selection, or a nearby correction); a distant commit lands at the caret instead of clobbering text; - the resync is armed on the FIRST anomalous commit (snapped or distant), not after a streak; the streak counter is gone; - imeSelectionRuneRange() lets the guard allow selection replacements anywhere. TestRealFile_IMEForceResync rewritten for the new semantics and extended with the distant-replacement case (must land at the caret and arm the resync).
This commit is contained in:
+56
-28
@@ -127,15 +127,15 @@ type EditorState struct {
|
||||
IMESelStartRune int
|
||||
IMESelEndRune int
|
||||
// IMEForceResync asks the next frame to re-push the snippet trimmed by
|
||||
// one rune (a forced restartInput): after several consecutive
|
||||
// anomalous commits (drift-snapped or empty) the IME's local text is
|
||||
// desynchronized from ours and it keeps re-sending the same fix (an
|
||||
// endless empty-commit loop). A forced restart makes it re-fetch the
|
||||
// real text and selection around the caret, which heals the model.
|
||||
// Set by HandleIMECommit, consumed by the next frame (EditorLayout).
|
||||
// one rune (a forced restartInput): an anomalous commit (drift-snapped
|
||||
// or distant — see HandleIMECommit) means the IME's local text is
|
||||
// desynchronized from ours, and it may keep re-sending the same fix in
|
||||
// a loop (observed on the phone: an endless stream of empty commits).
|
||||
// A selection push cannot heal this (it is deduplicated away after
|
||||
// commits); only a restart makes the IME re-fetch the real text and
|
||||
// selection around the caret. Set by HandleIMECommit on the first
|
||||
// anomaly, consumed by the next frame (EditorLayout).
|
||||
IMEForceResync bool
|
||||
// imeAnomStreak counts consecutive anomalous IME commits (see above).
|
||||
imeAnomStreak int
|
||||
// EditSeq counts content edits (incremented by markDirty). The shaped
|
||||
// glyph layout arriving via layoutChan is only applied to the WrapIndex
|
||||
// when its EditSeq matches, so a layout shaped before an edit can never
|
||||
@@ -2220,6 +2220,19 @@ func (s *State) computeIMESnippetWindow() {
|
||||
}
|
||||
}
|
||||
|
||||
// imeSelectionRuneRange returns the live selection in absolute file runes,
|
||||
// or (-1, -1) when there is none. Used by the anomalous-commit guard to
|
||||
// allow commits inside a user selection anywhere.
|
||||
func imeSelectionRuneRange() (int, int) {
|
||||
if !selActive() {
|
||||
return -1, -1
|
||||
}
|
||||
e := &TheState.Editor
|
||||
r0 := TheState.imeRuneOffsetAt(e.SelectionStart)
|
||||
r1 := r0 + utf8.RuneCountInString(TheState.fileContent(e.SelectionStart, e.SelectionEnd))
|
||||
return r0, r1
|
||||
}
|
||||
|
||||
// fileLenNow returns the current content length (chunked or string buffer).
|
||||
func (s *State) fileLenNow() int64 {
|
||||
if cb := s.Editor.ChunkedBuffer; cb != nil {
|
||||
@@ -2362,28 +2375,43 @@ func HandleIMECommit(data any) {
|
||||
if startRune > endRune {
|
||||
startRune, endRune = endRune, startRune
|
||||
}
|
||||
snapped := false
|
||||
if endRune-startRune <= 2 {
|
||||
caretRune := TheState.imeRuneOffsetAt(TheState.Editor.CursorPosition)
|
||||
if endRune != caretRune {
|
||||
log.Printf("IME DRIFT-SNAP range=[%d,%d) caret=%d -> snap to caret", startRune, endRune, caretRune)
|
||||
startRune, endRune = caretRune, caretRune
|
||||
snapped = true
|
||||
// Anomalous-commit guard. A legitimate IME commit is always local: at
|
||||
// the caret (a typed char, a composing word), inside the live
|
||||
// selection, or a correction of the word just typed (a few runes left
|
||||
// of the caret). Anything else means the IME's local text has
|
||||
// desynchronized from ours — its reported range addresses text that is
|
||||
// not where we think it is. Applying it verbatim clobbers distant text
|
||||
// (observed on the phone: a 5-rune autocorrect 19,000 runes from the
|
||||
// caret); the only safe action is to apply it at the caret and force a
|
||||
// re-syncing snippet re-push on the next frame (see IMEForceResync): a
|
||||
// desynced IME is healed only by a restart (a selection push is
|
||||
// deduplicated away after commits — see Renderer.FlushIME), so the
|
||||
// resync is armed on the FIRST anomaly, not after a streak.
|
||||
const maxCommitDistance = 1024
|
||||
caretRune := TheState.imeRuneOffsetAt(TheState.Editor.CursorPosition)
|
||||
selStartRune, selEndRune := imeSelectionRuneRange()
|
||||
anomalous := endRune-startRune <= 2 && endRune != caretRune
|
||||
if !anomalous && c.Text != "" {
|
||||
// The commit must be near the caret, or inside the live selection
|
||||
// (a replacement of a user selection, wherever it is).
|
||||
lo := caretRune - maxCommitDistance
|
||||
hi := caretRune + maxCommitDistance
|
||||
if selStartRune >= 0 {
|
||||
if selStartRune < lo {
|
||||
lo = selStartRune
|
||||
}
|
||||
if selEndRune > hi {
|
||||
hi = selEndRune
|
||||
}
|
||||
}
|
||||
if startRune > hi || endRune < lo {
|
||||
anomalous = true
|
||||
}
|
||||
}
|
||||
// Anomalous commits (snapped, or empty: an IME fix-up that changes
|
||||
// nothing) mean the IME's local text has desynchronized from ours.
|
||||
// After a few in a row, force a re-syncing snippet re-push on the next
|
||||
// frame (see IMEForceResync) so the IME re-fetches the real text and
|
||||
// the loop ends; a normal commit resets the streak.
|
||||
if snapped || c.Text == "" {
|
||||
TheState.Editor.imeAnomStreak++
|
||||
if TheState.Editor.imeAnomStreak >= 3 {
|
||||
TheState.Editor.imeAnomStreak = 0
|
||||
TheState.Editor.IMEForceResync = true
|
||||
}
|
||||
} else {
|
||||
TheState.Editor.imeAnomStreak = 0
|
||||
if anomalous {
|
||||
log.Printf("IME DRIFT-SNAP range=[%d,%d) text=%q caret=%d -> snap to caret + resync", startRune, endRune, c.Text, caretRune)
|
||||
startRune, endRune = caretRune, caretRune
|
||||
TheState.Editor.IMEForceResync = true
|
||||
}
|
||||
absStart, absEnd := runeToByteWhole(startRune), runeToByteWhole(endRune)
|
||||
log.Printf("IME COMMIT range=[%d,%d) text=%q -> [%d,%d)", startRune, endRune, c.Text, absStart, absEnd)
|
||||
|
||||
Reference in New Issue
Block a user