From 216f2c32955669fae06ec2fd570d416c81c3629d Mon Sep 17 00:00:00 2001 From: Elias Naur Date: Sat, 12 Feb 2022 12:01:10 +0100 Subject: [PATCH] app: always preserve IME snippet replacement text When a text range in the IME snippet is replaced, the replacement is discarded if the range don't overlap with the snippet range. However, the replacement is more relevant than whatever snippet is current. This change discards the snippet in case of no overlap. As a bonus, IMEs that leaves the snippet range at [0:0] will have the snippet track the composing region now. The FuzzIME test is adjusted to always generate replacements that overlap Editor content; otherwise the IME snippet and editor state can't be expected to match. Signed-off-by: Elias Naur --- app/ime_test.go | 5 +++-- app/window.go | 13 +++++++------ 2 files changed, 10 insertions(+), 8 deletions(-) diff --git a/app/ime_test.go b/app/ime_test.go index b73ae3cb..4092c44e 100644 --- a/app/ime_test.go +++ b/app/ime_test.go @@ -47,9 +47,10 @@ func FuzzIME(f *testing.F) { ) const cmdLen = 5 for len(cmds) >= cmdLen { + n := e.Len() rng := key.Range{ - Start: int(cmds[1]), - End: int(cmds[2]), + Start: int(cmds[1]) % (n + 1), + End: int(cmds[2]) % (n + 1), } switch cmds[0] % cmdLen { case cmdReplace: diff --git a/app/window.go b/app/window.go index b544af13..8753c2e2 100644 --- a/app/window.go +++ b/app/window.go @@ -528,13 +528,14 @@ func (e *editorState) Replace(r key.Range, text string) { } s := e.Snippet if r.End < s.Start || r.Start > s.End { - // Replacement does not overlap snippet. - e.Snippet.Start = adjust(s.Start) - e.Snippet.End = adjust(s.End) - return + // Discard snippet if it doesn't overlap with replacement. + s = key.Snippet{ + Range: key.Range{ + Start: r.Start, + End: r.Start, + }, + } } - // Replacement overlaps; replace content and expand snippet - // to include all of the replacement. var newSnippet []rune snippet := []rune(s.Text) // Append first part of existing snippet.