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 <mail@eliasnaur.com>
This commit is contained in:
Elias Naur
2022-02-12 12:01:10 +01:00
parent 9e23412a01
commit 216f2c3295
2 changed files with 10 additions and 8 deletions
+3 -2
View File
@@ -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:
+7 -6
View File
@@ -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.